@marko/run 0.4.12 → 0.5.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.
@@ -764,16 +764,19 @@ function hasVerb(route, verb) {
764
764
  }
765
765
 
766
766
  // src/vite/codegen/index.ts
767
- function renderRouteTemplate(route) {
767
+ function renderRouteTemplate(route, getRelativePath) {
768
768
  if (!route.page) {
769
769
  throw new Error(`Route ${route.key} has no page to render`);
770
770
  }
771
771
  return renderEntryTemplate(
772
772
  route.entryName,
773
- [...route.layouts, route.page].map((file) => `./${file.importPath}`)
773
+ [...route.layouts, route.page].map(
774
+ (file) => getRelativePath(file.importPath)
775
+ ),
776
+ route.key === RoutableFileTypes.Error ? ["error"] : []
774
777
  );
775
778
  }
776
- function renderEntryTemplate(name, files) {
779
+ function renderEntryTemplate(name, files, pageInputs = []) {
777
780
  if (!name) {
778
781
  throw new Error(`Invalid argument - 'name' cannot be empty`);
779
782
  }
@@ -781,27 +784,28 @@ function renderEntryTemplate(name, files) {
781
784
  throw new Error(`Invalid argument - 'files' cannot be empty`);
782
785
  }
783
786
  const writer = createStringWriter();
784
- writer.writeLines(`// ${virtualFilePrefix}/${name}.marko`);
787
+ writer.writeLines(`// ${name}.marko`);
785
788
  writer.branch("imports");
786
789
  writer.writeLines("");
787
- writeEntryTemplateTag(writer, files);
790
+ writeEntryTemplateTag(writer, files, pageInputs);
788
791
  return writer.end();
789
792
  }
790
- function writeEntryTemplateTag(writer, [file, ...rest], index = 1) {
793
+ function writeEntryTemplateTag(writer, [file, ...rest], pageInputs, index = 1) {
791
794
  if (file) {
792
795
  const isLast = !rest.length;
793
- const tag = isLast ? "page" : `layout${index}`;
796
+ const tag = isLast ? "Page" : `Layout${index}`;
794
797
  writer.branch("imports").writeLines(`import ${tag} from '${file}';`);
795
798
  if (isLast) {
796
- writer.writeLines(`<${tag} ...input/>`);
799
+ const attributes = pageInputs.length ? " " + pageInputs.map((name) => `${name}=input.${name}`).join(" ") : "";
800
+ writer.writeLines(`<${tag}${attributes}/>`);
797
801
  } else {
798
- writer.writeBlockStart(`<${tag} ...input>`);
799
- writeEntryTemplateTag(writer, rest, index + 1);
802
+ writer.writeBlockStart(`<${tag}>`);
803
+ writeEntryTemplateTag(writer, rest, pageInputs, index + 1);
800
804
  writer.writeBlockEnd(`</>`);
801
805
  }
802
806
  }
803
807
  }
804
- function renderRouteEntry(route) {
808
+ function renderRouteEntry(route, entriesDir) {
805
809
  var _a;
806
810
  const { key, index, handler, page, middleware, meta, entryName } = route;
807
811
  const verbs = getVerbs(route);
@@ -854,9 +858,8 @@ function renderRouteEntry(route) {
854
858
  );
855
859
  }
856
860
  if (page) {
857
- imports.writeLines(
858
- `import page from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
859
- );
861
+ const importPath = route.layouts.length ? `./${entriesDir}/${entryName}.marko` : `./${page.importPath}`;
862
+ imports.writeLines(`import page from '${importPath}${serverEntryQuery}';`);
860
863
  }
861
864
  if (meta) {
862
865
  imports.writeLines(
@@ -944,7 +947,7 @@ function writeRouteEntryHandler(writer, route, verb) {
944
947
  continuations.join();
945
948
  writer.writeBlockEnd("}");
946
949
  }
947
- function renderRouter(routes, options = {
950
+ function renderRouter(routes, entriesDir, options = {
948
951
  trailingSlashes: "RedirectWithout"
949
952
  }) {
950
953
  const writer = createStringWriter();
@@ -961,9 +964,10 @@ function renderRouter(routes, options = {
961
964
  `import { ${names.join(", ")} } from '${virtualFilePrefix}/${route.entryName}.js';`
962
965
  );
963
966
  }
964
- for (const { key, entryName } of Object.values(routes.special)) {
967
+ for (const page of Object.values(routes.special)) {
968
+ const importPath = page.layouts.length ? `./${entriesDir}/${page.entryName}.marko` : `./${page.importPath}`;
965
969
  imports.writeLines(
966
- `import page${key} from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
970
+ `import page${page.key} from '${importPath}${serverEntryQuery}';`
967
971
  );
968
972
  }
969
973
  writer.writeLines(
@@ -1737,18 +1741,23 @@ function prepareError(err) {
1737
1741
  }
1738
1742
 
1739
1743
  // src/vite/plugin.ts
1744
+ import { createHash } from "crypto";
1740
1745
  var debug = createDebug("@marko/run");
1741
1746
  var __dirname = path3.dirname(fileURLToPath(import.meta.url));
1742
1747
  var PLUGIN_NAME_PREFIX = "marko-run-vite";
1743
1748
  var POSIX_SEP = "/";
1744
1749
  var WINDOWS_SEP = "\\";
1750
+ var CLIENT_OUT_DIR = "public";
1751
+ var MIDDLEWARE_FILENAME = `${markoRunFilePrefix}middleware.js`;
1752
+ var ROUTER_FILENAME = `${markoRunFilePrefix}router.js`;
1745
1753
  var normalizePath = path3.sep === WINDOWS_SEP ? (id) => id.replace(/\\/g, POSIX_SEP) : (id) => id;
1746
1754
  function markoRun(opts = {}) {
1747
1755
  let { routesDir, adapter, ...markoVitePluginOptions } = opts;
1748
- let compiler;
1749
1756
  let store;
1750
1757
  let root;
1751
1758
  let resolvedRoutesDir;
1759
+ let entryFilesDir;
1760
+ let relativeEntryFilesDir;
1752
1761
  let typesDir;
1753
1762
  let isBuild = false;
1754
1763
  let isSSRBuild = false;
@@ -1768,6 +1777,9 @@ function markoRun(opts = {}) {
1768
1777
  routesBuild: 0,
1769
1778
  routesRender: 0
1770
1779
  };
1780
+ function getEntryFileRelativePath(to) {
1781
+ return path3.relative(entryFilesDir, to);
1782
+ }
1771
1783
  async function writeTypesFile(routes2) {
1772
1784
  if (routes2 && (tsConfigExists ?? (tsConfigExists = await globFileExists(
1773
1785
  root,
@@ -1798,27 +1810,12 @@ function markoRun(opts = {}) {
1798
1810
  throw new Error("No routes generated");
1799
1811
  }
1800
1812
  for (const route of routes.list) {
1801
- if (route.page) {
1802
- virtualFiles.set(
1803
- path3.posix.join(root, `${route.entryName}.marko`),
1804
- ""
1805
- );
1806
- }
1807
1813
  virtualFiles.set(path3.posix.join(root, `${route.entryName}.js`), "");
1808
1814
  }
1809
- for (const route of Object.values(routes.special)) {
1810
- virtualFiles.set(
1811
- path3.posix.join(root, `${route.entryName}.marko`),
1812
- ""
1813
- );
1814
- }
1815
1815
  if (routes.middleware.length) {
1816
- virtualFiles.set(
1817
- path3.posix.join(root, `${markoRunFilePrefix}middleware.js`),
1818
- ""
1819
- );
1816
+ virtualFiles.set(path3.posix.join(root, MIDDLEWARE_FILENAME), "");
1820
1817
  }
1821
- virtualFiles.set("@marko/run/router", "");
1818
+ virtualFiles.set(path3.posix.join(root, ROUTER_FILENAME), "");
1822
1819
  resolve(routes);
1823
1820
  } catch (err) {
1824
1821
  reject(err);
@@ -1829,11 +1826,12 @@ function markoRun(opts = {}) {
1829
1826
  function renderVirtualFiles(context) {
1830
1827
  return renderVirtualFilesResult ?? (renderVirtualFilesResult = new Promise(async (resolve) => {
1831
1828
  var _a;
1832
- const routerOptions = {
1833
- trailingSlashes: opts.trailingSlashes || "RedirectWithout"
1834
- };
1835
1829
  try {
1836
1830
  const routes2 = await buildVirtualFiles();
1831
+ let entryFilesDirExists = false;
1832
+ if (fs4.existsSync(entryFilesDir)) {
1833
+ fs4.rmSync(entryFilesDir, { recursive: true });
1834
+ }
1837
1835
  for (const route of routes2.list) {
1838
1836
  if (route.handler) {
1839
1837
  const exports = await getExportsFromFile(
@@ -1853,22 +1851,30 @@ function markoRun(opts = {}) {
1853
1851
  );
1854
1852
  }
1855
1853
  }
1856
- if (route.page) {
1857
- virtualFiles.set(
1858
- path3.posix.join(root, `${route.entryName}.marko`),
1859
- renderRouteTemplate(route)
1854
+ if (route.page && route.layouts.length) {
1855
+ entryFilesDirExists || (entryFilesDirExists = !!fs4.mkdirSync(entryFilesDir, {
1856
+ recursive: true
1857
+ }));
1858
+ fs4.writeFileSync(
1859
+ path3.posix.join(entryFilesDir, `${route.entryName}.marko`),
1860
+ renderRouteTemplate(route, getEntryFileRelativePath)
1860
1861
  );
1861
1862
  }
1862
1863
  virtualFiles.set(
1863
1864
  path3.posix.join(root, `${route.entryName}.js`),
1864
- renderRouteEntry(route)
1865
+ renderRouteEntry(route, relativeEntryFilesDir)
1865
1866
  );
1866
1867
  }
1867
1868
  for (const route of Object.values(routes2.special)) {
1868
- virtualFiles.set(
1869
- path3.posix.join(root, `${route.entryName}.marko`),
1870
- renderRouteTemplate(route)
1871
- );
1869
+ if (route.layouts.length) {
1870
+ entryFilesDirExists || (entryFilesDirExists = !!fs4.mkdirSync(entryFilesDir, {
1871
+ recursive: true
1872
+ }));
1873
+ fs4.writeFileSync(
1874
+ path3.posix.join(entryFilesDir, `${route.entryName}.marko`),
1875
+ renderRouteTemplate(route, getEntryFileRelativePath)
1876
+ );
1877
+ }
1872
1878
  }
1873
1879
  if (routes2.middleware.length) {
1874
1880
  for (const middleware of routes2.middleware) {
@@ -1879,13 +1885,15 @@ function markoRun(opts = {}) {
1879
1885
  }
1880
1886
  }
1881
1887
  virtualFiles.set(
1882
- path3.posix.join(root, `${markoRunFilePrefix}middleware.js`),
1888
+ path3.posix.join(root, MIDDLEWARE_FILENAME),
1883
1889
  renderMiddleware(routes2.middleware)
1884
1890
  );
1885
1891
  }
1886
1892
  virtualFiles.set(
1887
- "@marko/run/router",
1888
- renderRouter(routes2, routerOptions)
1893
+ path3.posix.join(root, ROUTER_FILENAME),
1894
+ renderRouter(routes2, relativeEntryFilesDir, {
1895
+ trailingSlashes: opts.trailingSlashes || "RedirectWithout"
1896
+ })
1889
1897
  );
1890
1898
  await writeTypesFile(routes2);
1891
1899
  if (adapter == null ? void 0 : adapter.routesGenerated) {
@@ -1906,7 +1914,7 @@ function markoRun(opts = {}) {
1906
1914
  throw err;
1907
1915
  }
1908
1916
  virtualFiles.set(
1909
- "@marko/run/router",
1917
+ path3.posix.join(root, ROUTER_FILENAME),
1910
1918
  `throw ${JSON.stringify(prepareError(err))}`
1911
1919
  );
1912
1920
  }
@@ -1918,7 +1926,7 @@ function markoRun(opts = {}) {
1918
1926
  name: `${PLUGIN_NAME_PREFIX}:pre`,
1919
1927
  enforce: "pre",
1920
1928
  async config(config2, env) {
1921
- var _a, _b, _c, _d, _e, _f, _g, _h;
1929
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1922
1930
  const externalPluginOptions = getExternalPluginOptions(config2);
1923
1931
  if (externalPluginOptions) {
1924
1932
  opts = mergeConfig(opts, externalPluginOptions);
@@ -1942,14 +1950,6 @@ function markoRun(opts = {}) {
1942
1950
  opts = mergeConfig(opts, adapterOptions);
1943
1951
  }
1944
1952
  }
1945
- compiler ?? (compiler = await import(opts.compiler || "@marko/compiler"));
1946
- compiler.taglib.register("@marko/run", {
1947
- "<dev-error-page>": {
1948
- template: normalizePath(
1949
- path3.resolve(__dirname, "../components/dev-error-page.marko")
1950
- )
1951
- }
1952
- });
1953
1953
  routesDir = opts.routesDir || "src/routes";
1954
1954
  store = new ReadOncePersistedStore(
1955
1955
  `vite-marko-run${opts.runtimeId ? `-${opts.runtimeId}` : ""}`
@@ -1957,12 +1957,23 @@ function markoRun(opts = {}) {
1957
1957
  markoVitePluginOptions.runtimeId = opts.runtimeId;
1958
1958
  markoVitePluginOptions.basePathVar = opts.basePathVar;
1959
1959
  resolvedRoutesDir = path3.resolve(root, routesDir);
1960
+ const modulesDir = getModulesDir() || path3.join(root, "node_modules");
1961
+ entryFilesDir = path3.join(
1962
+ modulesDir,
1963
+ ".marko",
1964
+ createHash("shake256", { outputLength: 4 }).update(root).digest("hex")
1965
+ );
1966
+ relativeEntryFilesDir = path3.relative(root, entryFilesDir);
1960
1967
  typesDir = path3.join(root, ".marko-run");
1961
1968
  devEntryFile = path3.join(root, "index.html");
1962
1969
  devEntryFilePosix = normalizePath(devEntryFile);
1963
- const assetsDir = ((_d = config2.build) == null ? void 0 : _d.assetsDir) || "assets";
1964
- let rollupOutputOptions = (_f = (_e = config2.build) == null ? void 0 : _e.rollupOptions) == null ? void 0 : _f.output;
1970
+ let outDir = ((_d = config2.build) == null ? void 0 : _d.outDir) || "dist";
1971
+ const assetsDir = ((_e = config2.build) == null ? void 0 : _e.assetsDir) || "assets";
1972
+ let rollupOutputOptions = (_g = (_f = config2.build) == null ? void 0 : _f.rollupOptions) == null ? void 0 : _g.output;
1965
1973
  if (isBuild) {
1974
+ if (!isSSRBuild) {
1975
+ outDir = path3.join(outDir, CLIENT_OUT_DIR);
1976
+ }
1966
1977
  const defaultRollupOutputOptions = {
1967
1978
  assetFileNames({ name }) {
1968
1979
  if (name && name.indexOf("_marko-virtual_id_") < 0) {
@@ -1998,7 +2009,7 @@ function markoRun(opts = {}) {
1998
2009
  }));
1999
2010
  }
2000
2011
  }
2001
- const browserslistTarget = isBuild && !((_g = config2.build) == null ? void 0 : _g.target) ? browserslist(void 0, {
2012
+ const browserslistTarget = isBuild && !((_h = config2.build) == null ? void 0 : _h.target) ? browserslist(void 0, {
2002
2013
  path: root
2003
2014
  }) : void 0;
2004
2015
  let pluginConfig = {
@@ -2013,19 +2024,22 @@ function markoRun(opts = {}) {
2013
2024
  devSourcemap: true
2014
2025
  },
2015
2026
  build: {
2027
+ outDir,
2028
+ assetsDir,
2016
2029
  target: (browserslistTarget == null ? void 0 : browserslistTarget.length) ? resolveToEsbuildTarget(browserslistTarget, {
2017
2030
  printUnknownTargets: false
2018
2031
  }) : void 0,
2019
2032
  emptyOutDir: isSSRBuild,
2020
2033
  // Avoid server & client deleting files from each other.
2021
- ssrEmitAssets: isSSRBuild,
2034
+ copyPublicDir: !isSSRBuild,
2035
+ ssrEmitAssets: false,
2022
2036
  rollupOptions: {
2023
2037
  output: rollupOutputOptions
2024
2038
  },
2025
2039
  modulePreload: { polyfill: false }
2026
2040
  },
2027
2041
  optimizeDeps: {
2028
- entries: !((_h = config2.optimizeDeps) == null ? void 0 : _h.entries) ? [
2042
+ entries: !((_i = config2.optimizeDeps) == null ? void 0 : _i.entries) ? [
2029
2043
  "src/pages/**/*+{page,layout}.marko",
2030
2044
  "!**/__snapshots__/**",
2031
2045
  `!**/__tests__/**`,
@@ -2135,7 +2149,9 @@ function markoRun(opts = {}) {
2135
2149
  async resolveId(importee, importer) {
2136
2150
  let resolved;
2137
2151
  let virtualFilePath;
2138
- if (importee.startsWith(virtualFilePrefix)) {
2152
+ if (importee === "@marko/run/router") {
2153
+ importee = path3.resolve(root, ROUTER_FILENAME);
2154
+ } else if (importee.startsWith(virtualFilePrefix)) {
2139
2155
  virtualFilePath = importee.slice(virtualFilePrefix.length + 1);
2140
2156
  importee = path3.resolve(root, virtualFilePath);
2141
2157
  } else if (!isBuild && importer && (importer === devEntryFile || normalizePath(importer) === devEntryFilePosix) && importee.startsWith(`/${markoRunFilePrefix}`)) {
@@ -2165,7 +2181,7 @@ function markoRun(opts = {}) {
2165
2181
  }
2166
2182
  if (virtualFiles.has(id)) {
2167
2183
  return virtualFiles.get(id);
2168
- } else if (/[/\\]__marko-run__[^?/\\]+\.(js|marko)$/.exec(id)) {
2184
+ } else if (!id.startsWith(entryFilesDir) && /[/\\]__marko-run__[^?/\\]+\.(js|marko)$/.exec(id)) {
2169
2185
  return "";
2170
2186
  }
2171
2187
  }
@@ -2304,6 +2320,12 @@ function getImporters(module, fileName, seen = /* @__PURE__ */ new Set()) {
2304
2320
  }
2305
2321
  return seen;
2306
2322
  }
2323
+ function getModulesDir(dir = __dirname) {
2324
+ const index = dir.indexOf("node_modules");
2325
+ if (index >= 0) {
2326
+ return dir.slice(0, index + 12);
2327
+ }
2328
+ }
2307
2329
 
2308
2330
  // src/cli/commands.ts
2309
2331
  var __dirname2 = path4.dirname(fileURLToPath2(import.meta.url));
@@ -2314,7 +2336,7 @@ async function preview(entry, distEntry, cwd, configFile, port, outDir, envFile,
2314
2336
  var _a;
2315
2337
  const resolvedConfig = await resolveConfig(
2316
2338
  { root: cwd, configFile, logLevel: "silent", build: { outDir } },
2317
- "build"
2339
+ "serve"
2318
2340
  );
2319
2341
  const [availablePort, adapter] = await Promise.all([
2320
2342
  getAvailablePort(
@@ -44,7 +44,7 @@ var pageResponseInit = {
44
44
  headers: { "content-type": "text/html;charset=UTF-8" }
45
45
  };
46
46
  function pageResponse(template, input) {
47
- return new Response(template.stream(input), pageResponseInit);
47
+ return new Response(template.render(input), pageResponseInit);
48
48
  }
49
49
  var NotHandled = Symbol();
50
50
  var NotMatched = Symbol();
@@ -4,7 +4,7 @@ var pageResponseInit = {
4
4
  headers: { "content-type": "text/html;charset=UTF-8" }
5
5
  };
6
6
  function pageResponse(template, input) {
7
- return new Response(template.stream(input), pageResponseInit);
7
+ return new Response(template.render(input), pageResponseInit);
8
8
  }
9
9
  var NotHandled = Symbol();
10
10
  var NotMatched = Symbol();
@@ -1,8 +1,7 @@
1
1
  import type { Adapter, Route, BuiltRoutes, RoutableFile, RouterOptions } from "../types";
2
- export declare function renderRouteTemplate(route: Route): string;
3
- export declare function renderEntryTemplate(name: string, files: string[]): string;
4
- export declare function renderRouteEntry(route: Route): string;
5
- export declare function renderErrorRouter(error: Error, options?: RouterOptions): string;
6
- export declare function renderRouter(routes: BuiltRoutes, options?: RouterOptions): string;
2
+ export declare function renderRouteTemplate(route: Route, getRelativePath: (path: string) => string): string;
3
+ export declare function renderEntryTemplate(name: string, files: string[], pageInputs?: string[]): string;
4
+ export declare function renderRouteEntry(route: Route, entriesDir: string): string;
5
+ export declare function renderRouter(routes: BuiltRoutes, entriesDir: string, options?: RouterOptions): string;
7
6
  export declare function renderMiddleware(middleware: RoutableFile[]): string;
8
7
  export declare function renderRouteTypeInfo(routes: BuiltRoutes, pathPrefix?: string, adapter?: Adapter | null): Promise<string>;