1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = void 0;
|
9 |
|
10 | var _lodash = _interopRequireDefault(require("lodash"));
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class BabelConfigure {
|
18 | constructor(api, config) {
|
19 | this._ENVS = ['server', 'web'];
|
20 | this._SERVER_TARGETS = {
|
21 | node: '10.14.0'
|
22 | };
|
23 | this._WEB_TARGETS = {
|
24 | browsers: '> 0.1%, not ie <= 8'
|
25 | };
|
26 | this._env = 'web';
|
27 | this._config = {
|
28 | server: {},
|
29 | web: {}
|
30 | };
|
31 |
|
32 | if (!~this._ENVS.indexOf(process.env.BABEL_ENV)) {
|
33 | throw new Error(`'process.env.BABEL_ENV' must set in ${JSON.stringify(this._ENVS)}`);
|
34 | }
|
35 |
|
36 | api.cache.never();
|
37 | this._env = process.env.BABEL_ENV;
|
38 | this._config = config;
|
39 | }
|
40 |
|
41 | _getCommonPlugins() {
|
42 | return [['@babel/plugin-proposal-decorators', {
|
43 | 'legacy': true
|
44 | }], ['@babel/plugin-proposal-class-properties', {
|
45 | 'loose': true
|
46 | }], ['@babel/plugin-proposal-export-default-from'], ['@babel/plugin-proposal-export-namespace-from'], ['@babel/plugin-syntax-dynamic-import'], ['@babel/plugin-proposal-function-bind']];
|
47 | }
|
48 |
|
49 | _getServerTemplate() {
|
50 | const defaultTemplate = {
|
51 | presets: [['@babel/preset-env', {
|
52 | targets: this._SERVER_TARGETS
|
53 | }]],
|
54 | plugins: [['module-resolver', {
|
55 | "alias": _lodash.default.get(this._config, 'server.alias', {})
|
56 | }], ['css-modules-transform', {
|
57 | generateScopedName: '[path][name]__[local]',
|
58 | extensions: ['.css']
|
59 | }], ['@babel/transform-runtime', {
|
60 | regenerator: false
|
61 | }], ...this._getCommonPlugins()]
|
62 | };
|
63 |
|
64 | const template = this._processOverwrite(defaultTemplate, this._config.server);
|
65 |
|
66 | return template;
|
67 | }
|
68 |
|
69 | _getWebTemplate() {
|
70 | const defaultTemplate = {
|
71 | presets: [['@babel/preset-env', {
|
72 | targets: this._WEB_TARGETS
|
73 | }]],
|
74 | plugins: [['module-resolver', {
|
75 | alias: _lodash.default.get(this._config, 'web.alias', {})
|
76 | }], ['@babel/transform-runtime'], ...this._getCommonPlugins()]
|
77 | };
|
78 |
|
79 | const template = this._processOverwrite(defaultTemplate, this._config.web);
|
80 |
|
81 | return template;
|
82 | }
|
83 |
|
84 | _processOverwrite(defaultTemplate, customTemplate) {
|
85 | const customPresets = _lodash.default.map(customTemplate.presets, preset => this._normalize(preset));
|
86 |
|
87 | const customPlugins = _lodash.default.map(customTemplate.plugins, plugin => this._normalize(plugin));
|
88 |
|
89 | const defaultPresets = _lodash.default.map(defaultTemplate.presets, preset => this._normalize(preset));
|
90 |
|
91 | const defaultPlugins = _lodash.default.map(defaultTemplate.plugins, plugin => this._normalize(plugin));
|
92 |
|
93 | const morePresets = _lodash.default.filter(customPresets, preset => !_lodash.default.find(defaultPresets, pst => pst[0] === preset[0]));
|
94 |
|
95 | const presets = _lodash.default.filter(_lodash.default.map(defaultPresets, (preset, index) => {
|
96 | const customPreset = _lodash.default.find(customPresets, pst => preset[0] === pst[0]);
|
97 |
|
98 | return customPreset ? customPreset : preset;
|
99 | }).concat(morePresets), preset => preset[1] !== 'exclude');
|
100 |
|
101 | const morePlugins = _lodash.default.filter(customPlugins, plugin => !_lodash.default.find(defaultPlugins, plg => plg[0] === plugin[0]));
|
102 |
|
103 | const plugins = _lodash.default.filter(_lodash.default.map(defaultPlugins, (plugin, index) => {
|
104 | const customPlugin = _lodash.default.find(customPlugins, plg => plugin[0] === plg[0]);
|
105 |
|
106 | return customPlugin ? customPlugin : plugin;
|
107 | }).concat(morePlugins), plugin => plugin[1] !== 'exclude');
|
108 |
|
109 | return {
|
110 | presets,
|
111 | plugins,
|
112 | ..._lodash.default.omit(customTemplate, ['presets', 'plugins', 'alias', 'targets'])
|
113 | };
|
114 | }
|
115 |
|
116 | _normalize(target) {
|
117 | const result = _lodash.default.isArray(target) ? target : [target];
|
118 | return result;
|
119 | }
|
120 |
|
121 | getTemplate() {
|
122 | if (this._env === 'server') {
|
123 | return this._getServerTemplate();
|
124 | } else {
|
125 | return this._getWebTemplate();
|
126 | }
|
127 | }
|
128 |
|
129 | }
|
130 |
|
131 | exports.default = BabelConfigure; |
\ | No newline at end of file |