导入节点的 hash.crypto 时出错 - (!) 未解决的依赖项

Error importing node's hash.crypto - (!) Unresolved dependencies

我在从节点的 crypto 包导入 hash 时遇到问题。这是我收到的错误:

错误

[~/Projects/rollup] yarn run build
yarn run v1.2.1
$ rollup -c

src/main.js → ./build/app.js...
(!) Unresolved dependencies
https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency
crypto (imported by src/main.js)
(!) Missing global variable name
Use options.globals to specify browser global variable names corresponding to external modules
crypto (guessing 'crypto')
created ./build/app.js in 791ms
✨  Done in 4.61s.

src/main.js

import { hash } from 'crypto';
hash.digest('hex');

rollup.config.js

import babel from 'rollup-plugin-babel'; 
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';

export default {
  input: 'src/main.js',
  output: {
    file: './build/app.js',
    format: 'iife'
  },
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
    babel({
      exclude: 'node_modules/**'
    })
  ]
};

package.json

{
  "name": "rollup",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "lodash-es": "^4.17.4",
    "rollup": "^0.50.0",
    "rollup-plugin-babel": "^3.0.2",
    "rollup-plugin-commonjs": "^8.2.5",
    "rollup-plugin-node-resolve": "^3.0.0"
  },
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w"
  }
}

奇怪的是,如果我尝试从 lodash-es 导入 template,我没有收到任何错误:

import template from 'lodash-es';
console.log('test');

我可以只安装用于浏览器的软件包吗?

使用节点模块时必须使用 rollup-plugin-node-builtins。我将以下内容添加到我的 rollup.config.js 文件中:

import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import builtins from 'rollup-plugin-node-builtins';

export default {
  input: 'src/main.js',
  output: {
    file: './build/app.js',
    format: 'iife'
  },
  plugins: [
    builtins(),
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
    babel({
      exclude: 'node_modules/**'
    })
  ]
};