@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.
@@ -79,7 +79,6 @@ var httpVerbs = [
79
79
  "patch",
80
80
  "options"
81
81
  ];
82
- var serverEntryQuery = "?marko-server-entry";
83
82
  var RoutableFileTypes = {
84
83
  Page: "page",
85
84
  Layout: "layout",
@@ -115,6 +114,11 @@ function getVerbs(route, noAutoHead) {
115
114
  }
116
115
  return [...verbs].sort((a, b) => httpVerbOrder[a] - httpVerbOrder[b]);
117
116
  }
117
+ function getUniqueSortedVerbs(verbs) {
118
+ return [...new Set(verbs)].sort(
119
+ (a, b) => httpVerbOrder[a] - httpVerbOrder[b]
120
+ );
121
+ }
118
122
  function hasVerb(route, verb) {
119
123
  var _a, _b;
120
124
  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");
@@ -366,7 +370,7 @@ function renderRouteEntry(route, rootDir) {
366
370
  }
367
371
  if (page) {
368
372
  imports.writeLines(
369
- `import page from "${normalizedRelativePath(rootDir, route.templateFilePath || page.filePath)}${serverEntryQuery}";`
373
+ `import page from "${normalizedRelativePath(rootDir, route.templateFilePath || page.filePath)}";`
370
374
  );
371
375
  }
372
376
  if (meta) {
@@ -499,7 +503,7 @@ function renderRouter(routes, rootDir, runtimeInclude, options = {
499
503
  }
500
504
  for (const route of Object.values(routes.special)) {
501
505
  imports.writeLines(
502
- `import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath || route.page.filePath)}${serverEntryQuery}";`
506
+ `import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath || route.page.filePath)}";`
503
507
  );
504
508
  }
505
509
  writer.writeLines(
@@ -1343,7 +1347,7 @@ async function buildRoutes(sources, outDir) {
1343
1347
  const uniqueRoutes = /* @__PURE__ */ new Map();
1344
1348
  const routes = [];
1345
1349
  const special = {};
1346
- const seenKeys = /* @__PURE__ */ new Map();
1350
+ const seenKeys = /* @__PURE__ */ new Set();
1347
1351
  const middlewares = /* @__PURE__ */ new Set();
1348
1352
  const unusedFiles = /* @__PURE__ */ new Set();
1349
1353
  const currentLayouts = /* @__PURE__ */ new Set();
@@ -1474,12 +1478,13 @@ async function buildRoutes(sources, outDir) {
1474
1478
  );
1475
1479
  }
1476
1480
  uniqueRoutes.set(pathInfo.id, { dir, index: routes.length });
1477
- let key = pathInfo.segments.map(replaceInvalidFilenameChars).concat("route").join("/");
1478
- const keyCount = (seenKeys.get(key) || 0) + 1;
1479
- seenKeys.set(key, keyCount);
1480
- if (keyCount > 1) {
1481
- key += keyCount;
1481
+ const keyBase = pathInfo.segments.map(replaceInvalidFilenameChars).join(".") || "index";
1482
+ let count = 2;
1483
+ let key = keyBase;
1484
+ while (seenKeys.has(key)) {
1485
+ key = keyBase + count++;
1482
1486
  }
1487
+ seenKeys.add(key);
1483
1488
  routes.push({
1484
1489
  index: nextRouteIndex++,
1485
1490
  key,
@@ -1515,7 +1520,7 @@ async function buildRoutes(sources, outDir) {
1515
1520
  }
1516
1521
  }
1517
1522
  function replaceInvalidFilenameChars(str) {
1518
- return str.replace(/[<>:"/\\|?*]+/g, "_");
1523
+ return str.replace(/[<>:"/\\|?*_]+/g, "-");
1519
1524
  }
1520
1525
 
1521
1526
  // src/vite/routes/walk.ts
@@ -1631,7 +1636,7 @@ var HttpVerbColors = {
1631
1636
  function verbColor(verb) {
1632
1637
  return verb in HttpVerbColors ? HttpVerbColors[verb] : kleur2.gray;
1633
1638
  }
1634
- function logRoutesTable(routes, bundle) {
1639
+ function logRoutesTable(routes, externalRoutes, bundle) {
1635
1640
  const hasMiddleware = routes.list.some((route) => route.middleware.length);
1636
1641
  const hasMeta = routes.list.some((route) => route.meta);
1637
1642
  const headings = ["Method", "Path", "Entry"];
@@ -1665,7 +1670,11 @@ function logRoutesTable(routes, bundle) {
1665
1670
  if (route.page && (verb === "get" || verb === "head")) {
1666
1671
  entryType.push(kleur2.yellow("page"));
1667
1672
  if (verb === "get") {
1668
- size = prettySize(computeRouteSize(route, bundle));
1673
+ const routeSize = computeRouteSize(
1674
+ route.templateFilePath,
1675
+ bundle
1676
+ ) || [0, 0];
1677
+ size = prettySize(routeSize);
1669
1678
  }
1670
1679
  }
1671
1680
  const row = [verbCell];
@@ -1687,9 +1696,40 @@ function logRoutesTable(routes, bundle) {
1687
1696
  const row = [kleur2.bold(kleur2.white("*")), key, kleur2.yellow("page")];
1688
1697
  hasMiddleware && row.push("");
1689
1698
  hasMeta && row.push("");
1690
- row.push(prettySize(computeRouteSize(route, bundle)));
1699
+ const routeSize = computeRouteSize(route.templateFilePath, bundle) || [
1700
+ 0,
1701
+ 0
1702
+ ];
1703
+ row.push(prettySize(routeSize));
1691
1704
  table.push(row);
1692
1705
  }
1706
+ for (const external of externalRoutes) {
1707
+ for (const route of external.routes) {
1708
+ const verbs = getUniqueSortedVerbs(route.verbs);
1709
+ let firstRow = true;
1710
+ for (const verb of verbs) {
1711
+ let size = "";
1712
+ const verbCell = verbColor(verb)(verb.toUpperCase());
1713
+ const row = [verbCell];
1714
+ if (verbs.length === 1 || firstRow) {
1715
+ const routeSize = computeRouteSize(route.entryFile, bundle);
1716
+ if (routeSize) {
1717
+ size = prettySize(routeSize);
1718
+ }
1719
+ row.push({
1720
+ rowSpan: verbs.length,
1721
+ content: prettyPath(route.path)
1722
+ });
1723
+ firstRow = false;
1724
+ }
1725
+ row.push(kleur2.magenta(external.name));
1726
+ hasMiddleware && row.push("");
1727
+ hasMeta && row.push("");
1728
+ row.push(size || "");
1729
+ table.push(row);
1730
+ }
1731
+ }
1732
+ }
1693
1733
  if (!table.length) {
1694
1734
  table.push([
1695
1735
  {
@@ -1701,16 +1741,14 @@ function logRoutesTable(routes, bundle) {
1701
1741
  }
1702
1742
  console.log(table.toString());
1703
1743
  }
1704
- function computeRouteSize(route, bundle) {
1705
- const facadeModuleId = route.templateFilePath && `${route.templateFilePath}.html`;
1706
- if (facadeModuleId) {
1744
+ function computeRouteSize(filePath, bundle) {
1745
+ if (filePath) {
1707
1746
  for (const chunk of Object.values(bundle)) {
1708
- if (chunk.type === "chunk" && chunk.isEntry && chunk.facadeModuleId === facadeModuleId) {
1747
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.facadeModuleId === `${filePath}.html`) {
1709
1748
  return computeChunkSize(chunk, bundle);
1710
1749
  }
1711
1750
  }
1712
1751
  }
1713
- return [0, 0];
1714
1752
  }
1715
1753
  function gzipSize(source) {
1716
1754
  return zlib.gzipSync(source, { level: 9 }).length;
@@ -1828,9 +1866,12 @@ function markoRun(opts = {}) {
1828
1866
  let devEntryFilePosix;
1829
1867
  let devServer;
1830
1868
  let routes;
1869
+ let entryTemplates;
1870
+ let entryTemplateImporters;
1831
1871
  let routeData;
1832
1872
  let resolvedConfig;
1833
1873
  let typesFile;
1874
+ const externalRoutes = /* @__PURE__ */ new Set();
1834
1875
  const seenErrors = /* @__PURE__ */ new Set();
1835
1876
  const virtualFiles = /* @__PURE__ */ new Map();
1836
1877
  let times = {
@@ -1864,6 +1905,7 @@ function markoRun(opts = {}) {
1864
1905
  let buildVirtualFilesResult;
1865
1906
  function buildVirtualFiles() {
1866
1907
  return buildVirtualFilesResult ?? (buildVirtualFilesResult = (async () => {
1908
+ var _a, _b;
1867
1909
  virtualFiles.clear();
1868
1910
  if (fs3.existsSync(resolvedRoutesDir)) {
1869
1911
  routes = await buildRoutes(
@@ -1885,16 +1927,43 @@ function markoRun(opts = {}) {
1885
1927
  console.warn(`Routes directory ${resolvedRoutesDir} does not exist`);
1886
1928
  }
1887
1929
  }
1930
+ entryTemplates = /* @__PURE__ */ new Set();
1931
+ entryTemplateImporters = /* @__PURE__ */ new Set();
1888
1932
  for (const route of routes.list) {
1933
+ const routeEntryPath = route.templateFilePath || ((_a = route.page) == null ? void 0 : _a.filePath);
1934
+ if (routeEntryPath) {
1935
+ entryTemplates.add(normalizePath(routeEntryPath));
1936
+ }
1937
+ for (const middleware of route.middleware) {
1938
+ entryTemplateImporters.add(normalizePath(middleware.filePath));
1939
+ }
1940
+ if (route.handler) {
1941
+ entryTemplateImporters.add(normalizePath(route.handler.filePath));
1942
+ }
1889
1943
  virtualFiles.set(
1890
1944
  path6.posix.join(root, getRouteVirtualFileName(route)),
1891
1945
  ""
1892
1946
  );
1893
1947
  }
1948
+ for (const route of Object.values(routes.special)) {
1949
+ const routeEntryPath = route.templateFilePath || ((_b = route.page) == null ? void 0 : _b.filePath);
1950
+ if (routeEntryPath) {
1951
+ entryTemplates.add(normalizePath(routeEntryPath));
1952
+ }
1953
+ }
1894
1954
  if (routes.middleware.length) {
1895
1955
  virtualFiles.set(path6.posix.join(root, MIDDLEWARE_FILENAME), "");
1896
1956
  }
1897
1957
  virtualFiles.set(path6.posix.join(root, ROUTER_FILENAME), "");
1958
+ for (const externalRoute of externalRoutes) {
1959
+ for (const { entryFile } of externalRoute.routes) {
1960
+ if (/\.marko(\?.*)?$/i.test(entryFile)) {
1961
+ entryTemplates.add(normalizePath(entryFile));
1962
+ } else {
1963
+ entryTemplateImporters.add(normalizePath(entryFile));
1964
+ }
1965
+ }
1966
+ }
1898
1967
  return routes;
1899
1968
  })());
1900
1969
  }
@@ -2001,6 +2070,14 @@ function markoRun(opts = {}) {
2001
2070
  {
2002
2071
  name: `${PLUGIN_NAME_PREFIX}:pre`,
2003
2072
  enforce: "pre",
2073
+ api: {
2074
+ addExternalRoutes(routes2) {
2075
+ externalRoutes.add(routes2);
2076
+ return () => {
2077
+ externalRoutes.delete(routes2);
2078
+ };
2079
+ }
2080
+ },
2004
2081
  async config(config2, env) {
2005
2082
  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;
2006
2083
  const externalPluginOptions = getExternalPluginOptions(config2);
@@ -2033,6 +2110,10 @@ function markoRun(opts = {}) {
2033
2110
  );
2034
2111
  markoVitePluginOptions.runtimeId = opts.runtimeId;
2035
2112
  markoVitePluginOptions.basePathVar = opts.basePathVar;
2113
+ markoVitePluginOptions.isEntry = (importee, importer) => {
2114
+ var _a2;
2115
+ return entryTemplates.has(importee) || entryTemplateImporters.has(importer) || ((_a2 = adapter == null ? void 0 : adapter.isEntryTemplate) == null ? void 0 : _a2.call(adapter, { template: importee, importer })) || false;
2116
+ };
2036
2117
  resolvedRoutesDir = path6.resolve(root, routesDir);
2037
2118
  outputDir = path6.join(root, ((_d = config2.build) == null ? void 0 : _d.outDir) || "dist");
2038
2119
  entryFilesDir = path6.join(outputDir, ".marko-run");
@@ -2051,23 +2132,9 @@ function markoRun(opts = {}) {
2051
2132
  outDir = path6.join(outDir, CLIENT_OUT_DIR);
2052
2133
  }
2053
2134
  const defaultRollupOutputOptions = {
2054
- assetFileNames({ name }) {
2055
- if (name && name.indexOf("_marko-virtual_id_") < 0) {
2056
- return `${assetsDir}/${getEntryFileName(name) || "[name]"}-[hash].[ext]`;
2057
- }
2058
- return `${assetsDir}/_[hash].[ext]`;
2059
- },
2135
+ assetFileNames: `${assetsDir}/[name]-[hash].[ext]`,
2060
2136
  entryFileNames(info) {
2061
- let name = getEntryFileName(info.facadeModuleId);
2062
- if (!name) {
2063
- for (const id of info.moduleIds) {
2064
- name = getEntryFileName(id);
2065
- if (name) {
2066
- break;
2067
- }
2068
- }
2069
- }
2070
- return `${assetsDir}/${name || "[name]"}-[hash].js`;
2137
+ return `${assetsDir}/${getEntryFileName(info.name) || "[name]"}-[hash].js`;
2071
2138
  },
2072
2139
  chunkFileNames: isSSRBuild ? `_[hash].js` : `${assetsDir}/_[hash].js`
2073
2140
  };
@@ -2221,16 +2288,14 @@ function markoRun(opts = {}) {
2221
2288
  }
2222
2289
  },
2223
2290
  async resolveId(importee, importer) {
2291
+ let virtualFilePath;
2224
2292
  if (importee === "@marko/run/router") {
2225
2293
  return normalizePath(path6.resolve(root, ROUTER_FILENAME));
2226
2294
  } else if (importee.endsWith(".marko") && importee.includes(relativeEntryFilesDirPosix)) {
2227
2295
  if (!importee.startsWith(root)) {
2228
2296
  importee = path6.resolve(root, "." + importee);
2229
2297
  }
2230
- return normalizePath(importee);
2231
- }
2232
- let virtualFilePath;
2233
- if (importee.startsWith(virtualFilePrefix)) {
2298
+ } else if (importee.startsWith(virtualFilePrefix)) {
2234
2299
  virtualFilePath = importee.slice(virtualFilePrefix.length + 1);
2235
2300
  importee = path6.resolve(root, virtualFilePath);
2236
2301
  } else if (!isBuild && importer && (importer === devEntryFile || normalizePath(importer) === devEntryFilePosix) && importee.startsWith(`/${markoRunFilePrefix}`)) {
@@ -2250,9 +2315,6 @@ function markoRun(opts = {}) {
2250
2315
  }
2251
2316
  },
2252
2317
  async load(id) {
2253
- if (id.endsWith(serverEntryQuery)) {
2254
- id = id.slice(0, -serverEntryQuery.length);
2255
- }
2256
2318
  if (!renderVirtualFilesResult) {
2257
2319
  await renderVirtualFiles(this);
2258
2320
  }
@@ -2301,8 +2363,8 @@ function markoRun(opts = {}) {
2301
2363
  }
2302
2364
  store.write(routeData);
2303
2365
  await ((_a = opts == null ? void 0 : opts.emitRoutes) == null ? void 0 : _a.call(opts, routes.list));
2304
- } else if (process.env.MR_EXPLORER !== "true") {
2305
- logRoutesTable(routes, bundle);
2366
+ } else {
2367
+ logRoutesTable(routes, [...externalRoutes], bundle);
2306
2368
  }
2307
2369
  },
2308
2370
  async closeBundle() {
@@ -2375,15 +2437,18 @@ async function resolveAdapter(root, options, log) {
2375
2437
  log && debug("Using default adapter");
2376
2438
  return module.default();
2377
2439
  }
2378
- var markoEntryFileRegex = /__marko-run__([^.]+)(?:\.(.+))?\.marko\.([^.]+)$/;
2440
+ var markoEntryFileRegex = /([^/\\]+)\.marko$/;
2379
2441
  function getEntryFileName(file) {
2380
2442
  const match = file && markoEntryFileRegex.exec(file);
2381
- return match ? match[2] || "index" : void 0;
2443
+ return match ? match[1] : void 0;
2444
+ }
2445
+ function getPlugin(config2) {
2446
+ return config2.plugins.find(
2447
+ (plugin) => plugin.name === `${PLUGIN_NAME_PREFIX}:pre`
2448
+ );
2382
2449
  }
2383
2450
  function isPluginIncluded(config2) {
2384
- return config2.plugins.some((plugin) => {
2385
- return plugin.name === `${PLUGIN_NAME_PREFIX}:pre`;
2386
- });
2451
+ return !!getPlugin(config2);
2387
2452
  }
2388
2453
  function getImporters(module, fileName, seen = /* @__PURE__ */ new Set()) {
2389
2454
  for (const importer of module.importers) {
@@ -2,8 +2,6 @@ type ValuesOf<T> = T[keyof T];
2
2
  export declare const markoRunFilePrefix = "__marko-run__";
3
3
  export declare const virtualFilePrefix = "virtual:marko-run";
4
4
  export declare const httpVerbs: readonly ["get", "head", "post", "put", "delete", "patch", "options"];
5
- export declare const serverEntryQuery = "?marko-server-entry";
6
- export declare const browserEntryQuery = "?marko-browser-entry";
7
5
  export declare const RoutableFileTypes: {
8
6
  readonly Page: "page";
9
7
  readonly Layout: "layout";