Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Nov 9, 2023
1 parent 4b18db3 commit 134a248
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type Connect,
type Plugin as VitePlugin,
type Manifest as ViteManifest,
type ManifestChunk,
type ResolvedConfig as ResolvedViteConfig,
type ViteDevServer,
type UserConfig as ViteUserConfig,
Expand Down Expand Up @@ -120,61 +121,66 @@ const getHash = (source: BinaryLike, maxLength?: number): string => {

const resolveBuildAssetPaths = (
pluginConfig: ResolvedRemixVitePluginConfig,
manifest: ViteManifest,
viteManifest: ViteManifest,
absoluteFilePath: string
): Manifest["entry"] & { css: string[] } => {
let rootRelativeFilePath = path.relative(
pluginConfig.rootDirectory,
absoluteFilePath
);
let manifestKey = normalizePath(rootRelativeFilePath);
let manifestEntry = manifest[manifestKey];
let entryChunk = viteManifest[manifestKey];

if (!manifestEntry) {
let knownManifestKeys = Object.keys(manifest)
if (!entryChunk) {
let knownManifestKeys = Object.keys(viteManifest)
.map((key) => '"' + key + '"')
.join(", ");
throw new Error(
`No manifest entry found for "${manifestKey}". Known manifest keys: ${knownManifestKeys}`
);
}

let keys = resolveViteManifestDepChunks(manifest, manifestKey);
let entries = [...keys].map((key) => manifest[key]);
let chunks = resolveDependantChunks(viteManifest, entryChunk);

return {
module: `${pluginConfig.publicPath}${manifestEntry.file}`,
module: `${pluginConfig.publicPath}${entryChunk.file}`,
imports:
dedupe(entries.flatMap((e) => e.imports ?? [])).map((imported) => {
return `${pluginConfig.publicPath}${manifest[imported].file}`;
dedupe(chunks.flatMap((e) => e.imports ?? [])).map((imported) => {
return `${pluginConfig.publicPath}${viteManifest[imported].file}`;
}) ?? [],
css:
dedupe(entries.flatMap((e) => e.css ?? [])).map((href) => {
dedupe(chunks.flatMap((e) => e.css ?? [])).map((href) => {
return `${pluginConfig.publicPath}${href}`;
}) ?? [],
};
};

function resolveViteManifestDepChunks(manifest: ViteManifest, base: string) {
let keys = new Set<string>();
function resolveDependantChunks(
viteManifest: ViteManifest,
entryChunk: ManifestChunk
): ManifestChunk[] {
let chunks = new Set<ManifestChunk>();

function dfs(key: string) {
if (keys.has(key)) {
function walk(chunk: ManifestChunk) {
if (chunks.has(chunk)) {
return;
}
keys.add(key);
let chunk = manifest[key];
for (let next of chunk.imports ?? []) {
dfs(next);

if (chunk.imports) {
for (let importKey of chunk.imports) {
walk(viteManifest[importKey]);
}
}

chunks.add(chunk);
}
dfs(base);

return keys;
walk(entryChunk);

return Array.from(chunks);
}

// common utils?
function dedupe(array: any[]) {
function dedupe<T>(array: T[]): T[] {
return [...new Set(array)];
}

Expand Down

0 comments on commit 134a248

Please sign in to comment.