Skip to content

Commit

Permalink
feat(#693): update druxt-blocks for @nuxt/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 30, 2024
1 parent 2600447 commit 7d7a575
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 115 deletions.
2 changes: 1 addition & 1 deletion packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Add module to `nuxt.config.js`

```js
module.exports = {
modules: ['druxt-blocks'],
modules: ['druxt-blocks/nuxt'],
druxt: {
baseUrl: 'https://demo-api.druxtjs.org',
blocks: {
Expand Down
5 changes: 4 additions & 1 deletion packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
"require": "./dist/druxt-blocks.ssr.js",
"import": "./dist/druxt-blocks.esm.js"
},
"./components/*": "./dist/components/*"
"./components/*": "./dist/components/*",
"./nuxt/*": "./nuxt/*"
},
"main": "dist/druxt-blocks.ssr.js",
"module": "dist/druxt-blocks.esm.js",
"files": [
"dist",
"nuxt",
"templates"
],
"dependencies": {
"@nuxt/kit": "^3.12.4",
"axios": "0.28.0",
"drupal-jsonapi-params": "^2.3.1",
"druxt": "^0.24.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/blocks/src/components/DruxtBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default {
*
* @return {object}
*/
block: ({ resource }) => (resource || {}).data,
block: ({ resource }) => resource?.data,
},
watch: {
Expand Down Expand Up @@ -156,7 +156,7 @@ export default {
const type = 'block--block'
const query = new DrupalJsonApiParams()
const fields = ((this.$druxt.settings.blocks || {}).query || {}).fields
const fields = this.$druxt?.settings.blocks?.query?.fields
if (Array.isArray(fields)) {
query.addFields(type, [
...fields,
Expand Down Expand Up @@ -214,10 +214,10 @@ export default {
scopedSlots.default = () => {
let summary, description
if ((this.block || {}).attributes) {
if (this.block?.attributes) {
summary = `Missing Vue template for the '${this.block.attributes.drupal_internal__id}' block`
description = [
h('p', `Create a Druxt theme component to render the "${this.block.attributes.settings.label}" block.`),
h('p', `Create a Druxt theme component to render the "${this.block.attributes?.settings?.label}" block.`),
]
// Ensure an ID or UUID.
} else if (!this.id && !this.uuid) {
Expand All @@ -229,7 +229,7 @@ export default {
{ props: { summary } },
[
h('div', description),
!!this.component.options.length && h('DruxtDevelTemplate', { props: { options: this.component.options }}),
!!this.component.options?.length && h('DruxtDevelTemplate', { props: { options: this.component.options }}),
!!this.block && h('details', [h('summary', 'JSON:API resource'), h('pre', [h('code', [JSON.stringify(this.block, null, ' ')])])])
]
)
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/components/DruxtBlockRegion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default {
type,
query
})
this.blocks = collection.data
this.blocks = collection?.data
},
/**
Expand Down Expand Up @@ -207,7 +207,7 @@ export default {
slots(h) {
// Build scoped slots for each block.
const scopedSlots = {}
this.blocks.map((block) => {
this.blocks?.map((block) => {
scopedSlots[block.attributes.drupal_internal__id] = (attrs) => {
delete (attrs || {})['data-fetch-key']
return h('DruxtBlock', {
Expand All @@ -223,7 +223,7 @@ export default {
})
// Build default slot.
scopedSlots.default = (attrs) => h('div', this.blocks.map((block) =>
scopedSlots.default = (attrs) => h('div', this.blocks?.map((block) =>
this.isVisible(block)
? scopedSlots[block.attributes.drupal_internal__id](attrs)
: false
Expand Down
33 changes: 9 additions & 24 deletions packages/blocks/src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
import { DruxtBlocksNuxtModule } from './nuxtModule'

/**
* The Nuxt.js module function.
*
* Installs the module functionality in a Nuxt application.
*
* @type {Function}
* @exports default
* @name DruxtBlocksModule
* @see {@link /api/packages/blocks/nuxtModule|DruxtBlocksModule}
*
* @example <caption>nuxt.config.js</caption> @lang js
* module.exports = {
* modules: [
* 'druxt-blocks'
* ],
* druxt: {
* baseUrl: 'https://demo-api.druxtjs.org'
* }
* }
*/
export default DruxtBlocksNuxtModule

/**
* The DruxtBlocksBlockMixin adds props and computed props to your DruxtBlock
* wrapper component.
Expand Down Expand Up @@ -52,3 +28,12 @@ export default DruxtBlocksNuxtModule
* }
*/
export { DruxtBlocksRegionMixin } from './mixins/region'

/**
* Default function to alert user to incorrectly installed module.
*
* This was added as part of the @nuxt/kit update due to breaking changes.
*/
export default () => {
throw new Error("DruxtBlocks Nuxt module must be installed as 'druxt-blocks/nuxt'")
}
73 changes: 73 additions & 0 deletions packages/blocks/src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { defineNuxtModule, installModule } from '@nuxt/kit'
import { join } from 'path'
// import DruxtBlocksStorybook from './storybook'

/**
* The Nuxt.js module function.
*
* - Adds the Vue.js components to the Nuxt.js frontend.
*
* 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-blocks'
* ],
* druxt: {
* baseUrl: 'https://demo-api.druxtjs.org'
* }
* }
*
* @param {object} moduleOptions - Nuxt.js module options object.
*/
const DruxtBlocksNuxtModule = defineNuxtModule({
meta: {
name: 'druxt-blocks'
},
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 || {},
blocks: {
query: {},
...nuxt.options?.druxt?.blocks,
...moduleOptions,
}
}

// Add Druxt module.
await installModule('druxt/nuxt', options, nuxt)

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

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

export default DruxtBlocksNuxtModule

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addTemplate } from '@nuxt/kit'
import { resolve } from 'path'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { DruxtClient } from 'druxt'
Expand All @@ -8,7 +9,7 @@ const titleFn = (parts) =>
).join('/')

export default async function ({ stories }) {
const { addTemplate, options } = this
const { options } = this

// Setup DruxtClient instance.
const druxt = new DruxtClient(options.druxt.baseUrl, { ...options.druxt, proxy: { api: false } })
Expand Down
55 changes: 0 additions & 55 deletions packages/blocks/src/nuxtModule.js

This file was deleted.

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

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

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

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

test('Nuxt module', async () => {
nuxtMock.options = {
druxt: {}
}
await DruxtBlocksNuxtModule.setup({}, nuxtMock)
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import DruxtBlocksStorybook from '../src/nuxtStorybook'
import DruxtBlocksStorybook from '../../src/nuxt/storybook'

jest.mock('axios')

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

const mock = {
addTemplate: jest.fn(),
options: {
Expand Down
24 changes: 0 additions & 24 deletions packages/blocks/test/nuxtModule.test.js

This file was deleted.

0 comments on commit 7d7a575

Please sign in to comment.