diff --git a/packages/entity/package.json b/packages/entity/package.json index 43686bfb..0e95259f 100644 --- a/packages/entity/package.json +++ b/packages/entity/package.json @@ -35,15 +35,18 @@ "require": "./dist/druxt-entity.ssr.js", "import": "./dist/druxt-entity.esm.js" }, - "./components/*": "./dist/components/*" + "./components/*": "./dist/components/*", + "./nuxt": "./nuxt/index.js" }, "main": "dist/druxt-entity.ssr.js", "module": "dist/druxt-entity.esm.js", "files": [ "dist", + "nuxt", "templates" ], "dependencies": { + "@nuxt/kit": "^3.12.2", "deepmerge": "^4.3.1", "drupal-jsonapi-params": "^2.2.0", "druxt": "^0.22.0", diff --git a/packages/entity/src/index.js b/packages/entity/src/index.js index 29dca6ec..2c2290ca 100644 --- a/packages/entity/src/index.js +++ b/packages/entity/src/index.js @@ -1,17 +1,3 @@ -import { DruxtEntityNuxtModule } from './nuxtModule' - -/** - * The Nuxt.js module function. - * - * Installs the module functionality in a Nuxt application. - * - * @type {Function} - * @exports default - * @name DruxtEntityNuxtModule - * @see {@link ./nuxtModule|DruxtEntityNuxtModule} - */ -export default DruxtEntityNuxtModule - /** * Vue.js mixins. * diff --git a/packages/entity/src/nuxt/index.js b/packages/entity/src/nuxt/index.js new file mode 100644 index 00000000..dd59fb68 --- /dev/null +++ b/packages/entity/src/nuxt/index.js @@ -0,0 +1,81 @@ +import { defineNuxtModule, installModule } from '@nuxt/kit' +import { join } from 'path' +// import DruxtEntityStorybook from '../nuxtStorybook' + +/** + * The Nuxt.js module function. + * + * - Adds dependant modules. + * - Adds Nuxt plugin. + * - 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-entity' + * ], + * druxt: { + * baseUrl: 'https://demo-api.druxtjs.org' + * } + * } + * + * @todo Document options. + * + * @property {object} options.druxt - DruxtJS root level options. + * @property {string} options.druxt.baseUrl - Base URL of Drupal JSON:API backend. + */ +const DruxtEntityNuxtModule = defineNuxtModule({ + meta: { + name: 'druxt-entity', + }, + defaults: { + baseUrl: '', + endpoint: '/jsonapi' + }, + + async setup(moduleOptions, nuxt) { + // Set default options. + const options = { + baseUrl: moduleOptions.baseUrl, + ...nuxt.options?.druxt, + entity: { + query: {}, + ...nuxt.options?.druxt?.entity, + ...moduleOptions, + components: { + fields: false, + ...nuxt.options?.druxt?.entity?.components, + ...moduleOptions?.components + } + } + } + + // Add dependant modules. + await installModule('druxt/nuxt', options, nuxt) + await installModule('druxt-schema', { baseUrl: options.baseUrl }, nuxt) + + // Register components directories. + nuxt.hook('components:dirs', dirs => { + dirs.push({ + path: join(__dirname, '../dist/components'), + ignore: ['fields'] + }) + if (options.entity.components.fields) { + dirs.push({ path: join(__dirname, '../dist/components/fields') }) + } + }) + + // Nuxt Storybook. + // @TODO - @nuxt/kit and @nuxt/storybook aren't compatible. + // this.nuxt.hook('storybook:config', async ({ stories }) => { + // await DruxtEntityStorybook.call(this, { stories }) + // }) + } +}) + +export default DruxtEntityNuxtModule diff --git a/packages/entity/src/nuxtModule.js b/packages/entity/src/nuxtModule.js deleted file mode 100644 index 8ab575be..00000000 --- a/packages/entity/src/nuxtModule.js +++ /dev/null @@ -1,71 +0,0 @@ -import { join } from 'path' -import DruxtEntityStorybook from './nuxtStorybook' - -/** - * The Nuxt.js module function. - * - * - Adds dependant modules. - * - Adds Nuxt plugin. - * - 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-entity' - * ], - * druxt: { - * baseUrl: 'https://demo-api.druxtjs.org' - * } - * } - * - * @todo Document options. - * - * @property {object} options.druxt - DruxtJS root level options. - * @property {string} options.druxt.baseUrl - Base URL of Drupal JSON:API backend. - */ -const DruxtEntityNuxtModule = async function (moduleOptions = {}) { - // Set default options. - const options = { - baseUrl: moduleOptions.baseUrl, - ...(this.options || {}).druxt || {}, - entity: { - query: {}, - ...((this.options || {}).druxt || {}).entity, - ...moduleOptions, - components: { - fields: false, - ...(((this.options || {}).druxt || {}).entity || {}).components, - ...moduleOptions.components - } - } - } - - // Add dependant modules. - await this.addModule(['druxt', options]) - await this.addModule(['druxt-schema', { baseUrl: options.baseUrl }]) - - // Register components directories. - this.nuxt.hook('components:dirs', dirs => { - dirs.push({ - path: join(__dirname, 'components'), - ignore: ['fields'] - }) - if (options.entity.components.fields) { - dirs.push({ path: join(__dirname, 'components/fields') }) - } - }) - - // Nuxt Storybook. - this.nuxt.hook('storybook:config', async ({ stories }) => { - await DruxtEntityStorybook.call(this, { stories }) - }) -} - -DruxtEntityNuxtModule.meta = require('../package.json') - -export { DruxtEntityNuxtModule }