diff --git a/packages/vite-plugin-cloudflare/package.json b/packages/vite-plugin-cloudflare/package.json index 7fdc5b32..1eff92fb 100644 --- a/packages/vite-plugin-cloudflare/package.json +++ b/packages/vite-plugin-cloudflare/package.json @@ -36,6 +36,7 @@ }, "dependencies": { "@hattip/adapter-node": "^0.0.49", + "@rollup/plugin-replace": "^6.0.1", "miniflare": "^3.20241205.0" }, "devDependencies": { diff --git a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts index c4d93fc2..2876e33a 100644 --- a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts +++ b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts @@ -1,5 +1,6 @@ import assert from 'node:assert'; import { builtinModules } from 'node:module'; +import replace from '@rollup/plugin-replace'; import * as vite from 'vite'; import { getNodeCompatExternals } from './node-js-compat'; import { INIT_PATH, UNKNOWN_HOST } from './shared'; @@ -153,6 +154,13 @@ export function createCloudflareEnvironmentOptions( // optimizeDeps.entries in the dev config) input: workerConfig.main, external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()], + plugins: [ + replace({ + 'process.env.NODE_ENV': JSON.stringify( + process.env.NODE_ENV ?? 'production', + ), + }), + ], }, }, optimizeDeps: { diff --git a/playground/module-resolution/__tests__/module-resolution.spec.ts b/playground/module-resolution/__tests__/module-resolution.spec.ts index 3f90891a..836d97cc 100644 --- a/playground/module-resolution/__tests__/module-resolution.spec.ts +++ b/playground/module-resolution/__tests__/module-resolution.spec.ts @@ -76,9 +76,7 @@ describe('module resolution', async () => { * special meaning to us. */ describe('third party packages resolutions', () => { - // TODO: we skip this test on build because a `ReferenceError: process is not defined` is thrown - // (https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/82) - test.skipIf(isBuild)('react', async () => { + test('react', async () => { const result = await getJsonResponse('/third-party/react'); expect(result).toEqual({ '(react) reactVersionsMatch': true, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f7ea1670..5638e674 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -68,6 +68,9 @@ importers: '@hattip/adapter-node': specifier: ^0.0.49 version: 0.0.49 + '@rollup/plugin-replace': + specifier: ^6.0.1 + version: 6.0.1(rollup@4.24.0) miniflare: specifier: ^3.20241205.0 version: 3.20241205.0 @@ -1335,6 +1338,24 @@ packages: typescript: optional: true + '@rollup/plugin-replace@6.0.1': + resolution: {integrity: sha512-2sPh9b73dj5IxuMmDAsQWVFT7mR+yoHweBaXG2W/R8vQ+IWZlnaI7BR7J6EguVQUp1hd8Z7XuozpDjEKQAAC2Q==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + + '@rollup/pluginutils@5.1.3': + resolution: {integrity: sha512-Pnsb6f32CD2W3uCaLZIzDmeFyQ2b8UWMFI7xtwUezpcGBDVDW6y9XgAWIlARiGAo6eNF5FK5aQTr0LFyNyqq5A==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + '@rollup/rollup-android-arm-eabi@4.22.4': resolution: {integrity: sha512-Fxamp4aEZnfPOcGA8KSNEohV8hX7zVHOemC8jVBoBUHu5zpJK/Eu3uJwt6BMgy9fkvzxDaurgj96F/NiLukF2w==} cpu: [arm] @@ -1836,6 +1857,9 @@ packages: estree-walker@0.6.1: resolution: {integrity: sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==} + estree-walker@2.0.2: + resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==} + estree-walker@3.0.3: resolution: {integrity: sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==} @@ -3627,6 +3651,21 @@ snapshots: optionalDependencies: typescript: 5.7.2 + '@rollup/plugin-replace@6.0.1(rollup@4.24.0)': + dependencies: + '@rollup/pluginutils': 5.1.3(rollup@4.24.0) + magic-string: 0.30.12 + optionalDependencies: + rollup: 4.24.0 + + '@rollup/pluginutils@5.1.3(rollup@4.24.0)': + dependencies: + '@types/estree': 1.0.6 + estree-walker: 2.0.2 + picomatch: 4.0.2 + optionalDependencies: + rollup: 4.24.0 + '@rollup/rollup-android-arm-eabi@4.22.4': optional: true @@ -4143,6 +4182,8 @@ snapshots: estree-walker@0.6.1: {} + estree-walker@2.0.2: {} + estree-walker@3.0.3: dependencies: '@types/estree': 1.0.6