1 | const path = require('path')
|
2 | const babel = require('@babel/core')
|
3 | const { isWindows } = require('@vue/cli-shared-utils')
|
4 |
|
5 | function genTranspileDepRegex (transpileDependencies) {
|
6 | const deps = transpileDependencies.map(dep => {
|
7 | if (typeof dep === 'string') {
|
8 | const depPath = path.join('node_modules', dep, '/')
|
9 | return isWindows
|
10 | ? depPath.replace(/\\/g, '\\\\')
|
11 | : depPath
|
12 | } else if (dep instanceof RegExp) {
|
13 | return dep.source
|
14 | }
|
15 | })
|
16 | return deps.length ? new RegExp(deps.join('|')) : null
|
17 | }
|
18 |
|
19 | module.exports = (api, options) => {
|
20 | const useThreads = process.env.NODE_ENV === 'production' && !!options.parallel
|
21 | const cliServicePath = path.dirname(require.resolve('@vue/cli-service'))
|
22 | const transpileDepRegex = genTranspileDepRegex(options.transpileDependencies)
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | babel.loadPartialConfigSync({ filename: api.resolve('src/main.js') })
|
30 |
|
31 | api.chainWebpack(webpackConfig => {
|
32 | webpackConfig.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))
|
33 |
|
34 | const jsRule = webpackConfig.module
|
35 | .rule('js')
|
36 | .test(/\.m?jsx?$/)
|
37 | .exclude
|
38 | .add(filepath => {
|
39 |
|
40 | if (/\.vue\.jsx?$/.test(filepath)) {
|
41 | return false
|
42 | }
|
43 |
|
44 | if (filepath.startsWith(cliServicePath)) {
|
45 | return true
|
46 | }
|
47 |
|
48 |
|
49 | if (
|
50 | process.env.VUE_CLI_TRANSPILE_BABEL_RUNTIME &&
|
51 | filepath.includes(path.join('@babel', 'runtime'))
|
52 | ) {
|
53 | return false
|
54 | }
|
55 |
|
56 |
|
57 | if (transpileDepRegex && transpileDepRegex.test(filepath)) {
|
58 | return false
|
59 | }
|
60 |
|
61 | return /node_modules/.test(filepath)
|
62 | })
|
63 | .end()
|
64 | .use('cache-loader')
|
65 | .loader(require.resolve('cache-loader'))
|
66 | .options(api.genCacheConfig('babel-loader', {
|
67 | '@babel/core': require('@babel/core/package.json').version,
|
68 | '@vue/babel-preset-app': require('@vue/babel-preset-app/package.json').version,
|
69 | 'babel-loader': require('babel-loader/package.json').version,
|
70 | modern: !!process.env.VUE_CLI_MODERN_BUILD,
|
71 | browserslist: api.service.pkg.browserslist
|
72 | }, [
|
73 | 'babel.config.js',
|
74 | '.browserslistrc'
|
75 | ]))
|
76 | .end()
|
77 |
|
78 | if (useThreads) {
|
79 | const threadLoaderConfig = jsRule
|
80 | .use('thread-loader')
|
81 | .loader(require.resolve('thread-loader'))
|
82 |
|
83 | if (typeof options.parallel === 'number') {
|
84 | threadLoaderConfig.options({ workers: options.parallel })
|
85 | }
|
86 | }
|
87 |
|
88 | jsRule
|
89 | .use('babel-loader')
|
90 | .loader(require.resolve('babel-loader'))
|
91 | })
|
92 | }
|