Skip to content

Commit

Permalink
[Wrangler] Build target ES2022 (#2720)
Browse files Browse the repository at this point in the history
* upgrade the target for JavaScript to ES2022 which is supported in the Worker runtime

* Include the ES2022 change to entry ESBuild config & included ES2022 to the publish testing

* changed the test to handle ES2022 with top level await which will not build in ESBuild before ES2022
  • Loading branch information
JacobMGEvans committed Feb 17, 2023
1 parent 0e10605 commit de0cb57
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .changeset/perfect-knives-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": patch
---

Fix: Upgraded to ES2022 for improved compatibility
Upgraded worker code target version from ES2020 to ES2022 for better compatibility and unblocking of a workaround related to [issue #2029](https://github.com/cloudflare/workers-sdk/issues/2029). The worker runtime now uses the same V8 version as recent Chrome and is 99% ES2016+ compliant. The only thing we don't support on the Workers runtime, the remaining 1%, is the ES2022 RegEx feature as seen in the compat table for the latest Chrome version.

Compatibility table: https://kangax.github.io/compat-table/es2016plus/

[resolves #2716](https://github.com/cloudflare/workers-sdk/issues/2716)
25 changes: 22 additions & 3 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6489,9 +6489,22 @@ addEventListener('fetch', event => {});`
`);
});

it("should output to target es2020 even if tsconfig says otherwise", async () => {
it.only("should output to target es2022 even if tsconfig says otherwise", async () => {
writeWranglerToml();
writeWorkerSource();
fs.writeFileSync(
"./index.js",
`
import { foo } from "./another";
const topLevelAwait = await new Promise((resolve) => setTimeout(resolve, 0));
export default {
async fetch(request) {
return new Response("Hello world!");
},
};`
);
fs.writeFileSync(
"tsconfig.json",
JSON.stringify({
Expand All @@ -6502,8 +6515,14 @@ addEventListener('fetch', event => {});`
})
);
mockSubDomainRequest();
/**
* When we compile with es2022, we should preserve the export statement and top level await
* If you attempt to target es2020 top level await will cause a build error
* @error Build failed with 1 error:
* index.js:3:25: ERROR: Top-level await is not available in the configured target environment ("es2020")
*/
mockUploadWorkerRequest({
expectedEntry: "export {", // just check that the export is preserved
expectedEntry: "export {", // check that the export is preserved
});
await runWrangler("publish index.js"); // this would throw if we tried to compile with es5
expect(std).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -7448,7 +7467,7 @@ function mockLastDeploymentRequest() {
function mockUploadWorkerRequest(
options: {
available_on_subdomain?: boolean;
expectedEntry?: string;
expectedEntry?: string | RegExp;
expectedMainModule?: string;
expectedType?: "esm" | "sw";
expectedBindings?: unknown;
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ export async function bundleWorker(
inject,
external: ["__STATIC_CONTENT_MANIFEST"],
format: entry.format === "modules" ? "esm" : "iife",
target: "es2020",
// Our workerd runtime uses the same V8 version as recent Chrome, which is highly ES2022 compliant: https://kangax.github.io/compat-table/es2016plus/
target: "es2022",
sourcemap: sourcemap ?? true, // this needs to use ?? to accept false
// Include a reference to the output folder in the sourcemap.
// This is omitted by default, but we need it to properly resolve source paths in error output.
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default async function guessWorkerFormat(
metafile: true,
bundle: false,
format: "esm",
target: "es2020",
target: "es2022",
write: false,
loader: {
".js": "jsx",
Expand Down

0 comments on commit de0cb57

Please sign in to comment.