Skip to content

Commit 55f30f4

Browse files
authored
fix(build)!: remove custom moduleSideEffects config (#4164)
1 parent 3052ae4 commit 55f30f4

17 files changed

Lines changed: 101 additions & 33 deletions

File tree

docs/3.config/0.index.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -976,18 +976,6 @@ export default defineNitroConfig({
976976
});
977977
```
978978

979-
### `moduleSideEffects`
980-
981-
Default: `['unenv/polyfill/']`
982-
983-
Specifies module imports that have side-effects.
984-
985-
```ts
986-
export default defineNitroConfig({
987-
moduleSideEffects: ["unenv/polyfill/", "reflect-metadata"],
988-
});
989-
```
990-
991979
### `replace`
992980

993981
Build-time string replacements.

docs/4.examples/vite-ssr-vue-router.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default defineConfig((_env) => ({
4848
environments: {
4949
client: { build: { rollupOptions: { input: "./app/entry-client.ts" } } },
5050
ssr: { build: { rollupOptions: { input: "./app/entry-server.ts" } } },
51+
nitro: { build: { rollupOptions: { treeshake: { moduleSideEffects: () => false } } } },
5152
},
5253
}));
5354

examples/vite-ssr-vue-router/vite.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig((_env) => ({
88
environments: {
99
client: { build: { rollupOptions: { input: "./app/entry-client.ts" } } },
1010
ssr: { build: { rollupOptions: { input: "./app/entry-server.ts" } } },
11+
nitro: { build: { rollupOptions: { treeshake: { moduleSideEffects: () => false } } } },
1112
},
1213
}));
1314

src/build/rolldown/config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ export const getRolldownConfig = async (nitro: Nitro): Promise<RolldownOptions>
3838
warn(warning);
3939
}
4040
},
41-
treeshake: {
42-
moduleSideEffects(id) {
43-
return nitro.options.moduleSideEffects.some((p) => id.startsWith(p));
44-
},
45-
},
4641
optimization: {
4742
inlineConst: true,
4843
},

src/build/rollup/config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ export const getRollupConfig = async (nitro: Nitro): Promise<RollupConfig> => {
5656
rollupWarn(warning);
5757
}
5858
},
59-
treeshake: {
60-
moduleSideEffects(id) {
61-
return nitro.options.moduleSideEffects.some((p) => id.startsWith(p));
62-
},
63-
},
6459
output: {
6560
format: "esm",
6661
entryFileNames: "index.mjs",

src/build/vite/bundler.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export const getBundlerConfig = async (
2121
input: nitro.options.entry,
2222
external: [...base.env.external],
2323
plugins: [...(await baseBuildPlugins(nitro, base))].filter(Boolean) as RollupPlugin[],
24-
treeshake: {
25-
moduleSideEffects(id) {
26-
return nitro.options.moduleSideEffects.some((p) => id.startsWith(p));
27-
},
28-
},
2924
onwarn(warning, warn) {
3025
if (!base.ignoreWarningCodes.has(warning.code || "")) {
3126
warn(warning);

src/config/defaults.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export const NitroDefaults: NitroConfig = {
6969

7070
// Builder
7171
builder: undefined,
72-
moduleSideEffects: ["unenv/polyfill/"],
7372
replace: {},
7473
node: true,
7574
sourcemap: false,

src/presets/cloudflare/preset.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ function guardCreateRequire(): Plugin {
3737
};
3838
}
3939

40+
// When code-splitting is enabled, bundlers hoist externalized `node:*` built-in
41+
// imports as bare side-effect imports (`import "node:buffer"`) into entry and
42+
// chunk files. These are no-ops (Node.js built-ins have no meaningful
43+
// module-level side effects) but they can cause issues on worker runtimes where
44+
// `node:*` modules may not be available or trigger unnecessary warnings.
45+
const BARE_NODE_IMPORT_RE = /^import\s*['"]node:[^'"]+['"];?\s*$/gm;
46+
function stripBareNodeImports(): Plugin {
47+
return {
48+
name: "nitro:cloudflare-strip-bare-node-imports",
49+
generateBundle(_options, bundle) {
50+
for (const chunk of Object.values(bundle)) {
51+
if (chunk.type === "chunk" && chunk.code.includes("node:")) {
52+
chunk.code = chunk.code.replace(BARE_NODE_IMPORT_RE, "");
53+
}
54+
}
55+
},
56+
};
57+
}
58+
4059
export type { CloudflareOptions as PresetOptions } from "./types.ts";
4160

4261
const cloudflarePages = defineNitroPreset(
@@ -69,7 +88,7 @@ const cloudflarePages = defineNitroPreset(
6988
format: "esm",
7089
inlineDynamicImports: false,
7190
},
72-
plugins: [guardCreateRequire()],
91+
plugins: [guardCreateRequire(), stripBareNodeImports()],
7392
},
7493
hooks: {
7594
"build:before": async (nitro) => {
@@ -149,7 +168,7 @@ const cloudflareModule = defineNitroPreset(
149168
exports: "named",
150169
inlineDynamicImports: false,
151170
},
152-
plugins: [guardCreateRequire()],
171+
plugins: [guardCreateRequire(), stripBareNodeImports()],
153172
},
154173
wasm: {
155174
lazy: false,

src/types/config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ export interface NitroOptions extends PresetOptions {
223223
inlineDynamicImports: boolean;
224224
sourcemap: boolean;
225225
node: boolean;
226-
moduleSideEffects: string[];
227226
oxc?: OXCOptions;
228227
replace: Record<string, string | ((id: string) => string)>;
229228
commonJS?: RollupCommonJSOptions;

test/examples.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ function setupTest(name: string) {
5353
test(`${name} (${mode})`, async () => {
5454
const res = await ctx.fetch("/");
5555
const expectedStatus = name === "custom-error-handler" ? 500 : 200;
56+
if (res.status !== expectedStatus) {
57+
const text = await res.text();
58+
console.error(`Unexpected response ${res.status} ${res.statusText}\n${text}`);
59+
}
5660
expect(res.status, res.statusText).toBe(expectedStatus);
5761
});
5862
}

0 commit comments

Comments
 (0)