1 | 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, '\\\\')
|
10 | : depPath
|
11 | } else if (dep instanceof RegExp) {
|
12 | return dep.source
|
13 | }
|
14 | })
|
15 | return deps.length ? new RegExp(deps.join('|')) : null
|
16 | }
|
17 |
|
18 | module.exports = (api, options) => {
|
19 | const useThreads = process.env.NODE_ENV === 'production' && !!options.parallel
|
20 | const cliServicePath = require('path').dirname(require.resolve('@vue/cli-service'))
|
21 | const transpileDepRegex = genTranspileDepRegex(options.transpileDependencies)
|
22 |
|
23 | api.chainWebpack(webpackConfig => {
|
24 | webpackConfig.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))
|
25 |
|
26 | const jsRule = webpackConfig.module
|
27 | .rule('js')
|
28 | .test(/\.m?jsx?$/)
|
29 | .exclude
|
30 | .add(filepath => {
|
31 |
|
32 | if (/\.vue\.jsx?$/.test(filepath)) {
|
33 | return false
|
34 | }
|
35 |
|
36 | if (filepath.startsWith(cliServicePath)) {
|
37 | return true
|
38 | }
|
39 |
|
40 | if (transpileDepRegex && transpileDepRegex.test(filepath)) {
|
41 | return false
|
42 | }
|
43 |
|
44 | return /node_modules/.test(filepath)
|
45 | })
|
46 | .end()
|
47 | .use('cache-loader')
|
48 | .loader('cache-loader')
|
49 | .options(api.genCacheConfig('babel-loader', {
|
50 | '@babel/core': require('@babel/core/package.json').version,
|
51 | '@vue/babel-preset-app': require('@vue/babel-preset-app/package.json').version,
|
52 | 'babel-loader': require('babel-loader/package.json').version,
|
53 | modern: !!process.env.VUE_CLI_MODERN_BUILD,
|
54 | browserslist: api.service.pkg.browserslist
|
55 | }, [
|
56 | 'babel.config.js',
|
57 | '.browserslistrc'
|
58 | ]))
|
59 | .end()
|
60 |
|
61 | if (useThreads) {
|
62 | const threadLoaderConfig = jsRule
|
63 | .use('thread-loader')
|
64 | .loader('thread-loader')
|
65 |
|
66 | if (typeof options.parallel === 'number') {
|
67 | threadLoaderConfig.options({ workers: options.parallel })
|
68 | }
|
69 | }
|
70 |
|
71 | jsRule
|
72 | .use('babel-loader')
|
73 | .loader('babel-loader')
|
74 | })
|
75 | }
|