How to consume npm package with es6 module via Webpack and 6to5?

后端 未结 2 492
一生所求
一生所求 2021-02-07 01:37

Let\'s say I want to use Immutable in my project (or any given npm package). I have npm installed it, so it is in node_modules. Of course, it has Commo

相关标签:
2条回答
  • 2021-02-07 01:49

    Just figured it out. (The solution is tool-specific --- but es6 modules only exist now insofar as they are tool-enabled, so I think that's enough of an "answer".)

    6to5's default module transpilation uses the common option, which results in the very problem I griped about above. But there is another option: commonInterop --- which must have been built to deal with exactly the situation I'm dealing with. See https://6to5.github.io/modules.html#common-interop

    So three cheers for 6to5.

    0 讨论(0)
  • 2021-02-07 01:54

    Babel.js contributor here. You're looking for the following:

    import * as Immutable from 'immutable';
    // compiles to:
    var Immutable = require('immutable');
    

    Interactive demo

    Note: This is with either the common or commonInterop modules option. For others, see: https://babeljs.io/docs/usage/modules/

    0 讨论(0)
提交回复
热议问题