如何使用 babel 插件将 commonjs 转换为 es2015 模块?
Posted
技术标签:
【中文标题】如何使用 babel 插件将 commonjs 转换为 es2015 模块?【英文标题】:How can I transform commonjs to es2015 module with babel plugin? 【发布时间】:2017-01-13 08:48:57 【问题描述】:我使用 webpack2 来构建我的网络,我想从 tree-shaking 中受益,它只适用于 es2015 模块。
所以我想问一下commonjs到es2015的babel插件的transformer,比如这个rollup插件https://github.com/rollup/rollup-plugin-commonjs。
【问题讨论】:
仅供参考,OP 在这里要求插件或转换 - 实际上并不是要求整个库,所以我不认为这是题外话。就像问“我如何转换......”并得到一个 babel 插件作为答案。 @CodingIntrigue 感谢您的建议 【参考方案1】:babel-plugin-transform-commonjs-es2015-modules 就是一个这样的插件。 可以通过运行安装:
npm install babel-plugin-transform-commonjs-es2015-modules
然后将'plugins':['transform-commonjs-es2015-modules']
添加到您的 babel 配置中。
【讨论】:
以上是关于如何使用 babel 插件将 commonjs 转换为 es2015 模块?的主要内容,如果未能解决你的问题,请参考以下文章