如何串联 ES6 模块?
var foo = 2; // This would normally be scoped to the module.
export function Bar() {}
// ...concatenate...
import { Bar } from 'javascripts/bar' //This file no longer exists in the concatenated scenario.
export function Bam() {}
最佳答案
2020-09-02 更新:世界语已替换为 Rollup前一段时间,对于这个问题来说是一个很好的选择。根据您的需求,Webpack或许也是一个不错的选择。
<小时/>如果您想要创建一个内部不使用 ES6 模块的 JavaScript 文件,以便您今天可以将其与浏览器/节点一起使用,那么我建议使用 Esperanto (完全披露,我是该项目的维护者)。它允许您创建一个将所有文件连接在一起的包,而无需使用像 browserify 或 webpack 这样的加载器。这通常会导致更小的代码(没有加载器)、更好的死代码消除(当使用 Google Closure Compiler 或 UglifyJS 等压缩器时)以及更好的性能,因为 JS 解释器能够更好地优化结果。
这是一个示例用法,但请注意有 plenty of tools to integrate Esperanto into your workflow :
var fs = require( 'fs' );
var esperanto = require( 'esperanto' );
esperanto.bundle({
base: 'src', // optional, defaults to current dir
entry: 'mean.js' // the '.js' is optional
}).then( function ( bundle ) {
var cjs = bundle.toCjs();
fs.writeFile( 'dist/mean.js', cjs.code );
});
关于javascript - 如何串联 ES6 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488485/