Skip to content

Commit

Permalink
feat(#693): update druxt-site for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 30, 2024
1 parent 1202487 commit 29fa813
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 135 deletions.
2 changes: 1 addition & 1 deletion packages/site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
```js
module.exports = {
modules: [
'druxt-site'
'druxt-site/nuxt'
],
druxt: {
baseUrl: 'https://demo-api.druxtjs.org'
Expand Down
7 changes: 5 additions & 2 deletions packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@
"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.3.1",
"druxt": "^0.24.0",
"druxt-blocks": "^0.17.1",
"druxt-blocks": "^0.17.2",
"druxt-breadcrumb": "^0.17.1",
"druxt-entity": "^0.28.0",
"druxt-menu": "^0.21.0",
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/nuxt',
'druxt-blocks/nuxt',
'druxt-breadcrumb/nuxt',
'druxt-entity/nuxt',
'druxt-menu/nuxt',
'druxt-router/nuxt',
'druxt-schema/nuxt',
'druxt-views/nuxt'
]
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}
*/
File renamed without changes.
73 changes: 0 additions & 73 deletions packages/site/src/nuxtModule.js

This file was deleted.

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

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

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

import { installModule } from '@nuxt/kit'

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

jest.mock('druxt-schema')

describe('DruxtJS Site module', () => {
test('Nuxt module', async () => {
nuxtMock.options = {
dir: { layouts: 'layouts' },
druxt: {},
srcDir: __dirname,
}

// Call DruxtSite module.
await await DruxtSiteNuxtModule.setup({}, nuxtMock)

// Expect 9 modules to be added.
expect(installModule).toHaveBeenCalledTimes(8)
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path'
import DruxtEntityStorybook from '../src/nuxtStorybook'
import DruxtEntityStorybook from '../../src/nuxt/storybook'

jest.mock('axios')

Expand Down
38 changes: 0 additions & 38 deletions packages/site/test/nuxtModule.test.js

This file was deleted.

0 comments on commit 29fa813

Please sign in to comment.