【问题标题】:rollup js to include NodeJS core library汇总 js 以包含 NodeJS 核心库
【发布时间】:2022-06-16 21:51:42
【问题描述】:

我在 akamai edgeworker 上部署了一些东西,因此使用了节点包汇总。我使用的其中一个包使用了require('crypto'),这给我带来了问题,因为我似乎无法让汇总包含代码。我能够解决并得到它,所以我没有顶部的导入,但现在得到TypeError: crypto.randomFillSync is not a function。只是想知道如何将需要加密的东西部署到 edgeworker 上。 我当前的 rollup.config 文件是:

banner.js

var window = {};
var TextDecoder = function() {};
var setTimeout = function(callback) { callback(); };

rollup.config.js

import * as fs from "fs";
import resolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import commonjs from "@rollup/plugin-commonjs";
import nodePolyfills from 'rollup-plugin-node-polyfills';
import { babel } from '@rollup/plugin-babel';


const BANNER = fs.readFileSync("./src/banner.js").toString();

function getPlugins(babelConfig) {
  return [
    // Convert CommonJS modules to ES6
    commonjs(),
    nodePolyfills(),
    // Resolve modules from node_modules
    resolve({
      // use the "browser" property in package.json
      browser: true,
      preferBuiltins: false
    }),
    babel(babelConfig),
    // Package json data as an ES6 module
    json()
  ];
}

export default [
  {
    input: "src/index.js",
    output: {
      banner: BANNER,
      name: "main",
      file: "dist/main.js",
      format: "esm",
      sourcemap: false
    },
    external: ['cookies', 'http-request', 'log', 'create-response'],
    plugins: getPlugins(
      {
        inputSourceMap: true,
        sourceMaps: true,
        exclude: ["node_modules/**", /\/core-js\//],

        presets: [
          [
            "@babel/preset-env",
            {
              useBuiltIns: "usage",
              corejs: 3,
              modules: false,
              targets: {
                browsers: [
                  "last 2 Chrome versions",
                  "last 2 Firefox versions",
                  "last 2 Safari versions"
                ]
              }
            }
          ]
        ],
        plugins: []
      }
    )
  }
];

【问题讨论】:

  • 您可能想更新您的问题,即为什么首先需要捆绑cryptocrypto 是一个核心 Node.js 模块,它不应该包含在包中,除非这里没有提到另一个问题。

标签: javascript node.js rollupjs akamai


【解决方案1】:

crypto 模块依赖于 Node.js 或浏览器环境,因为它通过包装底层平台 API 来提供实现。 Akamai EdgeWorkers 尚未提供本机加密功能,因此您需要使用提供纯 JS 实现的 JS 库。

只要您注意 CPU 限制,我已经成功使用 crypto-es (https://www.npmjs.com/package/crypto-es)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2019-06-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多