fix(vite): asset handling (#3225)

This PR reworks asset handling in our vite plugin. Importing assets in
js is now supported.
This commit is contained in:
Marvin Hagemeister
2025-08-19 16:45:33 +02:00
committed by GitHub
parent 54110c6372
commit afdf5eaf6c
47 changed files with 760 additions and 85 deletions
+99
View File
@@ -167,3 +167,102 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - fetch static assets",
fn: async () => {
await using res = await buildVite(DEMO_DIR);
await withChildProcessServer(
{
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}/tests/assets`, {
waitUntil: "networkidle2",
});
const url = await page.locator("img").evaluate((el) =>
// deno-lint-ignore no-explicit-any
(el as any).src
);
const res = await fetch(url);
await res.body?.cancel();
expect(res.status).toEqual(200);
expect(res.headers.get("Content-Type")).toEqual("image/png");
});
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - tailwind no _app",
fn: async () => {
const fixture = path.join(FIXTURE_DIR, "tailwind_no_app");
await using res = await buildVite(fixture);
await withChildProcessServer(
{
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const href = await page
.locator("link[rel='stylesheet']")
.evaluate((el) => {
// deno-lint-ignore no-explicit-any
return (el as any).href;
});
expect(href).toMatch(/\/assets\/client-entry-.*\.css$/);
});
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - tailwind _app",
fn: async () => {
const fixture = path.join(FIXTURE_DIR, "tailwind_app");
await using res = await buildVite(fixture);
await withChildProcessServer(
{
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const href = await page
.locator("link[rel='stylesheet']")
.evaluate((el) => {
// deno-lint-ignore no-explicit-any
return (el as any).href;
});
expect(href).toMatch(/\/assets\/client-entry-.*\.css/);
});
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
@@ -167,3 +167,52 @@ Deno.test({
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "vite dev - serves imported assets",
fn: async () => {
await withDevServer(DEMO_DIR, async (address) => {
const res = await fetch(`${address}/assets/deno-logo.png`);
expect(res.status).toEqual(200);
expect(res.headers.get("Content-Type")).toEqual("image/png");
});
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "vite dev - tailwind no _app",
fn: async () => {
const fixture = path.join(FIXTURE_DIR, "tailwind_no_app");
await withDevServer(fixture, async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
await page.locator("style[data-vite-dev-id$='style.css']").wait();
});
});
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "vite dev - tailwind _app",
fn: async () => {
const fixture = path.join(FIXTURE_DIR, "tailwind_app");
await withDevServer(fixture, async (address) => {
await withBrowser(async (page) => {
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
await page.locator("style[data-vite-dev-id$='style.css']").wait();
});
});
},
sanitizeResources: false,
sanitizeOps: false,
});
@@ -0,0 +1 @@
@import "tailwindcss";
@@ -0,0 +1 @@
import "./assets/style.css";
@@ -0,0 +1,3 @@
import { App } from "@fresh/core";
export const app = new App().fsRoutes();
@@ -0,0 +1,14 @@
import type { PageProps } from "@fresh/core";
export default function App(props: PageProps) {
return (
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<props.Component />
</body>
</html>
);
}
@@ -0,0 +1,3 @@
export default function Hello() {
return <h1 class="text-red-500">ok</h1>;
}
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [fresh(), tailwindcss()],
});
@@ -0,0 +1 @@
@import "tailwindcss";
@@ -0,0 +1 @@
import "./assets/style.css";
@@ -0,0 +1,3 @@
import { App } from "@fresh/core";
export const app = new App().fsRoutes();
@@ -0,0 +1,3 @@
export default function Hello() {
return <h1 class="text-red-500">ok</h1>;
}
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [fresh(), tailwindcss()],
});