我正在尝试将rollup.js合并到项目中。目前,我在控制台中收到下面提供的警告( Unresolved 依赖项),我不确定为什么或如何解决它:
'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()
]
};
更新:现在,“官方”汇总插件位于npm上的@rollup命名空间下,如果安装上述两个版本,则会收到“不推荐使用npm WARN”消息,因此请安装较新的版本: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';
关于javascript - Rollup.js Unresolved 依赖项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41495596/