Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Load babel-plugin-syntax-dynamic-import plugin #385

Merged
merged 5 commits into from
Jul 28, 2017

Conversation

npejo
Copy link
Member

@npejo npejo commented Jul 27, 2017

From Webpack2 we can use dynamic imports to asynchronously load modules in the project. Even Webpack will understands the import() statement, Babel will fail to parse this code if we don't install the appropriate plugins.

Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level

To avoid this issue, we can use the babel-plugin-syntax-dynamic-imports.

This plugin only allows Babel to parse this syntax.
https://babeljs.io/docs/plugins/syntax-dynamic-import/

@npejo npejo changed the title Load babel-plugin-syntax-dynamic-import on dev builds Load babel-plugin-syntax-dynamic-import for development builds Jul 27, 2017
@@ -50,7 +50,8 @@ export default {
// see: https://github.com/babel/babel/issues/4702
...(action === actions.DEVELOP || action === actions.TEST_UNIT ? [
[require.resolve('babel-plugin-transform-react-jsx-source'), {}],
[require.resolve('babel-plugin-transform-react-jsx-self'), {}]
[require.resolve('babel-plugin-transform-react-jsx-self'), {}],
[require.resolve('babel-plugin-syntax-dynamic-import'), {}]
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be only available in development mode. You could use the dynamic import feature for production builds as well.

Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to have an integration test that tests this "dynamic import" syntax to see if it really works.

Choose a reason for hiding this comment

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

Dynamic imports are handled natively by webpack >= 2. This plugin is a "syntax", not "transform", and it should be in dev only just for babel to not fail on parsing.

@npejo npejo changed the title Load babel-plugin-syntax-dynamic-import for development builds Load babel-plugin-syntax-dynamic-import plugin Jul 28, 2017
@deepsweet
Copy link

Dynamic imports are handled natively by webpack >= 2. This plugin is a "syntax", not "transform", and it should be in dev only (and not for production) just for babel to not fail on parsing.

@pirelenito
Copy link
Member

@deepsweet you can still use dynamic imports in production code, and that code needs to be understood by Babel (even if Webpack supports it), correct? Or am I missing something?

@deepsweet
Copy link

You are right.

@npejo
Copy link
Member Author

npejo commented Jul 28, 2017

Initially I added the plugin under the if (action === actions.DEVELOP) clause, but the integration test fails to execute actions.BUILD with dynamic import in the code.
When I moved the plugin out of the if, to be always required, Babel is not complaining any more 🙂

@pirelenito pirelenito merged commit 70c9249 into v11 Jul 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants