-
Notifications
You must be signed in to change notification settings - Fork 507
/
babelPluginTsdx.ts
162 lines (145 loc) · 4.59 KB
/
babelPluginTsdx.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { createConfigItem } from '@babel/core';
import babelPlugin from 'rollup-plugin-babel';
import merge from 'lodash.merge';
export const isTruthy = (obj?: any) => {
if (!obj) {
return false;
}
return obj.constructor !== Object || Object.keys(obj).length > 0;
};
const replacements = [{ original: 'lodash', replacement: 'lodash-es' }];
export const mergeConfigItems = (type: any, ...configItemsToMerge: any[]) => {
const mergedItems: any[] = [];
configItemsToMerge.forEach(configItemToMerge => {
configItemToMerge.forEach((item: any) => {
const itemToMergeWithIndex = mergedItems.findIndex(
mergedItem => mergedItem.file.resolved === item.file.resolved
);
if (itemToMergeWithIndex === -1) {
mergedItems.push(item);
return;
}
mergedItems[itemToMergeWithIndex] = createConfigItem(
[
mergedItems[itemToMergeWithIndex].file.resolved,
merge(mergedItems[itemToMergeWithIndex].options, item.options),
],
{
type,
}
);
});
});
return mergedItems;
};
export const createConfigItems = (type: any, items: any[]) => {
return items.map(({ name, ...options }) => {
return createConfigItem([require.resolve(name), options], { type });
});
};
export const babelPluginTsdx = babelPlugin.custom(() => ({
// Passed the plugin options.
options({ custom: customOptions, ...pluginOptions }: any) {
return {
// Pull out any custom options that the plugin might have.
customOptions,
// Pass the options back with the two custom options removed.
pluginOptions,
};
},
config(config: any, { customOptions }: any) {
const defaultPlugins = createConfigItems(
'plugin',
[
// {
// name: '@babel/plugin-transform-react-jsx',
// pragma: customOptions.jsx || 'h',
// pragmaFrag: customOptions.jsxFragment || 'Fragment',
// },
{ name: 'babel-plugin-annotate-pure-calls' },
{ name: 'babel-plugin-dev-expression' },
customOptions.format !== 'cjs' && {
name: 'babel-plugin-transform-rename-import',
replacements,
},
{
name: 'babel-plugin-transform-async-to-promises',
inlineHelpers: true,
externalHelpers: true,
},
{
name: '@babel/plugin-proposal-class-properties',
loose: true,
},
// Adds syntax support for optional chaining (.?)
{ name: '@babel/plugin-proposal-optional-chaining' },
// Adds syntax support for default value using ?? operator
{ name: '@babel/plugin-proposal-nullish-coalescing-operator' },
{
name: '@babel/plugin-transform-regenerator',
async: false,
},
{
name: 'babel-plugin-macros',
},
isTruthy(customOptions.extractErrors) && {
name: './errors/transformErrorMessages',
},
].filter(Boolean)
);
const babelOptions = config.options || {};
babelOptions.presets = babelOptions.presets || [];
const presetEnvIdx = babelOptions.presets.findIndex((preset: any) =>
preset.file.request.includes('@babel/preset-env')
);
// if they use preset-env, merge their options with ours
if (presetEnvIdx !== -1) {
const presetEnv = babelOptions.presets[presetEnvIdx];
babelOptions.presets[presetEnvIdx] = createConfigItem(
[
presetEnv.file.resolved,
merge(
{
loose: true,
targets: customOptions.targets,
},
presetEnv.options,
{
modules: false,
exclude: merge(
['transform-async-to-generator', 'transform-regenerator'],
(presetEnv.options && presetEnv.options.exclude) || []
),
}
),
],
{
type: `preset`,
}
);
} else {
// if no preset-env, add it & merge with their presets
const defaultPresets = createConfigItems('preset', [
{
name: '@babel/preset-env',
targets: customOptions.targets,
modules: false,
loose: true,
exclude: ['transform-async-to-generator', 'transform-regenerator'],
},
]);
babelOptions.presets = mergeConfigItems(
'preset',
defaultPresets,
babelOptions.presets
);
}
// Merge babelrc & our plugins together
babelOptions.plugins = mergeConfigItems(
'plugin',
defaultPlugins,
babelOptions.plugins || []
);
return babelOptions;
},
}));