Skip to content

Commit

Permalink
Merge pull request #328 from edivados/fix-vite-plugin-assets-order
Browse files Browse the repository at this point in the history
fix: vite plugin assets order
  • Loading branch information
ryansolid committed Jul 10, 2024
2 parents 9c33c0b + ce9c269 commit a6a3ed8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-kiwis-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vinxi": patch
---

fix: vite plugin assets order
67 changes: 31 additions & 36 deletions packages/vinxi/lib/manifest/dev-server-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a6a3ed8

Please sign in to comment.