Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(babel): prepare for Rollup 3 #1303

Merged
merged 1 commit into from Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/babel/README.md
Expand Up @@ -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

Expand All @@ -45,7 +45,7 @@ const config = {
input: 'src/index.js',
output: {
dir: 'output',
format: 'esm'
format: 'es'
},
plugins: [babel({ babelHelpers: 'bundled' })]
};
Expand Down Expand Up @@ -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' }
]
};
```
Expand All @@ -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'] })]
}
]
Expand All @@ -212,7 +212,7 @@ export default {
output: [
{
file: 'bundle.js',
format: 'esm',
format: 'es',
plugins: [getBabelOutputPlugin({ presets: ['@babel/preset-env'] })]
}
]
Expand All @@ -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' }], ...]
Expand All @@ -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 }]]
Expand Down
42 changes: 24 additions & 18 deletions packages/babel/package.json
Expand Up @@ -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"
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved
},
"scripts": {
"build": "rollup -c",
Expand All @@ -33,6 +38,7 @@
},
"files": [
"dist",
"!dist/**/*.map",
"types",
"README.md",
"LICENSE"
Expand All @@ -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
},
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved
"@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/**",
Expand Down
20 changes: 0 additions & 20 deletions packages/babel/rollup.config.js

This file was deleted.

18 changes: 18 additions & 0 deletions 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'
})
]
};
2 changes: 1 addition & 1 deletion 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 {
Expand Down
10 changes: 5 additions & 5 deletions 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,
Expand Down
3 changes: 3 additions & 0 deletions packages/babel/src/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
4 changes: 2 additions & 2 deletions 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. ' +
Expand Down