Skip to content

Commit afdf5ea

Browse files
fix(vite): asset handling (#3225)
This PR reworks asset handling in our vite plugin. Importing assets in js is now supported.
1 parent 54110c6 commit afdf5ea

47 files changed

Lines changed: 761 additions & 86 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

deno.lock

Lines changed: 136 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/fresh/src/build_cache.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface BuildSnapshot<State> {
1919
fsRoutes: FsRouteFile<State>[];
2020
staticFiles: Map<string, FileSnapshot>;
2121
islands: ServerIslandRegistry;
22+
entryAssets: string[];
2223
}
2324

2425
export interface StaticFile {
@@ -39,6 +40,7 @@ export interface BuildCache<State = any> {
3940
};
4041
getFsRoutes(): Command<State>[];
4142
readFile(pathname: string): Promise<StaticFile | null>;
43+
getEntryAssets(): string[];
4244
}
4345

4446
export class ProdBuildCache<State> implements BuildCache<State> {
@@ -54,6 +56,10 @@ export class ProdBuildCache<State> implements BuildCache<State> {
5456
this.clientEntry = snapshot.clientEntry;
5557
}
5658

59+
getEntryAssets(): string[] {
60+
return this.#snapshot.entryAssets;
61+
}
62+
5763
getFsRoutes(): Command<State>[] {
5864
return fsItemsToCommands(this.#snapshot.fsRoutes);
5965
}

packages/fresh/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export const MINUTE = SECOND * 60;
88
export const HOUR = MINUTE * 60;
99
export const DAY = HOUR * 24;
1010
export const WEEK = DAY * 7;
11+
12+
export const ASSET_CACHE_BUST_KEY = "__frsh_c";

packages/fresh/src/dev/dev_build_cache.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
8181
this.clientEntry = getClientEntry(config.buildId);
8282
}
8383

84+
getEntryAssets(): string[] {
85+
return [];
86+
}
87+
8488
getFsRoutes(): Command<State>[] {
8589
return this.#commands;
8690
}
@@ -247,6 +251,10 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
247251
this.root = config.root;
248252
}
249253

254+
getEntryAssets(): string[] {
255+
return [];
256+
}
257+
250258
getFsRoutes(): Command<State>[] {
251259
return [];
252260
}
@@ -357,6 +365,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
357365
},
358366
fsRoutesFiles: this.#fsRoutes.files,
359367
outDir: root,
368+
entryAssets: [],
360369
}),
361370
);
362371

@@ -422,6 +431,7 @@ export async function generateSnapshotServer(
422431
// deno-lint-ignore no-explicit-any
423432
fsRoutesFiles: FsRouteFileNoMod<any>[];
424433
staticFiles: PendingStaticFile[];
434+
entryAssets: string[];
425435
writeSpecifier: (filePath: string) => string;
426436
},
427437
): Promise<string> {
@@ -475,21 +485,13 @@ export async function generateSnapshotServer(
475485

476486
const staticFiles = await Promise.all(
477487
options.staticFiles.map(async (item) => {
478-
const file = await Deno.open(item.filePath);
479-
const hash = item.hash ? item.hash : await hashContent(file.readable);
480-
const url = new URL(item.pathname, "http://localhost");
481-
482-
return {
483-
name: url.pathname,
484-
hash,
485-
filePath: path.isAbsolute(item.filePath)
486-
? path.relative(outDir, item.filePath)
487-
: item.filePath,
488-
contentType: getContentType(item.filePath),
489-
};
488+
return await prepareStaticFile(item, outDir);
490489
}),
491490
);
492491

492+
const entryAssets = options.entryAssets.map((url) => JSON.stringify(url))
493+
.join(",\n");
494+
493495
return `${EDIT_WARNING}
494496
import { IslandPreparer } from "fresh/internal";
495497
${islandImports}
@@ -510,12 +512,34 @@ ${
510512
}
511513
]);
512514
515+
export const entryAssets = [${entryAssets}];
516+
513517
export const fsRoutes = [
514518
${serializedFsRoutes}
515519
];
516520
`.replaceAll(/\n[\n]+/g, "\n\n");
517521
}
518522

523+
export async function prepareStaticFile(
524+
item: PendingStaticFile,
525+
outDir: string,
526+
): Promise<
527+
{ name: string; hash: string; filePath: string; contentType: string }
528+
> {
529+
const file = await Deno.open(item.filePath);
530+
const hash = item.hash ? item.hash : await hashContent(file.readable);
531+
const url = new URL(item.pathname, "http://localhost");
532+
533+
return {
534+
name: url.pathname,
535+
hash,
536+
filePath: path.isAbsolute(item.filePath)
537+
? path.relative(outDir, item.filePath)
538+
: item.filePath,
539+
contentType: getContentType(item.filePath),
540+
};
541+
}
542+
519543
export function generateServerEntry(
520544
options: {
521545
root: string;

0 commit comments

Comments
 (0)