UNPKG

4.71 kBJavaScriptView Raw
1const path = require('path')
2const babel = require('@babel/core')
3const { isWindows } = require('@vue/cli-shared-utils')
4
5function 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, '\\\\') // double escape for windows style path
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/** @type {import('@vue/cli-service').ServicePlugin} */
22module.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 // try to load the project babel config;
27 // if the default preset is used,
28 // there will be a VUE_CLI_TRANSPILE_BABEL_RUNTIME env var set.
29 // the `filename` field is required
30 // in case there're filename-related options like `ignore` in the user config
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 // With data URI support in webpack 5, filepath could be undefined
45 if (!filepath) {
46 return SHOULD_SKIP
47 }
48
49 // Always transpile js in vue files
50 if (/\.vue\.jsx?$/.test(filepath)) {
51 return SHOULD_TRANSPILE
52 }
53 // Exclude dynamic entries from cli-service
54 if (filepath.startsWith(cliServicePath)) {
55 return SHOULD_SKIP
56 }
57
58 // To transpile `@babel/runtime`, the config needs to be
59 // carefully adjusted to avoid infinite loops.
60 // So we only do the tranpilation when the special flag is on.
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 // if `transpileDependencies` is set to true, transpile all deps
68 if (options.transpileDependencies === true) {
69 // Some of the deps cannot be transpiled, though
70 // https://stackoverflow.com/a/58517865/2302258
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 // Otherwise, check if this is something the user explicitly wants to transpile
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 // Don't transpile node_modules
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}