Skip to content

Commit

Permalink
feat(#693): update druxt-breadcrumb for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 30, 2024
1 parent 7d7a575 commit beecae9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/breadcrumb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Add module to `nuxt.config.js`

```js
module.exports = {
modules: ['druxt-breadcrumb'],
modules: ['druxt-breadcrumb/nuxt'],
druxt: {
baseUrl: 'https://demo-api.druxtjs.org',
},
Expand Down
7 changes: 5 additions & 2 deletions packages/breadcrumb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
"require": "./dist/druxt-breadcrumb.ssr.js",
"import": "./dist/druxt-breadcrumb.esm.js"
},
"./components/*": "./dist/components/*"
"./components/*": "./dist/components/*",
"./nuxt": "./nuxt/index.js"
},
"main": "dist/druxt-breadcrumb.ssr.js",
"module": "dist/druxt-breadcrumb.esm.js",
"files": [
"dist",
"nuxt",
"templates"
],
"dependencies": {
"@nuxt/kit": "^3.12.2",
"druxt": "^0.24.0",
"druxt-blocks": "^0.17.1",
"druxt-blocks": "^0.17.2",
"druxt-router": "^0.31.0"
},
"optionalDependencies": {
Expand Down
23 changes: 9 additions & 14 deletions packages/breadcrumb/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import { DruxtBreadcrumbModule } from './nuxtModule'

/**
* The Nuxt.js module function.
*
* Installs the module functionality in a Nuxt.js frontend.
*
* @type {Function}
* @exports default
* @name DruxtBreadcrumbModule
* @see {@link ./module|DruxtBreadcrumbModule}
*/
export default DruxtBreadcrumbModule

/**
* Vue.js Mixin.
*
Expand All @@ -20,3 +6,12 @@ export default DruxtBreadcrumbModule
* @see {@link ./mixins/breadcrumb|DruxtBreadcrumbMixin}
*/
export { DruxtBreadcrumbMixin } from './mixins/breadcrumb'

/**
* Default function to alert user to incorrectly installed module.
*
* This was added as part of the @nuxt/kit update due to breaking changes.
*/
export default () => {
throw new Error("DruxtBreadcrumb Nuxt module must be installed as 'druxt-breadcrumb/nuxt'")
}
67 changes: 67 additions & 0 deletions packages/breadcrumb/src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { defineNuxtModule, installModule } from '@nuxt/kit'
import { join } from 'path'

/**
* The Nuxt.js module function.
*
* - Adds the Vue.js components to the Nuxt.js frontend.
*
* The module function should not be used directly, but rather installed via yout Nuxt.js configuration file.
*
* Options are set on the root level `druxt` Nuxt.js config object.
*
* @example <caption>nuxt.config.js</caption> @lang js
* module.exports = {
* modules: [
* 'druxt-breadcrumb'
* ],
* druxt: {
* baseUrl: 'https://example.com'
* }
* }
*
* @param {object} moduleOptions - Nuxt.js module options object.
*/
const DruxtBreadcrumbModule = defineNuxtModule({
meta: {
name: 'druxt-breadcrumb',
},
defaults: {
baseUrl: '',
endpoint: '/jsonapi'
},

async setup(moduleOptions, nuxt) {
// Set default options.
const options = {
baseUrl: moduleOptions.baseUrl,
...nuxt.options?.druxt || {},
breadcrumb: {
...nuxt.options?.druxt?.breadcrumb,
...moduleOptions,
}
}

// Add dependent module.
await installModule('druxt/nuxt', options, nuxt)
await installModule('druxt-router/nuxt', options, nuxt)

// Register components directories.
nuxt.hook('components:dirs', dirs => {
dirs.push({ path: join(__dirname, '../dist/components') })
dirs.push({ path: join(__dirname, '../dist/components/blocks') })
})

// Nuxt Storybook.
// @TODO - @nuxt/kit and @nuxt/storybook aren't compatible.
// nuxt.hook('storybook:config', ({ stories }) => {
// addTemplate({
// src: resolve(__dirname, '../templates/druxt-breadcrumb.stories.js'),
// fileName: 'stories/druxt-breadcrumb.stories.js',
// })
// stories.push(resolve(nuxt.options.buildDir, './stories/druxt-breadcrumb.stories.js'))
// })
}
})

export default DruxtBreadcrumbModule
57 changes: 0 additions & 57 deletions packages/breadcrumb/src/nuxtModule.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/breadcrumb/test/nuxt/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import DruxtBreadcrumbModule from '../../src/nuxt'

jest.mock('@nuxt/kit', () => ({
defineNuxtModule: (module) => module,
installModule: jest.fn(),
}))

const nuxtMock = {
hook: jest.fn((hook, fn) => {
const arg = {
'components:dirs': [],
'storybook:config': { stories: [] }
}
return fn(arg[hook])
}),
}

test('Nuxt module', async () => {
nuxtMock.options = {
buildDir: 'build',
druxt: {}
}
await DruxtBreadcrumbModule.setup({}, nuxtMock)
})
23 changes: 0 additions & 23 deletions packages/breadcrumb/test/nuxtModule.test.js

This file was deleted.

0 comments on commit beecae9

Please sign in to comment.