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

Export createDynamicImportTransform and getImportSource #75

Merged
merged 1 commit into from Jun 18, 2019
Merged

Export createDynamicImportTransform and getImportSource #75

merged 1 commit into from Jun 18, 2019

Conversation

nicolo-ribaudo
Copy link
Contributor

I'd be very grateful if you could expose your internal transformation logic to use it in @babel/plugin-proposal-dynamic-import, which will then be used in @babel/preset-env. (I will mention in the docs that, when targeting commonjs, it internally uses this plugin).

Currently I'm working it around by doing this, but it is unnecessarily too hacky: link

  const transformImportCall = Function.call.bind(
    babelPluginDynamicImportNode(api).visitor.Import,
  );

Commit message:

While working on babel/babel#9552, I realized I needed two functions:

  • createDynamicImportTransform is basically all what this plugin
    does, but it can be invoked directly.
  • getImportSource is needed to re-use the same import argument
    stringifying logic for the AMD and SystemJS transforms

I added those functions as named exports of the main entry point,
but I had some compatibility problems because the "add-module-exports"
babel plugin automatically disables itself when there is a named
export. I resolved it by removing that plugin and manually writing
module.exports in the source file.
I couldn't disable the modules transform in the "airbnb" preset because
"transform-replace-object-assign" (v0.2.x) always injects import
declarations, even if "sourceType" is "script".

If you prefer not to have module.exports in the source file, I could create two separate entry points: babel-plugin-dynamic-import-node and babel-plugin-dynamic-import-node/utils, and export those new functions from the second one. I think that the current implementation is nicer.

NOTE: This isn't blocking the Babel release, since the workaround is working for now. I can't use getImportSource for AMD yet, but it is a minor problem.

While working on babel/babel#9552, I realized I needed two functions:
- createDynamicImportTransform is basically all what this plugin
  does, but it can be invoked directly.
- getImportSource is needed to re-use the same import argument
  stringifying logic for the AMD and SystemJS transforms
Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Let's extract these out to separate files instead of exporting multiple things from the same file.

.babelrc Outdated Show resolved Hide resolved
.babelrc Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
ljharb
ljharb previously approved these changes Jun 18, 2019
@ljharb ljharb merged commit 08ebb94 into airbnb:master Jun 18, 2019
@nicolo-ribaudo nicolo-ribaudo deleted the export-internals branch June 18, 2019 22:44
@nicolo-ribaudo
Copy link
Contributor Author

Thanks!

@ljharb
Copy link
Collaborator

ljharb commented Jun 18, 2019

v2.3.0 released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants