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

Add support for Babel 7 #62

Open
matteoantoci opened this issue Sep 3, 2018 · 8 comments
Open

Add support for Babel 7 #62

matteoantoci opened this issue Sep 3, 2018 · 8 comments

Comments

@matteoantoci
Copy link

Hi there!

Just wanted to point out that inject-loader doesn't work properly with Babel 7.

Running my tests suite I get this error whenever inject-loader is imported in my tests:

Module parse failed: 'import' and 'export' may only appear at the top level (975:4)
You may need an appropriate loader to handle this file type.

Maybe is due to the hardcoded usage of the deprecated babel-core module inside inject-loader? 🤔

@plasticine
Copy link
Owner

@matteoantoci Hey thanks for the report, I’ll try and find time to have a look :)

@miquelmasrieramrf
Copy link

miquelmasrieramrf commented Sep 21, 2018

that would be nice!
In the meantime if you want to use inject-loader with babel7, you can use @babel/plugin-transform-modules-commonjs.
It will transform the modules to commonjs so the loader will work

@wwarme
Copy link

wwarme commented Oct 5, 2018

Had the very same issue, we just migrated to Babel 7, @babel/plugin-transform-modules-commonjs came to rescue. Thanks alot!
If not dependant on module: "esnext" - module: "commonjs" in tsconfig does the trick too.

Ant-f added a commit to Ant-f/trap-finder that referenced this issue Oct 31, 2018
  - using 'plugin-transform-modules-commonjs' as inject-loader currently doesn't work well with Babel 7 (plasticine/inject-loader#62)
@plasticine
Copy link
Owner

Thanks tl;dr of this is that inject-loader operates on CJS modules only (at the moment anyway), so any code you want to inject needs to be compiled to CJS before inject-loader can operate on it. This should probably be documented better and/or an example for Babel 7.

@zfeher
Copy link

zfeher commented Aug 12, 2019

Hey' We are currently migrating to Babel 7.5.5 and Webpack 4.35.3 and we ran into some problem with some of our tests which needs inject-loader.

As soon as I add the import SomeComponent from 'inject-loader!.'; and run the tests it will print an error that it doesn't know about the object rest/spread operator (whereas babel preset env is properly configured and set to the correct node version).

Compiling the same file via babel cli works without an error. It seems that the babel configuration is not used by inject loader. (the same setup works with Babel 6 with Webpack 3)

I've tried to explicitly add the @babel/plugin-proposal-object-rest-spread plugin as well without any success. Also tried to add babel as an inline loader like babel-loader!inject-loader!. / !inject-loader!babel-loader!.

Is there a workaround for this for now?
When can we expect Babel 7 support?

@amccloud
Copy link

amccloud commented Mar 17, 2020

I was able to work around this issue by upgrading inject-loader to @seanparmelee's babel 7 fork seanparmelee@fcd20ba at and then setting babel-preset-env's modules to commonjs for my test env

{
  "env": {
    "test": {
      "presets": [
        [
          "@babel/preset-env",
          {
            "modules": "commonjs"
          }
        ]
      ]
   }
}

@seanparmelee
Copy link
Collaborator

I opened a couple PRs that should help out here:
#73 adds ESM support so you no longer have to transform your modules to CJS format before running them through inject-loader
#74 includes #73 and upgrades to Babel 7.

Would love it if someone could try out the ESM feature in either PR to help identify any use cases I may have missed.

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

No branches or pull requests

7 participants