假设我想在我的项目(或任何给定的 npm 包)中使用 Immutable。我已经npm install
编辑了它,所以它在node_modules
中。当然,它有 CommonJS 导出。但是,我想在我的项目中使用 es6 模块。
我使用 Webpack 将其全部编译在一起,并使用 6to5-loader 来处理 es6 模块语法。
在我的源文件中,我说 import Immutable from 'immutable';
--- 但这会导致问题,因为 es6 import
正在寻找 es6 default
已导出,但事实并非如此(对于 Immutable 或可能几乎任何其他 npm 包)。编译后的代码最终看起来像这样: var Immutable = require('immutable')["default"];
--- 这当然会引发错误,因为没有 default
要查找的属性。
我可以使用带有 es6 模块的 npm 包吗?
最佳答案
Babel.js贡献者在这里。您正在寻找以下内容:
import * as Immutable from 'immutable';
// compiles to:
var Immutable = require('immutable');
Note: This is with either the
common
orcommonInterop
modules option. For others, see: https://babeljs.io/docs/usage/modules/
关于commonjs - 如何通过Webpack和6to5使用带有es6模块的npm包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305954/