xanix 1.0.7 → 1.0.8

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.
@@ -0,0 +1,11 @@
1
+ import { XanixClientEntry } from "../types";
2
+ export type DependencyInfo = {
3
+ source: string;
4
+ resolved: string;
5
+ packageName: string;
6
+ };
7
+ export type CacheMetadata = Map<string, {
8
+ file: string;
9
+ }>;
10
+ declare const BuildCache: (entries: XanixClientEntry[]) => Promise<CacheMetadata>;
11
+ export default BuildCache;
@@ -0,0 +1,162 @@
1
+ import path from "node:path";
2
+ import { rollup } from "rollup";
3
+ import { nodeResolve } from "@rollup/plugin-node-resolve";
4
+ import { build } from "esbuild";
5
+ import { createRequire } from "node:module";
6
+ import { getClientRuntimeFile, getDocumentFile } from "../include/utils.js";
7
+ import esbuild from "rollup-plugin-esbuild";
8
+ import url from "@rollup/plugin-url";
9
+ import XanixDocument from "./plugins/XanixDocument/index.js";
10
+ const require = createRequire(import.meta.url);
11
+ const GenerateGraph = async (entries) => {
12
+ const collector = new Map();
13
+ const input = [];
14
+ for (const entry of entries) {
15
+ input.push(entry.file);
16
+ }
17
+ input.push(getClientRuntimeFile());
18
+ input.push(await getDocumentFile());
19
+ const bundle = await rollup({
20
+ input,
21
+ plugins: [
22
+ nodeResolve({
23
+ extensions: [".mjs", ".js", ".jsx", ".json", ".ts", ".tsx"],
24
+ }),
25
+ url({
26
+ include: [
27
+ "**/*.jpg",
28
+ "**/*.jpeg",
29
+ "**/*.png",
30
+ "**/*.gif",
31
+ "**/*.webp",
32
+ "**/*.svg",
33
+ "**/*.ico",
34
+ "**/*.woff",
35
+ "**/*.woff2",
36
+ "**/*.ttf",
37
+ "**/*.eot",
38
+ "**/*.css",
39
+ ],
40
+ limit: 0,
41
+ fileName: "assets/[name]-[hash][extname]",
42
+ emitFiles: false,
43
+ }),
44
+ XanixDocument(),
45
+ esbuild({
46
+ target: "es2022",
47
+ jsx: "automatic",
48
+ tsconfig: false,
49
+ }),
50
+ ],
51
+ external(id) {
52
+ if (id.startsWith(".") ||
53
+ path.isAbsolute(id) ||
54
+ id === "@" ||
55
+ id.startsWith("@/") ||
56
+ id === "virtual:xanix-document") {
57
+ return false;
58
+ }
59
+ collector.set(id, {
60
+ source: id,
61
+ resolved: require.resolve(id, {
62
+ paths: [process.cwd()],
63
+ }),
64
+ packageName: id,
65
+ });
66
+ return true;
67
+ },
68
+ });
69
+ await bundle.close();
70
+ return collector;
71
+ };
72
+ // Wraps each CJS dep in a virtual ESM file that re-exports named exports detected via require()
73
+ const makeCjsWrapperPlugin = (wrappers) => ({
74
+ name: "cjs-named-exports",
75
+ setup(build) {
76
+ build.onResolve({ filter: /\?cjs-wrapper$/ }, (args) => ({
77
+ path: args.path,
78
+ namespace: "cjs-wrapper",
79
+ }));
80
+ build.onLoad({ filter: /.*/, namespace: "cjs-wrapper" }, (args) => {
81
+ const contents = wrappers.get(args.path);
82
+ return { contents, resolveDir: process.cwd() };
83
+ });
84
+ },
85
+ });
86
+ const buildWrappers = (input) => {
87
+ const wrappers = new Map();
88
+ const wrappedInput = {};
89
+ for (const [name, resolved] of Object.entries(input)) {
90
+ const key = `${name}?cjs-wrapper`;
91
+ let namedExports = [];
92
+ let hasDefault = false;
93
+ try {
94
+ const mod = require(resolved);
95
+ hasDefault =
96
+ mod &&
97
+ (typeof mod === "object" || typeof mod === "function") &&
98
+ "default" in mod;
99
+ namedExports = Object.keys(mod).filter((key) => key !== "__esModule" && key !== "default");
100
+ }
101
+ catch (err) {
102
+ // console.log(err);
103
+ }
104
+ const lines = [`import * as __mod from ${JSON.stringify(resolved)};`];
105
+ if (hasDefault) {
106
+ lines.push(`export default __mod.default;`);
107
+ }
108
+ else {
109
+ lines.push(`export default __mod;`);
110
+ }
111
+ lines.push("");
112
+ for (const exportName of namedExports) {
113
+ if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(exportName)) {
114
+ lines.push(`export const ${exportName} = __mod[${JSON.stringify(exportName)}];`);
115
+ }
116
+ }
117
+ wrappers.set(key, lines.join("\n"));
118
+ wrappedInput[name] = key;
119
+ }
120
+ return {
121
+ wrappers,
122
+ wrappedInput,
123
+ };
124
+ };
125
+ const bundleDeps = async (input, outdir) => {
126
+ const { wrappers, wrappedInput } = buildWrappers(input);
127
+ await build({
128
+ entryPoints: wrappedInput,
129
+ bundle: true,
130
+ splitting: true,
131
+ format: "esm",
132
+ target: "es2022",
133
+ outdir,
134
+ entryNames: "[name]",
135
+ chunkNames: "chunks/[hash]",
136
+ platform: "browser",
137
+ sourcemap: true,
138
+ define: {
139
+ __XANIX_CLIENT__: "true",
140
+ __XANIX_SERVER__: "false",
141
+ __XANIX_DEV__: "true",
142
+ __XANIX_PROD__: "false",
143
+ },
144
+ plugins: [makeCjsWrapperPlugin(wrappers)],
145
+ });
146
+ };
147
+ const metadata = new Map();
148
+ const BuildCache = async (entries) => {
149
+ const input = {};
150
+ const dependencies = await GenerateGraph(entries);
151
+ for (const dependency of dependencies.values()) {
152
+ const entryName = dependency.source.replace(/^@/, "").replace(/\//g, "-");
153
+ input[entryName] = dependency.resolved;
154
+ metadata.set(dependency.source, {
155
+ file: `${entryName}.js`,
156
+ });
157
+ }
158
+ const cacheDir = path.resolve(process.cwd(), ".xanix/cache");
159
+ await bundleDeps(input, cacheDir);
160
+ return metadata;
161
+ };
162
+ export default BuildCache;
@@ -3,7 +3,7 @@ import { rollup } from "rollup";
3
3
  import { nodeResolve } from "@rollup/plugin-node-resolve";
4
4
  import { build } from "esbuild";
5
5
  import { createRequire } from "node:module";
6
- import { getClientRuntimeFile, getDocumentFile } from "../include/utils.js";
6
+ import { getClientRuntimeFile } from "../include/utils.js";
7
7
  import esbuild from "rollup-plugin-esbuild";
8
8
  import url from "@rollup/plugin-url";
9
9
  import XanixDocument from "./plugins/XanixDocument/index.js";
@@ -15,11 +15,9 @@ const GenerateGraph = async (entries) => {
15
15
  input.push(entry.file);
16
16
  }
17
17
  input.push(getClientRuntimeFile());
18
- input.push(await getDocumentFile());
19
18
  const bundle = await rollup({
20
19
  input,
21
20
  plugins: [
22
- // XanixTsconfigAlias(),
23
21
  nodeResolve({
24
22
  extensions: [".mjs", ".js", ".jsx", ".json", ".ts", ".tsx"],
25
23
  }),
@@ -100,7 +98,7 @@ const buildWrappers = (input) => {
100
98
  namedExports = Object.keys(mod).filter((key) => key !== "__esModule" && key !== "default");
101
99
  }
102
100
  catch (err) {
103
- console.log(err);
101
+ // console.log(err);
104
102
  }
105
103
  const lines = [`import * as __mod from ${JSON.stringify(resolved)};`];
106
104
  if (hasDefault) {
@@ -2,7 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import dotenv from "dotenv";
4
4
  import replace from "@rollup/plugin-replace";
5
- import { getClientRuntimeFileName, getDocumentFileName, } from "../../include/utils.js";
5
+ import { getClientRuntimeFileName } from "../../include/utils.js";
6
6
  export default function XanixEnvPlugin(options = {}) {
7
7
  const root = process.cwd();
8
8
  const mode = options.mode ?? "development";
@@ -34,9 +34,6 @@ export default function XanixEnvPlugin(options = {}) {
34
34
  "process.env.NODE_ENV": JSON.stringify(mode),
35
35
  ...(isClient ? clientEnv : serverEnv),
36
36
  __XANIX_CLIENT_RUNTIME_FILE_NAME__: JSON.stringify(getClientRuntimeFileName(mode)),
37
- __XANIX_DOCUMENT_FILE_NAME__: JSON.stringify(getDocumentFileName(mode)),
38
- // XANIX Data
39
- XANIXDT: JSON.stringify("globalThis.$XANIXDT"),
40
37
  __XANIX_CLIENT__: JSON.stringify(isClient),
41
38
  __XANIX_SERVER__: JSON.stringify(!isClient),
42
39
  __XANIX_DEV__: JSON.stringify(mode === "development"),
@@ -4,9 +4,7 @@ import { parse } from "@babel/parser";
4
4
  import traverse from "@babel/traverse";
5
5
  import generate from "@babel/generator";
6
6
  import * as t from "@babel/types";
7
- import { createRequire } from "node:module";
8
7
  import { getClientRuntimeFile } from "../../include/utils.js";
9
- const require = createRequire(import.meta.url);
10
8
  const root = process.cwd();
11
9
  export default function xanixReactRefresh() {
12
10
  return {
@@ -21,14 +19,10 @@ import * as RefreshRuntime from "react-refresh/runtime";
21
19
 
22
20
  ${code}
23
21
 
24
- if (typeof window !== "undefined") {
25
22
  RefreshRuntime.injectIntoGlobalHook(window);
26
-
27
23
  window.$RefreshReg$ = RefreshRuntime.register;
28
24
  window.$RefreshSig$ = () => (type) => type;
29
-
30
25
  const ws = new WebSocket("ws://localhost:8080");
31
-
32
26
  ws.onmessage = async (event) => {
33
27
  const files = JSON.parse(event.data);
34
28
 
@@ -48,7 +42,6 @@ if (typeof window !== "undefined") {
48
42
  RefreshRuntime.performReactRefresh();
49
43
  ws.send("reload")
50
44
  };
51
- }
52
45
  `;
53
46
  return {
54
47
  code: refreshCode,
@@ -59,7 +52,7 @@ if (typeof window !== "undefined") {
59
52
  if (path.resolve(id) === getClientRuntimeFile()) {
60
53
  return null;
61
54
  }
62
- if (id.includes("node_modules") || id.includes("xanix\\dist")) {
55
+ if (id.includes("node_modules")) {
63
56
  return null;
64
57
  }
65
58
  if (!/\.(tsx?|jsx?)$/.test(id)) {
@@ -1,5 +1,3 @@
1
1
  export declare function uid(value: string, length?: number): string;
2
2
  export declare const getClientRuntimeFile: () => string;
3
3
  export declare const getClientRuntimeFileName: (mode: "development" | "production") => string;
4
- export declare const getDocumentFile: () => Promise<string>;
5
- export declare const getDocumentFileName: (mode: "development" | "production") => string;
@@ -1,4 +1,3 @@
1
- import fs from "node:fs";
2
1
  import path from "node:path";
3
2
  import { fileURLToPath } from "node:url";
4
3
  import crypto from "node:crypto";
@@ -16,16 +15,3 @@ export const getClientRuntimeFileName = (mode) => {
16
15
  }
17
16
  return uid("xanix-runtime", 32);
18
17
  };
19
- export const getDocumentFile = async () => {
20
- const documentEntry = path.join(process.cwd(), "Document.tsx");
21
- if (fs.existsSync(documentEntry)) {
22
- return documentEntry;
23
- }
24
- return path.join(__dirname, "../../../components/BaseDocument.js");
25
- };
26
- export const getDocumentFileName = (mode) => {
27
- if (mode === "development") {
28
- return "xanix-documents";
29
- }
30
- return uid("xanix-document", 32);
31
- };
@@ -1,6 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { createRoot } from "react-dom/client";
3
- import Document from "virtual:xanix-document";
4
3
  const pages = new Map();
5
4
  const ROOT_KEY = "__xanix_root__";
6
5
  export const getPath = () => {
@@ -42,8 +41,7 @@ export const getPage = async (path) => {
42
41
  export async function mount(path, Component, doc) {
43
42
  const root = getRoot();
44
43
  pages.set(path, doc);
45
- // const Document = (await import(getImportUrl(__XANIX_DOCUMENT_FILE_NAME__)))
46
- // .default;
44
+ const Document = (await import("virtual:xanix-document")).default;
47
45
  root.render(_jsx(Document, { document: doc, children: _jsx(Component, { ...doc.props }) }));
48
46
  }
49
47
  if (__XANIX_CLIENT__) {
@@ -1,3 +1,3 @@
1
- import { type DocumentProps } from "../index.js";
1
+ import { type DocumentProps } from "xanix";
2
2
  declare const BaseDocument: ({ document, children }: DocumentProps) => import("react").JSX.Element;
3
3
  export default BaseDocument;
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { Head, Body, Document } from "../index.js";
2
+ import { Head, Body, Document } from "xanix";
3
3
  const BaseDocument = ({ document, children }) => {
4
4
  return (_jsxs(Document, { document: document, children: [_jsxs(Head, { children: [_jsx("meta", { charSet: "UTF-8" }), _jsx("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0" })] }), _jsx(Body, { children: children })] }));
5
5
  };
@@ -1,8 +1,5 @@
1
- let global = typeof __XANIX_SERVER__ !== "undefined" && __XANIX_SERVER__
2
- ? globalThis
3
- : typeof window !== "undefined"
4
- ? window
5
- : {};
1
+ import { IS_SERVER } from "./utils.js";
2
+ let global = IS_SERVER ? globalThis : {};
6
3
  global.$XANIXDT_INITIALED = new Map();
7
4
  global.$XANIXDT = new Map();
8
5
  global.$XANIXDT_PAGES = new Map();
package/dist/index.d.ts CHANGED
@@ -17,6 +17,7 @@ import useRequest from "./hooks/useRequest.js";
17
17
  import useHeaders from "./hooks/useHeaders.js";
18
18
  import useCookies from "./hooks/useCookies.js";
19
19
  import useData from "./hooks/useData.js";
20
+ export * from "./utils.js";
20
21
  import { navigate, back, forward, preload, onNavigateStart, onNavigateEnd, reload } from "./navigate.js";
21
22
  export { Link, Document, Head, Body, Script, useDocument, useLocation, useParams, usePathname, useSearchParams, usePage, useRequest, useHeaders, useCookies, useData, navigate, back, forward, preload, onNavigateStart, onNavigateEnd, reload, };
22
23
  export type { DocumentProps };
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ import useRequest from "./hooks/useRequest.js";
17
17
  import useHeaders from "./hooks/useHeaders.js";
18
18
  import useCookies from "./hooks/useCookies.js";
19
19
  import useData from "./hooks/useData.js";
20
+ export * from "./utils.js";
20
21
  // navigate
21
22
  import { navigate, back, forward, preload, onNavigateStart, onNavigateEnd, reload, } from "./navigate.js";
22
23
  export { Link, Document, Head, Body, Script,
@@ -3,7 +3,6 @@ import { renderToPipeableStream, renderToString } from "react-dom/server";
3
3
  import readManifest from "./readManifest.js";
4
4
  import { PassThrough } from "node:stream";
5
5
  import dataLoader from "../../dataLoader.js";
6
- import Document from "virtual:xanix-document";
7
6
  function renderPage(element) {
8
7
  return new Promise((resolve, reject) => {
9
8
  let html = "";
@@ -47,6 +46,7 @@ export default async function xanixPage({ pageId, req, res, Component, props, })
47
46
  res.status(404).send("Page Not Found");
48
47
  return;
49
48
  }
49
+ const Document = (await import("virtual:xanix-document")).default;
50
50
  if (!dataLoader.isInit(pageId)) {
51
51
  renderToString(_jsx(Document, { document: {
52
52
  pageId,
@@ -1,6 +1,5 @@
1
1
  declare global {
2
2
  const __XANIX_CLIENT_RUNTIME_FILE_NAME__: string;
3
- const __XANIX_DOCUMENT_FILE_NAME__: string;
4
3
  const __XANIX_CLIENT__: boolean;
5
4
  const __XANIX_SERVER__: boolean;
6
5
  const __XANIX_DEV__: boolean;
@@ -0,0 +1,4 @@
1
+ export declare const IS_SERVER: boolean;
2
+ export declare const IS_CLIENT: boolean;
3
+ export declare const IS_DEVELOPMENT: boolean;
4
+ export declare const IS_PRODUCTION: boolean;
package/dist/utils.js ADDED
@@ -0,0 +1,4 @@
1
+ export const IS_SERVER = typeof __XANIX_SERVER__ !== "undefined" && __XANIX_SERVER__;
2
+ export const IS_CLIENT = !IS_SERVER;
3
+ export const IS_DEVELOPMENT = typeof __XANIX_DEV__ !== "undefined" && __XANIX_DEV__;
4
+ export const IS_PRODUCTION = typeof __XANIX_PROD__ !== "undefined" && __XANIX_PROD__;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xanix",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -38,12 +38,16 @@
38
38
  "dependencies": {
39
39
  "@babel/generator": "^8.0.0",
40
40
  "@babel/parser": "^8.0.4",
41
+ "@babel/preset-react": "^8.0.1",
42
+ "@babel/preset-typescript": "^8.0.1",
41
43
  "@babel/traverse": "^8.0.4",
42
44
  "@rollup/plugin-alias": "^6.0.0",
45
+ "@rollup/plugin-babel": "^7.1.0",
43
46
  "@rollup/plugin-commonjs": "^29.0.3",
44
47
  "@rollup/plugin-json": "^6.1.0",
45
48
  "@rollup/plugin-node-resolve": "^16.0.3",
46
49
  "@rollup/plugin-replace": "^6.0.3",
50
+ "@rollup/plugin-typescript": "^12.3.0",
47
51
  "@rollup/plugin-url": "^8.0.2",
48
52
  "commander": "^15.0.0",
49
53
  "dotenv": "^17.4.2",