@gyoll/builder 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,19 +29,23 @@ async function scanRoutes(routesDir) {
29
29
  const routes = await walkDirectory(routesDir, routesDir);
30
30
  return routes;
31
31
  }
32
- async function walkDirectory(dir, baseDir, segments = []) {
32
+ async function walkDirectory(dir, baseDir, segments = [], parentLayouts = []) {
33
33
  const entries = await import_promises.default.readdir(dir, { withFileTypes: true });
34
34
  const files = entries.filter((e) => e.isFile());
35
35
  const dirs = entries.filter((e) => e.isDirectory());
36
36
  const routeFiles = collectRouteFiles(files, dir);
37
+ const currentLayouts = [...parentLayouts];
38
+ if (routeFiles.layout) {
39
+ currentLayouts.push(import_path.default.relative(baseDir, routeFiles.layout));
40
+ }
37
41
  const routes = [];
38
42
  if (routeFiles.page) {
39
43
  const route = {
40
44
  path: buildRoutePath(segments),
41
45
  component: import_path.default.relative(baseDir, routeFiles.page)
42
46
  };
43
- if (routeFiles.layout) {
44
- route.layout = import_path.default.relative(baseDir, routeFiles.layout);
47
+ if (currentLayouts.length > 0) {
48
+ route.layouts = [...currentLayouts];
45
49
  }
46
50
  if (routeFiles.error) {
47
51
  route.error = import_path.default.relative(baseDir, routeFiles.error);
@@ -54,10 +58,12 @@ async function walkDirectory(dir, baseDir, segments = []) {
54
58
  for (const subdir of dirs) {
55
59
  const segment = parseSegment(subdir.name);
56
60
  const subdirPath = import_path.default.join(dir, subdir.name);
57
- const childRoutes = await walkDirectory(subdirPath, baseDir, [
58
- ...segments,
59
- segment
60
- ]);
61
+ const childRoutes = await walkDirectory(
62
+ subdirPath,
63
+ baseDir,
64
+ [...segments, segment],
65
+ currentLayouts
66
+ );
61
67
  routes.push(...childRoutes);
62
68
  }
63
69
  return routes;
@@ -114,9 +120,27 @@ function buildRoutePath(segments) {
114
120
  }
115
121
  return "/" + filtered.join("/");
116
122
  }
117
- async function generateManifest(routes, outFile) {
123
+ async function generateManifest(routes, outFile, routesDir) {
124
+ const outDir = import_path.default.dirname(outFile);
125
+ const relativeRoutesDir = import_path.default.relative(outDir, routesDir);
126
+ const adjustedRoutes = JSON.stringify(routes, null, 2).replace(/"component": "(.+?)"/g, (match, p1) => {
127
+ const importPath = import_path.default.join(relativeRoutesDir, p1).replace(/\\/g, "/");
128
+ return `"component": () => import("./${importPath}")`;
129
+ }).replace(/"layouts": \[([\s\S]*?)\]/g, (match, layoutsContent) => {
130
+ const transformed = layoutsContent.replace(/"(.+?)"/g, (m, p1) => {
131
+ const importPath = import_path.default.join(relativeRoutesDir, p1).replace(/\\/g, "/");
132
+ return `() => import("./${importPath}")`;
133
+ });
134
+ return `"layouts": [${transformed}]`;
135
+ }).replace(/"error": "(.+?)"/g, (match, p1) => {
136
+ const importPath = import_path.default.join(relativeRoutesDir, p1).replace(/\\/g, "/");
137
+ return `"error": () => import("./${importPath}")`;
138
+ }).replace(/"loading": "(.+?)"/g, (match, p1) => {
139
+ const importPath = import_path.default.join(relativeRoutesDir, p1).replace(/\\/g, "/");
140
+ return `"loading": () => import("./${importPath}")`;
141
+ });
118
142
  const content = `// Auto-generated by @gyoll/builder - DO NOT EDIT
119
- export const routes = ${JSON.stringify(routes, null, 2).replace(/"component": "(.+?)"/g, '"component": () => import("./$1")').replace(/"layout": "(.+?)"/g, '"layout": () => import("./$1")').replace(/"error": "(.+?)"/g, '"error": () => import("./$1")').replace(/"loading": "(.+?)"/g, '"loading": () => import("./$1")')};
143
+ export const routes = ${adjustedRoutes};
120
144
  `;
121
145
  await import_promises.default.writeFile(outFile, content, "utf-8");
122
146
  }
@@ -149,7 +173,7 @@ async function build(options2) {
149
173
  const outFile = import_path2.default.resolve(options2.out);
150
174
  console.log(`Scanning routes in ${routesDir}...`);
151
175
  const routes = await scanRoutes(routesDir);
152
- await generateManifest(routes, outFile);
176
+ await generateManifest(routes, outFile, routesDir);
153
177
  console.log(`\u2713 Generated ${outFile}`);
154
178
  }
155
179
  async function watch(options2) {
@@ -7,19 +7,23 @@ async function scanRoutes(routesDir) {
7
7
  const routes = await walkDirectory(routesDir, routesDir);
8
8
  return routes;
9
9
  }
10
- async function walkDirectory(dir, baseDir, segments = []) {
10
+ async function walkDirectory(dir, baseDir, segments = [], parentLayouts = []) {
11
11
  const entries = await fs.readdir(dir, { withFileTypes: true });
12
12
  const files = entries.filter((e) => e.isFile());
13
13
  const dirs = entries.filter((e) => e.isDirectory());
14
14
  const routeFiles = collectRouteFiles(files, dir);
15
+ const currentLayouts = [...parentLayouts];
16
+ if (routeFiles.layout) {
17
+ currentLayouts.push(path.relative(baseDir, routeFiles.layout));
18
+ }
15
19
  const routes = [];
16
20
  if (routeFiles.page) {
17
21
  const route = {
18
22
  path: buildRoutePath(segments),
19
23
  component: path.relative(baseDir, routeFiles.page)
20
24
  };
21
- if (routeFiles.layout) {
22
- route.layout = path.relative(baseDir, routeFiles.layout);
25
+ if (currentLayouts.length > 0) {
26
+ route.layouts = [...currentLayouts];
23
27
  }
24
28
  if (routeFiles.error) {
25
29
  route.error = path.relative(baseDir, routeFiles.error);
@@ -32,10 +36,12 @@ async function walkDirectory(dir, baseDir, segments = []) {
32
36
  for (const subdir of dirs) {
33
37
  const segment = parseSegment(subdir.name);
34
38
  const subdirPath = path.join(dir, subdir.name);
35
- const childRoutes = await walkDirectory(subdirPath, baseDir, [
36
- ...segments,
37
- segment
38
- ]);
39
+ const childRoutes = await walkDirectory(
40
+ subdirPath,
41
+ baseDir,
42
+ [...segments, segment],
43
+ currentLayouts
44
+ );
39
45
  routes.push(...childRoutes);
40
46
  }
41
47
  return routes;
@@ -92,9 +98,27 @@ function buildRoutePath(segments) {
92
98
  }
93
99
  return "/" + filtered.join("/");
94
100
  }
95
- async function generateManifest(routes, outFile) {
101
+ async function generateManifest(routes, outFile, routesDir) {
102
+ const outDir = path.dirname(outFile);
103
+ const relativeRoutesDir = path.relative(outDir, routesDir);
104
+ const adjustedRoutes = JSON.stringify(routes, null, 2).replace(/"component": "(.+?)"/g, (match, p1) => {
105
+ const importPath = path.join(relativeRoutesDir, p1).replace(/\\/g, "/");
106
+ return `"component": () => import("./${importPath}")`;
107
+ }).replace(/"layouts": \[([\s\S]*?)\]/g, (match, layoutsContent) => {
108
+ const transformed = layoutsContent.replace(/"(.+?)"/g, (m, p1) => {
109
+ const importPath = path.join(relativeRoutesDir, p1).replace(/\\/g, "/");
110
+ return `() => import("./${importPath}")`;
111
+ });
112
+ return `"layouts": [${transformed}]`;
113
+ }).replace(/"error": "(.+?)"/g, (match, p1) => {
114
+ const importPath = path.join(relativeRoutesDir, p1).replace(/\\/g, "/");
115
+ return `"error": () => import("./${importPath}")`;
116
+ }).replace(/"loading": "(.+?)"/g, (match, p1) => {
117
+ const importPath = path.join(relativeRoutesDir, p1).replace(/\\/g, "/");
118
+ return `"loading": () => import("./${importPath}")`;
119
+ });
96
120
  const content = `// Auto-generated by @gyoll/builder - DO NOT EDIT
97
- export const routes = ${JSON.stringify(routes, null, 2).replace(/"component": "(.+?)"/g, '"component": () => import("./$1")').replace(/"layout": "(.+?)"/g, '"layout": () => import("./$1")').replace(/"error": "(.+?)"/g, '"error": () => import("./$1")').replace(/"loading": "(.+?)"/g, '"loading": () => import("./$1")')};
121
+ export const routes = ${adjustedRoutes};
98
122
  `;
99
123
  await fs.writeFile(outFile, content, "utf-8");
100
124
  }
@@ -127,7 +151,7 @@ async function build(options2) {
127
151
  const outFile = path2.resolve(options2.out);
128
152
  console.log(`Scanning routes in ${routesDir}...`);
129
153
  const routes = await scanRoutes(routesDir);
130
- await generateManifest(routes, outFile);
154
+ await generateManifest(routes, outFile, routesDir);
131
155
  console.log(`\u2713 Generated ${outFile}`);
132
156
  }
133
157
  async function watch(options2) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gyoll/builder",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "CLI tool for generating route manifests from file-system structure",
6
6
  "bin": {