saguijs / sagui

:monkey: Front-end tooling in a single dependency
http://sagui.js.org/
MIT License
669 stars 32 forks source link

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

Closed npejo closed 7 years ago

npejo commented 7 years ago

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/

deepsweet commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

You are right.

npejo commented 7 years ago

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 🙂