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:
@@ -1,5 +1,9 @@
|
||||
import type { Plugin } from "vite";
|
||||
import { generateServerEntry } from "fresh/internal-dev";
|
||||
import type { Manifest, Plugin } from "vite";
|
||||
import {
|
||||
generateServerEntry,
|
||||
type PendingStaticFile,
|
||||
prepareStaticFile,
|
||||
} from "fresh/internal-dev";
|
||||
import { pathWithRoot, type ResolvedFreshViteConfig } from "../utils.ts";
|
||||
import * as path from "@std/path";
|
||||
|
||||
@@ -10,6 +14,7 @@ export function serverEntryPlugin(
|
||||
|
||||
let serverEntry = "";
|
||||
let serverOutDir = "";
|
||||
let clientOutDir = "";
|
||||
let root = "";
|
||||
|
||||
let isDev = false;
|
||||
@@ -29,6 +34,10 @@ export function serverEntryPlugin(
|
||||
config.environments.ssr.build.outDir,
|
||||
config.root,
|
||||
);
|
||||
clientOutDir = pathWithRoot(
|
||||
config.environments.client.build.outDir,
|
||||
config.root,
|
||||
);
|
||||
},
|
||||
resolveId(id) {
|
||||
if (id === modName) {
|
||||
@@ -39,22 +48,71 @@ export function serverEntryPlugin(
|
||||
if (id !== `\0${modName}`) return;
|
||||
|
||||
let code = generateServerEntry({
|
||||
root: path.relative(serverOutDir, root),
|
||||
root: isDev ? path.relative(serverOutDir, root) : "..",
|
||||
serverEntry: path.toFileUrl(serverEntry).href,
|
||||
snapshotSpecifier: "fresh:server-snapshot",
|
||||
});
|
||||
|
||||
code += `
|
||||
|
||||
export function registerStaticFile(prepared) {
|
||||
snapshot.staticFiles.set(prepared.name, {
|
||||
name: prepared.name,
|
||||
contentType: prepared.contentType,
|
||||
filePath: prepared.filePath
|
||||
});
|
||||
}
|
||||
`;
|
||||
|
||||
if (isDev) {
|
||||
code += `if (import.meta.hot) import.meta.hot.accept();\n`;
|
||||
}
|
||||
|
||||
return code;
|
||||
},
|
||||
async writeBundle() {
|
||||
async writeBundle(_options, bundle) {
|
||||
const manifest = bundle[".vite/manifest.json"];
|
||||
|
||||
const staticFiles: PendingStaticFile[] = [];
|
||||
if (
|
||||
manifest && manifest.type === "asset" &&
|
||||
typeof manifest.source === "string"
|
||||
) {
|
||||
const json = JSON.parse(manifest.source) as Manifest;
|
||||
|
||||
for (const item of Object.values(json)) {
|
||||
if (item.assets) {
|
||||
for (let i = 0; i < item.assets.length; i++) {
|
||||
const id = item.assets[i];
|
||||
|
||||
staticFiles.push({
|
||||
filePath: path.join(serverOutDir, id),
|
||||
hash: null,
|
||||
pathname: `/${id}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const registered = await Promise.all(staticFiles.map(async (file) => {
|
||||
const prepared = await prepareStaticFile(file, serverOutDir);
|
||||
const rel = path.relative(serverOutDir, file.filePath);
|
||||
const target = path.join(clientOutDir, rel);
|
||||
await Deno.rename(file.filePath, target);
|
||||
|
||||
prepared.filePath = path.join("client", prepared.filePath);
|
||||
|
||||
return `registerStaticFile(${JSON.stringify(prepared)});`;
|
||||
}));
|
||||
|
||||
const outDir = path.dirname(serverOutDir);
|
||||
await Deno.writeTextFile(
|
||||
path.join(outDir, "server.js"),
|
||||
`import server from "./server/server-entry.mjs";
|
||||
`import server, { registerStaticFile } from "./server/server-entry.mjs";
|
||||
|
||||
${registered.join("\n")}
|
||||
|
||||
export default {
|
||||
fetch: server.fetch
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user