From 6faf7b0e68c429aa33e1ee5ebfbf21141c8eab42 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 13 Dec 2023 17:02:39 +0100 Subject: [PATCH 1/3] refactor(rollup): improve generated chunk names --- src/rollup/config.ts | 81 +++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/src/rollup/config.ts b/src/rollup/config.ts index c675c68e01..b2eb406bc7 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -1,7 +1,12 @@ import { pathToFileURL } from "node:url"; import { createRequire, builtinModules } from "node:module"; import { dirname, join, normalize, relative, resolve } from "pathe"; -import type { InputOptions, OutputOptions, Plugin } from "rollup"; +import type { + InputOptions, + OutputOptions, + Plugin, + PreRenderedChunk, +} from "rollup"; import { defu } from "defu"; // import terser from "@rollup/plugin-terser"; // TODO: Investigate jiti issue import commonjs from "@rollup/plugin-commonjs"; @@ -59,7 +64,50 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { const env = unenv.env(nodePreset, builtinPreset, nitro.options.unenv); const buildServerDir = join(nitro.options.buildDir, "dist/server"); - const runtimeAppDir = join(runtimeDir, "app"); + + const chunkNamePrefixes = [ + [nitro.options.buildDir, "build"], + [runtimeDir, "nitro"], + [buildServerDir, "app"], + ["\0raw:", "raw"], + ["\0", "internal"], + ]; + function getChunkName(id: string) { + // Known path prefixes + for (const [dir, name] of chunkNamePrefixes) { + if (id.startsWith(dir)) { + return `chunks/${name}/[name].mjs`; + } + } + + // Route handlers + const routeHandler = + nitro.options.handlers.find((h) => id.startsWith(h.handler as string)) || + nitro.scannedHandlers.find((h) => id.startsWith(h.handler as string)); + if (routeHandler) { + const path = + routeHandler.route + .replace(/:([^/]+)/g, "_$1") + .replace(/\/[^/]+$/g, "") || "/"; + return `chunks/routes${path}/[name].mjs`; + } + + // Task handlers + const taskHandler = Object.entries(nitro.options.tasks).find( + ([_, task]) => task.handler === id + ); + if (taskHandler) { + return `chunks/tasks/[name].mjs`; + } + + // Wasm + if (id.endsWith(".wasm")) { + return `chunks/wasm/[name].mjs`; + } + + // Unknown path + return `chunks/_/[name].mjs`; + } type _RollupConfig = Omit & { plugins: Plugin[] }; @@ -70,32 +118,9 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { output: { dir: nitro.options.output.serverDir, entryFileNames: "index.mjs", - chunkFileNames(chunkInfo) { - let prefix = ""; - const lastModule = normalize(chunkInfo.moduleIds.at(-1)); - if (lastModule.startsWith(buildServerDir)) { - prefix = join("app", relative(buildServerDir, dirname(lastModule))); - } else if (lastModule.startsWith(runtimeAppDir)) { - prefix = "app"; - } else if (lastModule.startsWith(nitro.options.buildDir)) { - prefix = "build"; - } else if (lastModule.startsWith(runtimeDir)) { - prefix = "nitro"; - } else if ( - nitro.options.handlers.some((m) => - lastModule.startsWith(m.handler as string) - ) - ) { - prefix = "handlers"; - } else if ( - lastModule.includes("assets") || - lastModule.startsWith("\0raw:") - ) { - prefix = "raw"; - } else if (lastModule.startsWith("\0")) { - prefix = "rollup"; - } - return join("chunks", prefix, "[name].mjs"); + chunkFileNames(chunk) { + const lastModule = normalize(chunk.moduleIds.at(-1)); + return getChunkName(lastModule); }, inlineDynamicImports: nitro.options.inlineDynamicImports, format: "esm", From df279db1a91fc417962ee8c04752d970e6c5e3e1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 13 Dec 2023 17:11:10 +0100 Subject: [PATCH 2/3] improve for wasm --- src/rollup/config.ts | 10 +++------- src/rollup/plugins/wasm.ts | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/rollup/config.ts b/src/rollup/config.ts index b2eb406bc7..bba8b497ce 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -70,8 +70,9 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { [runtimeDir, "nitro"], [buildServerDir, "app"], ["\0raw:", "raw"], - ["\0", "internal"], - ]; + ["\0nitro-wasm:", "wasm"], + ["\0", "virtual"], + ] as const; function getChunkName(id: string) { // Known path prefixes for (const [dir, name] of chunkNamePrefixes) { @@ -100,11 +101,6 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { return `chunks/tasks/[name].mjs`; } - // Wasm - if (id.endsWith(".wasm")) { - return `chunks/wasm/[name].mjs`; - } - // Unknown path return `chunks/_/[name].mjs`; } diff --git a/src/rollup/plugins/wasm.ts b/src/rollup/plugins/wasm.ts index 9cf8acd759..2a92a605c6 100644 --- a/src/rollup/plugins/wasm.ts +++ b/src/rollup/plugins/wasm.ts @@ -10,7 +10,7 @@ export function wasm(options: WasmOptions): Plugin { return options.esmImport ? wasmImport() : wasmBundle(options.rollup); } -const WASM_ID_PREFIX = "\0nitro:wasm/"; +const WASM_ID_PREFIX = "\0nitro-wasm:"; export function wasmImport(): Plugin { type WasmAssetInfo = { From fb37136fc9b68a2e4342a337325dad56814010bb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 13 Dec 2023 17:16:34 +0100 Subject: [PATCH 3/3] split runtime --- src/rollup/config.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/rollup/config.ts b/src/rollup/config.ts index bba8b497ce..28607ec6ea 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -67,13 +67,17 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => { const chunkNamePrefixes = [ [nitro.options.buildDir, "build"], - [runtimeDir, "nitro"], [buildServerDir, "app"], ["\0raw:", "raw"], ["\0nitro-wasm:", "wasm"], ["\0", "virtual"], ] as const; function getChunkName(id: string) { + // Runtime + if (id.startsWith(runtimeDir)) { + return `chunks/runtime.mjs`; + } + // Known path prefixes for (const [dir, name] of chunkNamePrefixes) { if (id.startsWith(dir)) {