Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions e2e/cases/diagnostic/process-undefined/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { rspackTest } from '@e2e/helper';

rspackTest(
'should print help message if undefined process.env.* is accessed in dev',
async ({ dev }) => {
const rsbuild = await dev();
await rsbuild.expectLog(
'To access `process.env.*`, define them in a `.env` file with the `PUBLIC_` prefix.',
);
},
);
1 change: 1 addition & 0 deletions e2e/cases/diagnostic/process-undefined/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(process.env.UNDEFINED_VALUE);
2 changes: 1 addition & 1 deletion packages/core/src/helpers/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function hintUnknownFiles(message: string): string {
`@rsbuild/plugin-${packageName}`,
)}" ${color.dim(
`(https://npmjs.com/package/@rsbuild/plugin-${packageName})`,
)}.`;
)}.\n`;
};

const recommendPlugins = [
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/server/browserLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ const formatErrorLocation = async (
return rawLocation;
};

const enhanceBrowserErrorLog = (log: string) => {
const isProcessUndefined = log.includes(
'ReferenceError: process is not defined',
);
if (isProcessUndefined) {
return `${log}\n${color.yellow(` - \`process\` is a Node.js global and not available in browsers.
- To access \`process.env.*\`, define them in a \`.env\` file with the \`PUBLIC_\` prefix.
- Or configure them via \`source.define\`.
- Alternatively, install \`@rsbuild/plugin-node-polyfill\` to polyfill Node.js globals.`)}`;
}

return log;
};

/**
* Formats error messages received from the browser into a log string with
* source location information.
Expand All @@ -127,5 +141,5 @@ export const formatBrowserErrorLog = async (
}
}

return log;
return enhanceBrowserErrorLog(log);
};
Loading