Skip to content

Commit

Permalink
feat(#693): initial druxt-site update for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 8, 2024
1 parent 44ab6c9 commit a620374
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 94 deletions.
5 changes: 4 additions & 1 deletion packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
"import": "./dist/druxt-site.esm.js"
},
"./components/*": "./dist/components/*",
"./layouts/*": "./dist/layouts/*"
"./layouts/*": "./dist/layouts/*",
"./nuxt": "./nuxt/index.js"
},
"main": "dist/druxt-site.ssr.js",
"module": "dist/druxt-site.esm.js",
"files": [
"dist",
"nuxt",
"templates"
],
"dependencies": {
"@nuxt/kit": "^3.11.2",
"drupal-jsonapi-params": "^2.2.0",
"druxt": "^0.22.0",
"druxt-blocks": "^0.17.1",
Expand Down
25 changes: 5 additions & 20 deletions packages/site/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DruxtSiteNuxtModule } from './nuxtModule'

/**
* Vue.js Mixin.
*
Expand All @@ -26,23 +24,10 @@ import { DruxtSiteNuxtModule } from './nuxtModule'
export { DruxtSiteMixin } from './mixins/site'

/**
* The Nuxt.js module functions.
*
* Installs and configures all DruxtJS Site modules.
* Default function to alert user to incorrectly installed module.
*
* @type {Function}
* @exports default
* @name DruxtSiteNuxtModule
* @see {@link ./nuxtModule|DruxtSiteNuxtModule}
*
* @example <caption>nuxt.config.js</caption> @lang js
* module.exports = {
* modules: [
* 'druxt-site'
* ],
* druxt: {
* baseUrl: 'https://demo-api.druxtjs.org'
* }
* }
* This was added as part of the @nuxt/kit update due to breaking changes.
*/
export default DruxtSiteNuxtModule
export default () => {
throw new Error("DruxtSite Nuxt module must be installed as 'druxt-site/nuxt'")
}
90 changes: 90 additions & 0 deletions packages/site/src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { addLayout, defineNuxtModule, installModule } from '@nuxt/kit'
import { existsSync } from 'fs'
import { resolve } from 'path'
// import DruxtSiteStorybook from '../nuxtStorybook'

/**
* Nuxt module function to install Druxt Site.
*
* - Adds the DruxtSite component.
* - Adds the core modules for DruxtJS Site.
* - Adds default configuration for @nuxtjs/proxy.
* - Enables Vuex store.
*
* @param {ModuleOptions} moduleOptions - The Nuxt.js module options.
*/
const DruxtSiteNuxtModule = defineNuxtModule({
meta: {
name: 'druxt-site',
},
defaults: {
baseUrl: '',
endpoint: '/jsonapi'
},

async setup(moduleOptions, nuxt) {
// Prevent issue "FATAL: Cannot determine nuxt version! Is current
// instance passed?".
nuxt._version = nuxt._version || '2.'
// This is required to prevent "FATAL: nuxt.options._layers is not iterable"
// error when using `installModule()`.
nuxt.options._layers = nuxt.options._layers || []

// Set default options.
const options = {
baseUrl: moduleOptions.baseUrl,
...nuxt.options?.druxt,
proxy: {
api: false,
files: true,
...nuxt.options?.druxt?.proxy,
},
site: {
layout: true,
...nuxt.options?.druxt?.site,
...moduleOptions,
},
}
nuxt.options.druxt = options

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

// Add Druxt modules.
const druxtModules = [
'druxt',
'druxt-blocks',
'druxt-breadcrumb',
'druxt-entity',
'druxt-menu',
'druxt-router',
'druxt-schema',
'druxt-views'
]
for (const module of druxtModules) {
await installModule(module, {}, nuxt)
}

// Add default layout.
if (!(await existsSync(resolve(nuxt.options.srcDir, nuxt.options.dir.layouts))) && options.site.layout) {
addLayout(resolve(__dirname, '../dist/layouts/default.vue'), 'default')
}

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

export default DruxtSiteNuxtModule

/**
* Module options object.
*
* @typedef {object} ModuleOptions
* @see {@link ./typedefs/moduleOptions|ModuleOptions}
*/
73 changes: 0 additions & 73 deletions packages/site/src/nuxtModule.js

This file was deleted.

0 comments on commit a620374

Please sign in to comment.