Skip to content

Using ES6-authored modules with CRA #3889

Closed
@laurie71

Description

@laurie71

I'm working on a project that has a dependency on an npm module written in ES6. The module's package.json includes the following:

  "main": "lib/index.js",
  "module": "src/index.js",

It's 'lib' directory contains transpiled ES5 compatible code; the 'src' directory contains the ES6 code 'lib' is derived from. This all worked great until I needed to sub-class a class from this module.

CRA's babel/webpack configuration is picking up the code from 'src/...', due to the module entry in the module's package.json, but is not transpiling it. Therefore babel fails to transpile the sub-class, as it doesn't support extending native classes.

Solutions/workarounds I've found so far include:

  • remove the module entry from package.json
    • Pros: module's transpiled sources are used and everything works
    • Cons: when debugging into the module in devtools I get the transpiled sources, not the original ES6 sources :-/
  • edit the react-scripts/config/webpack.config.*.js to include the module in babel transformation
    • Pros: module's code gets transpiled along with app, everything works, and module's ES6 source is available untranspiled for debugging
    • Cons: requires unsupported hacking of installed react-scripts files; will break on upgrade
  • fork react-scripts to make the above change, use forked version
    • Pros: is a supported way to get a custom config without ejecting
    • Cons: would have to have a react-scripts fork for every project that uses ES6-authored modules :-/

Is there any other approach to getting this working I haven't found? Could CRA's build system (or babel configuration) be tweaked to automatically include transpilation of modules like this so things would work out of the box? Any other ideas for how this could be improved?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions