From 6f0cd4d2747ae53c39413d9f54267d63477a98af Mon Sep 17 00:00:00 2001 From: Davide <43080019+edivados@users.noreply.github.com> Date: Wed, 10 Jul 2024 13:57:43 +0200 Subject: [PATCH 1/2] fix vite plugin assets order --- .../vinxi/lib/manifest/dev-server-manifest.js | 67 +++++++++---------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/packages/vinxi/lib/manifest/dev-server-manifest.js b/packages/vinxi/lib/manifest/dev-server-manifest.js index 52c985e6..bed7a60a 100644 --- a/packages/vinxi/lib/manifest/dev-server-manifest.js +++ b/packages/vinxi/lib/manifest/dev-server-manifest.js @@ -140,48 +140,43 @@ export function createDevManifest(app) { let isHandler = router.handler === relativePath; async function getVitePluginAssets() { - const pluginList = router.internals?.devServer + const plugins = router.internals?.devServer ? router.internals.devServer.config.plugins : []; - // @ts-ignore - const indexHtmlTransformers = []; - for ( - let i = 0, pre = 0, post = 0; - i < pluginList.length; - i++ - ) { - const plugin = pluginList[i]; - if (plugin != null && plugin.transformIndexHtml != null) { - const order = - typeof plugin.transformIndexHtml === "function" - ? null - : plugin.transformIndexHtml.order ?? - plugin.transformIndexHtml.enforce; - const func = - typeof plugin.transformIndexHtml === "function" - ? plugin.transformIndexHtml - : "handler" in plugin.transformIndexHtml - ? plugin.transformIndexHtml.handler - : plugin.transformIndexHtml.transform; - if (order === "pre") { - // @ts-ignore - indexHtmlTransformers.splice(pre, 0, func); - pre++; - } else if (order === "post") { - // @ts-ignore - indexHtmlTransformers.splice(post, 0, func); - post++; + + // https://github.com/vitejs/vite/blob/167006e74751a66776f4f48316262449b19bf186/packages/vite/src/node/plugins/html.ts#L1253-L1264 + + const preHooks = []; + const normalHooks = []; + const postHooks = []; + + for (const plugin of plugins) { + const hook = plugin.transformIndexHtml + if (!hook) continue + + if (typeof hook === 'function') { + normalHooks.push(hook) + } else { + // `enforce` had only two possible values for the `transformIndexHtml` hook + // `'pre'` and `'post'` (the default). `order` now works with three values + // to align with other hooks (`'pre'`, normal, and `'post'`). We map + // both `enforce: 'post'` to `order: undefined` to avoid a breaking change + const order = hook.order ?? (hook.enforce === 'pre' ? 'pre' : undefined) + // @ts-expect-error union type + const handler = hook.handler ?? hook.transform + if (order === 'pre') { + preHooks.push(handler) + } else if (order === 'post') { + postHooks.push(handler) } else { - // @ts-ignore - indexHtmlTransformers.splice( - Math.max(post - 1, 0), - 0, - func, - ); - post++; + normalHooks.push(handler) } } } + + // @ts-ignore + const indexHtmlTransformers = [preHooks, normalHooks, postHooks].flat(); + let pluginAssets = []; // @ts-ignore for (let transformer of indexHtmlTransformers) { From ce9c2692654d385cd2e7f96d7cf5b3dc9652b219 Mon Sep 17 00:00:00 2001 From: Ryan Carniato Date: Wed, 10 Jul 2024 12:45:20 -0700 Subject: [PATCH 2/2] Create sharp-kiwis-jump.md --- .changeset/sharp-kiwis-jump.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sharp-kiwis-jump.md diff --git a/.changeset/sharp-kiwis-jump.md b/.changeset/sharp-kiwis-jump.md new file mode 100644 index 00000000..81e41618 --- /dev/null +++ b/.changeset/sharp-kiwis-jump.md @@ -0,0 +1,5 @@ +--- +"vinxi": patch +--- + +fix: vite plugin assets order