diff --git a/packages/schema/package.json b/packages/schema/package.json index f8987e5bf..d2edc4b1c 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -30,15 +30,18 @@ "require": "./dist/druxt-schema.ssr.js", "import": "./dist/druxt-schema.esm.js" }, - "./components/*": "./dist/components/*" + "./components/*": "./dist/components/*", + "./nuxt": "./nuxt/index.js" }, "main": "dist/druxt-schema.ssr.js", "module": "dist/druxt-schema.esm.js", "files": [ "dist", + "nuxt", "templates" ], "dependencies": { + "@nuxt/kit": "^3.12.2", "consola": "^2.15.3", "drupal-jsonapi-params": "^2.2.0", "druxt": "^0.22.0" diff --git a/packages/schema/src/index.js b/packages/schema/src/index.js index dca74cad9..9a4fcac58 100644 --- a/packages/schema/src/index.js +++ b/packages/schema/src/index.js @@ -1,5 +1,3 @@ -import { DruxtSchemaNuxtModule } from './nuxtModule' - /** * The core module functionality. * @@ -44,25 +42,3 @@ export { DruxtSchemaMixin } from './mixins/schema' * @see {@link /api/packages/schema/store/schema|DruxtSchemaStore} */ export { DruxtSchemaStore } from './stores/schema' - -/** - * The Nuxt.js module function. - * - * Installs the module functionality in a Nuxt.js frontend. - * - * @type {Function} - * @exports default - * @name DruxtSchemaNuxtModule - * @see {@link ./module|DruxtSchemaNuxtModule} - * - * @example nuxt.config.js @lang js - * module.exports = { - * modules: [ - * 'druxt-schema' - * ], - * druxt: { - * baseUrl: 'https://example.com' - * } - * } - */ -export default DruxtSchemaNuxtModule diff --git a/packages/schema/src/nuxt/index.js b/packages/schema/src/nuxt/index.js new file mode 100644 index 000000000..f76602bc6 --- /dev/null +++ b/packages/schema/src/nuxt/index.js @@ -0,0 +1,102 @@ +import { addPluginTemplate, addTemplate, defineNuxtModule, useLogger } from '@nuxt/kit' +import { resolve } from 'path' + +import { DruxtSchema } from '../schema' + +/** + * The Nuxt.js module function. + * + * - Adds the Schema plugin to Nuxt.js. + * - Adds the Schema Vuex store to Nuxt.js. + * - Builds the Schema data via the `builder:prepared` hook. + * + * 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 @lang js + * // `nuxt.config.js` + * module.exports = { + * modules: [ + * 'druxt-schema' + * ], + * druxt: { + * baseUrl: 'https://example.com' + * } + * } + * + * @todo Document options. + * + * @param {object} moduleOptions - Nuxt.js module options object. + */ +const DruxtSchemaNuxtModule = defineNuxtModule({ + meta: { + name: 'druxt-schema', + }, + defaults: { + baseUrl: '', + endpoint: '/jsonapi' + }, + + async setup(moduleOptions, nuxt) { + const logger = useLogger('druxt-schema') + // Set default options. + const options = { + baseUrl: moduleOptions.baseUrl, + ...nuxt.options?.druxt || {}, + schema: { + ...nuxt.options?.druxt?.schema || {}, + ...moduleOptions, + } + } + + // Add plugin. + addPluginTemplate({ + src: resolve(__dirname, '../templates/plugin.js'), + fileName: 'druxt-schema.js', + options + }) + + // Enable Vuex Store. + nuxt.options.store = true + + // Add Vuex plugin. + addPluginTemplate({ + src: resolve(__dirname, '../templates/store.js'), + fileName: 'store/druxt-schema.js', + options + }) + + // Generate schema files. + nuxt.hook('modules:done', async () => { + const druxtSchema = new DruxtSchema(options.baseUrl, { + ...options, + // Disable API Proxy, as Proxies aren't available at build. + proxy: { ...options.proxy || {}, api: false }, + }) + const { schemas } = await druxtSchema.get() + + // Throw error if no schema files generated. + if (!Object.entries(schemas).length) { + throw new Error('No Druxt Schema files generated.\n Have you created any content types yet?') + } + + for (const name in schemas) { + const schema = schemas[name] + if (typeof schema === 'undefined') continue + + addTemplate({ + src: resolve(__dirname, '../templates/schema.json'), + fileName: `schemas/${name}.json`, + options: { schema }, + // @TODO - Does it need to be written to the file system? + write: true + }) + } + + logger.success('Druxt schema files generated') + }) + } +}) + +export default DruxtSchemaNuxtModule diff --git a/packages/schema/src/nuxtModule.js b/packages/schema/src/nuxtModule.js deleted file mode 100644 index 79f3e92b9..000000000 --- a/packages/schema/src/nuxtModule.js +++ /dev/null @@ -1,91 +0,0 @@ -import consola from 'consola' -import { resolve } from 'path' - -import { DruxtSchema } from './schema' - -/** - * The Nuxt.js module function. - * - * - Adds the Schema plugin to Nuxt.js. - * - Adds the Schema Vuex store to Nuxt.js. - * - Builds the Schema data via the `builder:prepared` hook. - * - * 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 @lang js - * // `nuxt.config.js` - * module.exports = { - * modules: [ - * 'druxt-schema' - * ], - * druxt: { - * baseUrl: 'https://example.com' - * } - * } - * - * @todo Document options. - * - * @param {object} moduleOptions - Nuxt.js module options object. - */ -const DruxtSchemaNuxtModule = function (moduleOptions = {}) { - // Set default options. - const options = { - baseUrl: moduleOptions.baseUrl, - ...(this.options || {}).druxt || {}, - schema: { - ...((this.options || {}).druxt || {}).schema || {}, - ...moduleOptions, - } - } - - // Add plugin. - this.addPlugin({ - src: resolve(__dirname, '../templates/plugin.js'), - fileName: 'druxt-schema.js', - options - }) - - // Enable Vuex Store. - this.options.store = true - - // Add Vuex plugin. - this.addPlugin({ - src: resolve(__dirname, '../templates/store.js'), - fileName: 'store/druxt-schema.js', - options - }) - - // Generate schemas. - this.nuxt.hook('builder:prepared', async () => { - const druxtSchema = new DruxtSchema(options.baseUrl, { - ...options, - // Disable API Proxy, as Proxies aren't available at build. - proxy: { ...options.proxy || {}, api: false }, - }) - const { schemas } = await druxtSchema.get() - - // Throw error if no schema files generated. - if (!Object.entries(schemas).length) { - throw new Error('No Druxt Schema files generated.\n Have you created any content types yet?') - } - - for (const name in schemas) { - const schema = schemas[name] - if (typeof schema === 'undefined') continue - - this.addTemplate({ - src: resolve(__dirname, '../templates/schema.json'), - fileName: `schemas/${name}.json`, - options: { schema } - }) - } - - consola.success('Druxt schema files generated') - }) -} - -DruxtSchemaNuxtModule.meta = require('../package.json') - -export { DruxtSchemaNuxtModule }