@marko/run 0.2.7 → 0.2.9

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.
@@ -344,10 +344,7 @@ function matchRoutableFile(filename) {
344
344
  function isSpecialType(type) {
345
345
  return type === RoutableFileTypes.NotFound || type === RoutableFileTypes.Error;
346
346
  }
347
- async function buildRoutes(walk, basePath = "") {
348
- if (basePath) {
349
- basePath = basePath.replace(/^\/+|\/+$/g, "");
350
- }
347
+ async function buildRoutes(sources) {
351
348
  const uniqueRoutes = /* @__PURE__ */ new Map();
352
349
  const routes = [];
353
350
  const special = {};
@@ -357,23 +354,30 @@ async function buildRoutes(walk, basePath = "") {
357
354
  const currentMiddleware = /* @__PURE__ */ new Set();
358
355
  const root = new VDir();
359
356
  const dirStack = [];
360
- let activeDirs = [root];
357
+ let basePath;
358
+ let importPrefix;
359
+ let activeDirs;
360
+ let isBaseDir;
361
361
  let nextFileId = 1;
362
362
  let nextRouteIndex = 1;
363
- let isBaseDir = true;
364
- await walk({
363
+ const walkOptions = {
365
364
  onEnter({ name }) {
366
- if (!name || isBaseDir) {
365
+ const prevDirStackLength = dirStack.length;
366
+ if (isBaseDir) {
367
367
  isBaseDir = false;
368
- return;
368
+ if (!basePath) {
369
+ return;
370
+ }
371
+ name = basePath;
372
+ } else {
373
+ dirStack.push(name);
369
374
  }
370
- dirStack.push(name);
371
375
  const previousDirs = activeDirs;
372
376
  const paths = parseFlatRoute(name);
373
377
  activeDirs = VDir.addPaths(previousDirs, paths);
374
378
  return () => {
375
379
  activeDirs = previousDirs;
376
- dirStack.pop();
380
+ dirStack.length = prevDirStackLength;
377
381
  };
378
382
  },
379
383
  onFile({ name, path: path3 }) {
@@ -401,14 +405,24 @@ async function buildRoutes(walk, basePath = "") {
401
405
  type,
402
406
  filePath: path3,
403
407
  relativePath,
404
- importPath: `${basePath}/${relativePath}`,
408
+ importPath: `${importPrefix}/${relativePath}`,
405
409
  verbs: type === RoutableFileTypes.Page ? ["get"] : void 0
406
410
  };
407
411
  for (const dir of dirs) {
408
412
  dir.addFile(file);
409
413
  }
410
414
  }
411
- });
415
+ };
416
+ if (!Array.isArray(sources)) {
417
+ sources = [sources];
418
+ }
419
+ for (const source of sources) {
420
+ importPrefix = source.importPrefix ? source.importPrefix.replace(/^\/+|\/+$/g, "") : "";
421
+ basePath = source.basePath || "";
422
+ activeDirs = [root];
423
+ isBaseDir = true;
424
+ await source.walker(walkOptions);
425
+ }
412
426
  traverse(root);
413
427
  return {
414
428
  list: routes,
@@ -1056,10 +1070,18 @@ export async function fetch(request, platform) {
1056
1070
  const route = match(request.method, pathname);
1057
1071
  return await invoke(route, request, platform, url);
1058
1072
  } catch (error) {
1059
- const body = import.meta.env.DEV
1060
- ? error.stack || error.message || "Internal Server Error"
1061
- : null;
1062
- return new Response(body, {
1073
+ if (import.meta.env.DEV) {
1074
+ let body;
1075
+ if (error.cause) {
1076
+ body = error.cause.stack || error.cause.message || error.cause;
1077
+ } else {
1078
+ body = error.stack || error.message || "Internal Server Error";
1079
+ }
1080
+ return new Response(body, {
1081
+ status: 500
1082
+ });
1083
+ }
1084
+ return new Response(null, {
1063
1085
  status: 500
1064
1086
  });
1065
1087
  }
@@ -1110,10 +1132,11 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
1110
1132
  writer.writeBlockStart(`switch (${value}.toLowerCase()) {`);
1111
1133
  }
1112
1134
  for (const { key, path: path3, route: route2 } of terminal) {
1135
+ const decodedKey = decodeURIComponent(key);
1113
1136
  if (useSwitch) {
1114
- writer.write(`case '${key}': `, true);
1137
+ writer.write(`case '${decodedKey}': `, true);
1115
1138
  } else {
1116
- writer.write(`if (${value}.toLowerCase() === '${key}') `, true);
1139
+ writer.write(`if (${value}.toLowerCase() === '${decodedKey}') `, true);
1117
1140
  }
1118
1141
  writer.write(
1119
1142
  `return ${renderMatch(verb, route2, path3)}; // ${path3.path}
@@ -1552,7 +1575,9 @@ function logRoutesTable(routes, bundle, options) {
1552
1575
  entryType.push(kleur.yellow("page"));
1553
1576
  size = prettySize(computeRouteSize(getRouteChunkName(route), bundle));
1554
1577
  }
1555
- const row = [kleur.bold(HttpVerbColors[verb](verb.toUpperCase()))];
1578
+ const row = [
1579
+ kleur.bold(HttpVerbColors[verb](verb.toUpperCase()))
1580
+ ];
1556
1581
  if (verbs.length === 1 || firstRow) {
1557
1582
  row.push({ rowSpan: verbs.length, content: prettyPath(path3.path) });
1558
1583
  firstRow = false;
@@ -1590,10 +1615,7 @@ function byteSize(source) {
1590
1615
  }
1591
1616
  function computeChunkSize(chunk, bundle, seen = /* @__PURE__ */ new Set()) {
1592
1617
  if (chunk.type === "asset") {
1593
- return [
1594
- byteSize(chunk.source),
1595
- gzipSize(chunk.source)
1596
- ];
1618
+ return [byteSize(chunk.source), gzipSize(chunk.source)];
1597
1619
  }
1598
1620
  const size = [byteSize(chunk.code), gzipSize(chunk.code)];
1599
1621
  for (const id of chunk.imports) {
@@ -1640,6 +1662,8 @@ var setExternalPluginOptions = (viteConfig, value) => setConfig(viteConfig, Plug
1640
1662
  var getExternalAdapterOptions = (viteConfig) => getConfig(viteConfig, AdapterConfigKey);
1641
1663
 
1642
1664
  // src/vite/plugin.ts
1665
+ import createDebug from "debug";
1666
+ var debug = createDebug("@marko/run");
1643
1667
  var __dirname = path2.dirname(fileURLToPath(import.meta.url));
1644
1668
  var PLUGIN_NAME_PREFIX = "marko-run-vite";
1645
1669
  var POSIX_SEP = "/";
@@ -1694,17 +1718,15 @@ function markoRun(opts = {}) {
1694
1718
  const routerOptions = {
1695
1719
  trailingSlashes: opts.trailingSlashes || "RedirectWithout"
1696
1720
  };
1697
- if (!render) {
1698
- virtualFiles.clear();
1699
- isRendered = false;
1700
- }
1701
1721
  try {
1702
1722
  if (isStale) {
1723
+ virtualFiles.clear();
1724
+ isRendered = false;
1703
1725
  const buildStartTime = performance.now();
1704
- routes = await buildRoutes(
1705
- createFSWalker(resolvedRoutesDir),
1706
- routesDir
1707
- );
1726
+ routes = await buildRoutes({
1727
+ walker: createFSWalker(resolvedRoutesDir),
1728
+ importPrefix: routesDir
1729
+ });
1708
1730
  times.routesBuild = performance.now() - buildStartTime;
1709
1731
  if (!routes.list.length) {
1710
1732
  throw new Error("No routes generated");
@@ -1750,6 +1772,12 @@ function markoRun(opts = {}) {
1750
1772
  times.routesRender = performance.now() - renderStartTime;
1751
1773
  if (render) {
1752
1774
  await writeTypesFile(routes);
1775
+ if (adapter == null ? void 0 : adapter.routesGenerated) {
1776
+ await adapter.routesGenerated(routes, new Map(virtualFiles.entries()), {
1777
+ buildTime: times.routesBuild,
1778
+ renderTime: times.routesRender
1779
+ });
1780
+ }
1753
1781
  if (!isBuild) {
1754
1782
  await ((_a = opts == null ? void 0 : opts.emitRoutes) == null ? void 0 : _a.call(opts, routes.list));
1755
1783
  }
@@ -1990,13 +2018,10 @@ function markoRun(opts = {}) {
1990
2018
  if (virtualFiles.has(importee)) {
1991
2019
  resolved = importee;
1992
2020
  } else if (virtualFilePath) {
1993
- const resolution = await this.resolve(
1994
- path2.resolve(__dirname, "..", virtualFilePath),
1995
- importer,
1996
- {
1997
- skipSelf: true
1998
- }
1999
- );
2021
+ const filePath = path2.resolve(__dirname, "..", virtualFilePath);
2022
+ const resolution = await this.resolve(filePath, importer, {
2023
+ skipSelf: true
2024
+ });
2000
2025
  return resolution;
2001
2026
  }
2002
2027
  return resolved || null;
@@ -2006,7 +2031,7 @@ function markoRun(opts = {}) {
2006
2031
  id = id.slice(0, -serverEntryQuery.length);
2007
2032
  }
2008
2033
  if (virtualFiles.has(id)) {
2009
- if (!isRendered) {
2034
+ if (isStale || !isRendered) {
2010
2035
  await buildVirtualFiles(true);
2011
2036
  }
2012
2037
  return virtualFiles.get(id);
@@ -2050,7 +2075,7 @@ function markoRun(opts = {}) {
2050
2075
  }
2051
2076
  await store.set(routeDataFilename, JSON.stringify(routeData));
2052
2077
  await ((_a = opts == null ? void 0 : opts.emitRoutes) == null ? void 0 : _a.call(opts, routes.list));
2053
- } else {
2078
+ } else if (process.env.MR_EXPLORER !== "true") {
2054
2079
  logRoutesTable(routes, bundle, options);
2055
2080
  }
2056
2081
  },
@@ -2157,19 +2182,19 @@ async function resolveAdapter(root, options, log) {
2157
2182
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
2158
2183
  try {
2159
2184
  const module2 = await import(name);
2160
- log && console.log(
2185
+ log && debug(
2161
2186
  `Using adapter ${name} listed in your package.json dependecies`
2162
2187
  );
2163
2188
  return module2.default();
2164
2189
  } catch (err) {
2165
- log && console.warn(`Attempt to use package '${name}' failed`, err);
2190
+ log && debug(`Attempt to use package '${name}' failed %O`, err);
2166
2191
  }
2167
2192
  }
2168
2193
  }
2169
2194
  }
2170
2195
  const defaultAdapter = "@marko/run/adapter";
2171
2196
  const module = await import(defaultAdapter);
2172
- log && console.log("Using default adapter");
2197
+ log && debug("Using default adapter");
2173
2198
  return module.default();
2174
2199
  }
2175
2200
  var markoEntryFileRegex = /__marko-run__([^.]+)\.(.+)\.marko\.([^.]+)$/;
@@ -3,4 +3,9 @@ import type { Walker } from "./walk";
3
3
  export declare function isRoutableFile(filename: string): boolean;
4
4
  export declare function matchRoutableFile(filename: string): RoutableFileType | null;
5
5
  export declare function isSpecialType(type: RoutableFileType): type is keyof SpecialRoutes;
6
- export declare function buildRoutes(walk: Walker, basePath?: string): Promise<BuiltRoutes>;
6
+ export interface RouteSource {
7
+ walker: Walker;
8
+ importPrefix?: string;
9
+ basePath?: string;
10
+ }
11
+ export declare function buildRoutes(sources: RouteSource | RouteSource[]): Promise<BuiltRoutes>;
@@ -19,6 +19,7 @@ export interface StartDevOptions extends StartOptions {
19
19
  }
20
20
  export interface StartPreviewOptions extends StartOptions {
21
21
  dir: string;
22
+ sourceEntry?: string;
22
23
  }
23
24
  export interface Adapter {
24
25
  readonly name: string;
@@ -30,6 +31,7 @@ export interface Adapter {
30
31
  startPreview?(entry: string | undefined, options: StartPreviewOptions): Promise<SpawnedServer> | SpawnedServer;
31
32
  buildEnd?(config: ResolvedConfig, routes: Route[], builtEntries: string[], sourceEntries: string[]): Promise<void> | void;
32
33
  typeInfo?(writer: (data: string) => void): Promise<string> | string;
34
+ routesGenerated?(routes: BuiltRoutes, virtualFiles: Map<string, string>, data: RouteGenerationData): Promise<void> | void;
33
35
  }
34
36
  export interface RouterOptions {
35
37
  trailingSlashes: "Ignore" | "RedirectWithout" | "RedirectWith" | "RewriteWithout" | "RewriteWith";
@@ -82,3 +84,12 @@ export interface PackageData {
82
84
  dependencies?: Record<string, string>;
83
85
  devDependencies?: Record<string, string>;
84
86
  }
87
+ export interface RouteGenerationData {
88
+ buildTime: number;
89
+ renderTime: number;
90
+ }
91
+ export interface ExplorerData {
92
+ meta: RouteGenerationData;
93
+ routes: Record<string, Route>;
94
+ files: Record<string, string>;
95
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/run",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "The Marko application framework.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/marko-js/run/tree/main/packages/run",
@@ -92,42 +92,47 @@
92
92
  "@marko/fixture-snapshots": "^2.1.7",
93
93
  "@marko/testing-library": "^6.1.2",
94
94
  "@types/glob": "^8.0.1",
95
- "@types/human-format": "^1.0.0",
96
- "@types/jsdom": "^21.1.0",
95
+ "@types/human-format": "^1.0.2",
96
+ "@types/jsdom": "^21.1.4",
97
97
  "@types/mocha": "^9.1.1",
98
- "@types/node": "^20.3.1",
98
+ "@types/node": "^20.8.7",
99
99
  "acorn": "^8.8.0",
100
+ "body-parser": "^1.20.2",
100
101
  "cross-env": "^7.0.3",
101
- "esbuild": "^0.19.0",
102
+ "esbuild": "^0.19.5",
103
+ "express": "^4.18.2",
102
104
  "jsdom": "^21.1.1",
103
- "marko": "^5.23.0",
104
105
  "mocha": "^10.2.0",
105
106
  "mocha-snap": "^4.3.0",
106
- "playwright": "^1.29.1",
107
+ "playwright": "^1.39.0",
107
108
  "prettier": "^2.7.1",
108
109
  "ts-mocha": "^10.0.0",
109
110
  "ts-node": "^10.9.1",
110
111
  "tslib": "^2.5.0",
111
112
  "tsm": "^2.3.0",
112
- "tsx": "^3.9.0",
113
+ "tsx": "^3.14.0",
113
114
  "typescript": "^4.7.4"
114
115
  },
115
116
  "dependencies": {
116
- "@marko/vite": "^3",
117
+ "@marko/run-explorer": "^0.1.0",
118
+ "@marko/vite": "^3.1.4",
117
119
  "browserslist": "^4.22.1",
118
120
  "cli-table3": "^0.6.3",
119
121
  "compression": "^1.7.4",
122
+ "debug": "^4.3.4",
120
123
  "dotenv": "^16.0.3",
121
124
  "esbuild-plugin-browserslist": "^0.9.1",
122
125
  "glob": "^8.1.0",
123
- "human-format": "^1.0.0",
126
+ "human-format": "^1.0.2",
124
127
  "kleur": "^4.1.5",
128
+ "marko": "^5",
125
129
  "parse-node-args": "^1.1.2",
126
130
  "sade": "^1.8.1",
127
131
  "serve-static": "^1.15.0",
128
132
  "strip-ansi": "^7.0.1",
129
- "undici": "^5.20.0",
130
- "vite": "^4.4.0",
133
+ "supports-color": "^9.4.0",
134
+ "undici": "^5.26.3",
135
+ "vite": "^4.5.0",
131
136
  "warp10": "^2.1.0"
132
137
  }
133
138
  }