@thexjs/core 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. package/dist/index.js +50 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -300,7 +300,7 @@ var CLIENT_NAV_SCRIPT = `
300
300
  if (url.origin !== location.origin) return null;
301
301
  var here = new URL(location.href);
302
302
  if (url.pathname === here.pathname && url.search === here.search && url.hash) return null;
303
- return url;
303
+ return anchor;
304
304
  }
305
305
 
306
306
  function setLoading(on) {
@@ -1646,7 +1646,7 @@ data: ${data}
1646
1646
  }
1647
1647
 
1648
1648
  // src/build.ts
1649
- import { cpSync, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, writeFileSync as writeFileSync3 } from "fs";
1649
+ import { cpSync, existsSync as existsSync2, mkdirSync, writeFileSync as writeFileSync3 } from "fs";
1650
1650
  import { basename, join as join5, relative as relative3 } from "path";
1651
1651
  import { createElement as createElement2 } from "react";
1652
1652
 
@@ -1749,18 +1749,27 @@ async function build(options) {
1749
1749
  if (legacyApiDir && existsSync2(legacyApiDir) && legacyApiDir !== apiDir) {
1750
1750
  routeEntries.push(...scanApiDir(legacyApiDir));
1751
1751
  }
1752
+ const actionModules = /* @__PURE__ */ new Map();
1752
1753
  if (actionsDir && existsSync2(actionsDir)) {
1753
1754
  for (const actionFile of scanRoutes(actionsDir)) {
1754
1755
  const mod = await import(actionFile.filePath);
1755
1756
  const segments = actionFile.routePath.split("/").filter(Boolean);
1756
1757
  const parentPath = `/${segments.slice(0, -1).join("/")}`;
1758
+ const fnNames = [];
1757
1759
  const actions = mod.actions;
1758
- if (actions) registerServerFunctions(parentPath, actionFile.paramNames, actions);
1760
+ if (actions) {
1761
+ registerServerFunctions(parentPath, actionFile.paramNames, actions);
1762
+ fnNames.push(...Object.keys(actions));
1763
+ }
1759
1764
  for (const [key, value] of Object.entries(mod)) {
1760
1765
  if (key === "default" || key === "actions" || typeof value !== "function") continue;
1761
1766
  registerServerFunctions(parentPath, actionFile.paramNames, {
1762
1767
  [key]: value
1763
1768
  });
1769
+ fnNames.push(key);
1770
+ }
1771
+ if (fnNames.length > 0) {
1772
+ actionModules.set(actionFile.filePath, { parentPath, fnNames });
1764
1773
  }
1765
1774
  }
1766
1775
  }
@@ -1824,7 +1833,7 @@ async function build(options) {
1824
1833
  let islandScripts = [];
1825
1834
  if (registry.entries.length > 0) {
1826
1835
  const uniqueNames = [...new Set(registry.entries.map((e) => e.name))];
1827
- islandScripts = await bundleRouteIslands(page.filePath, uniqueNames, islandsDir);
1836
+ islandScripts = await bundleRouteIslands(page.filePath, uniqueNames, islandsDir, actionModules);
1828
1837
  console.log(
1829
1838
  ` [islands] ${uniqueNames.length} island(s) on ${page.entry.routePath} -> ${islandScripts.join(", ")}`
1830
1839
  );
@@ -1876,7 +1885,7 @@ async function build(options) {
1876
1885
  }
1877
1886
  console.log(`[x] build complete -> ${outDir}`);
1878
1887
  }
1879
- async function bundleRouteIslands(routeFilePath, islandNames, islandsDir) {
1888
+ async function bundleRouteIslands(routeFilePath, islandNames, islandsDir, actionModules) {
1880
1889
  const entryId = `${basename(routeFilePath).replace(/\.(tsx|ts)$/, "")}-${hash(routeFilePath)}`;
1881
1890
  const outdir = join5(islandsDir, entryId);
1882
1891
  mkdirSync(outdir, { recursive: true });
@@ -1885,25 +1894,46 @@ async function bundleRouteIslands(routeFilePath, islandNames, islandsDir) {
1885
1894
  const hydrateEntry = generateHydrateEntry(routeRel, islandNames);
1886
1895
  const entryPath = join5(outdir, `${entryId}.tsx`);
1887
1896
  writeFileSync3(entryPath, hydrateEntry, "utf-8");
1888
- const result = Bun.spawnSync([
1889
- "bun",
1890
- "build",
1891
- "--target=browser",
1892
- "--external",
1893
- "react",
1894
- "--external",
1895
- "react-dom",
1896
- "--outdir",
1897
- outdir,
1898
- entryPath
1899
- ]);
1900
- if (result.success) {
1901
- assertNoEnvLeakage(readFileSync2(bundlePath, "utf-8"), bundlePath);
1902
- return [`/_islands/${entryId}/${entryId}.js`];
1897
+ try {
1898
+ const result = await Bun.build({
1899
+ entrypoints: [entryPath],
1900
+ target: "browser",
1901
+ external: ["react", "react-dom"],
1902
+ plugins: [actionsRewritePlugin(actionModules)]
1903
+ });
1904
+ if (result.success) {
1905
+ const outputs = result.outputs.filter((o) => o.kind === "entry-point");
1906
+ const built = outputs[0] ?? result.outputs[0];
1907
+ if (!built) throw new Error("bun build produced no output");
1908
+ const code = await built.text();
1909
+ assertNoEnvLeakage(code, bundlePath);
1910
+ writeFileSync3(bundlePath, code, "utf-8");
1911
+ return [`/_islands/${entryId}/${entryId}.js`];
1912
+ }
1913
+ for (const log of result.logs) {
1914
+ console.warn(` [islands] build error: ${log.message}`);
1915
+ }
1916
+ } catch (err) {
1917
+ console.warn(` [islands] build failed for ${routeFilePath}:`, err);
1903
1918
  }
1904
1919
  writeFileSync3(bundlePath, generateFallbackHydration(islandNames), "utf-8");
1905
1920
  return [`/_islands/${entryId}/${entryId}.js`];
1906
1921
  }
1922
+ function actionsRewritePlugin(actionModules) {
1923
+ return {
1924
+ name: "x-actions-rewrite",
1925
+ setup(build2) {
1926
+ if (actionModules.size === 0) return;
1927
+ build2.onLoad({ filter: /\.(ts|tsx)$/ }, (args) => {
1928
+ const info = actionModules.get(args.path);
1929
+ if (!info) return void 0;
1930
+ const endpointBase = info.parentPath === "/" ? "/__x/actions" : `/__x/actions${info.parentPath}`;
1931
+ const contents = generateServerFunctionClient(args.path, info.fnNames, endpointBase);
1932
+ return { contents, loader: "ts" };
1933
+ });
1934
+ }
1935
+ };
1936
+ }
1907
1937
  function generateHydrateEntry(routeRelPath, _islandNames) {
1908
1938
  return `import React from "react";
1909
1939
  import { hydrateRoot } from "react-dom/client";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thexjs/core",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Full-stack React framework core (router, SSR/SSG renderer, islands, server functions) for the x framework, built on Bun.",
5
5
  "keywords": [
6
6
  "react",