Skip to content

Commit

Permalink
replace process.env.NODE_ENV at build time and enable react module-…
Browse files Browse the repository at this point in the history
…resolution build test
  • Loading branch information
dario-piotrowicz committed Dec 6, 2024
1 parent 40c0c52 commit 68da74a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/vite-plugin-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"@hattip/adapter-node": "^0.0.48",
"@rollup/plugin-replace": "^6.0.1",
"miniflare": "beta"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions packages/vite-plugin-cloudflare/src/cloudflare-environment.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -126,6 +127,10 @@ export function createCloudflareEnvironmentOptions(
workerConfig: WorkerConfig,
userConfig: vite.UserConfig,
): vite.EnvironmentOptions {
console.log(
`\x1b[35m process.env.NODE_ENV = ${process.env.NODE_ENV} \x1b[0m`,
);

return {
resolve: {
// Note: in order for ssr pre-bundling to take effect we need to ask vite to treat all
Expand Down Expand Up @@ -153,6 +158,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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 68da74a

Please sign in to comment.