Skip to content

Commit

Permalink
fix(builder, vue-app): order of plugin execution based on order in ar…
Browse files Browse the repository at this point in the history
…ray (#5163)
  • Loading branch information
aldarund authored and pi0 committed Mar 7, 2019
1 parent 41028a4 commit a867dbd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
16 changes: 7 additions & 9 deletions packages/builder/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ export default class Builder {
}

normalizePlugins() {
const modes = ['client', 'server']
const modePattern = new RegExp(`\\.(${modes.join('|')})(\\.\\w+)?$`)
return uniqBy(
this.options.plugins.map((p) => {
if (typeof p === 'string') {
Expand All @@ -271,6 +273,11 @@ export default class Builder {
p.mode = 'client'
} else if (p.mode === undefined) {
p.mode = 'all'
p.src.replace(modePattern, (_, mode) => {
if (modes.includes(mode)) {
p.mode = mode
}
})
} else if (!['client', 'server', 'all'].includes(p.mode)) {
consola.warn(`Invalid plugin mode (server/client/all): '${p.mode}'. Falling back to 'all'`)
p.mode = 'all'
Expand Down Expand Up @@ -559,15 +566,6 @@ export default class Builder {
})
}

const modes = ['client', 'server']
const modePattern = new RegExp(`\\.(${modes.join('|')})\\.\\w+$`)
pluginFiles[0].replace(modePattern, (_, mode) => {
// mode in nuxt.config has higher priority
if (p.mode === 'all' && modes.includes(mode)) {
p.mode = mode
}
})

p.src = this.relativeToBuild(p.src)
}))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/test/builder.generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('builder: builder generate', () => {
watch: []
}
const builder = new Builder(nuxt, {})
builder.normalizePlugins = jest.fn(() => [{ name: 'test_plugin' }])
builder.normalizePlugins = jest.fn(() => [{ name: 'test_plugin', src: '/var/somesrc' }])
builder.resolveLayouts = jest.fn(() => 'resolveLayouts')
builder.resolveRoutes = jest.fn(() => 'resolveRoutes')
builder.resolveStore = jest.fn(() => 'resolveStore')
Expand Down
28 changes: 15 additions & 13 deletions packages/builder/test/builder.plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,27 @@ describe('builder: builder plugins', () => {
])
})

test('should detect plugin mode for client/server plugins', async () => {
test('should detect plugin mode for client/server plugins', () => {
const nuxt = createNuxt()
const builder = new Builder(nuxt, {})
builder.plugins = [
builder.options.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' },
{ src: '/var/nuxt/plugins/test.client', mode: 'all' },
{ src: '/var/nuxt/plugins/test.server', mode: 'all' }
{ src: '/var/nuxt/plugins/test.client' },
{ src: '/var/nuxt/plugins/test.server' }
]
builder.relativeToBuild = jest.fn(src => `relative(${src})`)
for (let step = 0; step < builder.plugins.length; step++) {
Glob.mockImplementationOnce(src => [`${src.replace(/\{.*\}/, '')}.js`])
}

await builder.resolvePlugins()
const plugins = builder.normalizePlugins()

expect(builder.plugins).toEqual([
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test.js)' },
{ mode: 'client', src: 'relative(/var/nuxt/plugins/test.client)' },
{ mode: 'server', src: 'relative(/var/nuxt/plugins/test.server)' }
expect(plugins).toEqual([
{ mode: 'all',
src: 'resolveAlias(/var/nuxt/plugins/test.js)',
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.js)' },
{ mode: 'client',
src: 'resolveAlias(/var/nuxt/plugins/test.client)',
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.client)' },
{ mode: 'server',
src: 'resolveAlias(/var/nuxt/plugins/test.server)',
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.server)' }
])
})
})
29 changes: 15 additions & 14 deletions packages/vue-app/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,21 @@ async function createApp(ssrContext) {

// Plugin execution
<%= isTest ? '/* eslint-disable camelcase */' : '' %>
<% plugins.filter(p => p.mode === 'all').forEach((plugin) => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %>

<% if (plugins.filter(p => p.mode === 'client').length) { %>
if (process.client) {
<% plugins.filter(p => p.mode === 'client').forEach((plugin) => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %>
}<% } %>

<% if (plugins.filter(p => p.mode === 'server').length) { %>
if (process.server) {
<% plugins.filter(p => p.mode === 'server').forEach((plugin) => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %>
}<% } %>
<% plugins.forEach((plugin) => { %>
<% if (plugin.mode == 'client') { %>
if (process.client && typeof <%= plugin.name %> === 'function') {
await <%= plugin.name %>(app.context, inject)
}
<% } else if (plugin.mode == 'server') { %>
if (process.server && typeof <%= plugin.name %> === 'function') {
await <%= plugin.name %>(app.context, inject)
}
<% } else { %>
if (typeof <%= plugin.name %> === 'function') {
await <%= plugin.name %>(app.context, inject)
}
<% } %>
<% }) %>
<%= isTest ? '/* eslint-enable camelcase */' : '' %>

// If server-side, wait for async component to be resolved first
Expand Down

0 comments on commit a867dbd

Please sign in to comment.