I'm trying to write a cloudflare worker following the documentation and use Webpack to generate one single file to upload.
After running the script as in the docs:
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"
I'm getting the following error:
{
"result": null,
"success": false,
"errors": [
{
"code": 10021,
"message": "Uncaught ReferenceError: exports is not defined\n at line 1\n"
}
],
"messages": []
}
My webpack config:
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',
},
};
I cannot find documentation or example about creating workers with webpack but I don't think this is a new thing :)
Thanks
libraryTarget: 'commonjs'is the problem here. I think you may want to trylibraryTarget: "this"instead? - Kenton Varda