-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-lambda-nodejs): "define" bundling option is not escaped #13842
Comments
I don't remember encountering this warning before, but maybe I overlooked. I certainly reproduce the problem and the types aren't currently correct when they are replaced by esbuild. Here's some insights: // construct.ts
new lambdaNodejs.NodejsFunction(this, "Function", {
entry: "function.ts",
bundling: {
define: {
"process.env.VAR_1": JSON.stringify("production"),
"process.env.VAR_2": "true",
"process.env.VAR_3": JSON.stringify('prod"u"ction'), // This one just crashes right now
"process.env.VAR_4": "123",
},
},
});
// function.ts
process.env.VAR_1;
process.env.VAR_2;
process.env.VAR_3;
process.env.VAR_4;
// function.ts (expected replacements by esbuild)
"production";
true;
'prod"u"ction';
123;
// function.ts (current incorrect replacements)
production;
true;
// process.env.VAR_3 crashes the CLI
123; The solution seems to be to additionally JSON-stringify all of the values before sending them to esbuild via CLI. The usage of the construct is still exactly the same, but the types passed will be correct. I will happily prepare a PR. |
Would love to use the esbuild JS API but not sure if that's feasible without refactoring quite a lot. EDIT: OK, esbuild is not supposed to work with multiple identifier replacements. You can replace either with a single literal (either bool, string, number...) or with an identifier. |
The PR is ready for review :) |
|
"define" bundling option appears to be insufficiently escaped
Reproduction Steps
What did you expect to happen?
Expected would be an option
--define:process.env.NODE_ENV=\"production\"
passed to esbuildWhat actually happened?
esbuild emits a warning:
suggesting that the quotes, added by
JSON.stringify
were removed, I assume, by the shell executing the esbuild commandEnvironment
Other
Manually escaping all double quotes with
JSON.stringify('production').replace(/"/g, '\\"')
is a workaround I use for nowI also expect that it would be easier to bundle the esbuild's js-api, since then there is less dealing with string escapes. (https://esbuild.github.io/getting-started/#build-scripts)
This is 🐛 Bug Report
The text was updated successfully, but these errors were encountered: