From b4138ae3eeef2dc20b2d5d23c878b357b6d2e88f Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 30 Sep 2022 06:34:56 +0200 Subject: [PATCH] fix(babel): prepare for Rollup 3 BREAKING CHANGES: Requires Node 14 --- packages/babel/README.md | 18 +-- packages/babel/package.json | 42 +++--- packages/babel/rollup.config.js | 20 --- packages/babel/rollup.config.mjs | 18 +++ packages/babel/src/bundledHelpersPlugin.js | 2 +- packages/babel/src/index.js | 10 +- packages/babel/src/package.json | 3 + packages/babel/src/preflightCheck.js | 4 +- ...as-input-plugin.js => as-input-plugin.mjs} | 130 ++++++++++-------- ...-output-plugin.js => as-output-plugin.mjs} | 80 ++++++----- .../babel/test/node_modules/current-package | 1 + pnpm-lock.yaml | 75 ++++++---- tsconfig.base.json | 2 +- 13 files changed, 233 insertions(+), 172 deletions(-) delete mode 100644 packages/babel/rollup.config.js create mode 100644 packages/babel/rollup.config.mjs create mode 100644 packages/babel/src/package.json rename packages/babel/test/{as-input-plugin.js => as-input-plugin.mjs} (78%) rename packages/babel/test/{as-output-plugin.js => as-output-plugin.mjs} (83%) create mode 120000 packages/babel/test/node_modules/current-package diff --git a/packages/babel/README.md b/packages/babel/README.md index 6d079e68a..498c8a1cd 100644 --- a/packages/babel/README.md +++ b/packages/babel/README.md @@ -26,7 +26,7 @@ Using Rollup with `@rollup/plugin-babel` makes the process far easier. ## Requirements -This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v10.0.0+) and Rollup v1.20.0+. +This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+. ## Install @@ -45,7 +45,7 @@ const config = { input: 'src/index.js', output: { dir: 'output', - format: 'esm' + format: 'es' }, plugins: [babel({ babelHelpers: 'bundled' })] }; @@ -174,7 +174,7 @@ export default { ], output: [ { file: 'bundle.cjs.js', format: 'cjs' }, - { file: 'bundle.esm.js', format: 'esm' } + { file: 'bundle.es.js', format: 'es' } ] }; ``` @@ -188,10 +188,10 @@ import { getBabelOutputPlugin } from '@rollup/plugin-babel'; export default { input: 'main.js', output: [ - { file: 'bundle.js', format: 'esm' }, + { file: 'bundle.js', format: 'es' }, { file: 'bundle.es5.js', - format: 'esm', + format: 'es', plugins: [getBabelOutputPlugin({ presets: ['@babel/preset-env'] })] } ] @@ -212,7 +212,7 @@ export default { output: [ { file: 'bundle.js', - format: 'esm', + format: 'es', plugins: [getBabelOutputPlugin({ presets: ['@babel/preset-env'] })] } ] @@ -231,7 +231,7 @@ getBabelOutputPlugin({ ### Using formats other than ES modules or CommonJS -As `getBabelOutputPlugin(...)` will run _after_ Rollup has done all its transformations, it needs to make sure it preserves the semantics of Rollup's output format. This is especially important for Babel plugins that add, modify or remove imports or exports, but also for other transformations that add new variables as they can accidentally become global variables depending on the format. Therefore it is recommended that for formats other than `esm` or `cjs`, you set Rollup to use the `esm` output format and let Babel handle the transformation to another format, e.g. via +As `getBabelOutputPlugin(...)` will run _after_ Rollup has done all its transformations, it needs to make sure it preserves the semantics of Rollup's output format. This is especially important for Babel plugins that add, modify or remove imports or exports, but also for other transformations that add new variables as they can accidentally become global variables depending on the format. Therefore it is recommended that for formats other than `es` or `cjs`, you set Rollup to use the `es` output format and let Babel handle the transformation to another format, e.g. via ``` presets: [['@babel/preset-env', { modules: 'umd' }], ...] @@ -256,12 +256,12 @@ By default, helpers e.g. when transpiling classes will be inserted at the top of Alternatively, you can use imported runtime helpers by adding the `@babel/transform-runtime` plugin. This will make `@babel/runtime` an external dependency of your project, see [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/babel-plugin-transform-runtime) for details. -Note that this will only work for `esm` and `cjs` formats, and you need to make sure to set the `useESModules` option of `@babel/plugin-transform-runtime` to `true` if you create ESM output: +Note that this will only work for `es` and `cjs` formats, and you need to make sure to set the `useESModules` option of `@babel/plugin-transform-runtime` to `true` if you create ES output: ```js rollup.rollup({...}) .then(bundle => bundle.generate({ - format: 'esm', + format: 'es', plugins: [getBabelOutputPlugin({ presets: ['@babel/preset-env'], plugins: [['@babel/plugin-transform-runtime', { useESModules: true }]] diff --git a/packages/babel/package.json b/packages/babel/package.json index b184b634f..9c682bed5 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -13,10 +13,15 @@ "author": "Rich Harris", "homepage": "https://github.com/rollup/plugins/tree/master/packages/babel#readme", "bugs": "https://github.com/rollup/plugins/issues", - "main": "dist/index.js", - "module": "dist/index.es.js", + "main": "./dist/cjs/index.js", + "module": "./dist/es/index.js", + "exports": { + "types": "./types/index.d.ts", + "import": "./dist/es/index.js", + "default": "./dist/cjs/index.js" + }, "engines": { - "node": ">= 10.0.0" + "node": ">=14.0.0" }, "scripts": { "build": "rollup -c", @@ -33,6 +38,7 @@ }, "files": [ "dist", + "!dist/**/*.map", "types", "README.md", "LICENSE" @@ -48,35 +54,35 @@ "peerDependencies": { "@babel/core": "^7.0.0", "@types/babel__core": "^7.1.9", - "rollup": "^1.20.0||^2.0.0" + "rollup": "^1.20.0||^2.0.0||^3.0.0" }, "peerDependenciesMeta": { + "rollup": { + "optional": true + }, "@types/babel__core": { "optional": true } }, "dependencies": { - "@babel/helper-module-imports": "^7.10.4", - "@rollup/pluginutils": "^3.1.0" + "@babel/helper-module-imports": "^7.18.6", + "@rollup/pluginutils": "^4.2.1" }, "devDependencies": { - "@babel/core": "^7.10.5", - "@babel/plugin-external-helpers": "^7.10.4", - "@babel/plugin-proposal-decorators": "^7.10.5", + "@babel/core": "^7.19.1", + "@babel/plugin-external-helpers": "^7.18.6", + "@babel/plugin-proposal-decorators": "^7.19.1", "@babel/plugin-syntax-dynamic-import": "^7.8.3", - "@babel/plugin-transform-runtime": "^7.10.5", - "@babel/preset-env": "^7.10.4", + "@babel/plugin-transform-runtime": "^7.19.1", + "@babel/preset-env": "^7.19.1", "@rollup/plugin-json": "^4.1.0", - "@rollup/plugin-node-resolve": "^10.0.0", + "@rollup/plugin-node-resolve": "^14.1.0", "@types/babel__core": "^7.1.9", - "rollup": "^2.67.3", - "source-map": "^0.7.3" + "rollup": "^3.0.0-7", + "source-map": "^0.7.4" }, - "types": "types/index.d.ts", + "types": "./types/index.d.ts", "ava": { - "babel": { - "compileEnhancements": false - }, "files": [ "!**/fixtures/**", "!**/helpers/**", diff --git a/packages/babel/rollup.config.js b/packages/babel/rollup.config.js deleted file mode 100644 index 98295afaf..000000000 --- a/packages/babel/rollup.config.js +++ /dev/null @@ -1,20 +0,0 @@ -import pkg from './package.json'; -import { babel } from './src/index'; - -const externalDeps = Object.keys(Object.assign({}, pkg.dependencies, pkg.peerDependencies)); -const nodeDeps = ['path']; - -export default { - input: './src/index.js', - external: externalDeps.concat(nodeDeps), - plugins: [ - babel({ - presets: [['@babel/preset-env', { targets: { node: 8 } }]], - babelHelpers: 'bundled' - }) - ], - output: [ - { file: pkg.main, format: 'cjs', exports: 'named' }, - { file: pkg.module, format: 'esm' } - ] -}; diff --git a/packages/babel/rollup.config.mjs b/packages/babel/rollup.config.mjs new file mode 100644 index 000000000..686d97f22 --- /dev/null +++ b/packages/babel/rollup.config.mjs @@ -0,0 +1,18 @@ +import { readFileSync } from 'fs'; + +import { createConfig } from '../../shared/rollup.config.mjs'; + +import { babel } from './src/index.js'; + +const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')); + +export default { + ...createConfig({ pkg }), + input: './src/index.js', + plugins: [ + babel({ + presets: [['@babel/preset-env', { targets: { node: 14 } }]], + babelHelpers: 'bundled' + }) + ] +}; diff --git a/packages/babel/src/bundledHelpersPlugin.js b/packages/babel/src/bundledHelpersPlugin.js index 878d26b3a..bec33f63b 100644 --- a/packages/babel/src/bundledHelpersPlugin.js +++ b/packages/babel/src/bundledHelpersPlugin.js @@ -1,6 +1,6 @@ import { addNamed } from '@babel/helper-module-imports'; -import { HELPERS } from './constants'; +import { HELPERS } from './constants.js'; export default function importHelperPlugin({ types: t }) { return { diff --git a/packages/babel/src/index.js b/packages/babel/src/index.js index e631522d3..f07666371 100644 --- a/packages/babel/src/index.js +++ b/packages/babel/src/index.js @@ -1,11 +1,11 @@ import * as babel from '@babel/core'; import { createFilter } from '@rollup/pluginutils'; -import { BUNDLED, HELPERS } from './constants'; -import bundledHelpersPlugin from './bundledHelpersPlugin'; -import preflightCheck from './preflightCheck'; -import transformCode from './transformCode'; -import { addBabelPlugin, escapeRegExpCharacters, warnOnce, stripQuery } from './utils'; +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, diff --git a/packages/babel/src/package.json b/packages/babel/src/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/packages/babel/src/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/packages/babel/src/preflightCheck.js b/packages/babel/src/preflightCheck.js index be0a20a02..1ca8c1c1c 100644 --- a/packages/babel/src/preflightCheck.js +++ b/packages/babel/src/preflightCheck.js @@ -1,7 +1,7 @@ import * as babel from '@babel/core'; -import { INLINE, RUNTIME, EXTERNAL, BUNDLED } from './constants'; -import { addBabelPlugin } from './utils'; +import { INLINE, RUNTIME, EXTERNAL, BUNDLED } from './constants.js'; +import { addBabelPlugin } from './utils.js'; const MODULE_ERROR = 'Rollup requires that your Babel configuration keeps ES6 module syntax intact. ' + diff --git a/packages/babel/test/as-input-plugin.js b/packages/babel/test/as-input-plugin.mjs similarity index 78% rename from packages/babel/test/as-input-plugin.js rename to packages/babel/test/as-input-plugin.mjs index 4f134fd39..c7d145558 100644 --- a/packages/babel/test/as-input-plugin.js +++ b/packages/babel/test/as-input-plugin.mjs @@ -1,5 +1,8 @@ -import * as nodePath from 'path'; -import * as fs from 'fs'; +import { createRequire } from 'node:module'; +import * as nodePath from 'node:path'; +import * as fs from 'node:fs'; + +import { fileURLToPath } from 'url'; import test from 'ava'; import { rollup } from 'rollup'; @@ -7,11 +10,12 @@ import jsonPlugin from '@rollup/plugin-json'; import nodeResolvePlugin from '@rollup/plugin-node-resolve'; import { createFilter } from '@rollup/pluginutils'; -import { getCode } from '../../../util/test'; +import babelPlugin, { getBabelOutputPlugin, createBabelInputPluginFactory } from 'current-package'; -import babelPlugin, { getBabelOutputPlugin, createBabelInputPluginFactory } from '../dist'; +import { getCode } from '../../../util/test.js'; -process.chdir(__dirname); +const DIRNAME = fileURLToPath(new URL('.', import.meta.url)); +const FIXTURES = `${DIRNAME}/fixtures/`; function getLocation(source, charIndex) { const lines = source.split('\n'); @@ -50,7 +54,7 @@ function replaceConsoleLogProperty({ types: t }) { async function generate(input, babelOptions = {}, generateOptions = {}, rollupOptions = {}) { const bundle = await rollup({ - input, + input: FIXTURES + input, plugins: [babelPlugin({ babelHelpers: 'bundled', ...babelOptions })], ...rollupOptions }); @@ -63,7 +67,7 @@ async function generate(input, babelOptions = {}, generateOptions = {}, rollupOp } test('runs code through babel', async (t) => { - const code = await generate('fixtures/basic/main.js'); + const code = await generate('basic/main.js'); t.false(code.includes('const')); t.is( code, @@ -76,17 +80,17 @@ console.log("the answer is ".concat(answer)); }); test('adds helpers', async (t) => { - const code = await generate('fixtures/class/main.js'); + const code = await generate('class/main.js'); t.true(code.includes('function _classCallCheck')); }); test('adds helpers in loose mode', async (t) => { - const code = await generate('fixtures/class-loose/main.js'); + const code = await generate('class-loose/main.js'); t.true(code.includes('function _inherits')); }); test('does not babelify excluded code', async (t) => { - const code = await generate('fixtures/exclusions/main.js', { exclude: '**/foo.js' }); + const code = await generate('exclusions/main.js', { exclude: '**/foo.js' }); // eslint-disable-next-line no-template-curly-in-string t.false(code.includes('${foo()}')); t.true(code.includes('=> 42')); @@ -103,7 +107,7 @@ console.log("the answer is ".concat(foo())); test('does not babelify excluded code with custom filter', async (t) => { const filter = createFilter([], '**/foo.js'); - const code = await generate('fixtures/exclusions/main.js', { filter }); + const code = await generate('exclusions/main.js', { filter }); // eslint-disable-next-line no-template-curly-in-string t.false(code.includes('${foo()}')); t.true(code.includes('=> 42')); @@ -120,9 +124,9 @@ console.log("the answer is ".concat(foo())); test('does babelify included code with custom filter', async (t) => { const filter = createFilter('**/foo.js', [], { - resolve: __dirname + resolve: DIRNAME }); - const code = await generate('fixtures/exclusions/main.js', { filter }); + const code = await generate('exclusions/main.js', { filter }); // eslint-disable-next-line no-template-curly-in-string t.true(code.includes('${foo()}')); t.false(code.includes('=> 42')); @@ -141,11 +145,11 @@ console.log(\`the answer is \${foo()}\`); test('can not pass include or exclude when custom filter specified', async (t) => { const filter = createFilter('**/foo.js', [], { - resolve: __dirname + resolve: DIRNAME }); let errorWithExclude = ''; try { - await generate('fixtures/exclusions/main.js', { filter, exclude: [] }); + await generate('exclusions/main.js', { filter, exclude: [] }); } catch (e) { errorWithExclude = e.message; } @@ -153,7 +157,7 @@ test('can not pass include or exclude when custom filter specified', async (t) = let errorWithInclude = ''; try { - await generate('fixtures/exclusions/main.js', { filter, include: [] }); + await generate('exclusions/main.js', { filter, include: [] }); } catch (e) { errorWithInclude = e.message; } @@ -162,7 +166,7 @@ test('can not pass include or exclude when custom filter specified', async (t) = test('generates sourcemap by default', async (t) => { const bundle = await rollup({ - input: 'fixtures/class/main.js', + input: `${FIXTURES}class/main.js`, plugins: [babelPlugin({ babelHelpers: 'bundled' })] }); @@ -184,7 +188,7 @@ test('generates sourcemap by default', async (t) => { const original = smc.originalPositionFor(loc); t.deepEqual(original, { - source: 'fixtures/class/main.js'.split(nodePath.sep).join('/'), + source: 'test/fixtures/class/main.js'.split(nodePath.sep).join('/'), line: 3, column: 12, name: target @@ -194,14 +198,14 @@ test('generates sourcemap by default', async (t) => { test('works with proposal-decorators (rollup/rollup-plugin-babel#18)', async (t) => { await t.notThrowsAsync(() => rollup({ - input: 'fixtures/proposal-decorators/main.js', + input: `${FIXTURES}proposal-decorators/main.js`, plugins: [babelPlugin({ babelHelpers: 'bundled' })] }) ); }); test('checks config per-file', async (t) => { - const code = await generate('fixtures/checks/main.js', {}, { format: 'esm' }); + const code = await generate('checks/main.js', {}, { format: 'es' }); t.true(code.includes('class Foo')); t.true(code.includes('var Bar')); t.false(code.includes('class Bar')); @@ -210,7 +214,7 @@ test('checks config per-file', async (t) => { test('allows transform-runtime to be used instead of bundled helpers', async (t) => { const warnings = []; const code = await generate( - 'fixtures/runtime-helpers/main.js', + 'runtime-helpers/main.js', { babelHelpers: 'runtime' }, {}, { @@ -220,21 +224,19 @@ test('allows transform-runtime to be used instead of bundled helpers', async (t) } ); t.deepEqual(warnings, [ - `'@babel/runtime/helpers/classCallCheck' is imported by fixtures${nodePath.sep}runtime-helpers${nodePath.sep}main.js, but could not be resolved – treating it as an external dependency` + `"@babel/runtime/helpers/createClass" is imported by "test/fixtures/runtime-helpers/main.js", but could not be resolved – treating it as an external dependency.`, + `"@babel/runtime/helpers/classCallCheck" is imported by "test/fixtures/runtime-helpers/main.js", but could not be resolved – treating it as an external dependency.` ]); t.is( code, `'use strict'; +var _createClass = require('@babel/runtime/helpers/createClass'); var _classCallCheck = require('@babel/runtime/helpers/classCallCheck'); -function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } - -var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck); - -var Foo = function Foo() { - _classCallCheck__default["default"](this, Foo); -}; +var Foo = /*#__PURE__*/_createClass(function Foo() { + _classCallCheck(this, Foo); +}); module.exports = Foo; ` @@ -244,10 +246,10 @@ module.exports = Foo; test('allows transform-runtime to inject esm version of helpers', async (t) => { const warnings = []; const code = await generate( - 'fixtures/runtime-helpers-esm/main.js', + 'runtime-helpers-esm/main.js', { babelHelpers: 'runtime' }, { - format: 'esm' + format: 'es' }, { onwarn(warning) { @@ -256,15 +258,17 @@ test('allows transform-runtime to inject esm version of helpers', async (t) => { } ); t.deepEqual(warnings, [ - `'@babel/runtime/helpers/esm/classCallCheck' is imported by fixtures${nodePath.sep}runtime-helpers-esm${nodePath.sep}main.js, but could not be resolved – treating it as an external dependency` + `"@babel/runtime/helpers/esm/createClass" is imported by "test/fixtures/runtime-helpers-esm/main.js", but could not be resolved – treating it as an external dependency.`, + `"@babel/runtime/helpers/esm/classCallCheck" is imported by "test/fixtures/runtime-helpers-esm/main.js", but could not be resolved – treating it as an external dependency.` ]); t.is( code, - `import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck'; + `import _createClass from '@babel/runtime/helpers/esm/createClass'; +import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck'; -var Foo = function Foo() { +var Foo = /*#__PURE__*/_createClass(function Foo() { _classCallCheck(this, Foo); -}; +}); export { Foo as default }; ` @@ -273,7 +277,7 @@ export { Foo as default }; test('allows transform-runtime to be used instead of bundled helpers, but throws when CommonJS is used', async (t) => { await t.throwsAsync( - () => generate('fixtures/runtime-helpers-commonjs/main.js', { babelHelpers: 'runtime' }), + () => generate('runtime-helpers-commonjs/main.js', { babelHelpers: 'runtime' }), { message: /Rollup requires that your Babel configuration keeps ES6 module syntax intact/ } @@ -281,7 +285,7 @@ test('allows transform-runtime to be used instead of bundled helpers, but throws }); test('allows using external-helpers plugin in combination with @babel/plugin-external-helpers', async (t) => { - const code = await generate('fixtures/external-helpers/main.js', { + const code = await generate('external-helpers/main.js', { babelHelpers: 'external' }); t.false(code.includes('function _classCallCheck')); @@ -290,13 +294,13 @@ test('allows using external-helpers plugin in combination with @babel/plugin-ext code, `'use strict'; -var Foo = function Foo() { +var Foo = /*#__PURE__*/babelHelpers.createClass(function Foo() { babelHelpers.classCallCheck(this, Foo); -}; +}); -var Bar = function Bar() { +var Bar = /*#__PURE__*/babelHelpers.createClass(function Bar() { babelHelpers.classCallCheck(this, Bar); -}; +}); var main = [new Foo(), new Bar()]; @@ -306,32 +310,32 @@ module.exports = main; }); test('correctly renames helpers (rollup/rollup-plugin-babel#22)', async (t) => { - const code = await generate('fixtures/named-function-helper/main.js'); + const code = await generate('named-function-helper/main.js'); t.false(code.includes('babelHelpers_get get'), 'helper was incorrectly renamed'); }); test('runs preflight check correctly in absence of class transformer (rollup/rollup-plugin-babel#23)', async (t) => { await t.notThrowsAsync(() => rollup({ - input: 'fixtures/no-class-transformer/main.js', + input: `${FIXTURES}no-class-transformer/main.js`, plugins: [babelPlugin({ babelHelpers: 'bundled' })] }) ); }); test('produces valid code with typeof helper', async (t) => { - const code = await generate('fixtures/typeof/main.js'); + const code = await generate('typeof/main.js'); t.false(code.includes('var typeof')); }); test('handles babelrc with ignore option used', async (t) => { - const code = await generate('fixtures/ignored-file/main.js'); + const code = await generate('ignored-file/main.js'); t.true(code.includes('class Ignored')); }); test('transpiles only files with default extensions', async (t) => { const code = await generate( - 'fixtures/extensions-default/main.js', + 'extensions-default/main.js', {}, {}, { @@ -347,7 +351,7 @@ test('transpiles only files with default extensions', async (t) => { }); test('transpiles only files with whitelisted extensions', async (t) => { - const code = await generate('fixtures/extensions-custom/main.js', { + const code = await generate('extensions-custom/main.js', { extensions: ['.js', '.other'] }); t.true(code.includes('class Es '), 'should not transpile .es'); @@ -360,7 +364,7 @@ test('transpiles only files with whitelisted extensions', async (t) => { test('transpiles files when path contains query and hash', async (t) => { const code = await generate( - 'fixtures/with-query-and-hash/main.js', + 'with-query-and-hash/main.js', {}, {}, { @@ -390,7 +394,7 @@ test('transpiles files when path contains query and hash', async (t) => { test('throws when trying to add babel helper unavailable in used @babel/core version', async (t) => { await t.throwsAsync( () => - generate('fixtures/basic/main.js', { + generate('basic/main.js', { plugins: [ function testPlugin() { return { @@ -405,7 +409,7 @@ test('throws when trying to add babel helper unavailable in used @babel/core ver }), { message: `${nodePath.resolve( - __dirname, + DIRNAME, 'fixtures', 'basic', 'main.js' @@ -419,7 +423,7 @@ test('works with minified bundled helpers', async (t) => { let counter = 0; await t.notThrowsAsync(() => - generate('fixtures/class/main.js', { + generate('class/main.js', { plugins: [ function testPlugin({ types }) { return { @@ -477,7 +481,7 @@ test('supports customizing the loader', async (t) => { }; }); const bundle = await rollup({ - input: 'fixtures/basic/main.js', + input: `${FIXTURES}basic/main.js`, plugins: [customBabelPlugin({ babelHelpers: 'bundled' })] }); const code = await getCode(bundle); @@ -512,7 +516,7 @@ test('supports overriding the plugin options in custom loader', async (t) => { }; }); const bundle = await rollup({ - input: 'fixtures/basic/main.js', + input: `${FIXTURES}basic/main.js`, plugins: [customBabelPlugin({ babelHelpers: 'bundled' })] }); const code = await getCode(bundle); @@ -525,7 +529,7 @@ test('supports overriding the plugin options in custom loader', async (t) => { }); test('uses babel plugins passed in to the rollup plugin', async (t) => { - const code = await generate('fixtures/basic/main.js', { + const code = await generate('basic/main.js', { plugins: [[replaceConsoleLogProperty, { replace: 'foobaz' }]] }); t.true(code.includes('console.foobaz')); @@ -533,7 +537,7 @@ test('uses babel plugins passed in to the rollup plugin', async (t) => { test('can be used as an input plugin while transforming the output', async (t) => { const bundle = await rollup({ - input: 'fixtures/basic/main.js', + input: `${FIXTURES}basic/main.js`, plugins: [ getBabelOutputPlugin({ presets: ['@babel/env'] @@ -544,3 +548,19 @@ test('can be used as an input plugin while transforming the output', async (t) = t.false(code.includes('const')); }); + +test('works as a CJS plugin', async (t) => { + const require = createRequire(import.meta.url); + const babelPluginCjs = require('current-package'); + const bundle = await rollup({ + input: `${FIXTURES}basic/main.js`, + plugins: [ + babelPluginCjs({ + presets: ['@babel/env'] + }) + ] + }); + const code = await getCode(bundle); + + t.false(code.includes('const')); +}); diff --git a/packages/babel/test/as-output-plugin.js b/packages/babel/test/as-output-plugin.mjs similarity index 83% rename from packages/babel/test/as-output-plugin.js rename to packages/babel/test/as-output-plugin.mjs index 1b3d6aa41..fbb64bbea 100644 --- a/packages/babel/test/as-output-plugin.js +++ b/packages/babel/test/as-output-plugin.mjs @@ -1,13 +1,16 @@ import * as nodePath from 'path'; +import { fileURLToPath } from 'url'; + import test from 'ava'; import { rollup } from 'rollup'; -import { getCode } from '../../../util/test'; +import { getBabelOutputPlugin, createBabelOutputPluginFactory } from 'current-package'; -import { getBabelOutputPlugin, createBabelOutputPluginFactory } from '../dist'; +import { getCode } from '../../../util/test.js'; -process.chdir(__dirname); +const DIRNAME = fileURLToPath(new URL('.', import.meta.url)); +const FIXTURES = `${DIRNAME}/fixtures/`; function getLocation(source, charIndex) { const lines = source.split('\n'); @@ -46,7 +49,7 @@ function replaceConsoleLogProperty({ types: t }) { async function generate(input, babelOptions = {}, generateOptions = {}, rollupOptions = {}) { const bundle = await rollup({ - input, + input: FIXTURES + input, ...rollupOptions }); @@ -59,20 +62,20 @@ async function generate(input, babelOptions = {}, generateOptions = {}, rollupOp } test('allows running the plugin on the output via output options', async (t) => { - const code = await generate('fixtures/basic/main.js', { + const code = await generate('basic/main.js', { presets: ['@babel/env'] }); t.false(code.includes('const')); }); test('ignores .babelrc when transforming the output by default', async (t) => { - const code = await generate('fixtures/basic/main.js'); + const code = await generate('basic/main.js'); t.true(code.includes('const')); }); test("allows transform-runtime to be used with `useESModules: false` (the default) and `format: 'cjs'`", async (t) => { const code = await generate( - 'fixtures/runtime-helpers/main.js', + 'runtime-helpers/main.js', { presets: ['@babel/env'], plugins: [['@babel/transform-runtime', { useESModules: false }]] @@ -83,33 +86,36 @@ test("allows transform-runtime to be used with `useESModules: false` (the defaul code, `'use strict'; +var _createClass = require("@babel/runtime/helpers/createClass"); + var _classCallCheck = require("@babel/runtime/helpers/classCallCheck"); -var Foo = function Foo() { +var Foo = /*#__PURE__*/_createClass(function Foo() { _classCallCheck(this, Foo); -}; +}); module.exports = Foo; ` ); }); -test("allows transform-runtime to be used with `useESModules: true` and `format: 'esm'`", async (t) => { +test("allows transform-runtime to be used with `useESModules: true` and `format: 'es'`", async (t) => { const code = await generate( - 'fixtures/runtime-helpers/main.js', + 'runtime-helpers/main.js', { presets: ['@babel/env'], plugins: [['@babel/transform-runtime', { useESModules: true }]] }, - { format: 'esm' } + { format: 'es' } ); t.is( code, - `import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; + `import _createClass from "@babel/runtime/helpers/esm/createClass"; +import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; -var Foo = function Foo() { +var Foo = /*#__PURE__*/_createClass(function Foo() { _classCallCheck(this, Foo); -}; +}); export { Foo as default }; ` @@ -117,7 +123,7 @@ export { Foo as default }; }); test('generates sourcemap by default', async (t) => { - const bundle = await rollup({ input: 'fixtures/class/main.js' }); + const bundle = await rollup({ input: `${FIXTURES}class/main.js` }); const { output: [{ code, map }] @@ -142,7 +148,7 @@ test('generates sourcemap by default', async (t) => { const original = smc.originalPositionFor(loc); t.deepEqual(original, { - source: 'fixtures/class/main.js'.split(nodePath.sep).join('/'), + source: 'test/fixtures/class/main.js'.split(nodePath.sep).join('/'), line: 3, column: 12, name: target @@ -152,7 +158,7 @@ test('generates sourcemap by default', async (t) => { test('allows using external-helpers plugin even if the externalHelpers flag is not passed', async (t) => { const warnings = []; const code = await generate( - 'fixtures/external-helpers/main.js', + 'external-helpers/main.js', { presets: ['@babel/env'], plugins: ['@babel/external-helpers'] @@ -171,14 +177,12 @@ test('allows using external-helpers plugin even if the externalHelpers flag is n code, `'use strict'; -var Foo = function Foo() { +var Foo = /*#__PURE__*/babelHelpers.createClass(function Foo() { babelHelpers.classCallCheck(this, Foo); -}; - -var Bar = function Bar() { +}); +var Bar = /*#__PURE__*/babelHelpers.createClass(function Bar() { babelHelpers.classCallCheck(this, Bar); -}; - +}); var main = [new Foo(), new Bar()]; module.exports = main; ` @@ -188,7 +192,7 @@ module.exports = main; test('warns when using the "include" option', async (t) => { const warnings = []; await generate( - 'fixtures/basic/main.js', + 'basic/main.js', { include: ['*.js'] }, @@ -205,11 +209,11 @@ test('warns when using the "include" option', async (t) => { }); test('transforms all chunks in a code-splitting setup', async (t) => { - const bundle = await rollup({ input: 'fixtures/chunks/main.js' }); + const bundle = await rollup({ input: `${FIXTURES}chunks/main.js` }); const output = await getCode( bundle, { - format: 'esm', + format: 'es', plugins: [ getBabelOutputPlugin({ plugins: ['@babel/syntax-dynamic-import'], @@ -223,7 +227,7 @@ test('transforms all chunks in a code-splitting setup', async (t) => { t.deepEqual( output.map(({ code }) => code), [ - `import('./dep-525a96b3.js').then(function (result) { + `import('./dep-0fdca0d5.js').then(function (result) { return console.log(result); }); `, @@ -239,13 +243,13 @@ export { dep as default }; test('transforms all chunks when preserving modules', async (t) => { const bundle = await rollup({ - input: 'fixtures/preserve-modules/main.js', - preserveModules: true + input: `${FIXTURES}preserve-modules/main.js` }); const output = await getCode( bundle, { - format: 'esm', + format: 'es', + preserveModules: true, plugins: [ getBabelOutputPlugin({ presets: ['@babel/env'] @@ -307,7 +311,7 @@ test('supports customizing the loader', async (t) => { } }; }); - const bundle = await rollup({ input: 'fixtures/basic/main.js' }); + const bundle = await rollup({ input: `${FIXTURES}basic/main.js` }); const code = await getCode(bundle, { format: 'cjs', plugins: [customBabelPlugin()] }); t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment'); @@ -315,14 +319,14 @@ test('supports customizing the loader', async (t) => { }); test('throws when using a Rollup output format other than esm or cjs', async (t) => { - await t.throwsAsync(() => generate('fixtures/basic/main.js', {}, { format: 'iife' }), { + await t.throwsAsync(() => generate('basic/main.js', {}, { format: 'iife' }), { message: `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: 'umd' }]]" to your Babel options. If you still want to proceed, add "allowAllFormats: true" to your plugin options.` }); }); test('allows using a Rollup output format other than esm or cjs with allowAllFormats', async (t) => { const code = await generate( - 'fixtures/basic/main.js', + 'basic/main.js', { presets: ['@babel/env'], allowAllFormats: true }, { format: 'iife' } ); @@ -340,9 +344,9 @@ test('allows using a Rollup output format other than esm or cjs with allowAllFor test('allows using Babel to transform to other formats', async (t) => { const code = await generate( - 'fixtures/basic/main.js', + 'basic/main.js', { presets: [['@babel/env', { modules: 'umd' }]] }, - { format: 'esm' } + { format: 'es' } ); t.is( code, @@ -369,8 +373,8 @@ test('allows using Babel to transform to other formats', async (t) => { }); test('loads configuration files when configFile is passed', async (t) => { - const code = await generate('fixtures/config-file/main.js', { - configFile: nodePath.resolve(__dirname, 'fixtures/config-file/config.json') + const code = await generate('config-file/main.js', { + configFile: nodePath.resolve(DIRNAME, 'fixtures/config-file/config.json') }); t.is( code, diff --git a/packages/babel/test/node_modules/current-package b/packages/babel/test/node_modules/current-package new file mode 120000 index 000000000..c25bddb6d --- /dev/null +++ b/packages/babel/test/node_modules/current-package @@ -0,0 +1 @@ +../.. \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f0cb40dad..21ff8648a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -107,22 +107,22 @@ importers: packages/babel: specifiers: - '@babel/core': ^7.10.5 - '@babel/helper-module-imports': ^7.10.4 - '@babel/plugin-external-helpers': ^7.10.4 - '@babel/plugin-proposal-decorators': ^7.10.5 + '@babel/core': ^7.19.1 + '@babel/helper-module-imports': ^7.18.6 + '@babel/plugin-external-helpers': ^7.18.6 + '@babel/plugin-proposal-decorators': ^7.19.1 '@babel/plugin-syntax-dynamic-import': ^7.8.3 - '@babel/plugin-transform-runtime': ^7.10.5 - '@babel/preset-env': ^7.10.4 + '@babel/plugin-transform-runtime': ^7.19.1 + '@babel/preset-env': ^7.19.1 '@rollup/plugin-json': ^4.1.0 - '@rollup/plugin-node-resolve': ^10.0.0 - '@rollup/pluginutils': ^3.1.0 + '@rollup/plugin-node-resolve': ^14.1.0 + '@rollup/pluginutils': ^4.2.1 '@types/babel__core': ^7.1.9 - rollup: ^2.67.3 - source-map: ^0.7.3 + rollup: ^3.0.0-7 + source-map: ^0.7.4 dependencies: '@babel/helper-module-imports': 7.18.6 - '@rollup/pluginutils': 3.1.0_rollup@2.79.1 + '@rollup/pluginutils': 4.2.1 devDependencies: '@babel/core': 7.19.1 '@babel/plugin-external-helpers': 7.18.6_@babel+core@7.19.1 @@ -130,10 +130,10 @@ importers: '@babel/plugin-syntax-dynamic-import': 7.8.3_@babel+core@7.19.1 '@babel/plugin-transform-runtime': 7.19.1_@babel+core@7.19.1 '@babel/preset-env': 7.19.1_@babel+core@7.19.1 - '@rollup/plugin-json': 4.1.0_rollup@2.79.1 - '@rollup/plugin-node-resolve': 10.0.0_rollup@2.79.1 + '@rollup/plugin-json': 4.1.0_rollup@3.0.0-7 + '@rollup/plugin-node-resolve': 14.1.0_rollup@3.0.0-7 '@types/babel__core': 7.1.19 - rollup: 2.79.1 + rollup: 3.0.0-7 source-map: 0.7.4 packages/beep: @@ -2098,34 +2098,43 @@ packages: rollup: 2.79.1 dev: true - /@rollup/plugin-node-resolve/10.0.0_rollup@2.79.1: - resolution: {integrity: sha512-sNijGta8fqzwA1VwUEtTvWCx2E7qC70NMsDh4ZG13byAXYigBNZMxALhKUSycBks5gupJdq0lFrKumFrRZ8H3A==} + /@rollup/plugin-json/4.1.0_rollup@3.0.0-7: + resolution: {integrity: sha512-yfLbTdNS6amI/2OpmbiBoW12vngr5NW2jCJVZSBEz+H5KfUJZ2M7sDjk0U6GOOdCWFVScShte29o9NezJ53TPw==} + peerDependencies: + rollup: ^1.20.0 || ^2.0.0 + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@3.0.0-7 + rollup: 3.0.0-7 + dev: true + + /@rollup/plugin-node-resolve/13.3.0_rollup@2.79.1: + resolution: {integrity: sha512-Lus8rbUo1eEcnS4yTFKLZrVumLPY+YayBdWXgFSHYhTT2iJbMhoaaBL3xl5NCdeRytErGr8tZ0L71BMRmnlwSw==} engines: {node: '>= 10.0.0'} peerDependencies: - rollup: ^1.20.0||^2.0.0 + rollup: ^2.42.0 dependencies: '@rollup/pluginutils': 3.1.0_rollup@2.79.1 '@types/resolve': 1.17.1 - builtin-modules: 3.3.0 deepmerge: 4.2.2 + is-builtin-module: 3.2.0 is-module: 1.0.0 resolve: 1.22.1 rollup: 2.79.1 dev: true - /@rollup/plugin-node-resolve/13.3.0_rollup@2.79.1: - resolution: {integrity: sha512-Lus8rbUo1eEcnS4yTFKLZrVumLPY+YayBdWXgFSHYhTT2iJbMhoaaBL3xl5NCdeRytErGr8tZ0L71BMRmnlwSw==} + /@rollup/plugin-node-resolve/14.1.0_rollup@3.0.0-7: + resolution: {integrity: sha512-5G2niJroNCz/1zqwXtk0t9+twOSDlG00k1Wfd7bkbbXmwg8H8dvgHdIWAun53Ps/rckfvOC7scDBjuGFg5OaWw==} engines: {node: '>= 10.0.0'} peerDependencies: - rollup: ^2.42.0 + rollup: ^2.78.0 dependencies: - '@rollup/pluginutils': 3.1.0_rollup@2.79.1 + '@rollup/pluginutils': 3.1.0_rollup@3.0.0-7 '@types/resolve': 1.17.1 deepmerge: 4.2.2 is-builtin-module: 3.2.0 is-module: 1.0.0 resolve: 1.22.1 - rollup: 2.79.1 + rollup: 3.0.0-7 dev: true /@rollup/plugin-node-resolve/8.4.0_rollup@2.67.3: @@ -2290,6 +2299,18 @@ packages: picomatch: 2.3.1 rollup: 2.79.1 + /@rollup/pluginutils/3.1.0_rollup@3.0.0-7: + resolution: {integrity: sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==} + engines: {node: '>= 8.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0 + dependencies: + '@types/estree': 0.0.39 + estree-walker: 1.0.1 + picomatch: 2.3.1 + rollup: 3.0.0-7 + dev: true + /@rollup/pluginutils/4.2.1: resolution: {integrity: sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==} engines: {node: '>= 8.0.0'} @@ -7162,6 +7183,14 @@ packages: optionalDependencies: fsevents: 2.3.2 + /rollup/3.0.0-7: + resolution: {integrity: sha512-Hq5hpq9IrR4YfGd4o4zDtsfZnkwoCpjrQ4ZQIZrVF4ryzW6FOgXKBm6N7vJfodIJ6v+51FsqsXD2bxW5wapiuA==} + engines: {node: '>=14.18.0', npm: '>=8.0.0'} + hasBin: true + optionalDependencies: + fsevents: 2.3.2 + dev: true + /run-parallel/1.2.0: resolution: {integrity: sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==} dependencies: diff --git a/tsconfig.base.json b/tsconfig.base.json index 63da44c4e..a198903e5 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -12,7 +12,7 @@ "pretty": true, "sourceMap": true, "strict": true, - "target": "es2017" + "target": "es2019" }, "exclude": ["dist", "node_modules", "test/types"] }