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

Dynamic import not working if using .babelrc #2442

Closed
cloud-walker opened this issue Dec 26, 2016 · 9 comments
Closed

Dynamic import not working if using .babelrc #2442

cloud-walker opened this issue Dec 26, 2016 · 9 comments

Comments

@cloud-walker
Copy link

I'm trying to use this: https://github.com/tc39/proposal-dynamic-import on my experimental boilerplate.

All is working fine if I move babel configuration to the package.json:

"babel": {
  "presets": ["latest", "stage-2", "react"]
}

The same exact configuration give the following error if the babel config is on its own .babelrc file:

source/shared/pages/Home/index.js:4
   import('./Home').then(function (mod) /* istanbul ignore next */{return mod.default;}));});
   ^^^^^^
SyntaxError: Unexpected token import

I've node 7.3, latest yarn and latest jest on OSX

@cloud-walker
Copy link
Author

Even using babel on package.json jest is failing if I try to use jest --coverage, reporting the same error.

@thymikee
Copy link
Collaborator

I've tried this boilerplate: https://github.com/cloud-walker/frontend-boilerplate, changed System.import() -> import() and it doesn't work for me in any of the cases. Webpack bundle is still working though.

Looks like babel-plugin-dynamic-import-node helps to resolve that. Managed to get it working by adding the following to babel config:

  "env": {
    "test": {
      "plugins": [
        "babel-plugin-dynamic-import-node"
      ]
    }
  }

Make sure you run jest with --no-cache flag every time you make a change to babel config (http://facebook.github.io/jest/docs/troubleshooting.html#caching-issues).

@conatus
Copy link

conatus commented Feb 9, 2018

Just encountered this issue from using react-loadable.

The workaround above works, but is this perhaps something to be added to the documentation? Thanks.

@SimenB
Copy link
Member

SimenB commented Feb 10, 2018

It's mentioned at the bottom here: https://facebook.github.io/jest/docs/en/webpack.html#using-with-webpack-2

@urrri
Copy link

urrri commented Mar 5, 2018

I tried using env + react + stage-0 presets and "babel-plugin-dynamic-import-node".
I tried it as explained in https://facebook.github.io/jest/docs/en/webpack.html#using-with-webpack-2
Nothing helps. Still the same error.

@robwierzbowski
Copy link

Would be nice to have in a section that's agnostic to Webpack versions. I wouldn't have read through the Webpack 2 docs cause I'm on Webpack 4. Thanks for pointing to it though!

@tomdohnal
Copy link

tomdohnal commented Sep 27, 2018

I made it work by using the babel-plugin-transform-dynamic-import plugin.
Make sure that you reference it in your .babelrc (or babel.config.js) plugin section.

...
plugins: [
   ...
   'babel-plugin-transform-dynamic-import'
]

@davidnormo
Copy link
Contributor

In case this helps anyone else:

My tests were failing in CI but not locally the reason for this being that the NODE_ENV was set to production in the Dockerfile. The .babelrc env property is matched against the BABEL_ENV environment variable. Failing that it uses NODE_ENV. Jest will set NODE_ENV to "test" if it is null! Therefore because it was production it wouldn't set it to test and babel would not run the transform.

Joy.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants