Skip to content

Commit 25076ae

Browse files
Copilothotlong
andcommitted
refactor(web): simplify Node.js builtin stubs to only shim crypto and path
Address review feedback: limit stubs to only the builtins actually imported by @objectstack/core (crypto, path), and provide explicit named exports for Rollup static analysis alongside a Proxy-based default export. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 80131d0 commit 25076ae

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

apps/web/vite.config.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ function htmlBaseUrl(): Plugin {
1717

1818
/**
1919
* Rollup plugin: stub Node.js built-in modules that leak into the browser
20-
* bundle via server-side transitive dependencies (e.g. @objectstack/core).
21-
* Each built-in is resolved to a virtual module exporting a Proxy so that
22-
* any named import (e.g. `import { createHash } from "crypto"`) receives a
23-
* no-op function instead of failing the build.
20+
* bundle via server-side transitive dependencies (e.g. @objectstack/core
21+
* imports "crypto" and "path").
22+
*
23+
* Each built-in is resolved to a virtual module whose default export is an
24+
* empty object and every named import becomes a no-op function, so that
25+
* `import { createHash } from "crypto"` does not crash the build.
2426
*/
25-
const nodeBuiltins = ['crypto', 'path', 'module', 'fs', 'os', 'util'];
27+
const nodeBuiltins = ['crypto', 'path'];
2628
const VIRTUAL_PREFIX = '\0node-stub:';
2729
function nodeBuiltinStubs(): Plugin {
2830
return {
@@ -33,13 +35,18 @@ function nodeBuiltinStubs(): Plugin {
3335
},
3436
load(id) {
3537
if (id.startsWith(VIRTUAL_PREFIX)) {
36-
// Return a module that exports a Proxy as its default + re-exports
37-
// any named binding the consumer asks for as a no-op function.
38+
// Proxy-based default that returns no-op for any property access;
39+
// explicit named exports for identifiers Rollup needs to resolve
40+
// statically during tree-shaking.
3841
return `
39-
const handler = { get: (_, prop) => typeof prop === 'string' ? () => '' : undefined };
40-
const stub = new Proxy({}, handler);
41-
export default stub;
42-
export const createHash = () => ({ update: () => ({ digest: () => '' }) });
42+
const noop = () => '';
43+
const chainable = () => ({ update: chainable, digest: noop });
44+
export default new Proxy({}, { get: () => noop });
45+
export const createHash = chainable;
46+
export const resolve = noop;
47+
export const join = noop;
48+
export const dirname = noop;
49+
export const basename = noop;
4350
`;
4451
}
4552
},

0 commit comments

Comments
 (0)