上传 Cloudflare Worker 时出现错误 10021

mar*_*ler 5 cloudflare webpack cloudflare-workers

我正在尝试按照文档编写一个 cloudflare Worker 并使用 Webpack 生成一个要上传的文件。

按照文档中的方式运行脚本后:

curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONE_ID/workers/script" -H "Content-Type:application/javascript" -H "X-Auth-Email:CLOUDFLARE_EMAIL" -H "X-Auth-Key:CLOUDFLARE_KEY" --data-binary "@dist/index.js"
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

{
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 10021,
      "message": "Uncaught ReferenceError: exports is not defined\n  at line 1\n"
    }
  ],
  "messages": []
}
Run Code Online (Sandbox Code Playgroud)

我的网络包配置:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: ['./src/index.js'],
  target: 'web',
  module: {
    rules: [{
      test: /\.js$/,
      loader: 'babel-loader',
      exclude: /node_modules/,
    }],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, 'dist'),
    filename: 'index.js',
  },
};
Run Code Online (Sandbox Code Playgroud)

我找不到有关使用 webpack 创建工人的文档或示例,但我不认为这是新事物:)

谢谢

Ken*_*rda 0

这里的问题是libraryTarget: 'commonjs'。这对工人来说是不正确的。您应该使用libraryTarget: "this"它,或者libraryTarget完全省略。

我们最近发布了一篇关于如何将 Webpack 与 Cloudflare Workers 结合使用的博客文章:https://blog.cloudflare.com/using-webpack-to-bundle-workers/