UNPKG

3.3 kBJavaScriptView Raw
1const path = require('path')
2const babel = require('@babel/core')
3const { isWindows } = require('@vue/cli-shared-utils')
4
5function 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, '\\\\') // double escape for windows style path
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
19module.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 // try to load the project babel config;
25 // if the default preset is used,
26 // there will be a VUE_CLI_TRANSPILE_BABEL_RUNTIME env var set.
27 // the `filename` field is required
28 // in case there're filename-related options like `ignore` in the user config
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 // always transpile js in vue files
40 if (/\.vue\.jsx?$/.test(filepath)) {
41 return false
42 }
43 // exclude dynamic entries from cli-service
44 if (filepath.startsWith(cliServicePath)) {
45 return true
46 }
47
48 // only include @babel/runtime when the @vue/babel-preset-app preset is used
49 if (
50 process.env.VUE_CLI_TRANSPILE_BABEL_RUNTIME &&
51 filepath.includes(path.join('@babel', 'runtime'))
52 ) {
53 return false
54 }
55
56 // check if this is something the user explicitly wants to transpile
57 if (transpileDepRegex && transpileDepRegex.test(filepath)) {
58 return false
59 }
60 // Don't transpile node_modules
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}