From bd77751f117dfef0c7e43242406d360bec68567d Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 3 Dec 2024 14:57:35 +0000 Subject: [PATCH 1/2] use the new `resolve.builtins` vite config to specify the cloudflare and node builtins and remove the ad-hoc `cloudflare:` handling logic --- .../src/cloudflare-environment.ts | 8 +- packages/vite-plugin-cloudflare/src/index.ts | 45 +++ .../src/miniflare-options.ts | 11 - pnpm-lock.yaml | 334 +----------------- pnpm-workspace.yaml | 2 +- 5 files changed, 67 insertions(+), 333 deletions(-) diff --git a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts index c4d93fc2..78c2d115 100644 --- a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts +++ b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts @@ -133,6 +133,7 @@ export function createCloudflareEnvironmentOptions( noExternal: true, // We want to use `workerd` package exports if available (e.g. for postgres). conditions: ['workerd', 'module', 'browser', 'development|production'], + builtins: [...cloudflareBuiltInModules], }, dev: { createEnvironment(name, config) { @@ -152,17 +153,12 @@ export function createCloudflareEnvironmentOptions( // dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set // optimizeDeps.entries in the dev config) input: workerConfig.main, - external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()], + external: [...getNodeCompatExternals()], }, }, optimizeDeps: { // Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false noDiscovery: false, - exclude: [ - ...cloudflareBuiltInModules, - // we have to exclude all node modules to work in dev-mode not just the unenv externals... - ...builtinModules.concat(builtinModules.map((m) => `node:${m}`)), - ], esbuildOptions: { platform: 'neutral', resolveExtensions: [ diff --git a/packages/vite-plugin-cloudflare/src/index.ts b/packages/vite-plugin-cloudflare/src/index.ts index af7ff84e..976810a7 100644 --- a/packages/vite-plugin-cloudflare/src/index.ts +++ b/packages/vite-plugin-cloudflare/src/index.ts @@ -1,9 +1,11 @@ import assert from 'node:assert'; import * as fs from 'node:fs'; +import { builtinModules } from 'node:module'; import path from 'node:path'; import { createMiddleware } from '@hattip/adapter-node'; import { Miniflare } from 'miniflare'; import * as vite from 'vite'; +import { unstable_getMiniflareWorkerOptions } from 'wrangler'; import { getRouterWorker } from './assets'; import { createCloudflareEnvironmentOptions, @@ -16,6 +18,7 @@ import { import { getNodeCompatAliases, injectGlobalCode, + isNodeCompat, resolveNodeAliases, } from './node-js-compat'; import { resolvePluginConfig } from './plugin-config'; @@ -92,6 +95,9 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin { }, }; }, + configResolved(config) { + addNodeBuiltinsToWorkerEnvironmentsIfNeeded(config, resolvedPluginConfig); + }, configEnvironment(name, options) { if (resolvedPluginConfig.type === 'workers') { options.build = { @@ -257,3 +263,42 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin { }, }; } + +/** + * In the resolved configuration adds to all the worker environments the necessary node builtin config options + * if the worker is under nodejs compat + * + * @param resolvedConfig the vite resolved config (that will be side-effectfully updated) + * @param resolvedPluginConfig the resolved plugin config + */ +function addNodeBuiltinsToWorkerEnvironmentsIfNeeded( + resolvedConfig: vite.ResolvedConfig, + resolvedPluginConfig: ResolvedPluginConfig, +) { + if (resolvedPluginConfig.type === 'workers') { + Object.entries(resolvedPluginConfig.workers).map( + ([environmentName, workerConfig]) => { + invariant( + resolvedConfig.environments[environmentName], + `environment with name "${environmentName}" not found`, + ); + + const nodeCompat = isNodeCompat(workerConfig); + + const nodeCompatModules = nodeCompat + ? [...builtinModules.concat(builtinModules.map((m) => `node:${m}`))] + : []; + + resolvedConfig.environments[environmentName].resolve.builtins = [ + ...resolvedConfig.environments[environmentName].resolve.builtins, + ...nodeCompatModules, + ]; + resolvedConfig.environments[environmentName].optimizeDeps.exclude = [ + ...(resolvedConfig.environments[environmentName].optimizeDeps + .exclude ?? []), + ...nodeCompatModules, + ]; + }, + ); + } +} diff --git a/packages/vite-plugin-cloudflare/src/miniflare-options.ts b/packages/vite-plugin-cloudflare/src/miniflare-options.ts index 75082b68..6c25dc90 100644 --- a/packages/vite-plugin-cloudflare/src/miniflare-options.ts +++ b/packages/vite-plugin-cloudflare/src/miniflare-options.ts @@ -290,17 +290,6 @@ export function getDevMiniflareOptions( const [moduleId] = invokePayloadData.data; - // For some reason we need this here for cloudflare built-ins (e.g. `cloudflare:workers`) but not for node built-ins (e.g. `node:path`) - // See https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/46 - if (moduleId.startsWith('cloudflare:')) { - const result = { - externalize: moduleId, - type: 'builtin', - } satisfies vite.FetchResult; - - return new MiniflareResponse(JSON.stringify({ result })); - } - // Sometimes Vite fails to resolve built-ins and converts them to "url-friendly" ids // that start with `/@id/...`. if (moduleId.startsWith('/@id/')) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f7ea1670..5bc59896 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19,8 +19,8 @@ catalogs: specifier: npm:unenv-nightly@2.0.0-20241204-140205-a5d5190 version: 2.0.0-20241204-140205-a5d5190 vite: - specifier: 6.0.3 - version: 6.0.3 + specifier: /Users/dario/Repos/my-repos/vite/packages/vite + version: 6.0.1 wrangler: specifier: ^3.93.0 version: 3.93.0 @@ -56,7 +56,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../my-repos/vite/packages/vite vitest: specifier: ^2.1.3 version: 2.1.3(@types/node@22.10.1) @@ -98,7 +98,7 @@ importers: version: unenv-nightly@2.0.0-20241204-140205-a5d5190 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -128,7 +128,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -149,7 +149,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -170,7 +170,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -212,7 +212,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -233,7 +233,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -272,7 +272,7 @@ importers: version: unenv-nightly@2.0.0-20241204-140205-a5d5190 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -303,13 +303,13 @@ importers: version: link:../../packages/typescript-config '@vitejs/plugin-react': specifier: ^4.3.4 - version: 4.3.4(vite@6.0.3(@types/node@22.10.1)) + version: 4.3.4(vite@my-repos+vite+packages+vite) typescript: specifier: catalog:default version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -340,13 +340,13 @@ importers: version: link:../../packages/typescript-config '@vitejs/plugin-react': specifier: ^4.3.4 - version: 4.3.4(vite@6.0.3(@types/node@22.10.1)) + version: 4.3.4(vite@my-repos+vite+packages+vite) typescript: specifier: catalog:default version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -367,7 +367,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -388,7 +388,7 @@ importers: version: 5.7.2 vite: specifier: catalog:default - version: 6.0.3(@types/node@22.10.1) + version: link:../../../my-repos/vite/packages/vite wrangler: specifier: catalog:default version: 3.93.0(@cloudflare/workers-types@4.20241205.0) @@ -623,12 +623,6 @@ packages: cpu: [ppc64] os: [aix] - '@esbuild/aix-ppc64@0.24.0': - resolution: {integrity: sha512-WtKdFM7ls47zkKHFVzMz8opM7LkcsIp9amDUBIAWirg70RM71WRSjdILPsY5Uv1D42ZpUfaPILDlfactHgsRkw==} - engines: {node: '>=18'} - cpu: [ppc64] - os: [aix] - '@esbuild/android-arm64@0.17.19': resolution: {integrity: sha512-KBMWvEZooR7+kzY0BtbTQn0OAYY7CsiydT63pVEaPtVYF0hXbUaOyZog37DKxK7NF3XacBJOpYT4adIJh+avxA==} engines: {node: '>=12'} @@ -647,12 +641,6 @@ packages: cpu: [arm64] os: [android] - '@esbuild/android-arm64@0.24.0': - resolution: {integrity: sha512-Vsm497xFM7tTIPYK9bNTYJyF/lsP590Qc1WxJdlB6ljCbdZKU9SY8i7+Iin4kyhV/KV5J2rOKsBQbB77Ab7L/w==} - engines: {node: '>=18'} - cpu: [arm64] - os: [android] - '@esbuild/android-arm@0.17.19': resolution: {integrity: sha512-rIKddzqhmav7MSmoFCmDIb6e2W57geRsM94gV2l38fzhXMwq7hZoClug9USI2pFRGL06f4IOPHHpFNOkWieR8A==} engines: {node: '>=12'} @@ -671,12 +659,6 @@ packages: cpu: [arm] os: [android] - '@esbuild/android-arm@0.24.0': - resolution: {integrity: sha512-arAtTPo76fJ/ICkXWetLCc9EwEHKaeya4vMrReVlEIUCAUncH7M4bhMQ+M9Vf+FFOZJdTNMXNBrWwW+OXWpSew==} - engines: {node: '>=18'} - cpu: [arm] - os: [android] - '@esbuild/android-x64@0.17.19': resolution: {integrity: sha512-uUTTc4xGNDT7YSArp/zbtmbhO0uEEK9/ETW29Wk1thYUJBz3IVnvgEiEwEa9IeLyvnpKrWK64Utw2bgUmDveww==} engines: {node: '>=12'} @@ -695,12 +677,6 @@ packages: cpu: [x64] os: [android] - '@esbuild/android-x64@0.24.0': - resolution: {integrity: sha512-t8GrvnFkiIY7pa7mMgJd7p8p8qqYIz1NYiAoKc75Zyv73L3DZW++oYMSHPRarcotTKuSs6m3hTOa5CKHaS02TQ==} - engines: {node: '>=18'} - cpu: [x64] - os: [android] - '@esbuild/darwin-arm64@0.17.19': resolution: {integrity: sha512-80wEoCfF/hFKM6WE1FyBHc9SfUblloAWx6FJkFWTWiCoht9Mc0ARGEM47e67W9rI09YoUxJL68WHfDRYEAvOhg==} engines: {node: '>=12'} @@ -719,12 +695,6 @@ packages: cpu: [arm64] os: [darwin] - '@esbuild/darwin-arm64@0.24.0': - resolution: {integrity: sha512-CKyDpRbK1hXwv79soeTJNHb5EiG6ct3efd/FTPdzOWdbZZfGhpbcqIpiD0+vwmpu0wTIL97ZRPZu8vUt46nBSw==} - engines: {node: '>=18'} - cpu: [arm64] - os: [darwin] - '@esbuild/darwin-x64@0.17.19': resolution: {integrity: sha512-IJM4JJsLhRYr9xdtLytPLSH9k/oxR3boaUIYiHkAawtwNOXKE8KoU8tMvryogdcT8AU+Bflmh81Xn6Q0vTZbQw==} engines: {node: '>=12'} @@ -743,12 +713,6 @@ packages: cpu: [x64] os: [darwin] - '@esbuild/darwin-x64@0.24.0': - resolution: {integrity: sha512-rgtz6flkVkh58od4PwTRqxbKH9cOjaXCMZgWD905JOzjFKW+7EiUObfd/Kav+A6Gyud6WZk9w+xu6QLytdi2OA==} - engines: {node: '>=18'} - cpu: [x64] - os: [darwin] - '@esbuild/freebsd-arm64@0.17.19': resolution: {integrity: sha512-pBwbc7DufluUeGdjSU5Si+P3SoMF5DQ/F/UmTSb8HXO80ZEAJmrykPyzo1IfNbAoaqw48YRpv8shwd1NoI0jcQ==} engines: {node: '>=12'} @@ -767,12 +731,6 @@ packages: cpu: [arm64] os: [freebsd] - '@esbuild/freebsd-arm64@0.24.0': - resolution: {integrity: sha512-6Mtdq5nHggwfDNLAHkPlyLBpE5L6hwsuXZX8XNmHno9JuL2+bg2BX5tRkwjyfn6sKbxZTq68suOjgWqCicvPXA==} - engines: {node: '>=18'} - cpu: [arm64] - os: [freebsd] - '@esbuild/freebsd-x64@0.17.19': resolution: {integrity: sha512-4lu+n8Wk0XlajEhbEffdy2xy53dpR06SlzvhGByyg36qJw6Kpfk7cp45DR/62aPH9mtJRmIyrXAS5UWBrJT6TQ==} engines: {node: '>=12'} @@ -791,12 +749,6 @@ packages: cpu: [x64] os: [freebsd] - '@esbuild/freebsd-x64@0.24.0': - resolution: {integrity: sha512-D3H+xh3/zphoX8ck4S2RxKR6gHlHDXXzOf6f/9dbFt/NRBDIE33+cVa49Kil4WUjxMGW0ZIYBYtaGCa2+OsQwQ==} - engines: {node: '>=18'} - cpu: [x64] - os: [freebsd] - '@esbuild/linux-arm64@0.17.19': resolution: {integrity: sha512-ct1Tg3WGwd3P+oZYqic+YZF4snNl2bsnMKRkb3ozHmnM0dGWuxcPTTntAF6bOP0Sp4x0PjSF+4uHQ1xvxfRKqg==} engines: {node: '>=12'} @@ -815,12 +767,6 @@ packages: cpu: [arm64] os: [linux] - '@esbuild/linux-arm64@0.24.0': - resolution: {integrity: sha512-TDijPXTOeE3eaMkRYpcy3LarIg13dS9wWHRdwYRnzlwlA370rNdZqbcp0WTyyV/k2zSxfko52+C7jU5F9Tfj1g==} - engines: {node: '>=18'} - cpu: [arm64] - os: [linux] - '@esbuild/linux-arm@0.17.19': resolution: {integrity: sha512-cdmT3KxjlOQ/gZ2cjfrQOtmhG4HJs6hhvm3mWSRDPtZ/lP5oe8FWceS10JaSJC13GBd4eH/haHnqf7hhGNLerA==} engines: {node: '>=12'} @@ -839,12 +785,6 @@ packages: cpu: [arm] os: [linux] - '@esbuild/linux-arm@0.24.0': - resolution: {integrity: sha512-gJKIi2IjRo5G6Glxb8d3DzYXlxdEj2NlkixPsqePSZMhLudqPhtZ4BUrpIuTjJYXxvF9njql+vRjB2oaC9XpBw==} - engines: {node: '>=18'} - cpu: [arm] - os: [linux] - '@esbuild/linux-ia32@0.17.19': resolution: {integrity: sha512-w4IRhSy1VbsNxHRQpeGCHEmibqdTUx61Vc38APcsRbuVgK0OPEnQ0YD39Brymn96mOx48Y2laBQGqgZ0j9w6SQ==} engines: {node: '>=12'} @@ -863,12 +803,6 @@ packages: cpu: [ia32] os: [linux] - '@esbuild/linux-ia32@0.24.0': - resolution: {integrity: sha512-K40ip1LAcA0byL05TbCQ4yJ4swvnbzHscRmUilrmP9Am7//0UjPreh4lpYzvThT2Quw66MhjG//20mrufm40mA==} - engines: {node: '>=18'} - cpu: [ia32] - os: [linux] - '@esbuild/linux-loong64@0.17.19': resolution: {integrity: sha512-2iAngUbBPMq439a+z//gE+9WBldoMp1s5GWsUSgqHLzLJ9WoZLZhpwWuym0u0u/4XmZ3gpHmzV84PonE+9IIdQ==} engines: {node: '>=12'} @@ -887,12 +821,6 @@ packages: cpu: [loong64] os: [linux] - '@esbuild/linux-loong64@0.24.0': - resolution: {integrity: sha512-0mswrYP/9ai+CU0BzBfPMZ8RVm3RGAN/lmOMgW4aFUSOQBjA31UP8Mr6DDhWSuMwj7jaWOT0p0WoZ6jeHhrD7g==} - engines: {node: '>=18'} - cpu: [loong64] - os: [linux] - '@esbuild/linux-mips64el@0.17.19': resolution: {integrity: sha512-LKJltc4LVdMKHsrFe4MGNPp0hqDFA1Wpt3jE1gEyM3nKUvOiO//9PheZZHfYRfYl6AwdTH4aTcXSqBerX0ml4A==} engines: {node: '>=12'} @@ -911,12 +839,6 @@ packages: cpu: [mips64el] os: [linux] - '@esbuild/linux-mips64el@0.24.0': - resolution: {integrity: sha512-hIKvXm0/3w/5+RDtCJeXqMZGkI2s4oMUGj3/jM0QzhgIASWrGO5/RlzAzm5nNh/awHE0A19h/CvHQe6FaBNrRA==} - engines: {node: '>=18'} - cpu: [mips64el] - os: [linux] - '@esbuild/linux-ppc64@0.17.19': resolution: {integrity: sha512-/c/DGybs95WXNS8y3Ti/ytqETiW7EU44MEKuCAcpPto3YjQbyK3IQVKfF6nbghD7EcLUGl0NbiL5Rt5DMhn5tg==} engines: {node: '>=12'} @@ -935,12 +857,6 @@ packages: cpu: [ppc64] os: [linux] - '@esbuild/linux-ppc64@0.24.0': - resolution: {integrity: sha512-HcZh5BNq0aC52UoocJxaKORfFODWXZxtBaaZNuN3PUX3MoDsChsZqopzi5UupRhPHSEHotoiptqikjN/B77mYQ==} - engines: {node: '>=18'} - cpu: [ppc64] - os: [linux] - '@esbuild/linux-riscv64@0.17.19': resolution: {integrity: sha512-FC3nUAWhvFoutlhAkgHf8f5HwFWUL6bYdvLc/TTuxKlvLi3+pPzdZiFKSWz/PF30TB1K19SuCxDTI5KcqASJqA==} engines: {node: '>=12'} @@ -959,12 +875,6 @@ packages: cpu: [riscv64] os: [linux] - '@esbuild/linux-riscv64@0.24.0': - resolution: {integrity: sha512-bEh7dMn/h3QxeR2KTy1DUszQjUrIHPZKyO6aN1X4BCnhfYhuQqedHaa5MxSQA/06j3GpiIlFGSsy1c7Gf9padw==} - engines: {node: '>=18'} - cpu: [riscv64] - os: [linux] - '@esbuild/linux-s390x@0.17.19': resolution: {integrity: sha512-IbFsFbxMWLuKEbH+7sTkKzL6NJmG2vRyy6K7JJo55w+8xDk7RElYn6xvXtDW8HCfoKBFK69f3pgBJSUSQPr+4Q==} engines: {node: '>=12'} @@ -983,12 +893,6 @@ packages: cpu: [s390x] os: [linux] - '@esbuild/linux-s390x@0.24.0': - resolution: {integrity: sha512-ZcQ6+qRkw1UcZGPyrCiHHkmBaj9SiCD8Oqd556HldP+QlpUIe2Wgn3ehQGVoPOvZvtHm8HPx+bH20c9pvbkX3g==} - engines: {node: '>=18'} - cpu: [s390x] - os: [linux] - '@esbuild/linux-x64@0.17.19': resolution: {integrity: sha512-68ngA9lg2H6zkZcyp22tsVt38mlhWde8l3eJLWkyLrp4HwMUr3c1s/M2t7+kHIhvMjglIBrFpncX1SzMckomGw==} engines: {node: '>=12'} @@ -1007,12 +911,6 @@ packages: cpu: [x64] os: [linux] - '@esbuild/linux-x64@0.24.0': - resolution: {integrity: sha512-vbutsFqQ+foy3wSSbmjBXXIJ6PL3scghJoM8zCL142cGaZKAdCZHyf+Bpu/MmX9zT9Q0zFBVKb36Ma5Fzfa8xA==} - engines: {node: '>=18'} - cpu: [x64] - os: [linux] - '@esbuild/netbsd-x64@0.17.19': resolution: {integrity: sha512-CwFq42rXCR8TYIjIfpXCbRX0rp1jo6cPIUPSaWwzbVI4aOfX96OXY8M6KNmtPcg7QjYeDmN+DD0Wp3LaBOLf4Q==} engines: {node: '>=12'} @@ -1031,24 +929,12 @@ packages: cpu: [x64] os: [netbsd] - '@esbuild/netbsd-x64@0.24.0': - resolution: {integrity: sha512-hjQ0R/ulkO8fCYFsG0FZoH+pWgTTDreqpqY7UnQntnaKv95uP5iW3+dChxnx7C3trQQU40S+OgWhUVwCjVFLvg==} - engines: {node: '>=18'} - cpu: [x64] - os: [netbsd] - '@esbuild/openbsd-arm64@0.23.1': resolution: {integrity: sha512-3x37szhLexNA4bXhLrCC/LImN/YtWis6WXr1VESlfVtVeoFJBRINPJ3f0a/6LV8zpikqoUg4hyXw0sFBt5Cr+Q==} engines: {node: '>=18'} cpu: [arm64] os: [openbsd] - '@esbuild/openbsd-arm64@0.24.0': - resolution: {integrity: sha512-MD9uzzkPQbYehwcN583yx3Tu5M8EIoTD+tUgKF982WYL9Pf5rKy9ltgD0eUgs8pvKnmizxjXZyLt0z6DC3rRXg==} - engines: {node: '>=18'} - cpu: [arm64] - os: [openbsd] - '@esbuild/openbsd-x64@0.17.19': resolution: {integrity: sha512-cnq5brJYrSZ2CF6c35eCmviIN3k3RczmHz8eYaVlNasVqsNY+JKohZU5MKmaOI+KkllCdzOKKdPs762VCPC20g==} engines: {node: '>=12'} @@ -1067,12 +953,6 @@ packages: cpu: [x64] os: [openbsd] - '@esbuild/openbsd-x64@0.24.0': - resolution: {integrity: sha512-4ir0aY1NGUhIC1hdoCzr1+5b43mw99uNwVzhIq1OY3QcEwPDO3B7WNXBzaKY5Nsf1+N11i1eOfFcq+D/gOS15Q==} - engines: {node: '>=18'} - cpu: [x64] - os: [openbsd] - '@esbuild/sunos-x64@0.17.19': resolution: {integrity: sha512-vCRT7yP3zX+bKWFeP/zdS6SqdWB8OIpaRq/mbXQxTGHnIxspRtigpkUcDMlSCOejlHowLqII7K2JKevwyRP2rg==} engines: {node: '>=12'} @@ -1091,12 +971,6 @@ packages: cpu: [x64] os: [sunos] - '@esbuild/sunos-x64@0.24.0': - resolution: {integrity: sha512-jVzdzsbM5xrotH+W5f1s+JtUy1UWgjU0Cf4wMvffTB8m6wP5/kx0KiaLHlbJO+dMgtxKV8RQ/JvtlFcdZ1zCPA==} - engines: {node: '>=18'} - cpu: [x64] - os: [sunos] - '@esbuild/win32-arm64@0.17.19': resolution: {integrity: sha512-yYx+8jwowUstVdorcMdNlzklLYhPxjniHWFKgRqH7IFlUEa0Umu3KuYplf1HUZZ422e3NU9F4LGb+4O0Kdcaag==} engines: {node: '>=12'} @@ -1115,12 +989,6 @@ packages: cpu: [arm64] os: [win32] - '@esbuild/win32-arm64@0.24.0': - resolution: {integrity: sha512-iKc8GAslzRpBytO2/aN3d2yb2z8XTVfNV0PjGlCxKo5SgWmNXx82I/Q3aG1tFfS+A2igVCY97TJ8tnYwpUWLCA==} - engines: {node: '>=18'} - cpu: [arm64] - os: [win32] - '@esbuild/win32-ia32@0.17.19': resolution: {integrity: sha512-eggDKanJszUtCdlVs0RB+h35wNlb5v4TWEkq4vZcmVt5u/HiDZrTXe2bWFQUez3RgNHwx/x4sk5++4NSSicKkw==} engines: {node: '>=12'} @@ -1139,12 +1007,6 @@ packages: cpu: [ia32] os: [win32] - '@esbuild/win32-ia32@0.24.0': - resolution: {integrity: sha512-vQW36KZolfIudCcTnaTpmLQ24Ha1RjygBo39/aLkM2kmjkWmZGEJ5Gn9l5/7tzXA42QGIoWbICfg6KLLkIw6yw==} - engines: {node: '>=18'} - cpu: [ia32] - os: [win32] - '@esbuild/win32-x64@0.17.19': resolution: {integrity: sha512-lAhycmKnVOuRYNtRtatQR1LPQf2oYCkRGkSFnseDAKPl8lu5SOsK/e1sXe5a0Pc5kHIHe6P2I/ilntNv2xf3cA==} engines: {node: '>=12'} @@ -1163,12 +1025,6 @@ packages: cpu: [x64] os: [win32] - '@esbuild/win32-x64@0.24.0': - resolution: {integrity: sha512-7IAFPrjSQIJrGsK6flwg7NFmwBoSTyF3rl7If0hNUFQU4ilTsEPL6GuMuU9BfIWVVGuRnuIidkSMC+c0Otu8IA==} - engines: {node: '>=18'} - cpu: [x64] - os: [win32] - '@fastify/busboy@2.1.1': resolution: {integrity: sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==} engines: {node: '>=14'} @@ -1816,11 +1672,6 @@ packages: engines: {node: '>=18'} hasBin: true - esbuild@0.24.0: - resolution: {integrity: sha512-FuLPevChGDshgSicjisSooU0cemp/sGXR841D5LHMB7mTVOmsEHcAxaH3irL53+8YDIeVNQEySh4DaYU/iuPqQ==} - engines: {node: '>=18'} - hasBin: true - escalade@3.2.0: resolution: {integrity: sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==} engines: {node: '>=6'} @@ -2732,46 +2583,6 @@ packages: terser: optional: true - vite@6.0.3: - resolution: {integrity: sha512-Cmuo5P0ENTN6HxLSo6IHsjCLn/81Vgrp81oaiFFMRa8gGDj5xEjIcEpf2ZymZtZR8oU0P2JX5WuUp/rlXcHkAw==} - engines: {node: ^18.0.0 || ^20.0.0 || >=22.0.0} - hasBin: true - peerDependencies: - '@types/node': ^18.0.0 || ^20.0.0 || >=22.0.0 - jiti: '>=1.21.0' - less: '*' - lightningcss: ^1.21.0 - sass: '*' - sass-embedded: '*' - stylus: '*' - sugarss: '*' - terser: ^5.16.0 - tsx: ^4.8.1 - yaml: ^2.4.2 - peerDependenciesMeta: - '@types/node': - optional: true - jiti: - optional: true - less: - optional: true - lightningcss: - optional: true - sass: - optional: true - sass-embedded: - optional: true - stylus: - optional: true - sugarss: - optional: true - terser: - optional: true - tsx: - optional: true - yaml: - optional: true - vitest@2.1.3: resolution: {integrity: sha512-Zrxbg/WiIvUP2uEzelDNTXmEMJXuzJ1kCpbDvaKByFA9MNeO95V+7r/3ti0qzJzrxdyuUw5VduN7k+D3VmVOSA==} engines: {node: ^18.0.0 || >=20.0.0} @@ -3161,9 +2972,6 @@ snapshots: '@esbuild/aix-ppc64@0.23.1': optional: true - '@esbuild/aix-ppc64@0.24.0': - optional: true - '@esbuild/android-arm64@0.17.19': optional: true @@ -3173,9 +2981,6 @@ snapshots: '@esbuild/android-arm64@0.23.1': optional: true - '@esbuild/android-arm64@0.24.0': - optional: true - '@esbuild/android-arm@0.17.19': optional: true @@ -3185,9 +2990,6 @@ snapshots: '@esbuild/android-arm@0.23.1': optional: true - '@esbuild/android-arm@0.24.0': - optional: true - '@esbuild/android-x64@0.17.19': optional: true @@ -3197,9 +2999,6 @@ snapshots: '@esbuild/android-x64@0.23.1': optional: true - '@esbuild/android-x64@0.24.0': - optional: true - '@esbuild/darwin-arm64@0.17.19': optional: true @@ -3209,9 +3008,6 @@ snapshots: '@esbuild/darwin-arm64@0.23.1': optional: true - '@esbuild/darwin-arm64@0.24.0': - optional: true - '@esbuild/darwin-x64@0.17.19': optional: true @@ -3221,9 +3017,6 @@ snapshots: '@esbuild/darwin-x64@0.23.1': optional: true - '@esbuild/darwin-x64@0.24.0': - optional: true - '@esbuild/freebsd-arm64@0.17.19': optional: true @@ -3233,9 +3026,6 @@ snapshots: '@esbuild/freebsd-arm64@0.23.1': optional: true - '@esbuild/freebsd-arm64@0.24.0': - optional: true - '@esbuild/freebsd-x64@0.17.19': optional: true @@ -3245,9 +3035,6 @@ snapshots: '@esbuild/freebsd-x64@0.23.1': optional: true - '@esbuild/freebsd-x64@0.24.0': - optional: true - '@esbuild/linux-arm64@0.17.19': optional: true @@ -3257,9 +3044,6 @@ snapshots: '@esbuild/linux-arm64@0.23.1': optional: true - '@esbuild/linux-arm64@0.24.0': - optional: true - '@esbuild/linux-arm@0.17.19': optional: true @@ -3269,9 +3053,6 @@ snapshots: '@esbuild/linux-arm@0.23.1': optional: true - '@esbuild/linux-arm@0.24.0': - optional: true - '@esbuild/linux-ia32@0.17.19': optional: true @@ -3281,9 +3062,6 @@ snapshots: '@esbuild/linux-ia32@0.23.1': optional: true - '@esbuild/linux-ia32@0.24.0': - optional: true - '@esbuild/linux-loong64@0.17.19': optional: true @@ -3293,9 +3071,6 @@ snapshots: '@esbuild/linux-loong64@0.23.1': optional: true - '@esbuild/linux-loong64@0.24.0': - optional: true - '@esbuild/linux-mips64el@0.17.19': optional: true @@ -3305,9 +3080,6 @@ snapshots: '@esbuild/linux-mips64el@0.23.1': optional: true - '@esbuild/linux-mips64el@0.24.0': - optional: true - '@esbuild/linux-ppc64@0.17.19': optional: true @@ -3317,9 +3089,6 @@ snapshots: '@esbuild/linux-ppc64@0.23.1': optional: true - '@esbuild/linux-ppc64@0.24.0': - optional: true - '@esbuild/linux-riscv64@0.17.19': optional: true @@ -3329,9 +3098,6 @@ snapshots: '@esbuild/linux-riscv64@0.23.1': optional: true - '@esbuild/linux-riscv64@0.24.0': - optional: true - '@esbuild/linux-s390x@0.17.19': optional: true @@ -3341,9 +3107,6 @@ snapshots: '@esbuild/linux-s390x@0.23.1': optional: true - '@esbuild/linux-s390x@0.24.0': - optional: true - '@esbuild/linux-x64@0.17.19': optional: true @@ -3353,9 +3116,6 @@ snapshots: '@esbuild/linux-x64@0.23.1': optional: true - '@esbuild/linux-x64@0.24.0': - optional: true - '@esbuild/netbsd-x64@0.17.19': optional: true @@ -3365,15 +3125,9 @@ snapshots: '@esbuild/netbsd-x64@0.23.1': optional: true - '@esbuild/netbsd-x64@0.24.0': - optional: true - '@esbuild/openbsd-arm64@0.23.1': optional: true - '@esbuild/openbsd-arm64@0.24.0': - optional: true - '@esbuild/openbsd-x64@0.17.19': optional: true @@ -3383,9 +3137,6 @@ snapshots: '@esbuild/openbsd-x64@0.23.1': optional: true - '@esbuild/openbsd-x64@0.24.0': - optional: true - '@esbuild/sunos-x64@0.17.19': optional: true @@ -3395,9 +3146,6 @@ snapshots: '@esbuild/sunos-x64@0.23.1': optional: true - '@esbuild/sunos-x64@0.24.0': - optional: true - '@esbuild/win32-arm64@0.17.19': optional: true @@ -3407,9 +3155,6 @@ snapshots: '@esbuild/win32-arm64@0.23.1': optional: true - '@esbuild/win32-arm64@0.24.0': - optional: true - '@esbuild/win32-ia32@0.17.19': optional: true @@ -3419,9 +3164,6 @@ snapshots: '@esbuild/win32-ia32@0.23.1': optional: true - '@esbuild/win32-ia32@0.24.0': - optional: true - '@esbuild/win32-x64@0.17.19': optional: true @@ -3431,9 +3173,6 @@ snapshots: '@esbuild/win32-x64@0.23.1': optional: true - '@esbuild/win32-x64@0.24.0': - optional: true - '@fastify/busboy@2.1.1': {} '@hattip/adapter-node@0.0.49': @@ -3779,14 +3518,14 @@ snapshots: dependencies: csstype: 3.1.3 - '@vitejs/plugin-react@4.3.4(vite@6.0.3(@types/node@22.10.1))': + '@vitejs/plugin-react@4.3.4(vite@my-repos+vite+packages+vite)': dependencies: '@babel/core': 7.26.0 '@babel/plugin-transform-react-jsx-self': 7.25.9(@babel/core@7.26.0) '@babel/plugin-transform-react-jsx-source': 7.25.9(@babel/core@7.26.0) '@types/babel__core': 7.20.5 react-refresh: 0.14.2 - vite: 6.0.3(@types/node@22.10.1) + vite: link:../my-repos/vite/packages/vite transitivePeerDependencies: - supports-color @@ -4108,33 +3847,6 @@ snapshots: '@esbuild/win32-ia32': 0.23.1 '@esbuild/win32-x64': 0.23.1 - esbuild@0.24.0: - optionalDependencies: - '@esbuild/aix-ppc64': 0.24.0 - '@esbuild/android-arm': 0.24.0 - '@esbuild/android-arm64': 0.24.0 - '@esbuild/android-x64': 0.24.0 - '@esbuild/darwin-arm64': 0.24.0 - '@esbuild/darwin-x64': 0.24.0 - '@esbuild/freebsd-arm64': 0.24.0 - '@esbuild/freebsd-x64': 0.24.0 - '@esbuild/linux-arm': 0.24.0 - '@esbuild/linux-arm64': 0.24.0 - '@esbuild/linux-ia32': 0.24.0 - '@esbuild/linux-loong64': 0.24.0 - '@esbuild/linux-mips64el': 0.24.0 - '@esbuild/linux-ppc64': 0.24.0 - '@esbuild/linux-riscv64': 0.24.0 - '@esbuild/linux-s390x': 0.24.0 - '@esbuild/linux-x64': 0.24.0 - '@esbuild/netbsd-x64': 0.24.0 - '@esbuild/openbsd-arm64': 0.24.0 - '@esbuild/openbsd-x64': 0.24.0 - '@esbuild/sunos-x64': 0.24.0 - '@esbuild/win32-arm64': 0.24.0 - '@esbuild/win32-ia32': 0.24.0 - '@esbuild/win32-x64': 0.24.0 - escalade@3.2.0: {} escape-string-regexp@1.0.5: {} @@ -4538,6 +4250,7 @@ snapshots: nanoid: 3.3.7 picocolors: 1.1.1 source-map-js: 1.2.1 + optional: true postgres-array@2.0.0: {} @@ -4943,15 +4656,6 @@ snapshots: '@types/node': 22.10.1 fsevents: 2.3.3 - vite@6.0.3(@types/node@22.10.1): - dependencies: - esbuild: 0.24.0 - postcss: 8.4.49 - rollup: 4.24.0 - optionalDependencies: - '@types/node': 22.10.1 - fsevents: 2.3.3 - vitest@2.1.3(@types/node@22.10.1): dependencies: '@vitest/expect': 2.1.3 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 625e0f62..7d1c74ac 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -6,7 +6,7 @@ packages: catalog: '@cloudflare/workers-types': '^4.20241205.0' 'typescript': '^5.7.2' - 'vite': '6.0.3' + 'vite': '/Users/dario/Repos/my-repos/vite/packages/vite' 'wrangler': '^3.93.0' '@types/node': '^22.10.1' 'unenv': 'npm:unenv-nightly@2.0.0-20241204-140205-a5d5190' From 147342e9883649aa70050f0719b4d16c883b58ee Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 3 Dec 2024 15:44:45 +0000 Subject: [PATCH 2/2] apply nodejs rollup externals conditionally --- .../src/cloudflare-environment.ts | 3 - packages/vite-plugin-cloudflare/src/index.ts | 83 +++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts index 78c2d115..a7f87315 100644 --- a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts +++ b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts @@ -1,7 +1,5 @@ import assert from 'node:assert'; -import { builtinModules } from 'node:module'; import * as vite from 'vite'; -import { getNodeCompatExternals } from './node-js-compat'; import { INIT_PATH, UNKNOWN_HOST } from './shared'; import { toMiniflareRequest } from './utils'; import type { ResolvedPluginConfig, WorkerConfig } from './plugin-config'; @@ -153,7 +151,6 @@ export function createCloudflareEnvironmentOptions( // dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set // optimizeDeps.entries in the dev config) input: workerConfig.main, - external: [...getNodeCompatExternals()], }, }, optimizeDeps: { diff --git a/packages/vite-plugin-cloudflare/src/index.ts b/packages/vite-plugin-cloudflare/src/index.ts index 976810a7..0e52b640 100644 --- a/packages/vite-plugin-cloudflare/src/index.ts +++ b/packages/vite-plugin-cloudflare/src/index.ts @@ -17,6 +17,7 @@ import { } from './miniflare-options'; import { getNodeCompatAliases, + getNodeCompatExternals, injectGlobalCode, isNodeCompat, resolveNodeAliases, @@ -97,6 +98,10 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin { }, configResolved(config) { addNodeBuiltinsToWorkerEnvironmentsIfNeeded(config, resolvedPluginConfig); + addBuildNodeExternalsToWorkerEnvironmentsIfNeeded( + config, + resolvedPluginConfig, + ); }, configEnvironment(name, options) { if (resolvedPluginConfig.type === 'workers') { @@ -302,3 +307,81 @@ function addNodeBuiltinsToWorkerEnvironmentsIfNeeded( ); } } + +/** + * Adds to the worker resolved configuration the necessary node external modules config options if the worker is under nodejs compat + * (this config is under `build.rollupOptions.externals`) + * + * Note: this includes polyfill methods as well as nodejs builtins (given the `resolve.builtins` option maybe externals here should + * not include such modules...?) + * + * @param resolvedConfig the vite resolved config (that will be side-effectfully updated) + * @param name the name of the worker + * @param resolvedPluginConfig the normalized/resolved plugin config + */ +function addBuildNodeExternalsToWorkerEnvironmentsIfNeeded( + resolvedConfig: vite.ResolvedConfig, + resolvedPluginConfig: ResolvedPluginConfig, +) { + if (resolvedPluginConfig.type === 'workers') { + Object.entries(resolvedPluginConfig.workers).map( + ([environmentName, workerConfig]) => { + invariant( + resolvedConfig.environments[environmentName], + `environment with name "${environmentName}" not found`, + ); + + const nodeCompat = isNodeCompat(workerConfig); + + if (!nodeCompat) { + return; + } + + const nodeCompatExternals = [...getNodeCompatExternals()]; + + const existingExternalOption = + resolvedConfig.environments[environmentName].build.rollupOptions + .external; + if (!existingExternalOption) { + resolvedConfig.environments[ + environmentName + ].build.rollupOptions.external = [...nodeCompatExternals]; + return; + } + + if (typeof existingExternalOption !== 'function') { + const existingExternals = Array.isArray(existingExternalOption) + ? existingExternalOption + : [existingExternalOption]; + resolvedConfig.environments[ + environmentName + ].build.rollupOptions.external = [ + ...nodeCompatExternals, + ...existingExternals, + ]; + return; + } + + const nodeCompatExternalsSet = new Set(nodeCompatExternals); + + resolvedConfig.environments[ + environmentName + ].build.rollupOptions.external = ( + source: string, + importer: string | undefined, + isResolved: boolean, + ) => { + const existingOptionResult = existingExternalOption( + source, + importer, + isResolved, + ); + if (existingOptionResult) { + return existingOptionResult; + } + return nodeCompatExternalsSet.has(source); + }; + }, + ); + } +}