@rollup/plugin-babel 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +174 -0
- package/LICENSE +21 -0
- package/README.md +352 -0
- package/dist/index.es.js +437 -0
- package/dist/index.js +444 -0
- package/package.json +77 -0
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import * as babel from '@babel/core';
|
|
2
|
+
import { transformAsync, loadPartialConfig, buildExternalHelpers, DEFAULT_EXTENSIONS } from '@babel/core';
|
|
3
|
+
import { createFilter } from '@rollup/pluginutils';
|
|
4
|
+
import { addNamed } from '@babel/helper-module-imports';
|
|
5
|
+
|
|
6
|
+
function _defineProperty(obj, key, value) {
|
|
7
|
+
if (key in obj) {
|
|
8
|
+
Object.defineProperty(obj, key, {
|
|
9
|
+
value: value,
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true
|
|
13
|
+
});
|
|
14
|
+
} else {
|
|
15
|
+
obj[key] = value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return obj;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function ownKeys(object, enumerableOnly) {
|
|
22
|
+
var keys = Object.keys(object);
|
|
23
|
+
|
|
24
|
+
if (Object.getOwnPropertySymbols) {
|
|
25
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
26
|
+
if (enumerableOnly) symbols = symbols.filter(function (sym) {
|
|
27
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
28
|
+
});
|
|
29
|
+
keys.push.apply(keys, symbols);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return keys;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function _objectSpread2(target) {
|
|
36
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
37
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
38
|
+
|
|
39
|
+
if (i % 2) {
|
|
40
|
+
ownKeys(Object(source), true).forEach(function (key) {
|
|
41
|
+
_defineProperty(target, key, source[key]);
|
|
42
|
+
});
|
|
43
|
+
} else if (Object.getOwnPropertyDescriptors) {
|
|
44
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
45
|
+
} else {
|
|
46
|
+
ownKeys(Object(source)).forEach(function (key) {
|
|
47
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return target;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
56
|
+
if (source == null) return {};
|
|
57
|
+
var target = {};
|
|
58
|
+
var sourceKeys = Object.keys(source);
|
|
59
|
+
var key, i;
|
|
60
|
+
|
|
61
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
|
62
|
+
key = sourceKeys[i];
|
|
63
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
64
|
+
target[key] = source[key];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return target;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function _objectWithoutProperties(source, excluded) {
|
|
71
|
+
if (source == null) return {};
|
|
72
|
+
|
|
73
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
74
|
+
|
|
75
|
+
var key, i;
|
|
76
|
+
|
|
77
|
+
if (Object.getOwnPropertySymbols) {
|
|
78
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
79
|
+
|
|
80
|
+
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
|
81
|
+
key = sourceSymbolKeys[i];
|
|
82
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
83
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
84
|
+
target[key] = source[key];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return target;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const BUNDLED = 'bundled';
|
|
92
|
+
const INLINE = 'inline';
|
|
93
|
+
const RUNTIME = 'runtime';
|
|
94
|
+
const EXTERNAL = 'external'; // NOTE: DO NOT REMOVE the null character `\0` as it may be used by other plugins
|
|
95
|
+
// e.g. https://github.com/rollup/rollup-plugin-node-resolve/blob/313a3e32f432f9eb18cc4c231cc7aac6df317a51/src/index.js#L74
|
|
96
|
+
|
|
97
|
+
const HELPERS = '\0rollupPluginBabelHelpers.js';
|
|
98
|
+
|
|
99
|
+
function importHelperPlugin() {
|
|
100
|
+
return {
|
|
101
|
+
pre(file) {
|
|
102
|
+
const cachedHelpers = {};
|
|
103
|
+
file.set('helperGenerator', name => {
|
|
104
|
+
if (!file.availableHelper(name)) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (cachedHelpers[name]) {
|
|
109
|
+
return cachedHelpers[name];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return cachedHelpers[name] = addNamed(file.path, name, HELPERS);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const addBabelPlugin = (options, plugin) => {
|
|
120
|
+
return _objectSpread2({}, options, {
|
|
121
|
+
plugins: options.plugins.concat(plugin)
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
const warned = {};
|
|
125
|
+
function warnOnce(ctx, msg) {
|
|
126
|
+
if (warned[msg]) return;
|
|
127
|
+
warned[msg] = true;
|
|
128
|
+
ctx.warn(msg);
|
|
129
|
+
}
|
|
130
|
+
const regExpCharactersRegExp = /[\\^$.*+?()[\]{}|]/g;
|
|
131
|
+
const escapeRegExpCharacters = str => str.replace(regExpCharactersRegExp, '\\$&');
|
|
132
|
+
|
|
133
|
+
const MODULE_ERROR = 'Rollup requires that your Babel configuration keeps ES6 module syntax intact. ' + 'Unfortunately it looks like your configuration specifies a module transformer ' + 'to replace ES6 modules with another module format. To continue you have to disable it.' + '\n\n' + "Most commonly it's a CommonJS transform added by @babel/preset-env - " + 'in such case you should disable it by adding `modules: false` option to that preset ' + '(described in more detail here - https://github.com/rollup/plugins/tree/master/packages/babel#modules ).';
|
|
134
|
+
const UNEXPECTED_ERROR = 'An unexpected situation arose. Please raise an issue at ' + 'https://github.com/rollup/plugins/issues. Thanks!';
|
|
135
|
+
const PREFLIGHT_TEST_STRING = '__ROLLUP__PREFLIGHT_CHECK_DO_NOT_TOUCH__';
|
|
136
|
+
const PREFLIGHT_INPUT = `export default "${PREFLIGHT_TEST_STRING}";`;
|
|
137
|
+
|
|
138
|
+
function helpersTestTransform() {
|
|
139
|
+
return {
|
|
140
|
+
visitor: {
|
|
141
|
+
StringLiteral(path, state) {
|
|
142
|
+
if (path.node.value === PREFLIGHT_TEST_STRING) {
|
|
143
|
+
path.replaceWith(state.file.addHelper('inherits'));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const mismatchError = (actual, expected, filename) => `You have declared using "${expected}" babelHelpers, but transforming ${filename} resulted in "${actual}". Please check your configuration.`;
|
|
152
|
+
|
|
153
|
+
const inheritsHelperRe = /\/helpers\/(esm\/)?inherits/;
|
|
154
|
+
async function preflightCheck(ctx, babelHelpers, transformOptions) {
|
|
155
|
+
const finalOptions = addBabelPlugin(transformOptions, helpersTestTransform);
|
|
156
|
+
const check = (await transformAsync(PREFLIGHT_INPUT, finalOptions)).code; // Babel sometimes splits ExportDefaultDeclaration into 2 statements, so we also check for ExportNamedDeclaration
|
|
157
|
+
|
|
158
|
+
if (!/export (d|{)/.test(check)) {
|
|
159
|
+
ctx.error(MODULE_ERROR);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (inheritsHelperRe.test(check)) {
|
|
163
|
+
if (babelHelpers === RUNTIME) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
ctx.error(mismatchError(RUNTIME, babelHelpers, transformOptions.filename));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (check.includes('babelHelpers.inherits')) {
|
|
171
|
+
if (babelHelpers === EXTERNAL) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
ctx.error(mismatchError(EXTERNAL, babelHelpers, transformOptions.filename));
|
|
176
|
+
} // test unminifiable string content
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
if (check.includes('Super expression must either be null or a function')) {
|
|
180
|
+
if (babelHelpers === INLINE || babelHelpers === BUNDLED) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (babelHelpers === RUNTIME && !transformOptions.plugins.length) {
|
|
185
|
+
ctx.error(`You must use the \`@babel/plugin-transform-runtime\` plugin when \`babelHelpers\` is "${RUNTIME}".\n`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
ctx.error(mismatchError(INLINE, babelHelpers, transformOptions.filename));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
ctx.error(UNEXPECTED_ERROR);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function transformCode(inputCode, babelOptions, overrides, customOptions, ctx, finalizeOptions) {
|
|
195
|
+
const config = loadPartialConfig(babelOptions); // file is ignored by babel
|
|
196
|
+
|
|
197
|
+
if (!config) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let transformOptions = !overrides.config ? config.options : await overrides.config.call(this, config, {
|
|
202
|
+
code: inputCode,
|
|
203
|
+
customOptions
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
if (finalizeOptions) {
|
|
207
|
+
transformOptions = await finalizeOptions(transformOptions);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!overrides.result) {
|
|
211
|
+
const {
|
|
212
|
+
code,
|
|
213
|
+
map
|
|
214
|
+
} = await transformAsync(inputCode, transformOptions);
|
|
215
|
+
return {
|
|
216
|
+
code,
|
|
217
|
+
map
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const result = await transformAsync(inputCode, transformOptions);
|
|
222
|
+
const {
|
|
223
|
+
code,
|
|
224
|
+
map
|
|
225
|
+
} = await overrides.result.call(ctx, result, {
|
|
226
|
+
code: inputCode,
|
|
227
|
+
customOptions,
|
|
228
|
+
config,
|
|
229
|
+
transformOptions
|
|
230
|
+
});
|
|
231
|
+
return {
|
|
232
|
+
code,
|
|
233
|
+
map
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const unpackOptions = (_ref = {}) => {
|
|
238
|
+
let {
|
|
239
|
+
extensions = DEFAULT_EXTENSIONS,
|
|
240
|
+
// rollup uses sourcemap, babel uses sourceMaps
|
|
241
|
+
// just normalize them here so people don't have to worry about it
|
|
242
|
+
sourcemap = true,
|
|
243
|
+
sourcemaps = true,
|
|
244
|
+
sourceMap = true,
|
|
245
|
+
sourceMaps = true
|
|
246
|
+
} = _ref,
|
|
247
|
+
rest = _objectWithoutProperties(_ref, ["extensions", "sourcemap", "sourcemaps", "sourceMap", "sourceMaps"]);
|
|
248
|
+
|
|
249
|
+
return _objectSpread2({
|
|
250
|
+
extensions,
|
|
251
|
+
plugins: [],
|
|
252
|
+
sourceMaps: sourcemap && sourcemaps && sourceMap && sourceMaps
|
|
253
|
+
}, rest, {
|
|
254
|
+
caller: _objectSpread2({
|
|
255
|
+
name: '@rollup/plugin-babel'
|
|
256
|
+
}, rest.caller)
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const unpackInputPluginOptions = (_ref2) => {
|
|
261
|
+
let {
|
|
262
|
+
skipPreflightCheck = false
|
|
263
|
+
} = _ref2,
|
|
264
|
+
rest = _objectWithoutProperties(_ref2, ["skipPreflightCheck"]);
|
|
265
|
+
|
|
266
|
+
if (!rest.babelHelpers) {
|
|
267
|
+
// eslint-disable-next-line no-console
|
|
268
|
+
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');
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return unpackOptions(_objectSpread2({}, rest, {
|
|
272
|
+
skipPreflightCheck,
|
|
273
|
+
babelHelpers: rest.babelHelpers || BUNDLED,
|
|
274
|
+
caller: _objectSpread2({
|
|
275
|
+
supportsStaticESM: true,
|
|
276
|
+
supportsDynamicImport: true,
|
|
277
|
+
supportsTopLevelAwait: true
|
|
278
|
+
}, rest.caller)
|
|
279
|
+
}));
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const unpackOutputPluginOptions = (options, {
|
|
283
|
+
format
|
|
284
|
+
}) => unpackOptions(_objectSpread2({
|
|
285
|
+
configFile: false,
|
|
286
|
+
sourceType: format === 'es' ? 'module' : 'script'
|
|
287
|
+
}, options, {
|
|
288
|
+
caller: _objectSpread2({
|
|
289
|
+
supportsStaticESM: format === 'es'
|
|
290
|
+
}, options.caller)
|
|
291
|
+
}));
|
|
292
|
+
|
|
293
|
+
function getOptionsWithOverrides(pluginOptions = {}, overrides = {}) {
|
|
294
|
+
if (!overrides.options) return {
|
|
295
|
+
customOptions: null,
|
|
296
|
+
pluginOptionsWithOverrides: pluginOptions
|
|
297
|
+
};
|
|
298
|
+
const overridden = overrides.options(pluginOptions);
|
|
299
|
+
|
|
300
|
+
if (typeof overridden.then === 'function') {
|
|
301
|
+
throw new Error(".options hook can't be asynchronous. It should return `{ customOptions, pluginsOptions }` synchronously.");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return {
|
|
305
|
+
customOptions: overridden.customOptions || null,
|
|
306
|
+
pluginOptionsWithOverrides: overridden.pluginOptions || pluginOptions
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const returnObject = () => {
|
|
311
|
+
return {};
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
function createBabelInputPluginFactory(customCallback = returnObject) {
|
|
315
|
+
const overrides = customCallback(babel);
|
|
316
|
+
return pluginOptions => {
|
|
317
|
+
const {
|
|
318
|
+
customOptions,
|
|
319
|
+
pluginOptionsWithOverrides
|
|
320
|
+
} = getOptionsWithOverrides(pluginOptions, overrides);
|
|
321
|
+
|
|
322
|
+
const _unpackInputPluginOpt = unpackInputPluginOptions(pluginOptionsWithOverrides),
|
|
323
|
+
{
|
|
324
|
+
exclude,
|
|
325
|
+
extensions,
|
|
326
|
+
babelHelpers,
|
|
327
|
+
include,
|
|
328
|
+
skipPreflightCheck
|
|
329
|
+
} = _unpackInputPluginOpt,
|
|
330
|
+
babelOptions = _objectWithoutProperties(_unpackInputPluginOpt, ["exclude", "extensions", "babelHelpers", "include", "skipPreflightCheck"]);
|
|
331
|
+
|
|
332
|
+
const extensionRegExp = new RegExp(`(${extensions.map(escapeRegExpCharacters).join('|')})$`);
|
|
333
|
+
const includeExcludeFilter = createFilter(include, exclude);
|
|
334
|
+
|
|
335
|
+
const filter = id => extensionRegExp.test(id) && includeExcludeFilter(id);
|
|
336
|
+
|
|
337
|
+
return {
|
|
338
|
+
name: 'babel',
|
|
339
|
+
|
|
340
|
+
resolveId(id) {
|
|
341
|
+
if (id !== HELPERS) {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return id;
|
|
346
|
+
},
|
|
347
|
+
|
|
348
|
+
load(id) {
|
|
349
|
+
if (id !== HELPERS) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return buildExternalHelpers(null, 'module');
|
|
354
|
+
},
|
|
355
|
+
|
|
356
|
+
transform(code, filename) {
|
|
357
|
+
if (!filter(filename)) return null;
|
|
358
|
+
if (filename === HELPERS) return null;
|
|
359
|
+
return transformCode(code, _objectSpread2({}, babelOptions, {
|
|
360
|
+
filename
|
|
361
|
+
}), overrides, customOptions, this, async transformOptions => {
|
|
362
|
+
if (!skipPreflightCheck) {
|
|
363
|
+
await preflightCheck(this, babelHelpers, transformOptions);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return babelHelpers === BUNDLED ? addBabelPlugin(transformOptions, importHelperPlugin) : transformOptions;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
};
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function getRecommendedFormat(rollupFormat) {
|
|
375
|
+
switch (rollupFormat) {
|
|
376
|
+
case 'amd':
|
|
377
|
+
return 'amd';
|
|
378
|
+
|
|
379
|
+
case 'iife':
|
|
380
|
+
case 'umd':
|
|
381
|
+
return 'umd';
|
|
382
|
+
|
|
383
|
+
case 'system':
|
|
384
|
+
return 'systemjs';
|
|
385
|
+
|
|
386
|
+
default:
|
|
387
|
+
return '<module format>';
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function createBabelOutputPluginFactory(customCallback = returnObject) {
|
|
392
|
+
const overrides = customCallback(babel);
|
|
393
|
+
return pluginOptions => {
|
|
394
|
+
const {
|
|
395
|
+
customOptions,
|
|
396
|
+
pluginOptionsWithOverrides
|
|
397
|
+
} = getOptionsWithOverrides(pluginOptions, overrides);
|
|
398
|
+
return {
|
|
399
|
+
name: 'babel',
|
|
400
|
+
|
|
401
|
+
renderStart(outputOptions) {
|
|
402
|
+
const {
|
|
403
|
+
extensions,
|
|
404
|
+
include,
|
|
405
|
+
exclude,
|
|
406
|
+
allowAllFormats
|
|
407
|
+
} = pluginOptionsWithOverrides;
|
|
408
|
+
|
|
409
|
+
if (extensions || include || exclude) {
|
|
410
|
+
warnOnce(this, 'The "include", "exclude" and "extensions" options are ignored when transforming the output.');
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (!allowAllFormats && outputOptions.format !== 'es' && outputOptions.format !== 'cjs') {
|
|
414
|
+
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.`);
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
renderChunk(code, chunk, outputOptions) {
|
|
419
|
+
/* eslint-disable no-unused-vars */
|
|
420
|
+
const _unpackOutputPluginOp = unpackOutputPluginOptions(pluginOptionsWithOverrides, outputOptions),
|
|
421
|
+
babelOptions = _objectWithoutProperties(_unpackOutputPluginOp, ["allowAllFormats", "exclude", "extensions", "externalHelpers", "externalHelpersWhitelist", "include", "runtimeHelpers"]);
|
|
422
|
+
/* eslint-enable no-unused-vars */
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
return transformCode(code, babelOptions, overrides, customOptions, this);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
};
|
|
429
|
+
};
|
|
430
|
+
} // export this for symmetry with output-related exports
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
const getBabelInputPlugin = createBabelInputPluginFactory();
|
|
434
|
+
const getBabelOutputPlugin = createBabelOutputPluginFactory();
|
|
435
|
+
|
|
436
|
+
export default getBabelInputPlugin;
|
|
437
|
+
export { getBabelInputPlugin as babel, createBabelInputPluginFactory, createBabelOutputPluginFactory, getBabelInputPlugin, getBabelOutputPlugin };
|