xanix 1.0.4 → 1.0.7

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 (143) hide show
  1. package/dist/bin/bundler/buildClient.js +3 -3
  2. package/dist/bin/bundler/buildServer.js +4 -4
  3. package/dist/bin/bundler/cacheNpmModules.js +8 -2
  4. package/dist/bin/bundler/config/output.d.ts +1 -1
  5. package/dist/bin/bundler/config/output.js +11 -3
  6. package/dist/bin/bundler/plugins/XanixDocument/index.d.ts +2 -0
  7. package/dist/bin/bundler/plugins/XanixDocument/index.js +35 -0
  8. package/dist/bin/bundler/plugins/XanixEnv.js +13 -3
  9. package/dist/bin/bundler/plugins/XanixReactRefresh.js +33 -33
  10. package/dist/bin/bundler/plugins/XanixTransform/transformer copy.d.ts +16 -0
  11. package/dist/bin/bundler/plugins/XanixTransform/transformer copy.js +631 -0
  12. package/dist/bin/bundler/plugins/plugins.js +3 -1
  13. package/dist/bin/bundler/watchClient.js +3 -3
  14. package/dist/bin/bundler/watchServer.js +8 -5
  15. package/dist/bin/cli/dev.js +2 -2
  16. package/dist/bin/include/utils.d.ts +3 -0
  17. package/dist/bin/include/utils.js +17 -0
  18. package/dist/bin/index.js +0 -0
  19. package/dist/client-runtime.js +17 -15
  20. package/dist/components/BaseDocument.d.ts +1 -1
  21. package/dist/components/BaseDocument.js +1 -3
  22. package/dist/components/Body.js +3 -1
  23. package/dist/components/Document.js +3 -1
  24. package/dist/components/Head.d.ts +1 -1
  25. package/dist/components/Head.js +37 -34
  26. package/dist/components/Link.d.ts +1 -1
  27. package/dist/components/Link.js +23 -18
  28. package/dist/dataLoader.d.ts +1 -1
  29. package/dist/dataLoader.js +14 -5
  30. package/dist/hooks/useData.d.ts +2 -6
  31. package/dist/hooks/useData.js +1 -1
  32. package/dist/index.d.ts +4 -0
  33. package/dist/index.js +3 -0
  34. package/dist/navigate.js +11 -8
  35. package/dist/server/runtime/xanixPage.js +1 -4
  36. package/dist/types/assets.d.ts +34 -0
  37. package/dist/types/express.d.ts +12 -0
  38. package/dist/types/globals.d.ts +27 -0
  39. package/dist/types/xanix-virtual.d.ts +26 -0
  40. package/package.json +75 -73
  41. package/dist/BaseDocument.d.ts +0 -3
  42. package/dist/BaseDocument.js +0 -8
  43. package/dist/bin/bundler/buildCache.d.ts +0 -11
  44. package/dist/bin/bundler/buildCache.js +0 -112
  45. package/dist/bin/bundler/plugins/XanixAssets/index.d.ts +0 -4
  46. package/dist/bin/bundler/plugins/XanixAssets/index.js +0 -22
  47. package/dist/bin/bundler/plugins/XanixBrowserCodeRemover.d.ts +0 -6
  48. package/dist/bin/bundler/plugins/XanixBrowserCodeRemover.js +0 -350
  49. package/dist/bin/bundler/plugins/XanixHydrate.d.ts +0 -3
  50. package/dist/bin/bundler/plugins/XanixHydrate.js +0 -34
  51. package/dist/bin/bundler/plugins/commonjs.d.ts +0 -3
  52. package/dist/bin/bundler/plugins/commonjs.js +0 -10
  53. package/dist/bin/bundler/plugins/dep-XanixWatchChange.d.ts +0 -9
  54. package/dist/bin/bundler/plugins/dep-XanixWatchChange.js +0 -27
  55. package/dist/bin/bundler/plugins/nodeResolve.d.ts +0 -3
  56. package/dist/bin/bundler/plugins/nodeResolve.js +0 -10
  57. package/dist/bin/bundler/plugins/rollupEsbuild.d.ts +0 -3
  58. package/dist/bin/bundler/plugins/rollupEsbuild.js +0 -14
  59. package/dist/bin/cli/start copy.d.ts +0 -2
  60. package/dist/bin/cli/start copy.js +0 -34
  61. package/dist/client/client-runtime.d.ts +0 -10
  62. package/dist/client/client-runtime.js +0 -98
  63. package/dist/client/context/RouteContext.d.ts +0 -8
  64. package/dist/client/context/RouteContext.js +0 -16
  65. package/dist/client/hooks/usePageProps.d.ts +0 -1
  66. package/dist/client/hooks/usePageProps.js +0 -2
  67. package/dist/client/hooks/useRoute.d.ts +0 -1
  68. package/dist/client/hooks/useRoute.js +0 -2
  69. package/dist/client/hooks/useSearchParams.d.ts +0 -1
  70. package/dist/client/hooks/useSearchParams.js +0 -2
  71. package/dist/client/index.d.ts +0 -2
  72. package/dist/client/index.js +0 -2
  73. package/dist/client/navigation.d.ts +0 -3
  74. package/dist/client/navigation.js +0 -27
  75. package/dist/client/runtime.d.ts +0 -10
  76. package/dist/client/runtime.js +0 -98
  77. package/dist/components/Document/context.d.ts +0 -11
  78. package/dist/components/Document/context.js +0 -14
  79. package/dist/components/Document/index.d.ts +0 -8
  80. package/dist/components/Document/index.js +0 -6
  81. package/dist/document/BaseDocument.d.ts +0 -3
  82. package/dist/document/BaseDocument.js +0 -8
  83. package/dist/document/Body.d.ts +0 -5
  84. package/dist/document/Body.js +0 -8
  85. package/dist/document/Document.d.ts +0 -7
  86. package/dist/document/Document.js +0 -9
  87. package/dist/document/DocumentContext.d.ts +0 -18
  88. package/dist/document/DocumentContext.js +0 -6
  89. package/dist/document/Head.d.ts +0 -5
  90. package/dist/document/Head.js +0 -41
  91. package/dist/document/Link.d.ts +0 -7
  92. package/dist/document/Link.js +0 -28
  93. package/dist/document/Script.d.ts +0 -3
  94. package/dist/document/Script.js +0 -5
  95. package/dist/document/index.d.ts +0 -8
  96. package/dist/document/index.js +0 -7
  97. package/dist/document/useDocument.d.ts +0 -2
  98. package/dist/document/useDocument.js +0 -10
  99. package/dist/hooks/useCookie.d.ts +0 -2
  100. package/dist/hooks/useCookie.js +0 -13
  101. package/dist/hooks/useRequest copy.d.ts +0 -2
  102. package/dist/hooks/useRequest copy.js +0 -8
  103. package/dist/navigation/index.d.ts +0 -2
  104. package/dist/navigation/index.js +0 -2
  105. package/dist/navigation/navigate.d.ts +0 -7
  106. package/dist/navigation/navigate.js +0 -31
  107. package/dist/navigation/useLocation.d.ts +0 -1
  108. package/dist/navigation/useLocation.js +0 -22
  109. package/dist/router/index.d.ts +0 -3
  110. package/dist/router/index.js +0 -3
  111. package/dist/router/useParams.d.ts +0 -1
  112. package/dist/router/useParams.js +0 -5
  113. package/dist/router/usePathname.d.ts +0 -1
  114. package/dist/router/usePathname.js +0 -8
  115. package/dist/router/useSearchParams.d.ts +0 -11
  116. package/dist/router/useSearchParams.js +0 -74
  117. package/dist/runtime/createServer.d.ts +0 -12
  118. package/dist/runtime/createServer.js +0 -40
  119. package/dist/runtime/index.d.ts +0 -12
  120. package/dist/runtime/index.js +0 -40
  121. package/dist/runtime/readManifest.d.ts +0 -14
  122. package/dist/runtime/readManifest.js +0 -8
  123. package/dist/runtime/xanixPage.d.ts +0 -9
  124. package/dist/runtime/xanixPage.js +0 -62
  125. package/dist/server/createServer.d.ts +0 -12
  126. package/dist/server/createServer.js +0 -40
  127. package/dist/server/page.d.ts +0 -9
  128. package/dist/server/page.js +0 -62
  129. package/dist/server/readManifest.d.ts +0 -14
  130. package/dist/server/readManifest.js +0 -8
  131. package/dist/server/runtime copy.d.ts +0 -12
  132. package/dist/server/runtime copy.js +0 -40
  133. package/dist/server/runtime.d.ts +0 -12
  134. package/dist/server/runtime.js +0 -40
  135. package/dist/server/xanixPage.d.ts +0 -9
  136. package/dist/server/xanixPage.js +0 -62
  137. package/dist/types.d.ts +0 -12
  138. package/dist/types.js +0 -7
  139. package/dist/utils/index.d.ts +0 -0
  140. package/dist/utils/index.js +0 -1
  141. package/dist/xanix.d.ts +0 -3
  142. package/dist/xanix.js +0 -40
  143. package/xanix-env.d.ts +0 -79
package/package.json CHANGED
@@ -1,73 +1,75 @@
1
- {
2
- "name": "xanix",
3
- "version": "1.0.4",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "module": "dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "bin": "./dist/bin/index.js",
9
- "type": "module",
10
- "files": [
11
- "dist",
12
- "xanix-env.d.ts"
13
- ],
14
- "exports": {
15
- ".": {
16
- "import": "./dist/index.js",
17
- "require": "./dist/index.js"
18
- },
19
- "./page": {
20
- "import": "./dist/server/page.js",
21
- "require": "./dist/server/page.js"
22
- },
23
- "./runtime": {
24
- "import": "./dist/server/runtime/index.js",
25
- "require": "./dist/server/runtime/index.js"
26
- }
27
- },
28
- "scripts": {
29
- "dev": "tsc --watch --preserveWatchOutput",
30
- "build": "tsc",
31
- "start": "node dist/index.js"
32
- },
33
- "keywords": [],
34
- "author": "Naxrul Ahmed",
35
- "license": "ISC",
36
- "dependencies": {
37
- "@babel/generator": "^8.0.0",
38
- "@babel/parser": "^8.0.4",
39
- "@babel/traverse": "^8.0.4",
40
- "@rollup/plugin-alias": "^6.0.0",
41
- "@rollup/plugin-commonjs": "^29.0.3",
42
- "@rollup/plugin-json": "^6.1.0",
43
- "@rollup/plugin-node-resolve": "^16.0.3",
44
- "@rollup/plugin-replace": "^6.0.3",
45
- "@rollup/plugin-url": "^8.0.2",
46
- "commander": "^15.0.0",
47
- "dotenv": "^17.4.2",
48
- "esbuild": "^0.28.2",
49
- "express": "^5.2.1",
50
- "picocolors": "^1.1.1",
51
- "react-refresh": "^0.18.0",
52
- "rollup": "^4.62.5",
53
- "rollup-plugin-esbuild": "^6.2.1",
54
- "tslib": "^2.8.1",
55
- "ws": "^8.21.3"
56
- },
57
- "devDependencies": {
58
- "@types/babel__core": "^7.20.5",
59
- "@types/express": "^5.0.6",
60
- "@types/node": "^26.2.0",
61
- "@types/prompts": "^2.4.9",
62
- "@types/react": "^19.2.17",
63
- "@types/react-dom": "^19.2.3",
64
- "@types/react-refresh": "^0.14.7",
65
- "@types/ws": "^8.18.1",
66
- "@xanui/core": "^1.3.38",
67
- "@xanui/icons": "^1.1.14",
68
- "@xanui/ui": "^1.2.24",
69
- "react": "^19.2.8",
70
- "react-dom": "^19.2.8",
71
- "typescript": "^5.9.3"
72
- }
73
- }
1
+ {
2
+ "name": "xanix",
3
+ "version": "1.0.7",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": "./dist/bin/index.js",
9
+ "type": "module",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.js"
17
+ },
18
+ "./page": {
19
+ "import": "./dist/server/page.js",
20
+ "require": "./dist/server/page.js"
21
+ },
22
+ "./runtime": {
23
+ "import": "./dist/server/runtime/index.js",
24
+ "require": "./dist/server/runtime/index.js"
25
+ },
26
+ "./BaseDocument": {
27
+ "types": "./dist/components/BaseDocument.d.ts",
28
+ "import": "./dist/components/BaseDocument.js"
29
+ }
30
+ },
31
+ "scripts": {
32
+ "dev": "tsc --watch --preserveWatchOutput",
33
+ "build": "tsc && node scripts/build-types.js"
34
+ },
35
+ "keywords": [],
36
+ "author": "Naxrul Ahmed",
37
+ "license": "ISC",
38
+ "dependencies": {
39
+ "@babel/generator": "^8.0.0",
40
+ "@babel/parser": "^8.0.4",
41
+ "@babel/traverse": "^8.0.4",
42
+ "@rollup/plugin-alias": "^6.0.0",
43
+ "@rollup/plugin-commonjs": "^29.0.3",
44
+ "@rollup/plugin-json": "^6.1.0",
45
+ "@rollup/plugin-node-resolve": "^16.0.3",
46
+ "@rollup/plugin-replace": "^6.0.3",
47
+ "@rollup/plugin-url": "^8.0.2",
48
+ "commander": "^15.0.0",
49
+ "dotenv": "^17.4.2",
50
+ "esbuild": "^0.28.2",
51
+ "express": "^5.2.1",
52
+ "picocolors": "^1.1.1",
53
+ "react-refresh": "^0.18.0",
54
+ "rollup": "^4.62.5",
55
+ "rollup-plugin-esbuild": "^6.2.1",
56
+ "tslib": "^2.8.1",
57
+ "ws": "^8.21.3"
58
+ },
59
+ "devDependencies": {
60
+ "@types/babel__core": "^7.20.5",
61
+ "@types/express": "^5.0.6",
62
+ "@types/node": "^26.2.0",
63
+ "@types/prompts": "^2.4.9",
64
+ "@types/react": "^19.2.17",
65
+ "@types/react-dom": "^19.2.3",
66
+ "@types/react-refresh": "^0.14.7",
67
+ "@types/ws": "^8.18.1",
68
+ "@xanui/core": "^1.3.38",
69
+ "@xanui/icons": "^1.1.14",
70
+ "@xanui/ui": "^1.2.24",
71
+ "react": "^19.2.8",
72
+ "react-dom": "^19.2.8",
73
+ "typescript": "^5.9.3"
74
+ }
75
+ }
@@ -1,3 +0,0 @@
1
- import { DocumentProps } from "./components/Document/index.js";
2
- declare const BaseDocument: ({ document, children }: DocumentProps) => import("react").JSX.Element;
3
- export default BaseDocument;
@@ -1,8 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import Head from "./components/Head.js";
3
- import Body from "./components/Body.js";
4
- import Document from "./components/Document/index.js";
5
- const BaseDocument = ({ document, children }) => {
6
- 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 })] }));
7
- };
8
- export default BaseDocument;
@@ -1,11 +0,0 @@
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;
@@ -1,112 +0,0 @@
1
- import path from "node:path";
2
- import { rollup } from "rollup";
3
- import { build } from "esbuild";
4
- import { createRequire } from "node:module";
5
- import xanixAssets from "./plugins/XanixAssets/index.js";
6
- import rollupEsbuild from "./plugins/rollupEsbuild.js";
7
- import { getDocumentFile } from "../include/utils.js";
8
- const require = createRequire(import.meta.url);
9
- const GenerateGraph = async (entries) => {
10
- const collector = new Map();
11
- const input = {};
12
- for (const entry of entries) {
13
- input[entry.name] = entry.file;
14
- }
15
- input["xanix-runtime"] = require.resolve("xanix/runtime");
16
- input["xanix-document"] = await getDocumentFile();
17
- const bundle = await rollup({
18
- input,
19
- plugins: [
20
- xanixAssets({
21
- external: true,
22
- }),
23
- rollupEsbuild({
24
- sourceMap: true,
25
- }),
26
- ],
27
- external(id) {
28
- if (id.startsWith(".") || path.isAbsolute(id)) {
29
- return false;
30
- }
31
- collector.set(id, {
32
- source: id,
33
- resolved: require.resolve(id, {
34
- paths: [process.cwd()],
35
- }),
36
- packageName: id,
37
- });
38
- return true;
39
- },
40
- });
41
- await bundle.close();
42
- return collector;
43
- };
44
- // Wraps each CJS dep in a virtual ESM file that re-exports named exports detected via require()
45
- const makeCjsWrapperPlugin = (wrappers) => ({
46
- name: "cjs-named-exports",
47
- setup(build) {
48
- build.onResolve({ filter: /\?cjs-wrapper$/ }, (args) => ({
49
- path: args.path,
50
- namespace: "cjs-wrapper",
51
- }));
52
- build.onLoad({ filter: /.*/, namespace: "cjs-wrapper" }, (args) => {
53
- const contents = wrappers.get(args.path);
54
- return { contents, resolveDir: process.cwd() };
55
- });
56
- },
57
- });
58
- const buildWrappers = (input) => {
59
- const wrappers = new Map();
60
- const wrappedInput = {};
61
- for (const [name, resolved] of Object.entries(input)) {
62
- const key = `${name}?cjs-wrapper`;
63
- let namedExports = [];
64
- try {
65
- const mod = require(resolved);
66
- namedExports = Object.keys(mod).filter((k) => k !== "__esModule" && k !== "default");
67
- }
68
- catch { }
69
- const lines = [
70
- `import __mod from ${JSON.stringify(resolved)};`,
71
- `export default __mod;`,
72
- ];
73
- if (namedExports.length > 0) {
74
- lines.push(`export const { ${namedExports.join(", ")} } = __mod;`);
75
- }
76
- wrappers.set(key, lines.join("\n"));
77
- wrappedInput[name] = key;
78
- }
79
- return { wrappers, wrappedInput };
80
- };
81
- const bundleDeps = async (input, outdir) => {
82
- const { wrappers, wrappedInput } = buildWrappers(input);
83
- await build({
84
- entryPoints: wrappedInput,
85
- bundle: true,
86
- splitting: true,
87
- format: "esm",
88
- target: "es2022",
89
- outdir,
90
- entryNames: "[name]",
91
- chunkNames: "chunks/[hash]",
92
- platform: "browser",
93
- sourcemap: true,
94
- plugins: [makeCjsWrapperPlugin(wrappers)],
95
- });
96
- };
97
- const metadata = new Map();
98
- const BuildCache = async (entries) => {
99
- const input = {};
100
- const dependencies = await GenerateGraph(entries);
101
- for (const dependency of dependencies.values()) {
102
- const entryName = dependency.source.replace(/^@/, "").replace(/\//g, "-");
103
- input[entryName] = dependency.resolved;
104
- metadata.set(dependency.source, {
105
- file: `${entryName}.js`,
106
- });
107
- }
108
- const cacheDir = path.resolve(process.cwd(), ".xanix/cache");
109
- await bundleDeps(input, cacheDir);
110
- return metadata;
111
- };
112
- export default BuildCache;
@@ -1,4 +0,0 @@
1
- declare const xanixAssets: (options?: {
2
- external: boolean;
3
- }) => import("rollup").Plugin<any>;
4
- export default xanixAssets;
@@ -1,22 +0,0 @@
1
- import url from "@rollup/plugin-url";
2
- const xanixAssets = (options) => {
3
- return url({
4
- include: [
5
- "**/*.jpg",
6
- "**/*.jpeg",
7
- "**/*.png",
8
- "**/*.gif",
9
- "**/*.webp",
10
- "**/*.svg",
11
- "**/*.ico",
12
- "**/*.woff",
13
- "**/*.woff2",
14
- "**/*.ttf",
15
- "**/*.eot",
16
- ],
17
- limit: 0,
18
- fileName: "assets/[name]-[hash][extname]",
19
- emitFiles: !(options?.external ?? false),
20
- });
21
- };
22
- export default xanixAssets;
@@ -1,6 +0,0 @@
1
- import type { Plugin } from "rollup";
2
- export interface XanixBrowserCodeOptions {
3
- target?: "server" | "client";
4
- globals?: string[];
5
- }
6
- export default function xanixBrowserCodeRemover(options?: XanixBrowserCodeOptions): Plugin;
@@ -1,350 +0,0 @@
1
- import { parse } from "@babel/parser";
2
- import traverse from "@babel/traverse";
3
- import generate from "@babel/generator";
4
- import * as t from "@babel/types";
5
- const DEFAULT_BROWSER_GLOBALS = [
6
- "window",
7
- "document",
8
- "localStorage",
9
- "sessionStorage",
10
- "navigator",
11
- "location",
12
- "history",
13
- "screen",
14
- "indexedDB",
15
- "caches",
16
- "WebSocket",
17
- "File",
18
- "FileReader",
19
- "Blob",
20
- "FormData",
21
- "EventSource",
22
- "Worker",
23
- "SharedWorker",
24
- "IntersectionObserver",
25
- "ResizeObserver",
26
- "MutationObserver",
27
- "PerformanceObserver",
28
- "Notification",
29
- "BroadcastChannel",
30
- "MessageChannel",
31
- "MessageEvent",
32
- "URL",
33
- "URLSearchParams",
34
- "MediaSource",
35
- "MediaRecorder",
36
- "Audio",
37
- "Image",
38
- "HTMLImageElement",
39
- ];
40
- export default function xanixBrowserCodeRemover(options = {}) {
41
- const { target = "server", globals = DEFAULT_BROWSER_GLOBALS } = options;
42
- const browserGlobals = new Set(globals);
43
- return {
44
- name: "xanix-browser-code-remover",
45
- transform(code, id) {
46
- if (target !== "server") {
47
- return null;
48
- }
49
- if (id.includes("/node_modules/") || id.includes("\\node_modules\\")) {
50
- return null;
51
- }
52
- if (!/\.(?:[cm]?[jt]sx?)$/.test(id)) {
53
- return null;
54
- }
55
- const ast = parse(code, {
56
- sourceType: "module",
57
- plugins: [
58
- "jsx",
59
- "typescript",
60
- // "decorators-legacy",
61
- // "classProperties",
62
- // "classPrivateProperties",
63
- // "classPrivateMethods",
64
- // "optionalChaining",
65
- // "nullishCoalescingOperator",
66
- // "dynamicImport",
67
- // "topLevelAwait",
68
- ],
69
- });
70
- const removePaths = new Set();
71
- const replacePaths = new Set();
72
- function isBrowserGlobal(path) {
73
- const name = path.node.name;
74
- if (!browserGlobals.has(name)) {
75
- return false;
76
- }
77
- if (!path.isReferencedIdentifier()) {
78
- return false;
79
- }
80
- // Don't transform locally declared variables.
81
- if (path.scope.getBinding(name)) {
82
- return false;
83
- }
84
- return true;
85
- }
86
- function containsBrowserGlobal(path) {
87
- let found = false;
88
- path.traverse({
89
- Identifier(innerPath) {
90
- if (found) {
91
- return;
92
- }
93
- if (isBrowserGlobal(innerPath)) {
94
- found = true;
95
- }
96
- },
97
- });
98
- return found;
99
- }
100
- function getBrowserExpression(path) {
101
- let current = path;
102
- while (current.parentPath) {
103
- const parent = current.parentPath;
104
- /*
105
- * window.foo
106
- * window.foo.bar
107
- * window["foo"]
108
- */
109
- if (parent.isMemberExpression() ||
110
- parent.isOptionalMemberExpression()) {
111
- current = parent;
112
- continue;
113
- }
114
- /*
115
- * window.foo()
116
- * window.foo.bar()
117
- */
118
- if (parent.isCallExpression() || parent.isOptionalCallExpression()) {
119
- const callee = parent.get("callee");
120
- if (!Array.isArray(callee) && callee.node === current.node) {
121
- current = parent;
122
- continue;
123
- }
124
- break;
125
- }
126
- /*
127
- * window.foo!
128
- */
129
- if (parent.isTSNonNullExpression()) {
130
- current = parent;
131
- continue;
132
- }
133
- /*
134
- * window.foo as Something
135
- */
136
- if (parent.isTSAsExpression()) {
137
- current = parent;
138
- continue;
139
- }
140
- break;
141
- }
142
- return current;
143
- }
144
- function findContainingStatement(path) {
145
- let current = path;
146
- while (current.parentPath) {
147
- const parent = current.parentPath;
148
- if (parent.isExpressionStatement()) {
149
- return parent;
150
- }
151
- if (parent.isFunctionDeclaration() ||
152
- parent.isFunctionExpression() ||
153
- parent.isArrowFunctionExpression()) {
154
- return null;
155
- }
156
- current = parent;
157
- }
158
- return null;
159
- }
160
- function markRemove(path) {
161
- if (!path.removed) {
162
- removePaths.add(path);
163
- }
164
- }
165
- function markReplace(path) {
166
- if (!path.removed) {
167
- replacePaths.add(path);
168
- }
169
- }
170
- traverse(ast, {
171
- /*
172
- * ---------------------------------------------------------
173
- * JSX
174
- *
175
- * <div>{window.innerWidth}</div>
176
- *
177
- * ->
178
- *
179
- * <div>{undefined}</div>
180
- * ---------------------------------------------------------
181
- */
182
- JSXExpressionContainer(path) {
183
- const expression = path.get("expression");
184
- if (!expression || Array.isArray(expression)) {
185
- return;
186
- }
187
- if (expression.isJSXEmptyExpression()) {
188
- return;
189
- }
190
- if (!expression.isExpression()) {
191
- return;
192
- }
193
- if (!containsBrowserGlobal(expression)) {
194
- return;
195
- }
196
- markReplace(expression);
197
- },
198
- /*
199
- * ---------------------------------------------------------
200
- * Standalone browser statements
201
- *
202
- * window.addEventListener(...)
203
- * document.title = "Xanix"
204
- * localStorage.setItem(...)
205
- *
206
- * ->
207
- *
208
- * removed
209
- * ---------------------------------------------------------
210
- */
211
- ExpressionStatement(path) {
212
- if (!containsBrowserGlobal(path)) {
213
- return;
214
- }
215
- markRemove(path);
216
- },
217
- /*
218
- * ---------------------------------------------------------
219
- * Variable declarations
220
- *
221
- * const width = window.innerWidth;
222
- *
223
- * ->
224
- *
225
- * const width = undefined;
226
- * ---------------------------------------------------------
227
- */
228
- VariableDeclarator(path) {
229
- const init = path.get("init");
230
- if (!init || Array.isArray(init)) {
231
- return;
232
- }
233
- if (!init.isExpression()) {
234
- return;
235
- }
236
- if (!containsBrowserGlobal(init)) {
237
- return;
238
- }
239
- markReplace(init);
240
- },
241
- /*
242
- * ---------------------------------------------------------
243
- * Return
244
- *
245
- * return window.innerWidth;
246
- *
247
- * ->
248
- *
249
- * return undefined;
250
- * ---------------------------------------------------------
251
- */
252
- ReturnStatement(path) {
253
- const argument = path.get("argument");
254
- if (!argument || Array.isArray(argument)) {
255
- return;
256
- }
257
- if (!argument.isExpression()) {
258
- return;
259
- }
260
- if (!containsBrowserGlobal(argument)) {
261
- return;
262
- }
263
- markReplace(argument);
264
- },
265
- /*
266
- * ---------------------------------------------------------
267
- * Identifier
268
- * ---------------------------------------------------------
269
- */
270
- Identifier(path) {
271
- if (!isBrowserGlobal(path)) {
272
- return;
273
- }
274
- const browserExpression = getBrowserExpression(path);
275
- /*
276
- * JSXExpressionContainer is handled above.
277
- */
278
- if (browserExpression.parentPath?.isJSXExpressionContainer()) {
279
- return;
280
- }
281
- /*
282
- * Example:
283
- *
284
- * window.addEventListener(...)
285
- *
286
- * Remove the complete statement.
287
- */
288
- const statement = findContainingStatement(browserExpression);
289
- if (statement) {
290
- markRemove(statement);
291
- return;
292
- }
293
- const parent = browserExpression.parentPath;
294
- /*
295
- * Variable initializer:
296
- *
297
- * const x = window.foo;
298
- */
299
- if (parent?.isVariableDeclarator()) {
300
- markReplace(browserExpression);
301
- return;
302
- }
303
- /*
304
- * Return:
305
- *
306
- * return window.foo;
307
- */
308
- if (parent?.isReturnStatement()) {
309
- markReplace(browserExpression);
310
- return;
311
- }
312
- /*
313
- * General expression:
314
- *
315
- * console.log(window.innerWidth)
316
- *
317
- * ->
318
- *
319
- * console.log(undefined)
320
- */
321
- markReplace(browserExpression);
322
- },
323
- });
324
- /*
325
- * Remove first.
326
- */
327
- for (const path of removePaths) {
328
- if (!path.removed) {
329
- path.remove();
330
- }
331
- }
332
- /*
333
- * Replace second.
334
- */
335
- for (const path of replacePaths) {
336
- if (!path.removed) {
337
- path.replaceWith(t.identifier("undefined"));
338
- }
339
- }
340
- const output = generate(ast, {
341
- sourceMaps: true,
342
- sourceFileName: id,
343
- }, code);
344
- return {
345
- code: output.code,
346
- map: output.map,
347
- };
348
- },
349
- };
350
- }