From 97cb5b13aac58924bb3967279667cf23a5655217 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Mon, 18 Mar 2024 11:43:33 +0000 Subject: [PATCH] refactor: use `esbuild-plugins-node-modules-polyfill` (#5209) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michaƫl De Boey --- .changeset/hungry-ladybugs-smash.md | 27 +++++++ packages/wrangler/package.json | 3 +- packages/wrangler/scripts/deps.ts | 3 +- .../wrangler/src/deployment-bundle/bundle.ts | 16 +++- pnpm-lock.yaml | 76 ++++--------------- templates/worker-aws/package.json | 2 +- templates/worker-aws/worktop.config.js | 4 +- 7 files changed, 60 insertions(+), 71 deletions(-) create mode 100644 .changeset/hungry-ladybugs-smash.md diff --git a/.changeset/hungry-ladybugs-smash.md b/.changeset/hungry-ladybugs-smash.md new file mode 100644 index 000000000000..4aa055d01a47 --- /dev/null +++ b/.changeset/hungry-ladybugs-smash.md @@ -0,0 +1,27 @@ +--- +"wrangler": major +--- + +refactor: use `esbuild-plugins-node-modules-polyfill` + +Replaces `@esbuild-plugins/node-globals-polyfill` & `@esbuild-plugins/node-modules-polyfill` with the up-to-date & maintained `esbuild-plugins-node-modules-polyfill` + +The `esbuild-plugins` repository actually points towards using `esbuild-plugin-polyfill-node` instead +https://github.com/remorses/esbuild-plugins/blob/373b44902ad3e669f7359c857de09a930ce1ce90/README.md?plain=1#L15-L16 + +But the Remix repo (see https://github.com/remix-run/remix/pull/5274) tried this and found some regressions. +So they chose to go for @imranbarbhuiya's `esbuild-plugins-node-modules-polyfill` instead (see https://github.com/remix-run/remix/pull/6562), which is an up-to-date and well maintained alternative. + +Users should no longer see the following deprecation warnings when installing Wrangler: + +```sh +npm WARN deprecated rollup-plugin-inject@3.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject. +npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead +``` + +Resolves https://github.com/cloudflare/workers-sdk/issues/1232 + +**Possible Breaking Change:** +Since we are swapping out the entire polyfill library for a new one, there is a chance that projects using `node_compat` will experience regressions when trying to deploy. + +If you have such a Worker, ensure that you test it carefully before deploying when migrating from Wrangler v3 to Wrangler v4. diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 7f5f10f1ec5a..578756ca48b3 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -102,11 +102,10 @@ }, "dependencies": { "@cloudflare/kv-asset-handler": "workspace:*", - "@esbuild-plugins/node-globals-polyfill": "^0.2.3", - "@esbuild-plugins/node-modules-polyfill": "^0.2.2", "blake3-wasm": "^2.1.5", "chokidar": "^3.5.3", "esbuild": "0.18.20", + "esbuild-plugins-node-modules-polyfill": "^1.6.3", "miniflare": "workspace:*", "nanoid": "^3.3.3", "path-to-regexp": "^6.2.0", diff --git a/packages/wrangler/scripts/deps.ts b/packages/wrangler/scripts/deps.ts index 22187e1754bd..1ebc6aff7f82 100644 --- a/packages/wrangler/scripts/deps.ts +++ b/packages/wrangler/scripts/deps.ts @@ -12,8 +12,7 @@ export const EXTERNAL_DEPENDENCIES = [ // todo - bundle miniflare too "selfsigned", "source-map", - "@esbuild-plugins/node-globals-polyfill", - "@esbuild-plugins/node-modules-polyfill", + "esbuild-plugins-node-modules-polyfill", "chokidar", // @cloudflare/workers-types is an optional peer dependency of wrangler, so users can // get the types by installing the package (to what version they prefer) themselves diff --git a/packages/wrangler/src/deployment-bundle/bundle.ts b/packages/wrangler/src/deployment-bundle/bundle.ts index 73aa37d15f13..0edfc3a4c5ec 100644 --- a/packages/wrangler/src/deployment-bundle/bundle.ts +++ b/packages/wrangler/src/deployment-bundle/bundle.ts @@ -1,8 +1,8 @@ import * as fs from "node:fs"; +import { builtinModules } from "node:module"; import * as path from "node:path"; -import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill"; -import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill"; import * as esbuild from "esbuild"; +import { nodeModulesPolyfillPlugin } from "esbuild-plugins-node-modules-polyfill"; import { UserError } from "../errors"; import { getBasePath, getWranglerTmpDir } from "../paths"; import { applyMiddlewareLoaderFacade } from "./apply-middleware"; @@ -34,6 +34,11 @@ export const COMMON_ESBUILD_OPTIONS = { // build conditions used by esbuild, and when resolving custom `import` calls export const BUILD_CONDITIONS = ["workerd", "worker", "browser"]; +const modulesToPolyfill: Record = { + ...Object.fromEntries(builtinModules.map((m) => [m, true])), + net: "empty", +}; + /** * Information about Wrangler's bundling process that needs passed through * for DevTools sourcemap transformation @@ -348,7 +353,12 @@ export async function bundleWorker( plugins: [ moduleCollector.plugin, ...(legacyNodeCompat - ? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()] + ? [ + nodeModulesPolyfillPlugin({ + globals: { Buffer: true, process: true }, + modules: modulesToPolyfill, + }), + ] : []), nodejsCompatPlugin(!!nodejsCompat), cloudflareInternalPlugin, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a02322d625a8..4680aab948ae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1446,12 +1446,6 @@ importers: '@cloudflare/kv-asset-handler': specifier: workspace:* version: link:../kv-asset-handler - '@esbuild-plugins/node-globals-polyfill': - specifier: ^0.2.3 - version: 0.2.3(esbuild@0.18.20) - '@esbuild-plugins/node-modules-polyfill': - specifier: ^0.2.2 - version: 0.2.2(esbuild@0.18.20) blake3-wasm: specifier: ^2.1.5 version: 2.1.5 @@ -1461,6 +1455,9 @@ importers: esbuild: specifier: 0.18.20 version: 0.18.20 + esbuild-plugins-node-modules-polyfill: + specifier: ^1.6.3 + version: 1.6.3(esbuild@0.18.20) miniflare: specifier: workspace:* version: link:../miniflare @@ -4196,24 +4193,6 @@ packages: get-tsconfig: 4.7.0 dev: true - /@esbuild-plugins/node-globals-polyfill@0.2.3(esbuild@0.18.20): - resolution: {integrity: sha512-r3MIryXDeXDOZh7ih1l/yE9ZLORCd5e8vWg02azWRGj5SPTuoh69A2AIyn0Z31V/kHBfZ4HgWJ+OK3GTTwLmnw==} - peerDependencies: - esbuild: '*' - dependencies: - esbuild: 0.18.20 - dev: false - - /@esbuild-plugins/node-modules-polyfill@0.2.2(esbuild@0.18.20): - resolution: {integrity: sha512-LXV7QsWJxRuMYvKbiznh+U1ilIop3g2TeKRzUxOG5X3YITc8JyyTa90BmLwqqv0YnX4v32CSlG+vsziZp9dMvA==} - peerDependencies: - esbuild: '*' - dependencies: - esbuild: 0.18.20 - escape-string-regexp: 4.0.0 - rollup-plugin-node-polyfills: 0.2.1 - dev: false - /@esbuild/aix-ppc64@0.19.12: resolution: {integrity: sha512-bmoCYyWdEL3wDQIVbcyzRyeKLgk2WtWLTWz1ZIAZF/EGbNOwSA6ew3PftJ1PqMiOOGu0OyFMzG53L0zqIpPeNA==} engines: {node: '>=12'} @@ -5674,7 +5653,6 @@ packages: /@jspm/core@2.0.1: resolution: {integrity: sha512-Lg3PnLp0QXpxwLIAuuJboLeRaIhrgJjeuh797QADg3xz8wGLugQOS5DpsE8A6i6Adgzf+bacllkKZG3J0tGfDw==} - dev: true /@manypkg/find-root@1.1.0: resolution: {integrity: sha512-mki5uBvhHzO8kYYix/WRy2WX8S3B5wdVSc9D6KcU5lQNglP2yt58/VfLuAK49glRXChosY8ap2oJ1qgma3GUVA==} @@ -10978,6 +10956,18 @@ packages: import-meta-resolve: 2.2.2 dev: true + /esbuild-plugins-node-modules-polyfill@1.6.3(esbuild@0.18.20): + resolution: {integrity: sha512-nydQGT3RijD8mBd3Hek+2gSAxndgceZU9GIjYYiqU+7CE7veN8utTmupf0frcKpwIXCXWpRofL9CY9k0yU70CA==} + engines: {node: '>=14.0.0'} + peerDependencies: + esbuild: ^0.14.0 || ^0.15.0 || ^0.16.0 || ^0.17.0 || ^0.18.0 || ^0.19.0 || ^0.20.0 + dependencies: + '@jspm/core': 2.0.1 + esbuild: 0.18.20 + local-pkg: 0.5.0 + resolve.exports: 2.0.2 + dev: false + /esbuild-register@3.4.2(esbuild@0.16.3): resolution: {integrity: sha512-kG/XyTDyz6+YDuyfB9ZoSIOOmgyFCH+xPRtsCa8W85HLRV5Csp+o3jWVbOSHgSLfyLc5DmP+KFDNwty4mEjC+Q==} peerDependencies: @@ -11939,10 +11929,6 @@ packages: '@types/unist': 2.0.6 dev: true - /estree-walker@0.6.1: - resolution: {integrity: sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==} - dev: false - /estree-walker@2.0.2: resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==} @@ -15111,12 +15097,6 @@ packages: resolution: {integrity: sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==} hasBin: true - /magic-string@0.25.9: - resolution: {integrity: sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==} - dependencies: - sourcemap-codec: 1.4.8 - dev: false - /magic-string@0.30.3: resolution: {integrity: sha512-B7xGbll2fG/VjP+SWg4sX3JynwIU0mjoTc6MPpKNuIvftk6u6vqhDnk1R80b8C2GBR6ywqy+1DcKBrevBg+bmw==} engines: {node: '>=12'} @@ -18089,27 +18069,6 @@ packages: semver-compare: 1.0.0 dev: true - /rollup-plugin-inject@3.0.2: - resolution: {integrity: sha512-ptg9PQwzs3orn4jkgXJ74bfs5vYz1NCZlSQMBUA0wKcGp5i5pA1AO3fOUEte8enhGUC+iapTCzEWw2jEFFUO/w==} - deprecated: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject. - dependencies: - estree-walker: 0.6.1 - magic-string: 0.25.9 - rollup-pluginutils: 2.8.2 - dev: false - - /rollup-plugin-node-polyfills@0.2.1: - resolution: {integrity: sha512-4kCrKPTJ6sK4/gLL/U5QzVT8cxJcofO0OU74tnB19F40cmuAKSzH5/siithxlofFEjwvw1YAhPmbvGNA6jEroA==} - dependencies: - rollup-plugin-inject: 3.0.2 - dev: false - - /rollup-pluginutils@2.8.2: - resolution: {integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==} - dependencies: - estree-walker: 0.6.1 - dev: false - /rollup@3.25.1: resolution: {integrity: sha512-tywOR+rwIt5m2ZAWSe5AIJcTat8vGlnPFAv15ycCrw33t6iFsXZ6mzHVFh2psSjxQPmI+xgzMZZizUAukBI4aQ==} engines: {node: '>=14.18.0', npm: '>=8.0.0'} @@ -18663,11 +18622,6 @@ packages: resolution: {integrity: sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==} engines: {node: '>= 8'} - /sourcemap-codec@1.4.8: - resolution: {integrity: sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==} - deprecated: Please use @jridgewell/sourcemap-codec instead - dev: false - /space-separated-tokens@2.0.2: resolution: {integrity: sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q==} dev: true diff --git a/templates/worker-aws/package.json b/templates/worker-aws/package.json index d74dbd9d2cbb..f94d17127b9b 100644 --- a/templates/worker-aws/package.json +++ b/templates/worker-aws/package.json @@ -15,7 +15,7 @@ "@aws-sdk/client-sqs": "^3.82.0" }, "devDependencies": { - "@esbuild-plugins/node-modules-polyfill": "0.1.4", + "esbuild-plugins-node-modules-polyfill": "^1.6.3", "worktop.build": "0.0.5", "wrangler": "^3.0.0" } diff --git a/templates/worker-aws/worktop.config.js b/templates/worker-aws/worktop.config.js index 429b36ee3c7a..7afe6a818c03 100644 --- a/templates/worker-aws/worktop.config.js +++ b/templates/worker-aws/worktop.config.js @@ -1,10 +1,10 @@ import { define } from 'worktop.build'; -import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'; +import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill'; // @ts-ignore export default define({ modify(config) { config.plugins = config.plugins || []; - config.plugins.push(NodeModulesPolyfillPlugin()); + config.plugins.push(nodeModulesPolyfillPlugin()); }, });