From 021ba5a1e21582755a1fff9771d8a444ae4fb682 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Sat, 5 May 2018 21:26:03 +0100 Subject: [PATCH 1/7] feat: make babel-loader exclude configurable --- lib/builder/webpack/base.js | 6 ++---- lib/common/nuxt.config.js | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/builder/webpack/base.js b/lib/builder/webpack/base.js index 71239229e1..04d4713645 100644 --- a/lib/builder/webpack/base.js +++ b/lib/builder/webpack/base.js @@ -40,6 +40,7 @@ export default class WebpackBaseConfig { ] } + delete options.exclude return options } @@ -139,10 +140,7 @@ export default class WebpackBaseConfig { }, { test: /\.jsx?$/, - exclude: file => ( - /node_modules/.test(file) && - !/\.vue\.js/.test(file) - ), + exclude: this.options.build.babel.exclude, use: perfLoader.pool('js', { loader: 'babel-loader', options: this.getBabelOptions() diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index 2de998ad71..a2da29d2d0 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -61,7 +61,11 @@ export default { }, babel: { babelrc: false, - cacheDirectory: undefined + cacheDirectory: undefined, + exclude: file => ( + /node_modules/.test(file) && + !/\.vue\.js/.test(file) + ) }, vueLoader: {}, postcss: {}, From 8b0c2f1a6794afea524d2c66fc62cfd5c7b70672 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Sun, 6 May 2018 19:46:02 +0100 Subject: [PATCH 2/7] feat: add build.transpile to transpile npm packages --- lib/builder/webpack/base.js | 21 +++++++++++++++++++-- lib/common/nuxt.config.js | 7 ++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/builder/webpack/base.js b/lib/builder/webpack/base.js index 04d4713645..3847ff8ba7 100644 --- a/lib/builder/webpack/base.js +++ b/lib/builder/webpack/base.js @@ -40,7 +40,6 @@ export default class WebpackBaseConfig { ] } - delete options.exclude return options } @@ -140,7 +139,25 @@ export default class WebpackBaseConfig { }, { test: /\.jsx?$/, - exclude: this.options.build.babel.exclude, + exclude: file => { + // not exclude files outside node_modules + if (/node_modules/.test(file)) { + let transpile = this.options.build.transpile || [] + // transpile supports string like 'vue-lib' + if (!Array.isArray(transpile)) { + transpile = [transpile] + } + // include SFCs in node_modules + transpile.push(/\.vue\.js/) + for (let pkg of transpile) { + // item in transpile can be string or regex object + if (new RegExp(pkg).test(file)) { + return false + } + } + return true + } + }, use: perfLoader.pool('js', { loader: 'babel-loader', options: this.getBabelOptions() diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index a2da29d2d0..50e08ef430 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -61,12 +61,9 @@ export default { }, babel: { babelrc: false, - cacheDirectory: undefined, - exclude: file => ( - /node_modules/.test(file) && - !/\.vue\.js/.test(file) - ) + cacheDirectory: undefined }, + transpile: [], // Name of NPM packages to be transpiled vueLoader: {}, postcss: {}, templates: [], From 63520aa337519ff3161a790abe55377699a11492 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Sun, 6 May 2018 20:29:59 +0100 Subject: [PATCH 3/7] test: build.transpile --- test/fixtures/with-config/nuxt.config.js | 1 + test/unit/basic.dev.test.js | 29 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js index 81891cfb67..68d5d8b53f 100644 --- a/test/fixtures/with-config/nuxt.config.js +++ b/test/fixtures/with-config/nuxt.config.js @@ -62,6 +62,7 @@ export default { return null // Coverage: Return null, so defaults will be used. } }, + transpile: 'vue-test', extend(config, options) { return Object.assign({}, config, { devtool: 'nosources-source-map' diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 4faf2703f2..44b98fcc7f 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -4,6 +4,7 @@ let port const url = route => 'http://localhost:' + port + route let nuxt = null +let transpile = null describe('basic dev', () => { beforeAll(async () => { @@ -12,23 +13,31 @@ describe('basic dev', () => { debug: true, buildDir: '.nuxt-dev', build: { - stats: 'none' + stats: 'none', + transpile: [ + '\\vue.test\\.js', + /vue-test/ + ], + extend({ module: { rules } }, { isClient }) { + if (isClient) { + const babelLoader = rules.find(loader => loader.test.test('.jsx')) + transpile = babelLoader.exclude + } + } } }) nuxt = new Nuxt(config) - new Builder(nuxt).build() + await new Builder(nuxt).build() port = await getPort() await nuxt.listen(port, 'localhost') }) - // TODO: enable test when style-loader.js:60 was resolved - // test.serial('/extractCSS', async t => { - // const window = await nuxt.renderAndGetWindow(url('/extractCSS')) - // const html = window.document.head.innerHTML - // t.true(html.includes('vendor.css')) - // t.true(!html.includes('30px')) - // t.is(window.getComputedStyle(window.document.body).getPropertyValue('font-size'), '30px') - // }) + test('Config: build.transpile', async () => { + expect(transpile('vue-test')).toBeUndefined() + expect(transpile('node_modules/test.js')).toBe(true) + expect(transpile('node_modules/vue-test')).toBe(false) + expect(transpile('node_modules/test.vue.js')).toBe(false) + }) test('/stateless', async () => { const window = await nuxt.renderAndGetWindow(url('/stateless')) From a888136ac4b7dd548cadb4646dda2e1a7e2b73d7 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Sun, 6 May 2018 20:35:32 +0100 Subject: [PATCH 4/7] test: add string for build.transpile --- test/unit/basic.dev.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 44b98fcc7f..cb251b949d 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -15,7 +15,7 @@ describe('basic dev', () => { build: { stats: 'none', transpile: [ - '\\vue.test\\.js', + 'vue\\.test\\.js', /vue-test/ ], extend({ module: { rules } }, { isClient }) { @@ -36,6 +36,7 @@ describe('basic dev', () => { expect(transpile('vue-test')).toBeUndefined() expect(transpile('node_modules/test.js')).toBe(true) expect(transpile('node_modules/vue-test')).toBe(false) + expect(transpile('node_modules/vue.test.js')).toBe(false) expect(transpile('node_modules/test.vue.js')).toBe(false) }) From 13ae634c744c531c360f86c817005d30b7c09487 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Sun, 6 May 2018 20:48:19 +0100 Subject: [PATCH 5/7] refactor: simplify build.transpile --- lib/builder/webpack/base.js | 9 +-------- lib/common/options.js | 4 ++++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/builder/webpack/base.js b/lib/builder/webpack/base.js index 3847ff8ba7..8681d6647b 100644 --- a/lib/builder/webpack/base.js +++ b/lib/builder/webpack/base.js @@ -142,14 +142,7 @@ export default class WebpackBaseConfig { exclude: file => { // not exclude files outside node_modules if (/node_modules/.test(file)) { - let transpile = this.options.build.transpile || [] - // transpile supports string like 'vue-lib' - if (!Array.isArray(transpile)) { - transpile = [transpile] - } - // include SFCs in node_modules - transpile.push(/\.vue\.js/) - for (let pkg of transpile) { + for (let pkg of this.options.build.transpile) { // item in transpile can be string or regex object if (new RegExp(pkg).test(file)) { return false diff --git a/lib/common/options.js b/lib/common/options.js index a8fd6e2b8d..6494fe747f 100644 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -187,5 +187,9 @@ Options.from = function (_options) { if (options.dev) { options.build.extractCSS = false } + + // include SFCs in node_modules + options.build.transpile = [/\.vue\.js/].concat(options.build.transpile || []) + return options } From f275fc8069ab2cfb9f0d824674bd274ca3050638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 14 May 2018 09:21:40 +0200 Subject: [PATCH 6/7] Update transpile to return the opposite of exclude --- test/unit/basic.dev.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index cb251b949d..090d077d35 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -21,7 +21,7 @@ describe('basic dev', () => { extend({ module: { rules } }, { isClient }) { if (isClient) { const babelLoader = rules.find(loader => loader.test.test('.jsx')) - transpile = babelLoader.exclude + transpile = (file) => !babelLoader.exclude(file) } } } @@ -34,10 +34,10 @@ describe('basic dev', () => { test('Config: build.transpile', async () => { expect(transpile('vue-test')).toBeUndefined() - expect(transpile('node_modules/test.js')).toBe(true) - expect(transpile('node_modules/vue-test')).toBe(false) - expect(transpile('node_modules/vue.test.js')).toBe(false) - expect(transpile('node_modules/test.vue.js')).toBe(false) + expect(transpile('node_modules/test.js')).toBe(false) + expect(transpile('node_modules/vue-test')).toBe(true) + expect(transpile('node_modules/vue.test.js')).toBe(true) + expect(transpile('node_modules/test.vue.js')).toBe(true) }) test('/stateless', async () => { From 085f18a339cbdf34047ba257b24387dbea343031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 14 May 2018 09:22:44 +0200 Subject: [PATCH 7/7] Update basic.dev.test.js --- test/unit/basic.dev.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 090d077d35..32837300ae 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -33,7 +33,7 @@ describe('basic dev', () => { }) test('Config: build.transpile', async () => { - expect(transpile('vue-test')).toBeUndefined() + expect(transpile('vue-test')).toBe(true) expect(transpile('node_modules/test.js')).toBe(false) expect(transpile('node_modules/vue-test')).toBe(true) expect(transpile('node_modules/vue.test.js')).toBe(true)