UNPKG

2.26 kBJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4const config = require('../../config')
5const paths = config.paths
6const maraConf = require(paths.marauder)
7const inlineJson = require.resolve('../../libs/babelInlineJson')
8
9const externalMoudles = [paths.src, paths.test].concat(
10 // 越来越多的库如 swiper 采用 es6 发布
11 // 将存在潜在的兼容问题,因此编译所有依赖
12 babelExternalMoudles('all')
13)
14
15function nodeModulesRegExp(...args) {
16 // path.sep 指定平台特定的分隔符
17 // Windows: \ POSIX: /
18 // 参考:http://nodejs.cn/api/path.html#path_path_sep
19 return args
20 .reduce((res, item) => res.concat(item), [])
21 .map(mod => new RegExp(`node_modules\\${path.sep}${mod}?`))
22}
23
24function babelExternalMoudles(esm) {
25 if (!(esm && esm.length)) return nodeModulesRegExp(config.esm)
26
27 // 当 esm 为 all 时,编译 node_modules 下所有模块
28 if (esm === 'all') esm = ''
29
30 // 仅编译 @mfelibs 下及 maraConf.esm 指定模块
31 return nodeModulesRegExp([config.esm, esm])
32}
33
34// function babelExternalMoudles(esm) {
35// // 无法强制约束使用者行为,故采用保守派策略
36// // 默认编译 node_modules 下所有模块
37// if (!(esm && esm.length)) return nodeModulesRegExp('')
38
39// // 仅编译 @mfelibs 下及 maraConf.esm 指定模块
40// return nodeModulesRegExp([config.esm, esm])
41// }
42// 读取marauder.config.js中的babelPlugins
43const plugins = []
44maraConf.babelPlugins && plugins.join(maraConf.babelPlugins)
45
46plugins.push('transform-decorators-legacy')
47// 加入了 inline-json,用于去除编译时的引入json(非全量引入)。
48// plugins.push(['inline-json', { matchPattern: '.' }])
49
50module.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 // `babel-loader` 特性
62 // 在 ./node_modules/.cache/babel-loader/ 中缓存执行结果
63 // 提升性能
64 cacheDirectory: !isProd,
65 highlightCode: true
66 }
67})
68
69module.exports.babelExternalMoudles = externalMoudles