Skip to content

Commit 7c98571

Browse files
authored
refactor(webpack): simplify transpile normalization (nuxt#6179)
1 parent e8f1532 commit 7c98571

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

packages/webpack/src/config/base.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export default class WebpackBaseConfig {
2222
constructor (builder) {
2323
this.builder = builder
2424
this.buildContext = builder.buildContext
25-
this.modulesToTranspile = this.normalizeTranspile()
2625
}
2726

2827
get colors () {
@@ -55,21 +54,29 @@ export default class WebpackBaseConfig {
5554
return this.buildContext.buildOptions.loaders
5655
}
5756

58-
normalizeTranspile () {
59-
// include SFCs in node_modules
60-
const items = [/\.vue\.js/i]
57+
get modulesToTranspile () {
58+
return [
59+
/\.vue\.js/i, // include SFCs in node_modules
60+
...this.normalizeTranspile({ pathNormalize: true })
61+
]
62+
}
63+
64+
normalizeTranspile ({ pathNormalize = false } = {}) {
65+
const transpile = []
6166
for (let pattern of this.buildContext.buildOptions.transpile) {
6267
if (typeof pattern === 'function') {
6368
pattern = pattern(this.nuxtEnv)
6469
}
6570
if (pattern instanceof RegExp) {
66-
items.push(pattern)
71+
transpile.push(pattern)
6772
} else if (typeof pattern === 'string') {
6873
const posixModule = pattern.replace(/\\/g, '/')
69-
items.push(new RegExp(escapeRegExp(path.normalize(posixModule))))
74+
transpile.push(new RegExp(escapeRegExp(
75+
pathNormalize ? path.normalize(posixModule) : posixModule
76+
)))
7077
}
7178
}
72-
return items
79+
return transpile
7380
}
7481

7582
getBabelOptions () {

packages/webpack/src/config/server.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path'
22
import fs from 'fs'
33
import webpack from 'webpack'
4-
import escapeRegExp from 'lodash/escapeRegExp'
54
import nodeExternals from 'webpack-node-externals'
65

76
import VueSSRServerPlugin from '../plugins/vue/server'
@@ -13,32 +12,19 @@ export default class WebpackServerConfig extends WebpackBaseConfig {
1312
super(...args)
1413
this.name = 'server'
1514
this.isServer = true
16-
this.whitelist = this.normalizeWhitelist()
17-
}
18-
19-
normalizeWhitelist () {
20-
const whitelist = [
21-
/\.(?!js(x|on)?$)/i
22-
]
23-
for (let pattern of this.buildContext.buildOptions.transpile) {
24-
if (typeof pattern === 'function') {
25-
pattern = pattern(this.nuxtEnv)
26-
}
27-
if (pattern instanceof RegExp) {
28-
whitelist.push(pattern)
29-
} else if (typeof pattern === 'string') {
30-
const posixModule = pattern.replace(/\\/g, '/')
31-
whitelist.push(new RegExp(escapeRegExp(posixModule)))
32-
}
33-
}
34-
35-
return whitelist
3615
}
3716

3817
get devtool () {
3918
return 'cheap-module-source-map'
4019
}
4120

21+
get externalsWhitelist () {
22+
return [
23+
/\.(?!js(x|on)?$)/i,
24+
...this.normalizeTranspile()
25+
]
26+
}
27+
4228
env () {
4329
return Object.assign(super.env(), {
4430
'process.env.VUE_ENV': JSON.stringify('server'),
@@ -117,7 +103,7 @@ export default class WebpackServerConfig extends WebpackBaseConfig {
117103
if (fs.existsSync(dir)) {
118104
config.externals.push(
119105
nodeExternals({
120-
whitelist: this.whitelist,
106+
whitelist: this.externalsWhitelist,
121107
modulesDir: dir
122108
})
123109
)

0 commit comments

Comments
 (0)