gpt4 book ai didi

commonjs - 如何通过Webpack和6to5使用带有es6模块的npm包?

转载 作者:行者123 更新时间:2023-12-02 16:35:48 26 4
gpt4 key购买 nike

假设我想在我的项目(或任何给定的 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');

Interactive demo

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

关于commonjs - 如何通过Webpack和6to5使用带有es6模块的npm包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com