1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 | const config = require('../../config')
|
5 | const paths = config.paths
|
6 | const maraConf = require(paths.marauder)
|
7 | const inlineJson = require.resolve('../../libs/babelInlineJson')
|
8 |
|
9 | const externalMoudles = [paths.src, paths.test].concat(
|
10 |
|
11 |
|
12 | babelExternalMoudles('all')
|
13 | )
|
14 |
|
15 | function nodeModulesRegExp(...args) {
|
16 |
|
17 |
|
18 |
|
19 | return args
|
20 | .reduce((res, item) => res.concat(item), [])
|
21 | .map(mod => new RegExp(`node_modules\\${path.sep}${mod}?`))
|
22 | }
|
23 |
|
24 | function babelExternalMoudles(esm) {
|
25 | if (!(esm && esm.length)) return nodeModulesRegExp(config.esm)
|
26 |
|
27 |
|
28 | if (esm === 'all') esm = ''
|
29 |
|
30 |
|
31 | return nodeModulesRegExp([config.esm, esm])
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const plugins = []
|
44 | maraConf.babelPlugins && plugins.join(maraConf.babelPlugins)
|
45 |
|
46 | plugins.push('transform-decorators-legacy')
|
47 |
|
48 |
|
49 |
|
50 | module.exports.babelLoader = isProd => ({
|
51 | test: /\.(js|jsx|mjs)$/,
|
52 | include: externalMoudles,
|
53 |
|
54 | exclude: ['node_modules/vue', 'node_modules/react', 'node_modules/react-dom'],
|
55 | loader: 'babel-loader',
|
56 | options: {
|
57 | babelrc: false,
|
58 | presets: ['babel-preset-react-app'],
|
59 | plugins: plugins,
|
60 | compact: isProd,
|
61 |
|
62 |
|
63 |
|
64 | cacheDirectory: !isProd,
|
65 | highlightCode: true
|
66 | }
|
67 | })
|
68 |
|
69 | module.exports.babelExternalMoudles = externalMoudles
|