Skip to content

Commit

Permalink
Fix routes generation on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune committed Jul 31, 2024
1 parent 3c195a5 commit 03c979d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: true
steps:
- name: Clone repository
Expand Down
47 changes: 29 additions & 18 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { walk } from "@std/fs/walk";
import { ensureDir } from "@std/fs/ensure-dir";
import { exists } from "@std/fs/exists";
import * as log from "@std/log";
import * as path from "@std/path/posix";
import * as path from "@std/path";
import * as esbuild from "esbuild";
import { denoPlugins } from "@luca/esbuild-deno-loader";

Expand Down Expand Up @@ -129,15 +129,18 @@ async function generateRoutes(routesUrl: string): Promise<Route> {
const rootRoute = { name: "", children: {} } as Route;

for await (
const entry of walk(routesUrl, {
const entry of walk(path.resolve(routesUrl), {
includeDirs: false,
match: [ROUTE_PATH],
skip: [TEST_PATH, IGNORE_PATH],
})
) {
const parsedPath = path.parse(entry.path);
const { name, ext, dir } = parsedPath;
const relativePath = path.relative(routesUrl, dir.replaceAll("\\", "/"));
const relativePath = path.relative(
routesUrl,
dir,
);
const layers = relativePath.length ? relativePath.split("/") : [];

let parentRoute = rootRoute;
Expand All @@ -161,8 +164,8 @@ async function generateRoutes(routesUrl: string): Promise<Route> {

function lazyImportLine(routeId: number, routePath: string, filePath: string) {
return `const $${routeId} = lazy(${
routePath ? `"/${routePath}", ` : ""
}() => import("./${filePath}"));`;
routePath ? `"/${routePath.replaceAll("\\", "/")}", ` : ""
}() => import("./${filePath.replaceAll("\\", "/")}"));`;
}

async function routeFileData(
Expand All @@ -180,7 +183,7 @@ async function routeFileData(
importLines.push(
lazyImportLine(
routeId,
relativePath,
path.join(relativePath),
path.join(relativePath, routeId === 0 ? "" : "../", file.react),
),
);
Expand All @@ -192,13 +195,15 @@ async function routeFileData(
importLines.push(
lazyImportLine(
routeId,
relativePath,
path.join(relativePath),
path.join(relativePath, main.react),
),
);
} else {
const mainPath = path.join(relativePath, main.react);
const mainMod = await Deno.readTextFile(path.join(routesUrl, mainPath));
const mainMod = await Deno.readTextFile(
path.join(routesUrl, mainPath),
);
importLines.push(`import * as $${routeId++} from "./${mainPath}";`);
if (ERROR_FALLBACK_EXPORT.test(mainMod)) {
importLines.push(
Expand Down Expand Up @@ -291,13 +296,13 @@ async function routeFileData(

function routeImportLines(routeId: number, relativePath: string) {
return [
`import "./${relativePath}";`,
`import * as $${routeId} from "./${relativePath}";`,
`import "./${relativePath.replaceAll("\\", "/")}";`,
`import * as $${routeId} from "./${relativePath.replaceAll("\\", "/")}";`,
];
}

function routerImportLine(routeId: number, relativePath: string) {
return `import $${routeId} from "./${relativePath}";`;
return `import $${routeId} from "./${relativePath.replaceAll("\\", "/")}";`;
}

function routerFileData(
Expand Down Expand Up @@ -572,12 +577,18 @@ export function getBuildOptions(

function postBuild(success: boolean, error: Error | null) {
performance.mark("buildEnd");
const duration =
performance.measure("build", "buildStart", "buildEnd").duration;
const routesDuration =
performance.measure("esbuild", "routesStart", "esbuildStart").duration;
const esbuildDuration =
performance.measure("esbuild", "esbuildStart", "buildEnd").duration;
let duration: number = 0;
let routesDuration: number | null = null;
let esbuildDuration: number | null = null;
try {
duration = performance.measure("build", "buildStart", "buildEnd").duration;
routesDuration =
performance.measure("esbuild", "routesStart", "esbuildStart").duration;
esbuildDuration =
performance.measure("esbuild", "esbuildStart", "buildEnd").duration;
} catch {
// Ignore measurement errors
}
const message = `Build ${success ? "completed" : "failed"} in ${
Math.round(duration)
} ms`;
Expand Down Expand Up @@ -609,7 +620,7 @@ export async function build(options: BuildOptions = {}): Promise<boolean> {
try {
const { workingDirectory, routesUrl: _routesUrl, publicUrl } =
getBuildOptions(options);
routesUrl = _routesUrl;
routesUrl = path.resolve(_routesUrl);
const entryPoint = path.join(routesUrl, "./_main.tsx");
let configPath = options.configPath ??
path.join(workingDirectory!, "deno.json");
Expand Down

0 comments on commit 03c979d

Please sign in to comment.