fix(builder, vue-app): order of plugin execution based on order in array (#5163)

This commit is contained in:
Dmitry Molotkov 2019-03-07 22:33:20 +03:00 committed by Pooya Parsa
parent 41028a407b
commit a867dbd34b
4 changed files with 38 additions and 37 deletions

View File

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

View File

@ -46,7 +46,7 @@ describe('builder: builder generate', () => {
watch: [] watch: []
} }
const builder = new Builder(nuxt, {}) 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.resolveLayouts = jest.fn(() => 'resolveLayouts')
builder.resolveRoutes = jest.fn(() => 'resolveRoutes') builder.resolveRoutes = jest.fn(() => 'resolveRoutes')
builder.resolveStore = jest.fn(() => 'resolveStore') builder.resolveStore = jest.fn(() => 'resolveStore')

View File

@ -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 nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, {})
builder.plugins = [ builder.options.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' }, { src: '/var/nuxt/plugins/test.js', mode: 'all' },
{ src: '/var/nuxt/plugins/test.client', mode: 'all' }, { src: '/var/nuxt/plugins/test.client' },
{ src: '/var/nuxt/plugins/test.server', mode: 'all' } { 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([ expect(plugins).toEqual([
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test.js)' }, { mode: 'all',
{ mode: 'client', src: 'relative(/var/nuxt/plugins/test.client)' }, src: 'resolveAlias(/var/nuxt/plugins/test.js)',
{ mode: 'server', src: 'relative(/var/nuxt/plugins/test.server)' } '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)' }
]) ])
}) })
}) })

View File

@ -166,20 +166,21 @@ async function createApp(ssrContext) {
// Plugin execution // Plugin execution
<%= isTest ? '/* eslint-disable camelcase */' : '' %> <%= isTest ? '/* eslint-disable camelcase */' : '' %>
<% plugins.filter(p => p.mode === 'all').forEach((plugin) => { %> <% plugins.forEach((plugin) => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %> <% if (plugin.mode == 'client') { %>
if (process.client && typeof <%= plugin.name %> === 'function') {
<% if (plugins.filter(p => p.mode === 'client').length) { %> await <%= plugin.name %>(app.context, inject)
if (process.client) { }
<% plugins.filter(p => p.mode === 'client').forEach((plugin) => { %> <% } else if (plugin.mode == 'server') { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %> if (process.server && typeof <%= plugin.name %> === 'function') {
}<% } %> await <%= plugin.name %>(app.context, inject)
}
<% if (plugins.filter(p => p.mode === 'server').length) { %> <% } else { %>
if (process.server) { if (typeof <%= plugin.name %> === 'function') {
<% plugins.filter(p => p.mode === 'server').forEach((plugin) => { %> await <%= plugin.name %>(app.context, inject)
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %> }
}<% } %> <% } %>
<% }) %>
<%= isTest ? '/* eslint-enable camelcase */' : '' %> <%= isTest ? '/* eslint-enable camelcase */' : '' %>
// If server-side, wait for async component to be resolved first // If server-side, wait for async component to be resolved first