我正在测试如何驱动卷式将一个节点应用程序打包到一个bundle.js中,我感到很困惑。
rollup支持捆绑完整的节点应用程序(包括
node_modules),还是只支持属于项目一部分的js文件?
我有一个标准的节点项目(1 index.js,node_modules中的数千个文件),并且只想要一个bundle.js。我试过:
rollup.config.js
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
export default {
entry: 'index.js',
dest: 'bundle.js',
format: 'iife',
plugins: [
commonjs({
// non-CommonJS modules will be ignored, but you can also
// specifically include/exclude files
include: 'node_modules/**', // Default: undefined
// if true then uses of `global` won't be dealt with by this plugin
ignoreGlobal: false, // Default: false
// if false then skip sourceMap generation for CommonJS modules
sourceMap: false, // Default: true
}),
nodeResolve({
jsnext: true,
main: false
})
]
};无论我尝试什么,rollup都会把这个index.js
module.exports = require('dat-node') // 88 MB node_modules使用此命令:
rollup index.js --format iife --output dist/bundle.js -c到此bundle.js,而不添加来自node_modules的任何内容。
(function () {
'use strict';
module.exports = require('dat-node');
}());我试过:
现在我在想,也许我理解错了,它不支持我想要的。非常感谢你的帮助!
发布于 2017-08-16 08:07:29
试试这个:
import commonjs from "rollup-plugin-commonjs";
import nodeResolve from "rollup-plugin-node-resolve";
export default {
entry : "index.js",
dest : "bundle.js",
moduleName : "myModule",
format : "iife",
plugins : [
commonjs({
// non-CommonJS modules will be ignored, but you can also
// specifically include/exclude files
include: [ "./index.js", "node_modules/**" ], // Default: undefined
// if true then uses of `global` won't be dealt with by this plugin
ignoreGlobal: false, // Default: false
// if false then skip sourceMap generation for CommonJS modules
sourceMap: false // Default: true
}),
nodeResolve({
jsnext: true,
main: false
})
]
};主要的更改是,您还需要在index.js调用中包括commonjs,否则它将不会转换为ES6模块(这正是nodeResolve所需要的)。
您还需要设置moduleName。
NB:我不是专门用dat-node测试的,而是用lodash测试的。
发布于 2022-01-01 06:40:32
我也面临着同样的问题,我搜索了很多,但答案大多是旧的语法。经过一番探索,这才是对我有用的。我不能百分之百肯定这是最好的方法。
如果对卷起有更多了解的人来验证它,那将是非常有帮助的。
因此,我发现的诀窍是将modulesOnly选项添加到nodeResolve插件中,如下所示:
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
export default {
input: "src/index.js",
output: [
{
format: "cjs",
file: "dist/index.cjs.js",
},
{
format: "esm",
file: "dist/index.esm.js",
},
],
plugins: [
commonjs(),
nodeResolve({ modulesOnly: true }),
],
};https://stackoverflow.com/questions/45707711
复制相似问题