1 | const path = require('path')
|
2 | const babel = require('@babel/core')
|
3 | const { isWindows } = require('@vue/cli-shared-utils')
|
4 |
|
5 | function getDepPathRegex (dependencies) {
|
6 | const deps = dependencies.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 | throw new Error('transpileDependencies only accepts an array of string or regular expressions')
|
17 | })
|
18 | return deps.length ? new RegExp(deps.join('|')) : null
|
19 | }
|
20 |
|
21 |
|
22 | module.exports = (api, options) => {
|
23 | const useThreads = process.env.NODE_ENV === 'production' && !!options.parallel
|
24 | const cliServicePath = path.dirname(require.resolve('@vue/cli-service'))
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | babel.loadPartialConfigSync({ filename: api.resolve('src/main.js') })
|
32 |
|
33 | api.chainWebpack(webpackConfig => {
|
34 | webpackConfig.resolveLoader.modules.prepend(path.join(__dirname, 'node_modules'))
|
35 |
|
36 | const jsRule = webpackConfig.module
|
37 | .rule('js')
|
38 | .test(/\.m?jsx?$/)
|
39 | .exclude
|
40 | .add(filepath => {
|
41 | const SHOULD_SKIP = true
|
42 | const SHOULD_TRANSPILE = false
|
43 |
|
44 |
|
45 | if (!filepath) {
|
46 | return SHOULD_SKIP
|
47 | }
|
48 |
|
49 |
|
50 | if (/\.vue\.jsx?$/.test(filepath)) {
|
51 | return SHOULD_TRANSPILE
|
52 | }
|
53 |
|
54 | if (filepath.startsWith(cliServicePath)) {
|
55 | return SHOULD_SKIP
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | if (getDepPathRegex(['@babel/runtime']).test(filepath)) {
|
62 | return process.env.VUE_CLI_TRANSPILE_BABEL_RUNTIME
|
63 | ? SHOULD_TRANSPILE
|
64 | : SHOULD_SKIP
|
65 | }
|
66 |
|
67 |
|
68 | if (options.transpileDependencies === true) {
|
69 |
|
70 |
|
71 | const NON_TRANSPILABLE_DEPS = [
|
72 | 'core-js',
|
73 | 'webpack',
|
74 | 'webpack-4',
|
75 | 'css-loader',
|
76 | 'mini-css-extract-plugin',
|
77 | 'promise-polyfill',
|
78 | 'html-webpack-plugin',
|
79 | 'whatwg-fetch'
|
80 | ]
|
81 | const nonTranspilableDepsRegex = getDepPathRegex(NON_TRANSPILABLE_DEPS)
|
82 | return nonTranspilableDepsRegex.test(filepath) ? SHOULD_SKIP : SHOULD_TRANSPILE
|
83 | }
|
84 |
|
85 |
|
86 | if (Array.isArray(options.transpileDependencies)) {
|
87 | const transpileDepRegex = getDepPathRegex(options.transpileDependencies)
|
88 | if (transpileDepRegex && transpileDepRegex.test(filepath)) {
|
89 | return SHOULD_TRANSPILE
|
90 | }
|
91 | }
|
92 |
|
93 |
|
94 | return /node_modules/.test(filepath) ? SHOULD_SKIP : SHOULD_TRANSPILE
|
95 | })
|
96 | .end()
|
97 |
|
98 | if (useThreads) {
|
99 | const threadLoaderConfig = jsRule
|
100 | .use('thread-loader')
|
101 | .loader(require.resolve('thread-loader'))
|
102 |
|
103 | if (typeof options.parallel === 'number') {
|
104 | threadLoaderConfig.options({ workers: options.parallel })
|
105 | }
|
106 | }
|
107 |
|
108 | jsRule
|
109 | .use('babel-loader')
|
110 | .loader(require.resolve('babel-loader'))
|
111 | .options({
|
112 | cacheCompression: false,
|
113 | ...api.genCacheConfig('babel-loader', {
|
114 | '@babel/core': require('@babel/core/package.json').version,
|
115 | '@vue/babel-preset-app': require('@vue/babel-preset-app/package.json').version,
|
116 | 'babel-loader': require('babel-loader/package.json').version,
|
117 | modern: !!process.env.VUE_CLI_MODERN_BUILD,
|
118 | browserslist: api.service.pkg.browserslist
|
119 | }, [
|
120 | 'babel.config.js',
|
121 | '.browserslistrc'
|
122 | ])
|
123 | })
|
124 | })
|
125 | }
|