refactor(webpack): simplify transpile normalization (#6179)

This commit is contained in:
Xin Du (Clark) 2019-08-04 15:23:33 +01:00 committed by GitHub
parent e8f1532124
commit 7c98571ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 29 deletions

View File

@ -22,7 +22,6 @@ export default class WebpackBaseConfig {
constructor (builder) { constructor (builder) {
this.builder = builder this.builder = builder
this.buildContext = builder.buildContext this.buildContext = builder.buildContext
this.modulesToTranspile = this.normalizeTranspile()
} }
get colors () { get colors () {
@ -55,21 +54,29 @@ export default class WebpackBaseConfig {
return this.buildContext.buildOptions.loaders return this.buildContext.buildOptions.loaders
} }
normalizeTranspile () { get modulesToTranspile () {
// include SFCs in node_modules return [
const items = [/\.vue\.js/i] /\.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) { for (let pattern of this.buildContext.buildOptions.transpile) {
if (typeof pattern === 'function') { if (typeof pattern === 'function') {
pattern = pattern(this.nuxtEnv) pattern = pattern(this.nuxtEnv)
} }
if (pattern instanceof RegExp) { if (pattern instanceof RegExp) {
items.push(pattern) transpile.push(pattern)
} else if (typeof pattern === 'string') { } else if (typeof pattern === 'string') {
const posixModule = pattern.replace(/\\/g, '/') 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 () { getBabelOptions () {

View File

@ -1,7 +1,6 @@
import path from 'path' import path from 'path'
import fs from 'fs' import fs from 'fs'
import webpack from 'webpack' import webpack from 'webpack'
import escapeRegExp from 'lodash/escapeRegExp'
import nodeExternals from 'webpack-node-externals' import nodeExternals from 'webpack-node-externals'
import VueSSRServerPlugin from '../plugins/vue/server' import VueSSRServerPlugin from '../plugins/vue/server'
@ -13,32 +12,19 @@ export default class WebpackServerConfig extends WebpackBaseConfig {
super(...args) super(...args)
this.name = 'server' this.name = 'server'
this.isServer = true 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 () { get devtool () {
return 'cheap-module-source-map' return 'cheap-module-source-map'
} }
get externalsWhitelist () {
return [
/\.(?!js(x|on)?$)/i,
...this.normalizeTranspile()
]
}
env () { env () {
return Object.assign(super.env(), { return Object.assign(super.env(), {
'process.env.VUE_ENV': JSON.stringify('server'), 'process.env.VUE_ENV': JSON.stringify('server'),
@ -117,7 +103,7 @@ export default class WebpackServerConfig extends WebpackBaseConfig {
if (fs.existsSync(dir)) { if (fs.existsSync(dir)) {
config.externals.push( config.externals.push(
nodeExternals({ nodeExternals({
whitelist: this.whitelist, whitelist: this.externalsWhitelist,
modulesDir: dir modulesDir: dir
}) })
) )