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