feat(dev): reduce manifest merge conflict potential (#2020)
Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
25470159b1
commit
536463775c
+3
-126
@@ -1,12 +1,6 @@
|
||||
import {
|
||||
gte,
|
||||
join,
|
||||
normalize,
|
||||
relative,
|
||||
semverParse,
|
||||
walk,
|
||||
WalkEntry,
|
||||
} from "./deps.ts";
|
||||
import { gte, join, relative, semverParse, walk, WalkEntry } from "./deps.ts";
|
||||
export { generate, type Manifest } from "./manifest.ts";
|
||||
import type { Manifest } from "./manifest.ts";
|
||||
import { error } from "./error.ts";
|
||||
const MIN_DENO_VERSION = "1.31.0";
|
||||
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
|
||||
@@ -54,11 +48,6 @@ async function collectDir(
|
||||
}
|
||||
}
|
||||
|
||||
export interface Manifest {
|
||||
routes: string[];
|
||||
islands: string[];
|
||||
}
|
||||
|
||||
const GROUP_REG = /[/\\\\]\((_[^/\\\\]+)\)[/\\\\]/;
|
||||
export async function collect(
|
||||
directory: string,
|
||||
@@ -103,115 +92,3 @@ export async function collect(
|
||||
|
||||
return { routes, islands };
|
||||
}
|
||||
|
||||
/**
|
||||
* Import specifiers must have forward slashes
|
||||
*/
|
||||
function toImportSpecifier(file: string) {
|
||||
let specifier = normalize(file).replace(/\\/g, "/");
|
||||
if (!specifier.startsWith(".")) {
|
||||
specifier = "./" + specifier;
|
||||
}
|
||||
return specifier;
|
||||
}
|
||||
|
||||
async function islandImportLine(
|
||||
file: string,
|
||||
i: number,
|
||||
directory: string,
|
||||
): Promise<string> {
|
||||
const specifier = toImportSpecifier(file);
|
||||
const moduleURL = new URL(`file://${directory}/${specifier}`);
|
||||
const module = await import(moduleURL.href);
|
||||
const entries = Object.entries(module);
|
||||
|
||||
const allExportsAreComponents = entries.every(
|
||||
([, exportedEntity]) => typeof exportedEntity === "function",
|
||||
);
|
||||
|
||||
// if all exports are components, we can use the simple, existing form
|
||||
if (allExportsAreComponents) {
|
||||
return `${JSON.stringify(specifier)}: $$${i},`;
|
||||
}
|
||||
|
||||
// if not all exports are components, list the component exports explicitly
|
||||
const componentExports = entries
|
||||
.filter(([, exportedEntity]) => typeof exportedEntity === "function")
|
||||
.map(([exportName]) => exportName);
|
||||
|
||||
const exportsString = componentExports
|
||||
.map((exp) => `${exp}: $$${i}.${exp}`)
|
||||
.join(", ");
|
||||
|
||||
return `"${specifier}": { ${exportsString} },`;
|
||||
}
|
||||
|
||||
export async function generate(directory: string, manifest: Manifest) {
|
||||
const { routes, islands } = manifest;
|
||||
|
||||
const islandImports = await Promise.all(
|
||||
islands.map((file, i) => islandImportLine(file, i, directory)),
|
||||
);
|
||||
|
||||
const output = `// DO NOT EDIT. This file is generated by Fresh.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running \`dev.ts\`.
|
||||
|
||||
${
|
||||
routes.map((file, i) =>
|
||||
`import * as $${i} from "${toImportSpecifier(file)}";`
|
||||
).join(
|
||||
"\n",
|
||||
)
|
||||
}
|
||||
${
|
||||
islands.map((file, i) =>
|
||||
`import * as $$${i} from "${toImportSpecifier(file)}";`
|
||||
)
|
||||
.join("\n")
|
||||
}
|
||||
import { Manifest } from "$fresh/server.ts";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
${
|
||||
routes.map((file, i) =>
|
||||
`${JSON.stringify(`${toImportSpecifier(file)}`)}: $${i},`
|
||||
)
|
||||
.join("\n ")
|
||||
}
|
||||
},
|
||||
islands: {
|
||||
${islandImports.join("\n ")}
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
} satisfies Manifest;
|
||||
|
||||
export default manifest;
|
||||
`;
|
||||
|
||||
const proc = new Deno.Command(Deno.execPath(), {
|
||||
args: ["fmt", "-"],
|
||||
stdin: "piped",
|
||||
stdout: "piped",
|
||||
stderr: "null",
|
||||
}).spawn();
|
||||
|
||||
const raw = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode(output));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
await raw.pipeTo(proc.stdin);
|
||||
const { stdout } = await proc.output();
|
||||
|
||||
const manifestStr = new TextDecoder().decode(stdout);
|
||||
const manifestPath = join(directory, "./fresh.gen.ts");
|
||||
|
||||
await Deno.writeTextFile(manifestPath, manifestStr);
|
||||
console.log(
|
||||
`%cThe manifest has been generated for ${routes.length} routes and ${islands.length} islands.`,
|
||||
"color: blue; font-weight: bold",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user