-
Notifications
You must be signed in to change notification settings - Fork 277
/
babel.ts
125 lines (118 loc) · 3.4 KB
/
babel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { transform } from '@umijs/bundler-utils/compiled/babel/core';
import { winPath } from '@umijs/utils';
import path from 'path';
import { IFatherBundlessTypes } from '../../../../types';
import {
addSourceMappingUrl,
ensureRelativePath,
getBabelPresetReactOpts,
getBabelStyledComponentsOpts,
getBundlessTargets,
} from '../../../utils';
import type { IJSTransformerFn } from '../types';
/**
* parse for stringify define value, use to babel-plugin-transform-define
*/
function getParsedDefine(define: Record<string, string>) {
return Object.entries(define).reduce<typeof define>(
(result, [name, value]) => ({
...result,
[name]: JSON.parse(value),
}),
{},
);
}
/**
* babel transformer
*/
const babelTransformer: IJSTransformerFn = function (content) {
const {
extraBabelPlugins = [],
extraBabelPresets = [],
define,
alias: oAlias = {},
} = this.config;
// TODO: correct optional in umi types and replace any here
const presetOpts: any = {
presetEnv: {
targets: getBundlessTargets(this.config),
modules: this.config.format === IFatherBundlessTypes.ESM ? false : 'auto',
},
presetReact: getBabelPresetReactOpts(
this.pkg,
path.dirname(this.paths.fileAbsPath),
),
presetTypeScript: {},
};
const pluginSCOpts = getBabelStyledComponentsOpts(this.pkg);
// transform alias to relative path for babel-plugin-module-resolver
const alias = Object.entries(oAlias).reduce<typeof oAlias>(
(result, [name, target]) => {
if (path.isAbsolute(target)) {
result[name] = winPath(path.relative(this.paths.cwd, target));
result[name] = ensureRelativePath(result[name]);
} else {
result[name] = target;
}
return result;
},
{},
);
if (this.pkg.dependencies?.['@babel/runtime']) {
presetOpts.pluginTransformRuntime = {
absoluteRuntime: false,
// still use legacy esm helpers, to avoid double imports of runtime helpers
// from webpack 4 bundlers, such as Umi 3, antd-tools & etc.
useESModules:
this.config.format === IFatherBundlessTypes.ESM ? true : false,
version: this.pkg.dependencies?.['@babel/runtime'],
};
}
const { code, map } = transform(content, {
filename: this.paths.fileAbsPath,
cwd: this.paths.cwd,
babelrc: false,
configFile: false,
sourceMaps: this.config.sourcemap,
sourceFileName: this.config.sourcemap
? path.relative(
path.dirname(this.paths.itemDistAbsPath),
this.paths.fileAbsPath,
)
: undefined,
presets: [
[require.resolve('@umijs/babel-preset-umi'), presetOpts],
...extraBabelPresets,
],
plugins: [
[
require.resolve('babel-plugin-module-resolver'),
{
alias: alias,
cwd: this.paths.cwd,
extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.json'],
},
],
...(pluginSCOpts
? [[require.resolve('babel-plugin-styled-components'), pluginSCOpts]]
: []),
...(define
? [
[
require.resolve('babel-plugin-transform-define'),
getParsedDefine(define),
],
]
: []),
...extraBabelPlugins,
],
})!;
if (map) {
return [
addSourceMappingUrl(code!, this.paths.itemDistAbsPath),
JSON.stringify(map),
];
}
return [code!];
};
export default babelTransformer;