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
@@ -1,8 +1,8 @@
import type { types } from "@babel/core";
import type { PluginObj, types } from "@babel/core";
export function cjsPlugin(
{ types: t }: { types: typeof types },
): babel.PluginObj {
): PluginObj {
const HAS_ES_MODULE = "esModule";
const REQUIRE_CALLS = "requireCalls";
const ROOT_SCOPE = "rootScope";
@@ -0,0 +1,66 @@
import type {
NodePath,
PluginObj,
PluginPass,
types,
Visitor,
} from "@babel/core";
function maybeRewrite(
t: typeof types,
path: NodePath,
source: string,
url: URL,
) {
if (source.startsWith("/")) {
const s = `deno-http::${url.origin}${source}`;
path.replaceWith(t.stringLiteral(s));
} else if (/^https?:\/\//.test(source)) {
const s = `deno-http::${source}`;
path.replaceWith(t.stringLiteral(s));
}
}
export function httpAbsolute(url: URL | null) {
return ({ types: t }: { types: typeof types }): PluginObj => {
const visitor: Visitor<PluginPass> = url !== null
? {
ImportDeclaration(path) {
const source = path.node.source.value;
maybeRewrite(t, path.get("source"), source, url);
},
ExportAllDeclaration(path) {
const source = path.node.source.value;
maybeRewrite(t, path.get("source"), source, url);
},
ExportNamedDeclaration(path) {
if (!path.node.source) return;
const source = path.node.source.value;
// deno-lint-ignore no-explicit-any
maybeRewrite(t, path.get("source") as any, source, url);
},
CallExpression(path) {
if (
t.isImport(path.node.callee) && path.node.arguments.length > 0 &&
t.isStringLiteral(path.node.arguments[0])
) {
const source = path.node.arguments[0].value;
maybeRewrite(
t,
path
.get("arguments")[0],
source,
url,
);
}
},
}
: {};
return {
name: "fresh-http-absolute",
visitor,
};
};
}
@@ -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"),
});
});
@@ -1,4 +1,4 @@
import type { NodePath, types } from "@babel/core";
import type { NodePath, PluginObj, types } from "@babel/core";
export function inlineEnvVarsPlugin(mode: string) {
const allowed = new Map<string, string>();
@@ -12,7 +12,7 @@ export function inlineEnvVarsPlugin(mode: string) {
return (
{ types: t }: { types: typeof types },
): babel.PluginObj => {
): PluginObj => {
function replace(path: NodePath, name: string) {
if (allowed.has(name)) {
const value = allowed.get(name);
@@ -1,6 +1,6 @@
import type { types } from "@babel/core";
import type { PluginObj, types } from "@babel/core";
export function jsxComments(): babel.PluginObj {
export function jsxComments(): PluginObj {
return {
name: "fresh-jsx-comment",
visitor: {