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

Specify runtime exports #10853

Merged
merged 9 commits into from Oct 14, 2020
Merged

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Dec 10, 2019

Q                       A
Fixed Issues? Fixes #10548, fixes #8462, fixes #12003, fixes #12058
License MIT

Solved issues

This PR add helper/esm exports to @babel/runtime(-corejs[23])?. Importing esm modular helpers are now broken on Babel 7.7.6 and Node.js 13.2

import "@babel/runtime/helpers/esm/applyDecoratedDescriptor"

// Error: Package exports for /path/to/babel/node_modules/@babel/runtime/package.json
// do not define a './helpers/esm/applyDecoratedDescriptor' subpath,
// imported from /path/to/code/babel/test.mjs

import "@babel/runtime/helpers/esm/applyDecoratedDescriptor.js"

// Error: Package exports for /path/to/babel/babel/node_modules/@babel/runtime/package.json
// do not define a './helpers/esm/applyDecoratedDescriptor.js' subpath,
// imported from /path/to/babel/test.mjs

This PR adds such missing exports so both

import "@babel/runtime/helpers/esm/applyDecoratedDescriptor"
// and
import "@babel/runtime/helpers/esm/applyDecoratedDescriptor.js"

should work.

Changes to @babel/runtime

Take @babel/runtime-corejs3 as an example:

7.11.2

import _Array$isArray from "../../core-js/array/is-array";
import arrayLikeToArray from "./arrayLikeToArray";
export default function _arrayWithoutHoles(arr) {
  if (_Array$isArray(arr)) return arrayLikeToArray(arr);
}

This PR

import _Array$isArray from "@babel/runtime-corejs3/core-js/array/is-array";
import arrayLikeToArray from "@babel/runtime-corejs3/helpers/esm/arrayLikeToArray";
export default function _arrayWithoutHoles(arr) {
  if (_Array$isArray(arr)) return arrayLikeToArray(arr);
}

The relative imports to core-js helpers are replaced by submodule imports @babel/runtime-corejs3/core-js. The relative imports to internal Babel helpers are also replaced by @babel/runtime-corejs3/helpers/esm/arrayLikeToArray.

This changes are also applied on non-esm helpers.

7.11.2

var _Array$isArray = require("../core-js/array/is-array");
var arrayLikeToArray = require("./arrayLikeToArray");
function _arrayWithoutHoles(arr) {
  if (_Array$isArray(arr)) return arrayLikeToArray(arr);
}
module.exports = _arrayWithoutHoles;

This PR

var _Array$isArray = require("@babel/runtime-corejs3/core-js/array/is-array");
var arrayLikeToArray = require("@babel/runtime-corejs3/helpers/arrayLikeToArray");
function _arrayWithoutHoles(arr) {
  if (_Array$isArray(arr)) return arrayLikeToArray(arr);
}
module.exports = _arrayWithoutHoles;

Node.js ESM requires relative imports (e.g. ./arrayLikeToArray) ending with specific extension .js. However, adding extension to import path will break people consuming @babel/runtime via AMD, because we removed file extension from the module specifier:

. So we generated define("@babel/runtime/arrayLikeToArray") for arrayLikeToArray.js and thus require("./arrayLikeToArray.js") will throw for AMD users.

Since it is a breaking to change the moduleName behaviour. This PR does not add extension but adding more exports so it can work with both Node.js ESM and AMD.

@JLHwung JLHwung added the PR: Internal 🏠 A type of pull request used for our changelog categories label Dec 10, 2019
"./helpers/": "./helpers/",
"./helpers/esm/": "./helpers/esm/",
"./regenerator": "./regenerator/index.js",
"./regenerator/": "./regenerator/",

This comment was marked as outdated.

@nicolo-ribaudo
Copy link
Member

No need to introduce removeImportsExtension option in 7.8.0

Don't we still need it for the useBuiltIns option anyway?

@JLHwung
Copy link
Contributor Author

JLHwung commented Dec 11, 2019

Don't we still need it for the useBuiltIns option anyway?

I think we can add .js to corejs and regenerator-runtime imports since they doesn't support amd. (See #10862 After that PR we don't have to introduce removeImportsExtension then.

We could consider introduce removeImportsExtension to transform-modules-amd so that the following usage

import "foo/bar.js"

can be correctly transpiled to AMD

define(["foo/bar"], function (_bar) {
  "use strict";
});

@politician
Copy link

What's the status on this PR?
Unless I missed something, as of today, it is not possible for Babel to transpile ES6 modules to be consumed natively by Node 13 and 14 when using plugin-transform-runtime.

#10549 was apparently fixing the issue but has been rolled back

@JLHwung JLHwung changed the base branch from master to main June 24, 2020 01:25
@babel-bot
Copy link
Collaborator

babel-bot commented Jun 24, 2020

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/29137/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 8, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4997566:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration
babel-repl-custom-plugin Issue #12003

"./package.json": "./package.json",
"./regenerator": "./regenerator/index.js",
"./regenerator/": "./regenerator/",
"./core-js/": "./core-js/",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the directory export map is deep: All files under core-js can be imported via path/filename.ext:

import "@babel/runtime-corejs2/core-js/object/assign.js"

Copy link
Contributor

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, maybe it will be good idea to add couple of tests

@@ -1,7 +1,7 @@
import arrayWithHoles from "./arrayWithHoles";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, some corejs 2 helper files are tracked in git repository. Yet it is convenient so you can review the actual change of helper imports.

/cc @rwjblue Will ember support self referencing package? In this case @babel/runtime-corejs2 is referencing itself. The import path extension .js is not added so this part should work with amd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet it is convenient so you can review the actual change of helper imports.

That's exactly why we don't ignore some of these files 😛

@@ -0,0 +1,69 @@
import assert from "assert";

export default {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote the testcases in such a way that hopefully we can use a codemod to convert the testcases to Jest BDD test styles, after Jest supports esm.

@@ -0,0 +1,11 @@
{
"name": "@babel/test-esm",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package is not visible to lerna. It is handled by yarn so we can import @babel/runtime from the workspace.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good!

import _Symbol from "../../core-js/symbol";
import _Array$from from "@babel/runtime-corejs2/core-js/array/from";
import _isIterable from "@babel/runtime-corejs2/core-js/is-iterable";
import _Symbol from "@babel/runtime-corejs2/core-js/symbol";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For users using PnP, is it allowed for a package to import itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yarn PnP does not support esm yet: yarnpkg/berry#638

@merceyz Does PnP support require("package/itself")?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's totally fine

@@ -1,7 +1,7 @@
import arrayWithHoles from "./arrayWithHoles";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet it is convenient so you can review the actual change of helper imports.

That's exactly why we don't ignore some of these files 😛

@JLHwung JLHwung added this to the v7.12.0 milestone Sep 17, 2020
@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Sep 30, 2020
@nicolo-ribaudo nicolo-ribaudo merged commit 4e66b8e into babel:main Oct 14, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the specify-runtime-exports branch October 14, 2020 18:07
amireh added a commit to amireh/instructure-ui that referenced this pull request Dec 17, 2020
refs FOO-1331

need the fix in babel/babel#10853 so that the
helpers injected by @babel/runtime et al do work in ESM mode (previously
they would be missing the file extension)

Change-Id: I41b13e03d248068b421065c6eada7f93defb48f7
amireh added a commit to amireh/instructure-ui that referenced this pull request Dec 17, 2020
refs FOO-1331

need the fix in babel/babel#10853 so that the
helpers injected by @babel/runtime et al do work in ESM mode (previously
they would be missing the file extension)

Change-Id: I41b13e03d248068b421065c6eada7f93defb48f7
matyasf pushed a commit to instructure/instructure-ui that referenced this pull request Jan 12, 2021
refs FOO-1331

need the fix in babel/babel#10853 so that the
helpers injected by @babel/runtime et al do work in ESM mode (previously
they would be missing the file extension)

Change-Id: I41b13e03d248068b421065c6eada7f93defb48f7
matyasf pushed a commit to instructure/instructure-ui that referenced this pull request Jan 13, 2021
refs FOO-1331

need the fix in babel/babel#10853 so that the
helpers injected by @babel/runtime et al do work in ESM mode (previously
they would be missing the file extension)

Change-Id: I41b13e03d248068b421065c6eada7f93defb48f7
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 14, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Internal 🏠 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release
Projects
None yet
7 participants