我正在尝试将 rollup.js 合并到一个项目中。目前,我在控制台中收到下面提供的警告(未解决的依赖项),但我不确定为什么或如何修复它:
'fs' is imported by node_modules\filereader\FileReader.js, but could not be resolved – treating it as an external dependency
'fs' is imported by commonjs-external:fs, but could not be resolved – treating it as an external dependency
preferring built-in module 'punycode' over local alternative at 'C:\Users\Ryan\OneDrive\Projects\Custom Coding\Zapier\Ryan Test\node_modules\punycode\punycode.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'punycode' over local alternative at 'C:\Users\Ryan\OneDrive\Projects\Custom Coding\Zapier\Ryan Test\node_modules\punycode\punycode.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
这是需要 FileReader 和 https 的 test.js 脚本:
var FileReader = require('filereader');
var https = require('https');
最后是执行创建包的 rollup.config.js 文件:
var rollup = require('rollup');
var commonjs = require('rollup-plugin-commonjs');
var nodeResolve = require('rollup-plugin-node-resolve');
var globals = require('rollup-plugin-node-globals');
var builtins = require('rollup-plugin-node-builtins');
// build bundle
rollup
.rollup({
entry: 'test.js',
plugins: [
nodeResolve(),
commonjs(),
globals(),
builtins()
]
})
.then(bundle => bundle.write({
dest: 'rollupBundle/bundle.js',
format: 'cjs'
}))
.catch(err => console.log(err.stack));
CLI 将生成更多信息性警告 - 如果您更新配置文件以使用标准形式,那么您可以使用
rollup -c
代替,它通常会为您提供一个 URL 来帮助诊断问题。
这是一个配置文件,其中包含必要的更改以消除这些警告:
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import globals from 'rollup-plugin-node-globals';
import builtins from 'rollup-plugin-node-builtins';
export default {
entry: 'test.js',
dest: 'rollupBundle/bundle.js',
format: 'cjs',
external: [ 'fs' ], // tells Rollup 'I know what I'm doing here'
plugins: [
nodeResolve({ preferBuiltins: false }), // or `true`
commonjs(),
globals(),
builtins()
]
};
更新:“官方”Rollup 插件现在位于 npm 上的 @rollup 命名空间下,如果您安装上述两个版本,您将收到“npm WARN deprecated”消息,因此请安装较新的版本:
npm install @rollup/plugin-commonjs --save-dev
npm install @rollup/plugin-node-resolve --save-dev
然后像这样使用它们:
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
我也有类似的问题。对我有用的方法如下。
根据 Rollup docs,您可以将
/node_modules/
添加到您的外部。这样,任何导入的模块都将被视为外部模块。
为了做到这一点。
@rollup/plugin-node-resolve
通过npm install @rollup/plugin-node-resolve --save-dev
import { nodeResolve } from "@rollup/plugin-node-resolve";
...
export default {
external: [/node_modules/], // <-- Add `node_modules` as regex
plugins: [
nodeResolve(), // <-- Resolver to handle node modules
],
};