@marko/run 0.8.1 → 0.9.1

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.
@@ -41,6 +41,7 @@ var vite_exports = {};
41
41
  __export(vite_exports, {
42
42
  default: () => markoRun,
43
43
  defaultConfigPlugin: () => defaultConfigPlugin,
44
+ getApi: () => getApi,
44
45
  getAvailablePort: () => getAvailablePort,
45
46
  getPackageData: () => getPackageData,
46
47
  isPortInUse: () => isPortInUse,
@@ -106,7 +107,6 @@ var httpVerbs = [
106
107
  "patch",
107
108
  "options"
108
109
  ];
109
- var serverEntryQuery = "?marko-server-entry";
110
110
  var RoutableFileTypes = {
111
111
  Page: "page",
112
112
  Layout: "layout",
@@ -142,6 +142,11 @@ function getVerbs(route, noAutoHead) {
142
142
  }
143
143
  return [...verbs].sort((a, b) => httpVerbOrder[a] - httpVerbOrder[b]);
144
144
  }
145
+ function getUniqueSortedVerbs(verbs) {
146
+ return [...new Set(verbs)].sort(
147
+ (a, b) => httpVerbOrder[a] - httpVerbOrder[b]
148
+ );
149
+ }
145
150
  function hasVerb(route, verb) {
146
151
  var _a, _b;
147
152
  return verb === "get" && !!route.page || ((_b = (_a = route.handler) == null ? void 0 : _a.verbs) == null ? void 0 : _b.includes(verb)) || verb === "head" && hasVerb(route, "get");
@@ -393,7 +398,7 @@ function renderRouteEntry(route, rootDir) {
393
398
  }
394
399
  if (page) {
395
400
  imports.writeLines(
396
- `import page from "${normalizedRelativePath(rootDir, route.templateFilePath || page.filePath)}${serverEntryQuery}";`
401
+ `import page from "${normalizedRelativePath(rootDir, route.templateFilePath || page.filePath)}";`
397
402
  );
398
403
  }
399
404
  if (meta) {
@@ -526,7 +531,7 @@ function renderRouter(routes, rootDir, runtimeInclude, options = {
526
531
  }
527
532
  for (const route of Object.values(routes.special)) {
528
533
  imports.writeLines(
529
- `import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath || route.page.filePath)}${serverEntryQuery}";`
534
+ `import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath || route.page.filePath)}";`
530
535
  );
531
536
  }
532
537
  writer.writeLines(
@@ -1370,7 +1375,7 @@ async function buildRoutes(sources, outDir) {
1370
1375
  const uniqueRoutes = /* @__PURE__ */ new Map();
1371
1376
  const routes = [];
1372
1377
  const special = {};
1373
- const seenKeys = /* @__PURE__ */ new Map();
1378
+ const seenKeys = /* @__PURE__ */ new Set();
1374
1379
  const middlewares = /* @__PURE__ */ new Set();
1375
1380
  const unusedFiles = /* @__PURE__ */ new Set();
1376
1381
  const currentLayouts = /* @__PURE__ */ new Set();
@@ -1501,12 +1506,13 @@ async function buildRoutes(sources, outDir) {
1501
1506
  );
1502
1507
  }
1503
1508
  uniqueRoutes.set(pathInfo.id, { dir, index: routes.length });
1504
- let key = pathInfo.segments.map(replaceInvalidFilenameChars).concat("route").join("/");
1505
- const keyCount = (seenKeys.get(key) || 0) + 1;
1506
- seenKeys.set(key, keyCount);
1507
- if (keyCount > 1) {
1508
- key += keyCount;
1509
+ const keyBase = pathInfo.segments.map(replaceInvalidFilenameChars).join(".") || "index";
1510
+ let count = 2;
1511
+ let key = keyBase;
1512
+ while (seenKeys.has(key)) {
1513
+ key = keyBase + count++;
1509
1514
  }
1515
+ seenKeys.add(key);
1510
1516
  routes.push({
1511
1517
  index: nextRouteIndex++,
1512
1518
  key,
@@ -1542,7 +1548,7 @@ async function buildRoutes(sources, outDir) {
1542
1548
  }
1543
1549
  }
1544
1550
  function replaceInvalidFilenameChars(str) {
1545
- return str.replace(/[<>:"/\\|?*]+/g, "_");
1551
+ return str.replace(/[<>:"/\\|?*_]+/g, "-");
1546
1552
  }
1547
1553
 
1548
1554
  // src/vite/routes/walk.ts
@@ -1657,7 +1663,7 @@ var HttpVerbColors = {
1657
1663
  function verbColor(verb) {
1658
1664
  return verb in HttpVerbColors ? HttpVerbColors[verb] : import_kleur2.default.gray;
1659
1665
  }
1660
- function logRoutesTable(routes, bundle) {
1666
+ function logRoutesTable(routes, externalRoutes, bundle) {
1661
1667
  const hasMiddleware = routes.list.some((route) => route.middleware.length);
1662
1668
  const hasMeta = routes.list.some((route) => route.meta);
1663
1669
  const headings = ["Method", "Path", "Entry"];
@@ -1691,7 +1697,11 @@ function logRoutesTable(routes, bundle) {
1691
1697
  if (route.page && (verb === "get" || verb === "head")) {
1692
1698
  entryType.push(import_kleur2.default.yellow("page"));
1693
1699
  if (verb === "get") {
1694
- size = prettySize(computeRouteSize(route, bundle));
1700
+ const routeSize = computeRouteSize(
1701
+ route.templateFilePath,
1702
+ bundle
1703
+ ) || [0, 0];
1704
+ size = prettySize(routeSize);
1695
1705
  }
1696
1706
  }
1697
1707
  const row = [verbCell];
@@ -1713,9 +1723,40 @@ function logRoutesTable(routes, bundle) {
1713
1723
  const row = [import_kleur2.default.bold(import_kleur2.default.white("*")), key, import_kleur2.default.yellow("page")];
1714
1724
  hasMiddleware && row.push("");
1715
1725
  hasMeta && row.push("");
1716
- row.push(prettySize(computeRouteSize(route, bundle)));
1726
+ const routeSize = computeRouteSize(route.templateFilePath, bundle) || [
1727
+ 0,
1728
+ 0
1729
+ ];
1730
+ row.push(prettySize(routeSize));
1717
1731
  table.push(row);
1718
1732
  }
1733
+ for (const external of externalRoutes) {
1734
+ for (const route of external.routes) {
1735
+ const verbs = getUniqueSortedVerbs(route.verbs);
1736
+ let firstRow = true;
1737
+ for (const verb of verbs) {
1738
+ let size = "";
1739
+ const verbCell = verbColor(verb)(verb.toUpperCase());
1740
+ const row = [verbCell];
1741
+ if (verbs.length === 1 || firstRow) {
1742
+ const routeSize = computeRouteSize(route.entryFile, bundle);
1743
+ if (routeSize) {
1744
+ size = prettySize(routeSize);
1745
+ }
1746
+ row.push({
1747
+ rowSpan: verbs.length,
1748
+ content: prettyPath(route.path)
1749
+ });
1750
+ firstRow = false;
1751
+ }
1752
+ row.push(import_kleur2.default.magenta(external.name));
1753
+ hasMiddleware && row.push("");
1754
+ hasMeta && row.push("");
1755
+ row.push(size || "");
1756
+ table.push(row);
1757
+ }
1758
+ }
1759
+ }
1719
1760
  if (!table.length) {
1720
1761
  table.push([
1721
1762
  {
@@ -1727,16 +1768,14 @@ function logRoutesTable(routes, bundle) {
1727
1768
  }
1728
1769
  console.log(table.toString());
1729
1770
  }
1730
- function computeRouteSize(route, bundle) {
1731
- const facadeModuleId = route.templateFilePath && `${route.templateFilePath}.html`;
1732
- if (facadeModuleId) {
1771
+ function computeRouteSize(filePath, bundle) {
1772
+ if (filePath) {
1733
1773
  for (const chunk of Object.values(bundle)) {
1734
- if (chunk.type === "chunk" && chunk.isEntry && chunk.facadeModuleId === facadeModuleId) {
1774
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.facadeModuleId === `${filePath}.html`) {
1735
1775
  return computeChunkSize(chunk, bundle);
1736
1776
  }
1737
1777
  }
1738
1778
  }
1739
- return [0, 0];
1740
1779
  }
1741
1780
  function gzipSize(source) {
1742
1781
  return import_node_zlib.default.gzipSync(source, { level: 9 }).length;
@@ -1854,9 +1893,12 @@ function markoRun(opts = {}) {
1854
1893
  let devEntryFilePosix;
1855
1894
  let devServer;
1856
1895
  let routes;
1896
+ let entryTemplates;
1897
+ let entryTemplateImporters;
1857
1898
  let routeData;
1858
1899
  let resolvedConfig;
1859
1900
  let typesFile;
1901
+ const externalRoutes = /* @__PURE__ */ new Set();
1860
1902
  const seenErrors = /* @__PURE__ */ new Set();
1861
1903
  const virtualFiles = /* @__PURE__ */ new Map();
1862
1904
  let times = {
@@ -1890,6 +1932,7 @@ function markoRun(opts = {}) {
1890
1932
  let buildVirtualFilesResult;
1891
1933
  function buildVirtualFiles() {
1892
1934
  return buildVirtualFilesResult ?? (buildVirtualFilesResult = (async () => {
1935
+ var _a, _b;
1893
1936
  virtualFiles.clear();
1894
1937
  if (import_fs4.default.existsSync(resolvedRoutesDir)) {
1895
1938
  routes = await buildRoutes(
@@ -1911,16 +1954,43 @@ function markoRun(opts = {}) {
1911
1954
  console.warn(`Routes directory ${resolvedRoutesDir} does not exist`);
1912
1955
  }
1913
1956
  }
1957
+ entryTemplates = /* @__PURE__ */ new Set();
1958
+ entryTemplateImporters = /* @__PURE__ */ new Set();
1914
1959
  for (const route of routes.list) {
1960
+ const routeEntryPath = route.templateFilePath || ((_a = route.page) == null ? void 0 : _a.filePath);
1961
+ if (routeEntryPath) {
1962
+ entryTemplates.add(normalizePath(routeEntryPath));
1963
+ }
1964
+ for (const middleware of route.middleware) {
1965
+ entryTemplateImporters.add(normalizePath(middleware.filePath));
1966
+ }
1967
+ if (route.handler) {
1968
+ entryTemplateImporters.add(normalizePath(route.handler.filePath));
1969
+ }
1915
1970
  virtualFiles.set(
1916
1971
  import_path6.default.posix.join(root, getRouteVirtualFileName(route)),
1917
1972
  ""
1918
1973
  );
1919
1974
  }
1975
+ for (const route of Object.values(routes.special)) {
1976
+ const routeEntryPath = route.templateFilePath || ((_b = route.page) == null ? void 0 : _b.filePath);
1977
+ if (routeEntryPath) {
1978
+ entryTemplates.add(normalizePath(routeEntryPath));
1979
+ }
1980
+ }
1920
1981
  if (routes.middleware.length) {
1921
1982
  virtualFiles.set(import_path6.default.posix.join(root, MIDDLEWARE_FILENAME), "");
1922
1983
  }
1923
1984
  virtualFiles.set(import_path6.default.posix.join(root, ROUTER_FILENAME), "");
1985
+ for (const externalRoute of externalRoutes) {
1986
+ for (const { entryFile } of externalRoute.routes) {
1987
+ if (/\.marko(\?.*)?$/i.test(entryFile)) {
1988
+ entryTemplates.add(normalizePath(entryFile));
1989
+ } else {
1990
+ entryTemplateImporters.add(normalizePath(entryFile));
1991
+ }
1992
+ }
1993
+ }
1924
1994
  return routes;
1925
1995
  })());
1926
1996
  }
@@ -2027,6 +2097,14 @@ function markoRun(opts = {}) {
2027
2097
  {
2028
2098
  name: `${PLUGIN_NAME_PREFIX}:pre`,
2029
2099
  enforce: "pre",
2100
+ api: {
2101
+ addExternalRoutes(routes2) {
2102
+ externalRoutes.add(routes2);
2103
+ return () => {
2104
+ externalRoutes.delete(routes2);
2105
+ };
2106
+ }
2107
+ },
2030
2108
  async config(config2, env) {
2031
2109
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
2032
2110
  const externalPluginOptions = getExternalPluginOptions(config2);
@@ -2059,6 +2137,10 @@ function markoRun(opts = {}) {
2059
2137
  );
2060
2138
  markoVitePluginOptions.runtimeId = opts.runtimeId;
2061
2139
  markoVitePluginOptions.basePathVar = opts.basePathVar;
2140
+ markoVitePluginOptions.isEntry = (importee, importer) => {
2141
+ var _a2;
2142
+ return entryTemplates.has(importee) || entryTemplateImporters.has(importer) || ((_a2 = adapter == null ? void 0 : adapter.isEntryTemplate) == null ? void 0 : _a2.call(adapter, { template: importee, importer })) || false;
2143
+ };
2062
2144
  resolvedRoutesDir = import_path6.default.resolve(root, routesDir);
2063
2145
  outputDir = import_path6.default.join(root, ((_d = config2.build) == null ? void 0 : _d.outDir) || "dist");
2064
2146
  entryFilesDir = import_path6.default.join(outputDir, ".marko-run");
@@ -2077,23 +2159,9 @@ function markoRun(opts = {}) {
2077
2159
  outDir = import_path6.default.join(outDir, CLIENT_OUT_DIR);
2078
2160
  }
2079
2161
  const defaultRollupOutputOptions = {
2080
- assetFileNames({ name }) {
2081
- if (name && name.indexOf("_marko-virtual_id_") < 0) {
2082
- return `${assetsDir}/${getEntryFileName(name) || "[name]"}-[hash].[ext]`;
2083
- }
2084
- return `${assetsDir}/_[hash].[ext]`;
2085
- },
2162
+ assetFileNames: `${assetsDir}/[name]-[hash].[ext]`,
2086
2163
  entryFileNames(info) {
2087
- let name = getEntryFileName(info.facadeModuleId);
2088
- if (!name) {
2089
- for (const id of info.moduleIds) {
2090
- name = getEntryFileName(id);
2091
- if (name) {
2092
- break;
2093
- }
2094
- }
2095
- }
2096
- return `${assetsDir}/${name || "[name]"}-[hash].js`;
2164
+ return `${assetsDir}/${getEntryFileName(info.name) || "[name]"}-[hash].js`;
2097
2165
  },
2098
2166
  chunkFileNames: isSSRBuild ? `_[hash].js` : `${assetsDir}/_[hash].js`
2099
2167
  };
@@ -2247,16 +2315,14 @@ function markoRun(opts = {}) {
2247
2315
  }
2248
2316
  },
2249
2317
  async resolveId(importee, importer) {
2318
+ let virtualFilePath;
2250
2319
  if (importee === "@marko/run/router") {
2251
2320
  return normalizePath(import_path6.default.resolve(root, ROUTER_FILENAME));
2252
2321
  } else if (importee.endsWith(".marko") && importee.includes(relativeEntryFilesDirPosix)) {
2253
2322
  if (!importee.startsWith(root)) {
2254
2323
  importee = import_path6.default.resolve(root, "." + importee);
2255
2324
  }
2256
- return normalizePath(importee);
2257
- }
2258
- let virtualFilePath;
2259
- if (importee.startsWith(virtualFilePrefix)) {
2325
+ } else if (importee.startsWith(virtualFilePrefix)) {
2260
2326
  virtualFilePath = importee.slice(virtualFilePrefix.length + 1);
2261
2327
  importee = import_path6.default.resolve(root, virtualFilePath);
2262
2328
  } else if (!isBuild && importer && (importer === devEntryFile || normalizePath(importer) === devEntryFilePosix) && importee.startsWith(`/${markoRunFilePrefix}`)) {
@@ -2276,9 +2342,6 @@ function markoRun(opts = {}) {
2276
2342
  }
2277
2343
  },
2278
2344
  async load(id) {
2279
- if (id.endsWith(serverEntryQuery)) {
2280
- id = id.slice(0, -serverEntryQuery.length);
2281
- }
2282
2345
  if (!renderVirtualFilesResult) {
2283
2346
  await renderVirtualFiles(this);
2284
2347
  }
@@ -2327,8 +2390,8 @@ function markoRun(opts = {}) {
2327
2390
  }
2328
2391
  store.write(routeData);
2329
2392
  await ((_a = opts == null ? void 0 : opts.emitRoutes) == null ? void 0 : _a.call(opts, routes.list));
2330
- } else if (process.env.MR_EXPLORER !== "true") {
2331
- logRoutesTable(routes, bundle);
2393
+ } else {
2394
+ logRoutesTable(routes, [...externalRoutes], bundle);
2332
2395
  }
2333
2396
  },
2334
2397
  async closeBundle() {
@@ -2401,10 +2464,22 @@ async function resolveAdapter(root, options, log) {
2401
2464
  log && debug("Using default adapter");
2402
2465
  return module2.default();
2403
2466
  }
2404
- var markoEntryFileRegex = /__marko-run__([^.]+)(?:\.(.+))?\.marko\.([^.]+)$/;
2467
+ var markoEntryFileRegex = /([^/\\]+)\.marko$/;
2405
2468
  function getEntryFileName(file) {
2406
2469
  const match = file && markoEntryFileRegex.exec(file);
2407
- return match ? match[2] || "index" : void 0;
2470
+ return match ? match[1] : void 0;
2471
+ }
2472
+ function getPlugin(config2) {
2473
+ return config2.plugins.find(
2474
+ (plugin) => plugin.name === `${PLUGIN_NAME_PREFIX}:pre`
2475
+ );
2476
+ }
2477
+ function getApi(config2) {
2478
+ const plugin = getPlugin(config2);
2479
+ if (!plugin) {
2480
+ throw new Error("Marko Run vite plugin not found");
2481
+ }
2482
+ return plugin.api;
2408
2483
  }
2409
2484
  function getImporters(module2, fileName, seen = /* @__PURE__ */ new Set()) {
2410
2485
  for (const importer of module2.importers) {
@@ -2567,6 +2642,7 @@ function sleep(ms) {
2567
2642
  // Annotate the CommonJS export names for ESM import in node:
2568
2643
  0 && (module.exports = {
2569
2644
  defaultConfigPlugin,
2645
+ getApi,
2570
2646
  getAvailablePort,
2571
2647
  getPackageData,
2572
2648
  isPortInUse,
@@ -1,4 +1,4 @@
1
- export { default, defaultConfigPlugin, getPackageData } from "./plugin";
2
- export type { Adapter, AdapterConfig, BuiltRoutes, ExplorerData, HttpVerb, Options, PackageData, PathInfo, RoutableFile, RoutableFileType, Route, RouteGenerationData, } from "./types";
1
+ export { default, defaultConfigPlugin, getApi, getPackageData } from "./plugin";
2
+ export type { Adapter, AdapterConfig, BuiltRoutes, ExplorerData, ExternalRoutes, HttpVerb, Options, PackageData, PathInfo, RoutableFile, RoutableFileType, Route, RouteGenerationData, } from "./types";
3
3
  export type { SpawnedServer } from "./utils/server";
4
4
  export { getAvailablePort, isPortInUse, loadEnv, parseEnv, spawnServer, spawnServerWorker, } from "./utils/server";