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) {
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 () {

View File

@ -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
})
)