@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.
@@ -750,16 +750,19 @@ function hasVerb(route, verb) {
750
750
  }
751
751
 
752
752
  // src/vite/codegen/index.ts
753
- function renderRouteTemplate(route) {
753
+ function renderRouteTemplate(route, getRelativePath) {
754
754
  if (!route.page) {
755
755
  throw new Error(`Route ${route.key} has no page to render`);
756
756
  }
757
757
  return renderEntryTemplate(
758
758
  route.entryName,
759
- [...route.layouts, route.page].map((file) => `./${file.importPath}`)
759
+ [...route.layouts, route.page].map(
760
+ (file) => getRelativePath(file.importPath)
761
+ ),
762
+ route.key === RoutableFileTypes.Error ? ["error"] : []
760
763
  );
761
764
  }
762
- function renderEntryTemplate(name, files) {
765
+ function renderEntryTemplate(name, files, pageInputs = []) {
763
766
  if (!name) {
764
767
  throw new Error(`Invalid argument - 'name' cannot be empty`);
765
768
  }
@@ -767,27 +770,28 @@ function renderEntryTemplate(name, files) {
767
770
  throw new Error(`Invalid argument - 'files' cannot be empty`);
768
771
  }
769
772
  const writer = createStringWriter();
770
- writer.writeLines(`// ${virtualFilePrefix}/${name}.marko`);
773
+ writer.writeLines(`// ${name}.marko`);
771
774
  writer.branch("imports");
772
775
  writer.writeLines("");
773
- writeEntryTemplateTag(writer, files);
776
+ writeEntryTemplateTag(writer, files, pageInputs);
774
777
  return writer.end();
775
778
  }
776
- function writeEntryTemplateTag(writer, [file, ...rest], index = 1) {
779
+ function writeEntryTemplateTag(writer, [file, ...rest], pageInputs, index = 1) {
777
780
  if (file) {
778
781
  const isLast = !rest.length;
779
- const tag = isLast ? "page" : `layout${index}`;
782
+ const tag = isLast ? "Page" : `Layout${index}`;
780
783
  writer.branch("imports").writeLines(`import ${tag} from '${file}';`);
781
784
  if (isLast) {
782
- writer.writeLines(`<${tag} ...input/>`);
785
+ const attributes = pageInputs.length ? " " + pageInputs.map((name) => `${name}=input.${name}`).join(" ") : "";
786
+ writer.writeLines(`<${tag}${attributes}/>`);
783
787
  } else {
784
- writer.writeBlockStart(`<${tag} ...input>`);
785
- writeEntryTemplateTag(writer, rest, index + 1);
788
+ writer.writeBlockStart(`<${tag}>`);
789
+ writeEntryTemplateTag(writer, rest, pageInputs, index + 1);
786
790
  writer.writeBlockEnd(`</>`);
787
791
  }
788
792
  }
789
793
  }
790
- function renderRouteEntry(route) {
794
+ function renderRouteEntry(route, entriesDir) {
791
795
  var _a;
792
796
  const { key, index, handler, page, middleware, meta, entryName } = route;
793
797
  const verbs = getVerbs(route);
@@ -840,9 +844,8 @@ function renderRouteEntry(route) {
840
844
  );
841
845
  }
842
846
  if (page) {
843
- imports.writeLines(
844
- `import page from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
845
- );
847
+ const importPath = route.layouts.length ? `./${entriesDir}/${entryName}.marko` : `./${page.importPath}`;
848
+ imports.writeLines(`import page from '${importPath}${serverEntryQuery}';`);
846
849
  }
847
850
  if (meta) {
848
851
  imports.writeLines(
@@ -930,7 +933,7 @@ function writeRouteEntryHandler(writer, route, verb) {
930
933
  continuations.join();
931
934
  writer.writeBlockEnd("}");
932
935
  }
933
- function renderRouter(routes, options = {
936
+ function renderRouter(routes, entriesDir, options = {
934
937
  trailingSlashes: "RedirectWithout"
935
938
  }) {
936
939
  const writer = createStringWriter();
@@ -947,9 +950,10 @@ function renderRouter(routes, options = {
947
950
  `import { ${names.join(", ")} } from '${virtualFilePrefix}/${route.entryName}.js';`
948
951
  );
949
952
  }
950
- for (const { key, entryName } of Object.values(routes.special)) {
953
+ for (const page of Object.values(routes.special)) {
954
+ const importPath = page.layouts.length ? `./${entriesDir}/${page.entryName}.marko` : `./${page.importPath}`;
951
955
  imports.writeLines(
952
- `import page${key} from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
956
+ `import page${page.key} from '${importPath}${serverEntryQuery}';`
953
957
  );
954
958
  }
955
959
  writer.writeLines(
@@ -1737,18 +1741,23 @@ function prepareError(err) {
1737
1741
  }
1738
1742
 
1739
1743
  // src/vite/plugin.ts
1744
+ var import_crypto = require("crypto");
1740
1745
  var debug = (0, import_debug.default)("@marko/run");
1741
1746
  var __dirname = import_path3.default.dirname((0, import_url2.fileURLToPath)(__importMetaURL));
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 = import_path3.default.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 import_path3.default.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
- import_path3.default.posix.join(root, `${route.entryName}.marko`),
1804
- ""
1805
- );
1806
- }
1807
1813
  virtualFiles.set(import_path3.default.posix.join(root, `${route.entryName}.js`), "");
1808
1814
  }
1809
- for (const route of Object.values(routes.special)) {
1810
- virtualFiles.set(
1811
- import_path3.default.posix.join(root, `${route.entryName}.marko`),
1812
- ""
1813
- );
1814
- }
1815
1815
  if (routes.middleware.length) {
1816
- virtualFiles.set(
1817
- import_path3.default.posix.join(root, `${markoRunFilePrefix}middleware.js`),
1818
- ""
1819
- );
1816
+ virtualFiles.set(import_path3.default.posix.join(root, MIDDLEWARE_FILENAME), "");
1820
1817
  }
1821
- virtualFiles.set("@marko/run/router", "");
1818
+ virtualFiles.set(import_path3.default.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 (import_fs3.default.existsSync(entryFilesDir)) {
1833
+ import_fs3.default.rmSync(entryFilesDir, { recursive: true });
1834
+ }
1837
1835
  for (const route of routes2.list) {
1838
1836
  if (route.handler) {
1839
1837
  const exports2 = await getExportsFromFile(
@@ -1853,22 +1851,30 @@ function markoRun(opts = {}) {
1853
1851
  );
1854
1852
  }
1855
1853
  }
1856
- if (route.page) {
1857
- virtualFiles.set(
1858
- import_path3.default.posix.join(root, `${route.entryName}.marko`),
1859
- renderRouteTemplate(route)
1854
+ if (route.page && route.layouts.length) {
1855
+ entryFilesDirExists || (entryFilesDirExists = !!import_fs3.default.mkdirSync(entryFilesDir, {
1856
+ recursive: true
1857
+ }));
1858
+ import_fs3.default.writeFileSync(
1859
+ import_path3.default.posix.join(entryFilesDir, `${route.entryName}.marko`),
1860
+ renderRouteTemplate(route, getEntryFileRelativePath)
1860
1861
  );
1861
1862
  }
1862
1863
  virtualFiles.set(
1863
1864
  import_path3.default.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
- import_path3.default.posix.join(root, `${route.entryName}.marko`),
1870
- renderRouteTemplate(route)
1871
- );
1869
+ if (route.layouts.length) {
1870
+ entryFilesDirExists || (entryFilesDirExists = !!import_fs3.default.mkdirSync(entryFilesDir, {
1871
+ recursive: true
1872
+ }));
1873
+ import_fs3.default.writeFileSync(
1874
+ import_path3.default.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
- import_path3.default.posix.join(root, `${markoRunFilePrefix}middleware.js`),
1888
+ import_path3.default.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
+ import_path3.default.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
+ import_path3.default.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 = (0, import_vite.mergeConfig)(opts, externalPluginOptions);
@@ -1942,14 +1950,6 @@ function markoRun(opts = {}) {
1942
1950
  opts = (0, import_vite.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
- import_path3.default.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 = import_path3.default.resolve(root, routesDir);
1960
+ const modulesDir = getModulesDir() || import_path3.default.join(root, "node_modules");
1961
+ entryFilesDir = import_path3.default.join(
1962
+ modulesDir,
1963
+ ".marko",
1964
+ (0, import_crypto.createHash)("shake256", { outputLength: 4 }).update(root).digest("hex")
1965
+ );
1966
+ relativeEntryFilesDir = import_path3.default.relative(root, entryFilesDir);
1960
1967
  typesDir = import_path3.default.join(root, ".marko-run");
1961
1968
  devEntryFile = import_path3.default.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 = import_path3.default.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) ? (0, import_browserslist.default)(void 0, {
2012
+ const browserslistTarget = isBuild && !((_h = config2.build) == null ? void 0 : _h.target) ? (0, import_browserslist.default)(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) ? (0, import_esbuild_plugin_browserslist.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 = import_path3.default.resolve(root, ROUTER_FILENAME);
2154
+ } else if (importee.startsWith(virtualFilePrefix)) {
2139
2155
  virtualFilePath = importee.slice(virtualFilePrefix.length + 1);
2140
2156
  importee = import_path3.default.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
  }
@@ -2299,6 +2315,12 @@ function getImporters(module2, fileName, seen = /* @__PURE__ */ new Set()) {
2299
2315
  }
2300
2316
  return seen;
2301
2317
  }
2318
+ function getModulesDir(dir = __dirname) {
2319
+ const index = dir.indexOf("node_modules");
2320
+ if (index >= 0) {
2321
+ return dir.slice(0, index + 12);
2322
+ }
2323
+ }
2302
2324
 
2303
2325
  // src/vite/utils/server.ts
2304
2326
  var import_net = __toESM(require("net"), 1);