Skip to content

Commit

Permalink
feat(#693): update druxt-menu for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 30, 2024
1 parent b280e85 commit 0b075d7
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 131 deletions.
2 changes: 1 addition & 1 deletion packages/menu/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-menu'],
modules: ['druxt-menu/nuxt'],
druxt: {
baseUrl: 'https://demo-api.druxtjs.org',
menu: {
Expand Down
5 changes: 4 additions & 1 deletion packages/menu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
"require": "./dist/druxt-menu.ssr.js",
"import": "./dist/druxt-menu.esm.js"
},
"./components/*": "./dist/components/*"
"./components/*": "./dist/components/*",
"./nuxt": "./nuxt/index.js"
},
"main": "dist/druxt-menu.ssr.js",
"module": "dist/druxt-menu.esm.js",
"files": [
"dist",
"nuxt",
"templates"
],
"dependencies": {
"@nuxt/kit": "^3.12.2",
"deepmerge": "^4.3.1",
"drupal-jsonapi-params": "^2.3.1",
"druxt": "^0.24.0",
Expand Down
23 changes: 9 additions & 14 deletions packages/menu/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DruxtMenuNuxtModule } from './nuxtModule'

/**
* The DruxtMenu class.
*
Expand All @@ -22,18 +20,6 @@ export { DruxtMenu } from './menu.js'
*/
export { DruxtMenuStore } from './stores/menu.js'

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

/**
* Vue.js Mixin.
*
Expand All @@ -42,3 +28,12 @@ export default DruxtMenuNuxtModule
* @see {@link ./mixins/menu|DruxtMenuMixin}
*/
export { DruxtMenuMixin } from './mixins/menu'

/**
* 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("DruxtMenu Nuxt module must be installed as 'druxt-menu/nuxt'")
}
81 changes: 81 additions & 0 deletions packages/menu/src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { addPluginTemplate, defineNuxtModule, installModule } from '@nuxt/kit'
import { join, resolve } from 'path'
// import DruxtMenuStorybook from './storybook'

/**
* The Nuxt.js module function.
*
* - Adds Nuxt plugin.
* - Adds Vuex store.
* - Adds Nuxt Storybook integration.
*
* The module function should not be used directly, but rather installed via your Nuxt configuration file.
*
* Options are set on the root level `druxt` Nuxt config object.
*
* @example @lang js
* // `nuxt.config.js`
* module.exports = {
* modules: [
* 'druxt-menu'
* ],
* druxt: {
* baseUrl: 'https://demo-api.druxtjs.org'
* }
* }
*
* @param {object} moduleOptions - Module options object.
*/
const DruxtMenuNuxtModule = defineNuxtModule({
meta: {
name: 'druxt-menu',
},
defaults: {
baseUrl: '',
endpoint: '/jsonapi'
},

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

// Add dependant modules.
await installModule('druxt/nuxt', options)

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

// Add plugin.
addPluginTemplate({
src: resolve(__dirname, '../templates/plugin.js'),
fileName: 'druxt-menu.js',
options
})

// Add Vuex plugin.
addPluginTemplate({
src: resolve(__dirname, '../templates/store.js'),
fileName: 'store/druxt-menu.js',
options
})

// Nuxt Storybook.
// @TODO - @nuxt/kit and @nuxt/storybook aren't compatible.
// this.nuxt.hook('storybook:config', async ({ stories }) => {
// await DruxtMenuStorybook.call(this, { stories })
// })
}
})

export default DruxtMenuNuxtModule
File renamed without changes.
71 changes: 0 additions & 71 deletions packages/menu/src/nuxtModule.js

This file was deleted.

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

jest.mock('../../src/nuxt/storybook')

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

import { addPluginTemplate, installModule } from '@nuxt/kit'

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

test('Nuxt module', async () => {
// Use module with defaults.
await DruxtMenuNuxtModule.setup({}, nuxtMock)
expect(addPluginTemplate).toHaveBeenCalled()

// Expect JSON:API Menu Items tp be enabled.
expect(installModule).toHaveBeenLastCalledWith('druxt/nuxt', {
baseUrl: undefined,
menu: {
jsonApiMenuItems: true
}
})

// Use overridden options; Drupal content menu items.
await DruxtMenuNuxtModule.setup({}, { ...nuxtMock, options: { druxt: { menu: { jsonApiMenuItems: false }}} })
expect(installModule).toHaveBeenLastCalledWith('druxt/nuxt', {
baseUrl: undefined,
menu: {
jsonApiMenuItems: false
}
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DruxtMenuStorybook from '../src/nuxtStorybook'
import DruxtMenuStorybook from '../../src/nuxt/storybook'

jest.mock('axios')

Expand Down
43 changes: 0 additions & 43 deletions packages/menu/test/nuxtModule.test.js

This file was deleted.

0 comments on commit 0b075d7

Please sign in to comment.