diff --git a/packages/menu/package.json b/packages/menu/package.json index 3b774b255..c3f7dc45a 100644 --- a/packages/menu/package.json +++ b/packages/menu/package.json @@ -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.2.0", "druxt": "^0.24.0", diff --git a/packages/menu/src/index.js b/packages/menu/src/index.js index e7f735181..4e9d40d31 100644 --- a/packages/menu/src/index.js +++ b/packages/menu/src/index.js @@ -1,5 +1,3 @@ -import { DruxtMenuNuxtModule } from './nuxtModule' - /** * The DruxtMenu class. * @@ -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. * diff --git a/packages/menu/src/nuxt/index.js b/packages/menu/src/nuxt/index.js new file mode 100644 index 000000000..bed6c4c8f --- /dev/null +++ b/packages/menu/src/nuxt/index.js @@ -0,0 +1,81 @@ +import { addPluginTemplate, defineNuxtModule, installModule } from '@nuxt/kit' +import { join, resolve } from 'path' +// import DruxtMenuStorybook from '../nuxtStorybook' + +/** + * 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 diff --git a/packages/menu/src/nuxtModule.js b/packages/menu/src/nuxtModule.js deleted file mode 100644 index 1661bb9fe..000000000 --- a/packages/menu/src/nuxtModule.js +++ /dev/null @@ -1,71 +0,0 @@ -import { join, resolve } from 'path' -import DruxtMenuStorybook from './nuxtStorybook' - -/** - * 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 = async function (moduleOptions = {}) { - // Set default options. - const options = { - baseUrl: moduleOptions.baseUrl, - ...(this.options || {}).druxt || {}, - menu: { - jsonApiMenuItems: true, - ...((this.options || {}).druxt || {}).menu, - ...moduleOptions, - } - } - - // Add dependant modules. - await this.addModule(['druxt', options]) - - // Register components directories. - this.nuxt.hook('components:dirs', dirs => { - dirs.push({ path: join(__dirname, 'components') }) - dirs.push({ path: join(__dirname, 'components/blocks') }) - }) - - // Add plugin. - this.addPlugin({ - src: resolve(__dirname, '../templates/plugin.js'), - fileName: 'druxt-menu.js', - options - }) - - // Add Vuex plugin. - this.addPlugin({ - src: resolve(__dirname, '../templates/store.js'), - fileName: 'store/druxt-menu.js', - options - }) - - // Nuxt Storybook. - this.nuxt.hook('storybook:config', async ({ stories }) => { - await DruxtMenuStorybook.call(this, { stories }) - }) -} - -DruxtMenuNuxtModule.meta = require('../package.json') - -export { DruxtMenuNuxtModule }