gpt4 book ai didi

javascript - 如何让 Rollup 转换 require() 调用?

转载 作者:行者123 更新时间:2023-11-29 21:14:49 25 4
gpt4 key购买 nike

我正在尝试使用 Rollup 构建我的项目。我得到了它的大部分工作(包括 Angular 2),但我无法转换我的一些 polyfill,因为它们调用了 require()在他们中。即使是 rollup-plugin-node-resolverollup-plugin-commonjs它不会触及 require完全没有。

那么我该如何改造它们呢?我可以使用的解决方法是浏览器化 polyfill,然后在汇总过程中包含浏览器化的 bundle ,但这需要大量额外的插件并且不是一个干净的解决方案,特别是因为我认为汇总应该能够理解CommonJS.

下面是一些 Node.js 的测试代码:

"use strict";
let rollup = require("rollup");
let fs = require("fs");
let resolve = require("rollup-plugin-node-resolve");
let commonjs = require("rollup-plugin-commonjs");

rollup.rollup({
entry: "src/main.js",
plugins: [
resolve({
jsnext: true,
main: true
}),
commonjs({
include: '**', // also tried src/** node_modules/** and a lot of other stuff.
// Leaving this undefined stops rollup from working
}),
]
}).then(function(bundle) {
bundle.write({
format: "iife",
dest: "www/bundle.js"
})
});

然后使用一个简单的文件作为 src/main.js只包含行 require("./some-file.js");和一个 src/some-file.js包含任何东西都说明了问题。

最佳答案

我在 rollup-gitter 的好人的帮助下解决了这个问题。

我需要将 moduleName 添加到 bundle.write 选项中,如下所示:

bundle.write({
format: "iife",
dest: "www/bundle.js"
moduleName: "someModule"
})

关于javascript - 如何让 Rollup 转换 require() 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39869234/

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