如何在汇总中将所有节点模块设置为外部?
How to set as external all node modules in rollup?
我想将从 node_modules 导入的所有模块作为外部模块。
这是什么配置?
我试了没有成功:
import path from "path";
import glob from "glob";
import multiEntry from "rollup-plugin-multi-entry";
export default {
entry: "src/**/*.js",
format: "cjs",
plugins: [
multiEntry()
],
external: glob.sync("node_modules/**/*.js").map(file => path.resolve(file)),
dest: "dist/bundle.js"
}];
或
import multiEntry from "rollup-plugin-multi-entry";
export default {
entry: "src/**/*.js",
format: "cjs",
plugins: [
multiEntry()
],
external: id => id.indexOf("node_modules") !== -1,
dest: "dist/bundle.js"
}];
您可以使用 package.json 依赖项字段完成此操作:
const pkg = require('./package.json');
export default {
// ...
external : Object.keys(pkg.dependencies),
// ...
}
更新:
使用rollup-plugin-auto-external.
当然要感谢@IsidroTorregrosa for (and @maxkueng)。我通过添加节点 built-in 模块(如 fs
和 path
)以及 peerDependencies
对其进行了改进。使用:
import builtins from 'builtin-modules/static'
const pkg = require('./package.json')
// ...
export default {
external: builtins.concat(Object.keys(pkg.dependencies || {})).concat(Object.keys(pkg.peerDependencies || {})),
// ...
}
请参阅 npmjs.com 上的 builtin-modules 以及:
https://github.com/rollup/rollup-plugin-node-resolve#resolving-built-ins-like-fs
使用 rollup 2.63 这对我有用
export default {
// ...
external: [/node_modules/],
// ...
}
汇总配置
import pkg from "./package.json";
/**
* Rollup bundler configuration.
*
* @see https://rollupjs.org/
* @type {import("rollup").RollupOptions}
*/
export default {
...
external: [...Object.keys(pkg.dependencies), /^node:/]
}
应用程序源代码
import path from "node:path";
import fs from "node:fs/promises";
...
我想将从 node_modules 导入的所有模块作为外部模块。 这是什么配置?
我试了没有成功:
import path from "path";
import glob from "glob";
import multiEntry from "rollup-plugin-multi-entry";
export default {
entry: "src/**/*.js",
format: "cjs",
plugins: [
multiEntry()
],
external: glob.sync("node_modules/**/*.js").map(file => path.resolve(file)),
dest: "dist/bundle.js"
}];
或
import multiEntry from "rollup-plugin-multi-entry";
export default {
entry: "src/**/*.js",
format: "cjs",
plugins: [
multiEntry()
],
external: id => id.indexOf("node_modules") !== -1,
dest: "dist/bundle.js"
}];
您可以使用 package.json 依赖项字段完成此操作:
const pkg = require('./package.json');
export default {
// ...
external : Object.keys(pkg.dependencies),
// ...
}
更新:
使用rollup-plugin-auto-external.
当然要感谢@IsidroTorregrosa for fs
和 path
)以及 peerDependencies
对其进行了改进。使用:
import builtins from 'builtin-modules/static'
const pkg = require('./package.json')
// ...
export default {
external: builtins.concat(Object.keys(pkg.dependencies || {})).concat(Object.keys(pkg.peerDependencies || {})),
// ...
}
请参阅 npmjs.com 上的 builtin-modules 以及:
https://github.com/rollup/rollup-plugin-node-resolve#resolving-built-ins-like-fs
使用 rollup 2.63 这对我有用
export default {
// ...
external: [/node_modules/],
// ...
}
汇总配置
import pkg from "./package.json";
/**
* Rollup bundler configuration.
*
* @see https://rollupjs.org/
* @type {import("rollup").RollupOptions}
*/
export default {
...
external: [...Object.keys(pkg.dependencies), /^node:/]
}
应用程序源代码
import path from "node:path";
import fs from "node:fs/promises";
...