Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mode for plugin #4592

Merged
merged 7 commits into from Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/builder/src/builder.js
Expand Up @@ -110,9 +110,18 @@ export default class Builder {
/[^a-zA-Z?\d\s:]/g,
''
)

// TODO: remove deprecated check in Nuxt 3
if (p.ssr === false) {
p.mode = 'client'
consola.warn(`ssr in plugin options has been deprecated, please use mode instead`)
clarkdo marked this conversation as resolved.
Show resolved Hide resolved
} else if (!['client', 'server'].includes(p.mode)) {
p.mode = 'all'
clarkdo marked this conversation as resolved.
Show resolved Hide resolved
}

return {
src: this.nuxt.resolver.resolveAlias(p.src),
ssr: p.ssr !== false,
mode: p.mode,
name: 'nuxt_plugin_' + pluginBaseName + '_' + hash(p.src)
}
}),
Expand All @@ -135,6 +144,15 @@ export default class Builder {
})
}

const modes = ['client', 'server']
const modernPattern = new RegExp(`\\.(${modes.join('|')})\\.\\w+$`)
pluginFiles[0].replace(modernPattern, (_, 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 Expand Up @@ -204,8 +222,6 @@ export default class Builder {
// Generate routes and interpret the template files
await this.generateRoutesAndFiles()

await this.resolvePlugins()

// Start bundle build: webpack, rollup, parcel...
await this.bundleBuilder.build()

Expand All @@ -224,6 +240,8 @@ export default class Builder {
// Plugins
this.plugins = Array.from(this.normalizePlugins())

await this.resolvePlugins()
clarkdo marked this conversation as resolved.
Show resolved Hide resolved

// -- Templates --
let templatesFiles = Array.from(this.template.templatesFiles)

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/module.js
Expand Up @@ -63,7 +63,9 @@ export default class ModuleContainer {
// Add to nuxt plugins
this.options.plugins.unshift({
src: path.join(this.options.buildDir, dst),
ssr: template.ssr
// TODO: remove deprecated option in Nuxt 3
ssr: template.ssr,
mode: template.mode
})
}

Expand Down
15 changes: 11 additions & 4 deletions packages/vue-app/template/index.js
Expand Up @@ -12,7 +12,7 @@ import { setContext, getLocation, getRouteData, normalizeError } from './utils'

/* Plugins */
<%= isTest ? '/* eslint-disable camelcase */' : '' %>
<% plugins.forEach((plugin) => { %>import <%= plugin.name %> from '<%= plugin.name %>' // Source: <%= relativeToBuild(plugin.src) %><%= (plugin.ssr===false) ? ' (ssr: false)' : '' %>
<% plugins.forEach((plugin) => { %>import <%= plugin.name %> from '<%= plugin.name %>' // Source: <%= plugin.src %> (mode: '<%= plugin.mode %>')
<% }) %>
<%= isTest ? '/* eslint-enable camelcase */' : '' %>

Expand Down Expand Up @@ -168,11 +168,18 @@ async function createApp(ssrContext) {

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

<% if (plugins.filter(p => p.mode === 'client').length) { %>
if (process.client) {
<% plugins.filter((p) => !p.ssr).forEach((plugin) => { %>
<% 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)<% }) %>
}<% } %>
<%= isTest ? '/* eslint-enable camelcase */' : '' %>
Expand Down
7 changes: 3 additions & 4 deletions packages/webpack/src/builder.js
Expand Up @@ -63,18 +63,17 @@ export class WebpackBundler {
for (const p of this.context.plugins) {
// Client config
if (!clientConfig.resolve.alias[p.name]) {
clientConfig.resolve.alias[p.name] = p.src
clientConfig.resolve.alias[p.name] = p.mode === 'server' ? './empty.js' : p.src
}

// Server config
if (serverConfig && !serverConfig.resolve.alias[p.name]) {
// Alias to noop for ssr:false plugins
serverConfig.resolve.alias[p.name] = p.ssr ? p.src : './empty.js'
serverConfig.resolve.alias[p.name] = p.mode === 'client' ? './empty.js' : p.src
}

// Modern config
if (modernConfig && !modernConfig.resolve.alias[p.name]) {
modernConfig.resolve.alias[p.name] = p.src
modernConfig.resolve.alias[p.name] = p.mode === 'client' ? './empty.js' : p.src
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/with-config/nuxt.config.js
Expand Up @@ -38,6 +38,8 @@ export default {
plugins: [
'~/plugins/test',
'~/plugins/test.plugin',
'~/plugins/test.client',
'~/plugins/test.server',
{ src: '~/plugins/only-client.js', ssr: false }
],
loading: '~/components/loading',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/with-config/plugins/test.client.js
@@ -0,0 +1 @@
window.__test_plugin_client = 'test_plugin_client'
1 change: 1 addition & 0 deletions test/fixtures/with-config/plugins/test.server.js
@@ -0,0 +1 @@
global.__test_plugin_server = 'test_plugin_server'
5 changes: 4 additions & 1 deletion test/fixtures/with-config/with-config.test.js
Expand Up @@ -14,12 +14,15 @@ const hooks = [

describe('with-config', () => {
buildFixture('with-config', () => {
expect(consola.warn).toHaveBeenCalledTimes(4)
expect(consola.warn).toHaveBeenCalledTimes(5)
expect(consola.fatal).toHaveBeenCalledTimes(0)
expect(consola.warn.mock.calls).toMatchObject([
[
'Unknown mode: unknown. Falling back to universal'
],
[
'ssr in plugin options has been deprecated, please use mode instead'
],
[{
message: 'Found 2 plugins that match the configuration, suggest to specify extension:',
additional: expect.stringContaining('plugins/test.json')
Expand Down
2 changes: 2 additions & 0 deletions test/unit/with-config.test.js
Expand Up @@ -91,6 +91,8 @@ describe('with-config', () => {

expect(window.__test_plugin).toBe(true)
expect(window.__test_plugin_ext).toBe(true)
expect(window.__test_plugin_client).toBe('test_plugin_client')
expect(window.__test_plugin_server).toBeUndefined()
})

test('/test/about (custom layout)', async () => {
Expand Down