Skip to content

Export createDynamicImportTransform and getImportSource#75

Merged
ljharb merged 1 commit intoairbnb:masterfrom
nicolo-ribaudo:export-internals
Jun 18, 2019
Merged

Export createDynamicImportTransform and getImportSource#75
ljharb merged 1 commit intoairbnb:masterfrom
nicolo-ribaudo:export-internals

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
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
Copy Markdown
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.

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
Copy Markdown
Contributor Author

Thanks!

@ljharb
Copy link
Copy Markdown
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.

2 participants