Files
sweet/packages/plugin-vite/src/plugins/patches/http_absolute_test.ts
T
Marvin Hagemeister afdf5eaf6c fix(vite): asset handling (#3225)
This PR reworks asset handling in our vite plugin. Importing assets in
js is now supported.
2025-08-19 16:45:33 +02:00

47 lines
1.3 KiB
TypeScript

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"),
});
});