15

我正在尝试将 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));
4

1 回答 1

20

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';
于 2017-01-06T13:01:47.210 回答