@marko/run 0.9.2 → 0.9.4

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.
@@ -256,15 +256,22 @@ function normalizedRelativePath(from, to) {
256
256
  const relativePath = normalizePath(path2.relative(from, to));
257
257
  return relativePath.startsWith(".") ? relativePath : "./" + relativePath;
258
258
  }
259
- function renderRouteTemplate(route, rootDir) {
259
+ function renderRouteTemplate(route, markoApi) {
260
260
  if (!route.page) {
261
261
  throw new Error(`Route ${route.key} has no page to render`);
262
262
  }
263
263
  if (!route.templateFilePath) {
264
264
  throw new Error(`Route ${route.key} has no template file path`);
265
265
  }
266
- return renderEntryTemplate(
267
- normalizedRelativePath(rootDir, route.templateFilePath),
266
+ const writer = createStringWriter();
267
+ if (markoApi) {
268
+ writer.writeLines(`<!-- use ${markoApi} -->
269
+ `);
270
+ }
271
+ writer.branch("imports");
272
+ writer.writeLines("");
273
+ writeEntryTemplateTag(
274
+ writer,
268
275
  [...route.layouts, route.page].map(
269
276
  (file) => normalizedRelativePath(
270
277
  path2.dirname(route.templateFilePath),
@@ -273,16 +280,6 @@ function renderRouteTemplate(route, rootDir) {
273
280
  ),
274
281
  route.key === RoutableFileTypes.Error ? ["error"] : []
275
282
  );
276
- }
277
- function renderEntryTemplate(name, files, pageInputs = []) {
278
- if (!files.length) {
279
- throw new Error(`Invalid argument - 'files' cannot be empty`);
280
- }
281
- const writer = createStringWriter();
282
- writer.writeLines(`// ${name}`);
283
- writer.branch("imports");
284
- writer.writeLines("");
285
- writeEntryTemplateTag(writer, files, pageInputs);
286
283
  return writer.end();
287
284
  }
288
285
  function writeEntryTemplateTag(writer, [file, ...rest], pageInputs, index = 1) {
@@ -310,7 +307,6 @@ function renderRouteEntry(route, rootDir) {
310
307
  );
311
308
  }
312
309
  const writer = createStringWriter();
313
- writer.writeLines(`// ${virtualFilePrefix}${getRouteVirtualFileName(route)}`);
314
310
  const imports = writer.branch("imports");
315
311
  const runtimeImports = [];
316
312
  if (handler) {
@@ -469,7 +465,6 @@ function renderRouter(routes, rootDir, runtimeInclude, options = {
469
465
  const writer = createStringWriter();
470
466
  const hasErrorPage = Boolean(routes.special[RoutableFileTypes.Error]);
471
467
  const hasNotFoundPage = Boolean(routes.special[RoutableFileTypes.NotFound]);
472
- writer.writeLines(`// @marko/run/router`);
473
468
  const imports = writer.branch("imports");
474
469
  if (runtimeInclude) {
475
470
  imports.writeLines(`import "${normalizePath(runtimeInclude)}";`);
@@ -779,9 +774,6 @@ function renderMatch(verb, route, path7, pathIndex) {
779
774
  }
780
775
  function renderMiddleware(middleware, rootDir) {
781
776
  const writer = createStringWriter();
782
- writer.writeLines(
783
- `// ${virtualFilePrefix}/${markoRunFilePrefix}middleware.js`
784
- );
785
777
  const imports = writer.branch("imports");
786
778
  imports.writeLines(
787
779
  `import { normalize } from "${virtualFilePrefix}/runtime/internal";`
@@ -807,9 +799,6 @@ function stripTsExtension(path7) {
807
799
  }
808
800
  return path7;
809
801
  }
810
- function decodePath(path7) {
811
- return path7;
812
- }
813
802
  async function renderRouteTypeInfo(routes, outDir, adapter) {
814
803
  var _a, _b, _c, _d;
815
804
  const writer = createStringWriter();
@@ -862,7 +851,7 @@ async function renderRouteTypeInfo(routes, outDir, adapter) {
862
851
  }
863
852
  routeDefinition += " }";
864
853
  }
865
- const pathType = `"${decodePath(route.path.path)}"`;
854
+ const pathType = `"${route.path.path}"`;
866
855
  routeType += routeType ? " | " + pathType : pathType;
867
856
  routesWriter.writeLines(`${pathType}: ${routeDefinition};`);
868
857
  for (const file of [route.handler, route.page]) {
@@ -1603,7 +1592,6 @@ var getExternalAdapterOptions = (viteConfig) => getConfig(viteConfig, AdapterCon
1603
1592
 
1604
1593
  // src/vite/utils/log.ts
1605
1594
  import zlib from "node:zlib";
1606
- import { Blob } from "buffer";
1607
1595
  import Table from "cli-table3";
1608
1596
  import format from "human-format";
1609
1597
  import kleur2 from "kleur";
@@ -1854,6 +1842,7 @@ function markoRun(opts = {}) {
1854
1842
  let routeData;
1855
1843
  let resolvedConfig;
1856
1844
  let typesFile;
1845
+ let runtimeInclude;
1857
1846
  const externalRoutes = /* @__PURE__ */ new Set();
1858
1847
  const seenErrors = /* @__PURE__ */ new Set();
1859
1848
  const virtualFiles = /* @__PURE__ */ new Map();
@@ -1861,6 +1850,13 @@ function markoRun(opts = {}) {
1861
1850
  routesBuild: 0,
1862
1851
  routesRender: 0
1863
1852
  };
1853
+ async function loadModule(context, id) {
1854
+ if ("transformRequest" in context.environment) {
1855
+ await context.environment.transformRequest(id);
1856
+ return context.getModuleInfo(id);
1857
+ }
1858
+ return await context.load({ id });
1859
+ }
1864
1860
  async function getExportsFromFile(context, filePath) {
1865
1861
  if (devServer) {
1866
1862
  const result2 = await devServer.transformRequest(filePath, { ssr: false });
@@ -1872,6 +1868,17 @@ function markoRun(opts = {}) {
1872
1868
  });
1873
1869
  return result.exports || [];
1874
1870
  }
1871
+ let routeMarkoApiCache;
1872
+ async function getMarkoApiForRoute(context, route) {
1873
+ var _a, _b;
1874
+ routeMarkoApiCache ?? (routeMarkoApiCache = /* @__PURE__ */ new Map());
1875
+ if (!routeMarkoApiCache.has(route)) {
1876
+ const markoAPI = route.templateFilePath && ((_b = (_a = await loadModule(context, normalizePath(route.layouts[0].filePath))) == null ? void 0 : _a.meta) == null ? void 0 : _b.markoAPI);
1877
+ routeMarkoApiCache.set(route, markoAPI);
1878
+ return markoAPI;
1879
+ }
1880
+ return routeMarkoApiCache.get(route);
1881
+ }
1875
1882
  async function writeTypesFile(routes2) {
1876
1883
  if (routes2 && (tsConfigExists ?? (tsConfigExists = await globFileExists(
1877
1884
  root,
@@ -1984,7 +1991,10 @@ function markoRun(opts = {}) {
1984
1991
  });
1985
1992
  fs3.writeFileSync(
1986
1993
  route.templateFilePath,
1987
- renderRouteTemplate(route, root)
1994
+ renderRouteTemplate(
1995
+ route,
1996
+ await getMarkoApiForRoute(context, route)
1997
+ )
1988
1998
  );
1989
1999
  }
1990
2000
  virtualFiles.set(
@@ -1999,7 +2009,10 @@ function markoRun(opts = {}) {
1999
2009
  });
2000
2010
  fs3.writeFileSync(
2001
2011
  route.templateFilePath,
2002
- renderRouteTemplate(route, root)
2012
+ renderRouteTemplate(
2013
+ route,
2014
+ await getMarkoApiForRoute(context, route)
2015
+ )
2003
2016
  );
2004
2017
  }
2005
2018
  }
@@ -2016,7 +2029,7 @@ function markoRun(opts = {}) {
2016
2029
  renderMiddleware(routes2.middleware, root)
2017
2030
  );
2018
2031
  }
2019
- const runtimeInclude = await ((_a = adapter == null ? void 0 : adapter.runtimeInclude) == null ? void 0 : _a.call(adapter));
2032
+ runtimeInclude = await ((_a = adapter == null ? void 0 : adapter.runtimeInclude) == null ? void 0 : _a.call(adapter));
2020
2033
  virtualFiles.set(
2021
2034
  path6.posix.join(root, ROUTER_FILENAME),
2022
2035
  renderRouter(routes2, root, runtimeInclude, {
@@ -2220,10 +2233,11 @@ function markoRun(opts = {}) {
2220
2233
  const routableFileType = matchRoutableFile(
2221
2234
  path6.parse(filename).base
2222
2235
  );
2223
- if (filename.startsWith(resolvedRoutesDir) && routableFileType) {
2224
- if (type === "add" || type === "unlink" || type === "change" && (routableFileType === RoutableFileTypes.Handler || routableFileType === RoutableFileTypes.Middleware)) {
2236
+ if (filename.startsWith(resolvedRoutesDir) && routableFileType || filename === runtimeInclude) {
2237
+ if (type === "add" || type === "unlink" || type === "change" && (routableFileType === RoutableFileTypes.Handler || routableFileType === RoutableFileTypes.Middleware || filename === runtimeInclude)) {
2225
2238
  buildVirtualFilesResult = void 0;
2226
2239
  renderVirtualFilesResult = void 0;
2240
+ routeMarkoApiCache = void 0;
2227
2241
  const module = devServer.moduleGraph.getModuleById(filename);
2228
2242
  const importers = module && getImporters(module, filename);
2229
2243
  if (importers == null ? void 0 : importers.size) {
@@ -2297,8 +2311,7 @@ function markoRun(opts = {}) {
2297
2311
  await renderVirtualFiles(this);
2298
2312
  }
2299
2313
  if (virtualFiles.has(id)) {
2300
- const file = virtualFiles.get(id);
2301
- return file;
2314
+ return virtualFiles.get(id);
2302
2315
  } else if (!id.startsWith(entryFilesDirPosix) && /[/\\]__marko-run__[^?/\\]+\.(js|marko)$/.exec(id)) {
2303
2316
  return "";
2304
2317
  }
@@ -2500,9 +2513,12 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
2500
2513
  windowsHide: true,
2501
2514
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
2502
2515
  });
2503
- const close = () => {
2516
+ const close = async () => {
2504
2517
  proc.unref();
2505
2518
  proc.kill();
2519
+ if (!await waitForExit(proc, 500)) {
2520
+ proc.kill("SIGKILL");
2521
+ }
2506
2522
  };
2507
2523
  try {
2508
2524
  await Promise.race([waitForError(proc, port), waitForServer(port, wait)]);
@@ -2550,6 +2566,15 @@ async function spawnServerWorker(module, args = [], port = 0, env, wait = true)
2550
2566
  cluster.settings.execArgv = originalArgs;
2551
2567
  }
2552
2568
  }
2569
+ async function waitForExit(proc, wait = 0) {
2570
+ if (proc.exitCode !== null) return;
2571
+ return await new Promise((resolve) => {
2572
+ proc.once("exit", resolve), proc.once("close", resolve);
2573
+ if (wait) {
2574
+ setTimeout(resolve, wait, null);
2575
+ }
2576
+ });
2577
+ }
2553
2578
  async function waitForError(proc, port) {
2554
2579
  return new Promise((_, reject) => {
2555
2580
  proc.once("error", reject);
@@ -11,4 +11,4 @@ export interface WalkOptions {
11
11
  export type Walker = (options: WalkOptions) => Promise<void>;
12
12
  export declare function createFSWalker(dir: string): Walker;
13
13
  export type TestFileTree = [string, (string | TestFileTree)[]];
14
- export declare function createTestWalker(dir: TestFileTree, root: string): Walker;
14
+ export declare function createTestWalker(dir: TestFileTree): Walker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/run",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "The Marko application framework.",
5
5
  "keywords": [
6
6
  "marko"
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "dependencies": {
57
57
  "@marko/run-explorer": "^2.0.1",
58
- "@marko/vite": "^5.3.2",
58
+ "@marko/vite": "^5.4.2",
59
59
  "browserslist": "^4.24.4",
60
60
  "cli-table3": "^0.6.5",
61
61
  "compression": "^1.8.0",