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

Create @babel/plugin-proposal-dynamic-import #9552

Merged
merged 5 commits into from Jun 30, 2019

Conversation

@nicolo-ribaudo
Copy link
Member

commented Feb 20, 2019

Q                       A
Fixed Issues? Fixes #9551
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This is an implementation of what I proposed in #9551. I had two goals:

  1. It shouldn't be possible, for example, to transform dynamic imports using commonjs and static imports using amd
  2. Since dynamic import is still a proposal, it needs not to be enabled by default by the module transform plugins

Note: sysntemjs was already supported and enabled by default, but this PR merged it's entry point with the one for commonjs/amd.

#4986 is similar, but it only implements CommonJS support and introduces a spec: true mode for commonjs, using different helpers.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Feb 20, 2019

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

@nicolo-ribaudo nicolo-ribaudo referenced this pull request Apr 30, 2019
2 of 2 tasks complete
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:dynamic-import branch 2 times, most recently from 223c1c3 to 33ba9d9 May 7, 2019
@nicolo-ribaudo nicolo-ribaudo added this to the v7.5.0 milestone May 11, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:dynamic-import branch 3 times, most recently from 5d543c1 to 3a1c335 May 13, 2019
export default declare((api, options) => {
api.assertVersion(7);

// TODO: expose a better interface
const transformImportCall = Function.call.bind(
babelPluginDynamicImportNode(api).visitor.Import,

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo May 16, 2019

Author Member

Please ignore the ugliness of this line. If this PR gets approved, I will open a PR in the babel-plugin-dynamic-import-node repo to directly export the transformImportCall function.

@chicoxyzzy chicoxyzzy referenced this pull request Jun 4, 2019
@xtuc

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

As we did for ESM, we should force to preserve the import function when using Babel loader in webpack (once it's in babel/preset-env).

@xtuc
xtuc approved these changes Jun 4, 2019
define(["require"], function (_require) {
"use strict";

var modP = new Promise(_resolve => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported))));

This comment has been minimized.

Copy link
@Jessidhia

Jessidhia Jun 5, 2019

Member

Is there any case where AMD will synchronously execute "mod" if the code for "mod" is already cached, or is it always async?

If so, the AMD require needs to be moved to a .then to ensure evaluation is asynchronous.

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Jun 9, 2019

Author Member

I checked the require source, and apparentely the callback is always called after a context.nextTick:
https://github.com/requirejs/requirejs/blob/fdf4186d3e68df06a04bd71cb6ea0f24eb1600d1/require.js#L1460

It is always guaranteed to be async: requirejs/requirejs#738 (comment)

Reading RequireJS's source code made me realize that it can also generate an error.

const MISSING_PLUGIN_WARNING = `\
WARNING: Dynamic import() transformation must be enabled using the
@babel/plugin-proposal-dynamic-import plugin. As of Babel 8,
without that plugin it won't be transformed.

This comment has been minimized.

Copy link
@Jessidhia

Jessidhia Jun 5, 2019

Member

"Babel 8 will no longer transform import() without using that plugin."

Do not duplicate code, which will unavoidably lead
to bugs being fixed in one plugin and not in the other.
@nicolo-ribaudo

This comment has been minimized.

Copy link
Member Author

commented Jun 18, 2019

@nicolo-ribaudo nicolo-ribaudo merged commit 38f8bba into babel:master Jun 30, 2019
6 of 8 checks passed
6 of 8 checks passed
Is version commit Is version commit
Details
On master branch On master branch
Details
babel/repl REPL preview is available
Details
buildsize No prior size to compare - 7.98 MB
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 87.49% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@nicolo-ribaudo nicolo-ribaudo deleted the nicolo-ribaudo:dynamic-import branch Jun 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.