Skip to content

Commit a867dbd

Browse files
aldarundpi0
authored andcommitted
fix(builder, vue-app): order of plugin execution based on order in array (#5163)
1 parent 41028a4 commit a867dbd

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

packages/builder/src/builder.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ export default class Builder {
257257
}
258258

259259
normalizePlugins() {
260+
const modes = ['client', 'server']
261+
const modePattern = new RegExp(`\\.(${modes.join('|')})(\\.\\w+)?$`)
260262
return uniqBy(
261263
this.options.plugins.map((p) => {
262264
if (typeof p === 'string') {
@@ -271,6 +273,11 @@ export default class Builder {
271273
p.mode = 'client'
272274
} else if (p.mode === undefined) {
273275
p.mode = 'all'
276+
p.src.replace(modePattern, (_, mode) => {
277+
if (modes.includes(mode)) {
278+
p.mode = mode
279+
}
280+
})
274281
} else if (!['client', 'server', 'all'].includes(p.mode)) {
275282
consola.warn(`Invalid plugin mode (server/client/all): '${p.mode}'. Falling back to 'all'`)
276283
p.mode = 'all'
@@ -559,15 +566,6 @@ export default class Builder {
559566
})
560567
}
561568

562-
const modes = ['client', 'server']
563-
const modePattern = new RegExp(`\\.(${modes.join('|')})\\.\\w+$`)
564-
pluginFiles[0].replace(modePattern, (_, mode) => {
565-
// mode in nuxt.config has higher priority
566-
if (p.mode === 'all' && modes.includes(mode)) {
567-
p.mode = mode
568-
}
569-
})
570-
571569
p.src = this.relativeToBuild(p.src)
572570
}))
573571
}

packages/builder/test/builder.generate.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('builder: builder generate', () => {
4646
watch: []
4747
}
4848
const builder = new Builder(nuxt, {})
49-
builder.normalizePlugins = jest.fn(() => [{ name: 'test_plugin' }])
49+
builder.normalizePlugins = jest.fn(() => [{ name: 'test_plugin', src: '/var/somesrc' }])
5050
builder.resolveLayouts = jest.fn(() => 'resolveLayouts')
5151
builder.resolveRoutes = jest.fn(() => 'resolveRoutes')
5252
builder.resolveStore = jest.fn(() => 'resolveStore')

packages/builder/test/builder.plugin.test.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,27 @@ describe('builder: builder plugins', () => {
121121
])
122122
})
123123

124-
test('should detect plugin mode for client/server plugins', async () => {
124+
test('should detect plugin mode for client/server plugins', () => {
125125
const nuxt = createNuxt()
126126
const builder = new Builder(nuxt, {})
127-
builder.plugins = [
127+
builder.options.plugins = [
128128
{ src: '/var/nuxt/plugins/test.js', mode: 'all' },
129-
{ src: '/var/nuxt/plugins/test.client', mode: 'all' },
130-
{ src: '/var/nuxt/plugins/test.server', mode: 'all' }
129+
{ src: '/var/nuxt/plugins/test.client' },
130+
{ src: '/var/nuxt/plugins/test.server' }
131131
]
132-
builder.relativeToBuild = jest.fn(src => `relative(${src})`)
133-
for (let step = 0; step < builder.plugins.length; step++) {
134-
Glob.mockImplementationOnce(src => [`${src.replace(/\{.*\}/, '')}.js`])
135-
}
136132

137-
await builder.resolvePlugins()
133+
const plugins = builder.normalizePlugins()
138134

139-
expect(builder.plugins).toEqual([
140-
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test.js)' },
141-
{ mode: 'client', src: 'relative(/var/nuxt/plugins/test.client)' },
142-
{ mode: 'server', src: 'relative(/var/nuxt/plugins/test.server)' }
135+
expect(plugins).toEqual([
136+
{ mode: 'all',
137+
src: 'resolveAlias(/var/nuxt/plugins/test.js)',
138+
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.js)' },
139+
{ mode: 'client',
140+
src: 'resolveAlias(/var/nuxt/plugins/test.client)',
141+
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.client)' },
142+
{ mode: 'server',
143+
src: 'resolveAlias(/var/nuxt/plugins/test.server)',
144+
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.server)' }
143145
])
144146
})
145147
})

packages/vue-app/template/index.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,21 @@ async function createApp(ssrContext) {
166166

167167
// Plugin execution
168168
<%= isTest ? '/* eslint-disable camelcase */' : '' %>
169-
<% plugins.filter(p => p.mode === 'all').forEach((plugin) => { %>
170-
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %>
171-
172-
<% if (plugins.filter(p => p.mode === 'client').length) { %>
173-
if (process.client) {
174-
<% plugins.filter(p => p.mode === 'client').forEach((plugin) => { %>
175-
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %>
176-
}<% } %>
177-
178-
<% if (plugins.filter(p => p.mode === 'server').length) { %>
179-
if (process.server) {
180-
<% plugins.filter(p => p.mode === 'server').forEach((plugin) => { %>
181-
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(app.context, inject)<% }) %>
182-
}<% } %>
169+
<% plugins.forEach((plugin) => { %>
170+
<% if (plugin.mode == 'client') { %>
171+
if (process.client && typeof <%= plugin.name %> === 'function') {
172+
await <%= plugin.name %>(app.context, inject)
173+
}
174+
<% } else if (plugin.mode == 'server') { %>
175+
if (process.server && typeof <%= plugin.name %> === 'function') {
176+
await <%= plugin.name %>(app.context, inject)
177+
}
178+
<% } else { %>
179+
if (typeof <%= plugin.name %> === 'function') {
180+
await <%= plugin.name %>(app.context, inject)
181+
}
182+
<% } %>
183+
<% }) %>
183184
<%= isTest ? '/* eslint-enable camelcase */' : '' %>
184185

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

0 commit comments

Comments
 (0)