From e8f1532124c349224a756dbf5d2227d6f8f6d2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sat, 3 Aug 2019 22:09:38 +0200 Subject: [PATCH] feat(webpack): allow function entries for `build.transpile` (#6120) --- packages/webpack/src/config/base.js | 10 +++++++--- packages/webpack/src/config/server.js | 7 +++++-- test/unit/basic.dev.test.js | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/webpack/src/config/base.js b/packages/webpack/src/config/base.js index a81ef7b336..1a9e2fc697 100644 --- a/packages/webpack/src/config/base.js +++ b/packages/webpack/src/config/base.js @@ -38,7 +38,8 @@ export default class WebpackBaseConfig { isDev: this.dev, isServer: this.isServer, isClient: !this.isServer, - isModern: Boolean(this.isModern) + isModern: Boolean(this.isModern), + isLegacy: Boolean(!this.isModern) } } @@ -57,10 +58,13 @@ export default class WebpackBaseConfig { normalizeTranspile () { // include SFCs in node_modules const items = [/\.vue\.js/i] - for (const pattern of this.buildContext.buildOptions.transpile) { + for (let pattern of this.buildContext.buildOptions.transpile) { + if (typeof pattern === 'function') { + pattern = pattern(this.nuxtEnv) + } if (pattern instanceof RegExp) { items.push(pattern) - } else { + } else if (typeof pattern === 'string') { const posixModule = pattern.replace(/\\/g, '/') items.push(new RegExp(escapeRegExp(path.normalize(posixModule)))) } diff --git a/packages/webpack/src/config/server.js b/packages/webpack/src/config/server.js index bdd20602f8..a67cc366a9 100644 --- a/packages/webpack/src/config/server.js +++ b/packages/webpack/src/config/server.js @@ -20,10 +20,13 @@ export default class WebpackServerConfig extends WebpackBaseConfig { const whitelist = [ /\.(?!js(x|on)?$)/i ] - for (const pattern of this.buildContext.buildOptions.transpile) { + for (let pattern of this.buildContext.buildOptions.transpile) { + if (typeof pattern === 'function') { + pattern = pattern(this.nuxtEnv) + } if (pattern instanceof RegExp) { whitelist.push(pattern) - } else { + } else if (typeof pattern === 'string') { const posixModule = pattern.replace(/\\/g, '/') whitelist.push(new RegExp(escapeRegExp(posixModule))) } diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 22a1fa29a5..499f9a0a20 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -30,7 +30,8 @@ describe('basic dev', () => { '@scoped/packageA', '@scoped\\packageB', 'vue.test.js', - /vue-test/ + /vue-test/, + ({ isModern }) => isModern ? 'modern-test' : 'normal-test' ], loaders: { cssModules: { @@ -77,6 +78,7 @@ describe('basic dev', () => { expect(transpile(path.normalize('node_modules/test.vue.js'))).toBe(true) expect(transpile(path.normalize('node_modules/@scoped/packageA/src/index.js'))).toBe(true) expect(transpile(path.normalize('node_modules/@scoped/packageB/src/index.js'))).toBe(true) + expect(transpile(path.normalize('node_modules/normal-test'))).toBe(true) }) test('Config: build.filenames', () => {