Skip to content

Commit fb71653

Browse files
jkzinghaoqunjiang
authored andcommittedJan 30, 2019
fix(cli-plugin-babel): transpileDependencies should only match packages inside node_modules, close #3057 (#3229)
1 parent 10c253a commit fb71653

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed
 

‎packages/@vue/cli-plugin-babel/__tests__/transpileDependencies.spec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ beforeAll(async () => {
4040

4141
$packageJson = JSON.parse($packageJson)
4242
$packageJson.dependencies['external-dep'] = '1.0.0'
43+
$packageJson.dependencies['@scope/external-dep'] = '1.0.0'
4344
$packageJson = JSON.stringify($packageJson)
4445

4546
await project.write(
@@ -70,10 +71,23 @@ test('dep from node_modules should not been transpiled', async () => {
7071
test('dep from node_modules should been transpiled', async () => {
7172
await project.write(
7273
'vue.config.js',
73-
`module.exports = { transpileDependencies: ['external-dep'] }`
74+
`module.exports = { transpileDependencies: ['external-dep', '@scope/external-dep'] }`
7475
)
7576
await project.run('vue-cli-service build')
7677
expect(await readVendorFile()).toMatch('return "__TEST__"')
7778

7879
expect(await readVendorFile()).toMatch('return "__SCOPE_TEST__"')
7980
})
81+
82+
// https://github.com/vuejs/vue-cli/issues/3057
83+
test('only transpile package with same name specified in transpileDependencies', async () => {
84+
await project.write(
85+
'vue.config.js',
86+
`module.exports = { transpileDependencies: ['babel-transpile-deps'] }`
87+
)
88+
try {
89+
await project.run('vue-cli-service build')
90+
} catch (e) {}
91+
expect(await readVendorFile()).toMatch('() => "__TEST__"')
92+
expect(await readVendorFile()).toMatch('() => "__SCOPE_TEST__"')
93+
})

‎packages/@vue/cli-plugin-babel/index.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
const path = require('path')
2+
const { isWindows } = require('@vue/cli-shared-utils')
3+
4+
function genTranspileDepRegex (transpileDependencies) {
5+
const deps = transpileDependencies.map(dep => {
6+
if (typeof dep === 'string') {
7+
const depPath = path.join('node_modules', dep, '/')
8+
return isWindows
9+
? depPath.replace(/\\/g, '\\\\') // double escape for windows style path
10+
: depPath
11+
} else if (dep instanceof RegExp) {
12+
return dep.source
13+
}
14+
})
15+
return deps.length ? new RegExp(deps.join('|')) : null
16+
}
217

318
module.exports = (api, options) => {
419
const useThreads = process.env.NODE_ENV === 'production' && options.parallel
520
const cliServicePath = require('path').dirname(require.resolve('@vue/cli-service'))
21+
const transpileDepRegex = genTranspileDepRegex(options.transpileDependencies)
622

723
api.chainWebpack(webpackConfig => {
824
webpackConfig.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))
@@ -21,13 +37,7 @@ module.exports = (api, options) => {
2137
return true
2238
}
2339
// check if this is something the user explicitly wants to transpile
24-
if (options.transpileDependencies.some(dep => {
25-
if (typeof dep === 'string') {
26-
return filepath.includes(path.normalize(dep))
27-
} else {
28-
return filepath.match(dep)
29-
}
30-
})) {
40+
if (transpileDepRegex && transpileDepRegex.test(filepath)) {
3141
return false
3242
}
3343
// Don't transpile node_modules

‎packages/@vue/cli-plugin-babel/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dependencies": {
2222
"@babel/core": "^7.0.0",
2323
"@vue/babel-preset-app": "^3.3.0",
24+
"@vue/cli-shared-utils": "^3.3.0",
2425
"babel-loader": "^8.0.4"
2526
},
2627
"publishConfig": {

0 commit comments

Comments
 (0)
Please sign in to comment.