Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vite plugin assets order #328

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading