webpack: Uncaught SyntaxError: Unexpected token import
Bug
What is the current behavior? In case of development environment such code
if (process.env.NODE_ENV === 'testing') {
import('somemodule');
}
is transformed by webpack 2.5.1
into this
if (false) {
import('somemodule');
}
which leads to Uncaught SyntaxError: Unexpected token import
error in the latest Chrome.
Node.JS 7.10.0, babel 6.24.1 has syntax-dynamic-import
plugin
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 20 (3 by maintainers)
Fixed in
4.0.0-alpha.5
.Hey guys, maybe I’ll be a little help for you. @klimashkin Could you maybe share your webpack and babel configs? I just tried to run following code through webpack:
and it gets transpiled properly. Just make sure(and it’s also a tip for @anlexN), that you have
babel-loader
included properly in module section in your configuration withsyntax-dynamic-import
plugin already mentioned above:and my
.babelrc
config(you can also include this configuration right withinwebpack.config.js
, see https://github.com/babel/babel-loader#options):Babel should at least transpile dynamic import on unreachable blocks. This should solve that problem without this painful.
This completely breaks the entire concept of webpack.
This is 100% broken.
Because I use
if (module.hot)
my app does not work in production…so i am going in and manually deleting all that code everywhere each time i want to go to production… stupid as fuck lolzUglify does not work or i would use that… some it fails because some node module has some file that isnt actually included in anything that has some syntax error
I had this same problem (also with module.hot that was turned into false at transpile-time, creating an unreachable block).
I managed to work around it by including something that could not be evaluated to false at transpile-time, forcing webpack to transpile the contents of the if-block:
Maybe this can help someone else while this gets fixed.
@anlexN It is only needed if you want to use newer syntax and language future in your configuration. See https://webpack.js.org/configuration/configuration-languages/ for more info.
Don’t! It’s painful!. This is webpack authors’ wrong, not our developers’ wrong…
Running into this issue as well with latest version of webpack, a reasonable solution would be to use the UglifyJsPlugin to just strip the false conditional blocks since that is what you meant to do anyway, however, there doesn’t appear to be a straight forward way to do this without having your dev source mangled a bit and UglifyJs does add to the compilation time as well (in my case +65%).
Any updates on this? I am having this issue as well with the following code. Chrome seems to throw an error any time it sees
import
.I’ve tried using
syntax-dynamic-import
to fix the issue, but it persists even with that plugin. However, the issue is fixed when I use UglifyJSPlugin leading me to believe it changes the name of the import function and then the browser doesn’t care. FYI this is being built with a production webpack config (minus uglification), so the HMR plugins are not there.@michael-ciniawsky, i don’t use babel anything! why rename webpack.config.js to webpack.config.babel.js?
@michael-ciniawsky Sure thats how
can be transformed to
If you read first message of this thread