From 7c98571ac990e3e7eee54af6c46d8266aabf414e Mon Sep 17 00:00:00 2001 From: "Xin Du (Clark)" Date: Sun, 4 Aug 2019 15:23:33 +0100 Subject: [PATCH] refactor(webpack): simplify transpile normalization (#6179) --- packages/webpack/src/config/base.js | 21 ++++++++++++------- packages/webpack/src/config/server.js | 30 +++++++-------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/packages/webpack/src/config/base.js b/packages/webpack/src/config/base.js index 1a9e2fc697..699915dd1c 100644 --- a/packages/webpack/src/config/base.js +++ b/packages/webpack/src/config/base.js @@ -22,7 +22,6 @@ export default class WebpackBaseConfig { constructor (builder) { this.builder = builder this.buildContext = builder.buildContext - this.modulesToTranspile = this.normalizeTranspile() } get colors () { @@ -55,21 +54,29 @@ export default class WebpackBaseConfig { return this.buildContext.buildOptions.loaders } - normalizeTranspile () { - // include SFCs in node_modules - const items = [/\.vue\.js/i] + get modulesToTranspile () { + return [ + /\.vue\.js/i, // include SFCs in node_modules + ...this.normalizeTranspile({ pathNormalize: true }) + ] + } + + normalizeTranspile ({ pathNormalize = false } = {}) { + const transpile = [] for (let pattern of this.buildContext.buildOptions.transpile) { if (typeof pattern === 'function') { pattern = pattern(this.nuxtEnv) } if (pattern instanceof RegExp) { - items.push(pattern) + transpile.push(pattern) } else if (typeof pattern === 'string') { const posixModule = pattern.replace(/\\/g, '/') - items.push(new RegExp(escapeRegExp(path.normalize(posixModule)))) + transpile.push(new RegExp(escapeRegExp( + pathNormalize ? path.normalize(posixModule) : posixModule + ))) } } - return items + return transpile } getBabelOptions () { diff --git a/packages/webpack/src/config/server.js b/packages/webpack/src/config/server.js index a67cc366a9..24803dc610 100644 --- a/packages/webpack/src/config/server.js +++ b/packages/webpack/src/config/server.js @@ -1,7 +1,6 @@ import path from 'path' import fs from 'fs' import webpack from 'webpack' -import escapeRegExp from 'lodash/escapeRegExp' import nodeExternals from 'webpack-node-externals' import VueSSRServerPlugin from '../plugins/vue/server' @@ -13,32 +12,19 @@ export default class WebpackServerConfig extends WebpackBaseConfig { super(...args) this.name = 'server' this.isServer = true - this.whitelist = this.normalizeWhitelist() - } - - normalizeWhitelist () { - const whitelist = [ - /\.(?!js(x|on)?$)/i - ] - for (let pattern of this.buildContext.buildOptions.transpile) { - if (typeof pattern === 'function') { - pattern = pattern(this.nuxtEnv) - } - if (pattern instanceof RegExp) { - whitelist.push(pattern) - } else if (typeof pattern === 'string') { - const posixModule = pattern.replace(/\\/g, '/') - whitelist.push(new RegExp(escapeRegExp(posixModule))) - } - } - - return whitelist } get devtool () { return 'cheap-module-source-map' } + get externalsWhitelist () { + return [ + /\.(?!js(x|on)?$)/i, + ...this.normalizeTranspile() + ] + } + env () { return Object.assign(super.env(), { 'process.env.VUE_ENV': JSON.stringify('server'), @@ -117,7 +103,7 @@ export default class WebpackServerConfig extends WebpackBaseConfig { if (fs.existsSync(dir)) { config.externals.push( nodeExternals({ - whitelist: this.whitelist, + whitelist: this.externalsWhitelist, modulesDir: dir }) )