/
index.js
260 lines (229 loc) · 8 KB
/
index.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import * as babel from '@babel/core';
import { createFilter } from '@rollup/pluginutils';
import { BUNDLED, HELPERS } from './constants.js';
import bundledHelpersPlugin from './bundledHelpersPlugin.js';
import preflightCheck from './preflightCheck.js';
import transformCode from './transformCode.js';
import { addBabelPlugin, escapeRegExpCharacters, warnOnce, stripQuery } from './utils.js';
const unpackOptions = ({
extensions = babel.DEFAULT_EXTENSIONS,
// rollup uses sourcemap, babel uses sourceMaps
// just normalize them here so people don't have to worry about it
sourcemap = true,
sourcemaps = true,
sourceMap = true,
sourceMaps = true,
...rest
} = {}) => {
return {
extensions,
plugins: [],
sourceMaps: sourcemap && sourcemaps && sourceMap && sourceMaps,
...rest,
caller: {
name: '@rollup/plugin-babel',
...rest.caller
}
};
};
const warnAboutDeprecatedHelpersOption = ({ deprecatedOption, suggestion }) => {
// eslint-disable-next-line no-console
console.warn(
`\`${deprecatedOption}\` has been removed in favor a \`babelHelpers\` option. Try changing your configuration to \`${suggestion}\`. ` +
`Refer to the documentation to learn more: https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers`
);
};
const unpackInputPluginOptions = ({ skipPreflightCheck = false, ...rest }, rollupVersion) => {
if ('runtimeHelpers' in rest) {
warnAboutDeprecatedHelpersOption({
deprecatedOption: 'runtimeHelpers',
suggestion: `babelHelpers: 'runtime'`
});
} else if ('externalHelpers' in rest) {
warnAboutDeprecatedHelpersOption({
deprecatedOption: 'externalHelpers',
suggestion: `babelHelpers: 'external'`
});
} else if (!rest.babelHelpers) {
// eslint-disable-next-line no-console
console.warn(
"babelHelpers: 'bundled' option was used by default. It is recommended to configure this option explicitly, read more here: " +
'https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers'
);
}
return unpackOptions({
...rest,
skipPreflightCheck,
babelHelpers: rest.babelHelpers || BUNDLED,
caller: {
supportsStaticESM: true,
supportsDynamicImport: true,
supportsTopLevelAwait: true,
// todo: remove version checks for 1.20 - 1.25 when we bump peer deps
supportsExportNamespaceFrom: !rollupVersion.match(/^1\.2[0-5]\./),
...rest.caller
}
});
};
const unpackOutputPluginOptions = (options, { format }) =>
unpackOptions({
configFile: false,
sourceType: format === 'es' ? 'module' : 'script',
...options,
caller: {
supportsStaticESM: format === 'es',
...options.caller
}
});
function getOptionsWithOverrides(pluginOptions = {}, overrides = {}) {
if (!overrides.options) return { customOptions: null, pluginOptionsWithOverrides: pluginOptions };
const overridden = overrides.options(pluginOptions);
if (typeof overridden.then === 'function') {
throw new Error(
".options hook can't be asynchronous. It should return `{ customOptions, pluginsOptions }` synchronously."
);
}
return {
customOptions: overridden.customOptions || null,
pluginOptionsWithOverrides: overridden.pluginOptions || pluginOptions
};
}
const returnObject = () => {
return {};
};
function createBabelInputPluginFactory(customCallback = returnObject) {
const overrides = customCallback(babel);
return (pluginOptions) => {
const { customOptions, pluginOptionsWithOverrides } = getOptionsWithOverrides(
pluginOptions,
overrides
);
let babelHelpers;
let babelOptions;
let filter;
let skipPreflightCheck;
return {
name: 'babel',
options() {
// todo: remove options hook and hoist declarations when version checks are removed
let exclude;
let include;
let extensions;
let customFilter;
({
exclude,
extensions,
babelHelpers,
include,
filter: customFilter,
skipPreflightCheck,
...babelOptions
} = unpackInputPluginOptions(pluginOptionsWithOverrides, this.meta.rollupVersion));
const extensionRegExp = new RegExp(
`(${extensions.map(escapeRegExpCharacters).join('|')})$`
);
if (customFilter && (include || exclude)) {
throw new Error('Could not handle include or exclude with custom filter together');
}
const userDefinedFilter =
typeof customFilter === 'function' ? customFilter : createFilter(include, exclude);
filter = (id) => extensionRegExp.test(stripQuery(id).bareId) && userDefinedFilter(id);
return null;
},
resolveId(id) {
if (id !== HELPERS) {
return null;
}
return id;
},
load(id) {
if (id !== HELPERS) {
return null;
}
return babel.buildExternalHelpers(null, 'module');
},
transform(code, filename) {
if (!filter(filename)) return null;
if (filename === HELPERS) return null;
return transformCode(
code,
{ ...babelOptions, filename },
overrides,
customOptions,
this,
async (transformOptions) => {
if (!skipPreflightCheck) {
await preflightCheck(this, babelHelpers, transformOptions);
}
return babelHelpers === BUNDLED
? addBabelPlugin(transformOptions, bundledHelpersPlugin)
: transformOptions;
}
);
}
};
};
}
function getRecommendedFormat(rollupFormat) {
switch (rollupFormat) {
case 'amd':
return 'amd';
case 'iife':
case 'umd':
return 'umd';
case 'system':
return 'systemjs';
default:
return '<module format>';
}
}
function createBabelOutputPluginFactory(customCallback = returnObject) {
const overrides = customCallback(babel);
return (pluginOptions) => {
const { customOptions, pluginOptionsWithOverrides } = getOptionsWithOverrides(
pluginOptions,
overrides
);
return {
name: 'babel',
renderStart(outputOptions) {
const { extensions, include, exclude, allowAllFormats } = pluginOptionsWithOverrides;
if (extensions || include || exclude) {
warnOnce(
this,
'The "include", "exclude" and "extensions" options are ignored when transforming the output.'
);
}
if (!allowAllFormats && outputOptions.format !== 'es' && outputOptions.format !== 'cjs') {
this.error(
`Using Babel on the generated chunks is strongly discouraged for formats other than "esm" or "cjs" as it can easily break wrapper code and lead to accidentally created global variables. Instead, you should set "output.format" to "esm" and use Babel to transform to another format, e.g. by adding "presets: [['@babel/env', { modules: '${getRecommendedFormat(
outputOptions.format
)}' }]]" to your Babel options. If you still want to proceed, add "allowAllFormats: true" to your plugin options.`
);
}
},
renderChunk(code, chunk, outputOptions) {
/* eslint-disable no-unused-vars */
const {
allowAllFormats,
exclude,
extensions,
externalHelpers,
externalHelpersWhitelist,
include,
runtimeHelpers,
...babelOptions
} = unpackOutputPluginOptions(pluginOptionsWithOverrides, outputOptions);
/* eslint-enable no-unused-vars */
return transformCode(code, babelOptions, overrides, customOptions, this);
}
};
};
}
// export this for symmetry with output-related exports
export const getBabelInputPlugin = createBabelInputPluginFactory();
export const getBabelOutputPlugin = createBabelOutputPluginFactory();
export { createBabelInputPluginFactory, createBabelOutputPluginFactory };
export default getBabelInputPlugin;
// support `rollup -c —plugin babel`
export { getBabelInputPlugin as babel };