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
@@ -0,0 +1,46 @@
import { expect } from "@std/expect/expect";
import * as babel from "@babel/core";
import { httpAbsolute } from "./http_absolute.ts";
function runTest(options: { input: string; expected: string; url?: URL }) {
const res = babel.transformSync(options.input, {
filename: "foo.js",
babelrc: false,
plugins: [httpAbsolute(options.url ?? null)],
});
const output = res?.code ?? "";
expect(output).toEqual(options.expected);
}
Deno.test("http absolute - change import sources", () => {
runTest({
input: `import foo from "/foo.js"`,
expected: `import foo from "deno-http::http://localhost/foo.js";`,
url: new URL("http://localhost"),
});
});
Deno.test("http absolute - change export all sources", () => {
runTest({
input: `export * as foo from "/foo.js"`,
expected: `export * as foo from "deno-http::http://localhost/foo.js";`,
url: new URL("http://localhost"),
});
});
Deno.test("http absolute - change export sources", () => {
runTest({
input: `export { foo } from "/foo.js"`,
expected: `export { foo } from "deno-http::http://localhost/foo.js";`,
url: new URL("http://localhost"),
});
});
Deno.test("http absolute - change import()", () => {
runTest({
input: `import("/foo.js")`,
expected: `import("deno-http::http://localhost/foo.js");`,
url: new URL("http://localhost"),
});
});