@tamagui/compiler-core 0.0.0-bootstrap.0 → 3.0.0-beta.637.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.
Files changed (98) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -1
  3. package/dist/cjs/ast.cjs +95 -0
  4. package/dist/cjs/contracts.cjs +62 -0
  5. package/dist/cjs/diagnostics.cjs +69 -0
  6. package/dist/cjs/evaluate.cjs +351 -0
  7. package/dist/cjs/graph.cjs +279 -0
  8. package/dist/cjs/hash.cjs +49 -0
  9. package/dist/cjs/index.cjs +44 -0
  10. package/dist/cjs/ir.cjs +33 -0
  11. package/dist/cjs/lower.cjs +225 -0
  12. package/dist/cjs/materialize.cjs +200 -0
  13. package/dist/cjs/normalize.cjs +622 -0
  14. package/dist/cjs/output.cjs +149 -0
  15. package/dist/cjs/planCache.cjs +224 -0
  16. package/dist/cjs/session.cjs +119 -0
  17. package/dist/cjs/yuku.cjs +159 -0
  18. package/dist/cjs/zero.cjs +387 -0
  19. package/dist/esm/ast.mjs +65 -0
  20. package/dist/esm/ast.mjs.map +1 -0
  21. package/dist/esm/contracts.mjs +34 -0
  22. package/dist/esm/contracts.mjs.map +1 -0
  23. package/dist/esm/diagnostics.mjs +44 -0
  24. package/dist/esm/diagnostics.mjs.map +1 -0
  25. package/dist/esm/evaluate.mjs +327 -0
  26. package/dist/esm/evaluate.mjs.map +1 -0
  27. package/dist/esm/graph.mjs +256 -0
  28. package/dist/esm/graph.mjs.map +1 -0
  29. package/dist/esm/hash.mjs +26 -0
  30. package/dist/esm/hash.mjs.map +1 -0
  31. package/dist/esm/index.mjs +30 -0
  32. package/dist/esm/ir.mjs +12 -0
  33. package/dist/esm/ir.mjs.map +1 -0
  34. package/dist/esm/lower.mjs +202 -0
  35. package/dist/esm/lower.mjs.map +1 -0
  36. package/dist/esm/materialize.mjs +179 -0
  37. package/dist/esm/materialize.mjs.map +1 -0
  38. package/dist/esm/normalize.mjs +596 -0
  39. package/dist/esm/normalize.mjs.map +1 -0
  40. package/dist/esm/output.mjs +119 -0
  41. package/dist/esm/output.mjs.map +1 -0
  42. package/dist/esm/planCache.mjs +194 -0
  43. package/dist/esm/planCache.mjs.map +1 -0
  44. package/dist/esm/session.mjs +99 -0
  45. package/dist/esm/session.mjs.map +1 -0
  46. package/dist/esm/yuku.mjs +136 -0
  47. package/dist/esm/yuku.mjs.map +1 -0
  48. package/dist/esm/zero.mjs +352 -0
  49. package/dist/esm/zero.mjs.map +1 -0
  50. package/package.json +42 -4
  51. package/src/ast.ts +86 -0
  52. package/src/contracts.ts +148 -0
  53. package/src/diagnostics.ts +96 -0
  54. package/src/evaluate.ts +497 -0
  55. package/src/graph.ts +347 -0
  56. package/src/hash.ts +36 -0
  57. package/src/index.ts +15 -0
  58. package/src/ir.ts +154 -0
  59. package/src/lower.ts +418 -0
  60. package/src/materialize.ts +330 -0
  61. package/src/normalize.ts +846 -0
  62. package/src/output.ts +175 -0
  63. package/src/planCache.ts +327 -0
  64. package/src/session.ts +172 -0
  65. package/src/yuku.ts +189 -0
  66. package/src/zero.ts +598 -0
  67. package/types/ast.d.ts +10 -0
  68. package/types/ast.d.ts.map +1 -0
  69. package/types/contracts.d.ts +97 -0
  70. package/types/contracts.d.ts.map +1 -0
  71. package/types/diagnostics.d.ts +31 -0
  72. package/types/diagnostics.d.ts.map +1 -0
  73. package/types/evaluate.d.ts +36 -0
  74. package/types/evaluate.d.ts.map +1 -0
  75. package/types/graph.d.ts +37 -0
  76. package/types/graph.d.ts.map +1 -0
  77. package/types/hash.d.ts +8 -0
  78. package/types/hash.d.ts.map +1 -0
  79. package/types/index.d.ts +16 -0
  80. package/types/index.d.ts.map +1 -0
  81. package/types/ir.d.ts +118 -0
  82. package/types/ir.d.ts.map +1 -0
  83. package/types/lower.d.ts +99 -0
  84. package/types/lower.d.ts.map +1 -0
  85. package/types/materialize.d.ts +104 -0
  86. package/types/materialize.d.ts.map +1 -0
  87. package/types/normalize.d.ts +8 -0
  88. package/types/normalize.d.ts.map +1 -0
  89. package/types/output.d.ts +32 -0
  90. package/types/output.d.ts.map +1 -0
  91. package/types/planCache.d.ts +113 -0
  92. package/types/planCache.d.ts.map +1 -0
  93. package/types/session.d.ts +44 -0
  94. package/types/session.d.ts.map +1 -0
  95. package/types/yuku.d.ts +4 -0
  96. package/types/yuku.d.ts.map +1 -0
  97. package/types/zero.d.ts +98 -0
  98. package/types/zero.d.ts.map +1 -0
@@ -0,0 +1,149 @@
1
+
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all) __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true
12
+ });
13
+ };
14
+ var __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
+ get: () => from[key],
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var output_exports = {};
29
+ __export(output_exports, {
30
+ applyLoweredModule: () => applyLoweredModule,
31
+ sourceContentHash: () => sourceContentHash,
32
+ validateSourceEdits: () => validateSourceEdits
33
+ });
34
+ module.exports = __toCommonJS(output_exports);
35
+ var import_gen_mapping = require("@jridgewell/gen-mapping");
36
+ var import_magic_string = __toESM(require("magic-string"), 1);
37
+ var import_hash = require("./hash.cjs");
38
+ function compareEdits(left, right) {
39
+ return left.start - right.start || left.end - right.end || left.index - right.index;
40
+ }
41
+ function sourceContentHash(source) {
42
+ return (0, import_hash.contentHash)(source);
43
+ }
44
+ function validateSourceEdits(source, edits) {
45
+ const sorted = edits.map((edit, index) => ({
46
+ ...edit,
47
+ index
48
+ })).sort(compareEdits);
49
+ let previousEnd = 0;
50
+ for (const edit of sorted) {
51
+ if (!Number.isInteger(edit.start) || !Number.isInteger(edit.end) || edit.start < 0 || edit.end < edit.start || edit.end > source.length) {
52
+ throw new Error(`Invalid UTF-16 source edit [${edit.start}, ${edit.end})`);
53
+ }
54
+ if (edit.start < previousEnd) {
55
+ throw new Error(`Overlapping UTF-16 source edit at ${edit.start}`);
56
+ }
57
+ previousEnd = Math.max(previousEnd, edit.end);
58
+ }
59
+ }
60
+ function sourcePositions(source) {
61
+ const positions = new Array(source.length + 1);
62
+ let line = 0;
63
+ let column = 0;
64
+ for (let index = 0; index <= source.length; index++) {
65
+ positions[index] = {
66
+ line,
67
+ column
68
+ };
69
+ if (index === source.length) break;
70
+ if (source.charCodeAt(index) === 10) {
71
+ line++;
72
+ column = 0;
73
+ } else {
74
+ column++;
75
+ }
76
+ }
77
+ return positions;
78
+ }
79
+ function mappedSourceMap(source, id, edits) {
80
+ const map = new import_gen_mapping.GenMapping();
81
+ (0, import_gen_mapping.setSourceContent)(map, id, source);
82
+ const positions = sourcePositions(source);
83
+ let generatedLine = 0;
84
+ let generatedColumn = 0;
85
+ let sourceCursor = 0;
86
+ const emit = (content, sourceStart, sourceEnd) => {
87
+ const available = Math.max(1, sourceEnd - sourceStart);
88
+ for (let offset = 0; offset < content.length; offset++) {
89
+ const originalIndex = Math.min(sourceStart + offset, sourceStart + available - 1);
90
+ const original = positions[originalIndex];
91
+ (0, import_gen_mapping.addSegment)(map, generatedLine, generatedColumn, id, original.line, original.column);
92
+ if (content.charCodeAt(offset) === 10) {
93
+ generatedLine++;
94
+ generatedColumn = 0;
95
+ } else {
96
+ generatedColumn++;
97
+ }
98
+ }
99
+ };
100
+ for (const edit of edits) {
101
+ emit(source.slice(sourceCursor, edit.start), sourceCursor, edit.start);
102
+ if (edit.origin.id !== id) {
103
+ throw new Error(`Source edit for ${id} must use a local mapping origin`);
104
+ }
105
+ emit(edit.content, edit.origin.start, edit.start === edit.end ? edit.origin.start + 1 : edit.origin.end);
106
+ sourceCursor = edit.end;
107
+ }
108
+ emit(source.slice(sourceCursor), sourceCursor, source.length);
109
+ const encoded = (0, import_gen_mapping.toEncodedMap)(map);
110
+ return {
111
+ version: 3,
112
+ file: encoded.file ?? void 0,
113
+ names: encoded.names,
114
+ sources: encoded.sources,
115
+ sourcesContent: encoded.sourcesContent ?? [source],
116
+ mappings: encoded.mappings
117
+ };
118
+ }
119
+ function applyLoweredModule(source, id, plan) {
120
+ if (plan.id !== id) {
121
+ throw new Error(`Lowered module plan ${plan.id} cannot be applied to ${id}`);
122
+ }
123
+ if (plan.sourceHash !== sourceContentHash(source)) {
124
+ throw new Error(`Lowered module plan for ${id} does not match the supplied source`);
125
+ }
126
+ if (plan.edits.length === 0) return {
127
+ changed: false,
128
+ code: source,
129
+ map: null
130
+ };
131
+ validateSourceEdits(source, plan.edits);
132
+ const output = new import_magic_string.default(source);
133
+ const sorted = plan.edits.map((edit, index) => ({
134
+ ...edit,
135
+ index
136
+ })).sort(compareEdits);
137
+ for (const edit of sorted) {
138
+ if (edit.start === edit.end) {
139
+ output.appendLeft(edit.start, edit.content);
140
+ } else {
141
+ output.overwrite(edit.start, edit.end, edit.content);
142
+ }
143
+ }
144
+ return {
145
+ changed: true,
146
+ code: output.toString(),
147
+ map: mappedSourceMap(source, id, sorted)
148
+ };
149
+ }
@@ -0,0 +1,224 @@
1
+
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all) __defProp(target, name, {
8
+ get: all[name],
9
+ enumerable: true
10
+ });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var planCache_exports = {};
23
+ __export(planCache_exports, {
24
+ JsonFileCache: () => JsonFileCache,
25
+ ModulePlanCache: () => ModulePlanCache,
26
+ PLAN_CACHE_MAX_ENTRIES: () => PLAN_CACHE_MAX_ENTRIES,
27
+ PLAN_CACHE_SCHEMA_VERSION: () => PLAN_CACHE_SCHEMA_VERSION,
28
+ defaultPlanCacheRoot: () => defaultPlanCacheRoot,
29
+ moduleClosureDigest: () => moduleClosureDigest,
30
+ moduleClosureNode: () => moduleClosureNode,
31
+ planCacheKey: () => planCacheKey,
32
+ tamaguiCacheRoot: () => tamaguiCacheRoot
33
+ });
34
+ module.exports = __toCommonJS(planCache_exports);
35
+ var import_node_crypto = require("node:crypto");
36
+ var import_promises = require("node:fs/promises");
37
+ var import_node_path = require("node:path");
38
+ var import_hash = require("./hash.cjs");
39
+ var import_lower = require("./lower.cjs");
40
+ var import_graph = require("./graph.cjs");
41
+ const PLAN_CACHE_SCHEMA_VERSION = 1;
42
+ const PLAN_CACHE_MAX_ENTRIES = 2e4;
43
+ function tamaguiCacheRoot(projectRoot) {
44
+ return (0, import_node_path.join)(projectRoot, "node_modules", ".cache", "tamagui");
45
+ }
46
+ function defaultPlanCacheRoot(projectRoot, target) {
47
+ return (0, import_node_path.join)(tamaguiCacheRoot(projectRoot), "plans", target);
48
+ }
49
+ function moduleClosureNode(input) {
50
+ return {
51
+ contentHash: (0, import_graph.moduleContentHash)(input),
52
+ dependencies: input.imports.filter(({ external }) => !external).map(({ resolvedId }) => resolvedId)
53
+ };
54
+ }
55
+ function moduleClosureDigest(id, lookup, memo) {
56
+ const cached = memo?.get(id);
57
+ if (cached !== void 0) return cached;
58
+ const reached = /* @__PURE__ */ new Map();
59
+ const queue = [id];
60
+ let complete = true;
61
+ while (queue.length) {
62
+ const current = queue.pop();
63
+ if (reached.has(current)) continue;
64
+ const node = lookup(current);
65
+ if (!node) {
66
+ complete = false;
67
+ break;
68
+ }
69
+ reached.set(current, node.contentHash);
70
+ for (const dependency of node.dependencies) queue.push(dependency);
71
+ }
72
+ const digest = complete ? (0, import_hash.contentHash)([...reached].sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0).map(([current, hash]) => `${current}\0${hash}`).join("\0")) : null;
73
+ memo?.set(id, digest);
74
+ return digest;
75
+ }
76
+ function planCacheKey(identity, id, closureDigest) {
77
+ return (0, import_hash.contentHash)((0, import_hash.stableStringify)({
78
+ schema: PLAN_CACHE_SCHEMA_VERSION,
79
+ plan: import_lower.LOWERED_MODULE_PLAN_VERSION,
80
+ stamp: identity.stamp,
81
+ target: identity.target,
82
+ structuralPassHash: identity.structuralPassHash,
83
+ id,
84
+ closureDigest
85
+ }));
86
+ }
87
+ var JsonFileCache = class {
88
+ constructor(root, schemaVersion, maxEntries = PLAN_CACHE_MAX_ENTRIES) {
89
+ this.root = root;
90
+ this.schemaVersion = schemaVersion;
91
+ this.maxEntries = maxEntries;
92
+ }
93
+ root;
94
+ schemaVersion;
95
+ maxEntries;
96
+ #hits = 0;
97
+ #misses = 0;
98
+ #writes = 0;
99
+ #writesSincePrune = 0;
100
+ #prunedThisProcess = false;
101
+ #created = /* @__PURE__ */ new Set();
102
+ get stats() {
103
+ return {
104
+ hits: this.#hits,
105
+ misses: this.#misses,
106
+ writes: this.#writes
107
+ };
108
+ }
109
+ resetStats() {
110
+ this.#hits = 0;
111
+ this.#misses = 0;
112
+ this.#writes = 0;
113
+ }
114
+ #path(key) {
115
+ return (0, import_node_path.join)(this.root, `v${this.schemaVersion}`, key.slice(0, 2), `${key}.json`);
116
+ }
117
+ /**
118
+ * Null on anything short of an entry `validate` fully accepts. A miss
119
+ * recompiles; nothing here repairs or partially trusts an entry, and a later
120
+ * successful compile overwrites the bad file.
121
+ */
122
+ async read(key, validate) {
123
+ let parsed;
124
+ try {
125
+ parsed = JSON.parse(await (0, import_promises.readFile)(this.#path(key), "utf8"));
126
+ } catch {
127
+ this.#misses++;
128
+ return null;
129
+ }
130
+ const entry = validate(parsed);
131
+ if (entry === null) {
132
+ this.#misses++;
133
+ return null;
134
+ }
135
+ this.#hits++;
136
+ return entry;
137
+ }
138
+ async write(key, value) {
139
+ const path = this.#path(key);
140
+ const directory = (0, import_node_path.dirname)(path);
141
+ if (!this.#created.has(directory)) {
142
+ await (0, import_promises.mkdir)(directory, { recursive: true });
143
+ this.#created.add(directory);
144
+ }
145
+ const temporaryPath = `${path}.${process.pid}-${(0, import_node_crypto.randomBytes)(6).toString("hex")}.tmp`;
146
+ await (0, import_promises.writeFile)(temporaryPath, `${(0, import_hash.stableStringify)(value)}
147
+ `, "utf8");
148
+ await (0, import_promises.rename)(temporaryPath, path);
149
+ this.#writes++;
150
+ this.#writesSincePrune++;
151
+ const batch = Math.max(1, Math.floor(this.maxEntries / 4));
152
+ if (!this.#prunedThisProcess || this.#writesSincePrune >= batch) {
153
+ this.#prunedThisProcess = true;
154
+ this.#writesSincePrune = 0;
155
+ await this.#prune();
156
+ }
157
+ }
158
+ /**
159
+ * Drops the oldest entries once the store exceeds its cap, oldest by the later
160
+ * of read and write time so an entry a build keeps hitting is not treated as
161
+ * dead on filesystems that maintain access times.
162
+ */
163
+ async #prune() {
164
+ const versionRoot = (0, import_node_path.join)(this.root, `v${this.schemaVersion}`);
165
+ let shards;
166
+ try {
167
+ shards = await (0, import_promises.readdir)(versionRoot);
168
+ } catch {
169
+ return;
170
+ }
171
+ const paths = [];
172
+ for (const shard of shards) {
173
+ const directory = (0, import_node_path.join)(versionRoot, shard);
174
+ let files;
175
+ try {
176
+ files = await (0, import_promises.readdir)(directory);
177
+ } catch {
178
+ continue;
179
+ }
180
+ for (const file of files) {
181
+ if (file.endsWith(".json")) paths.push((0, import_node_path.join)(directory, file));
182
+ }
183
+ }
184
+ if (paths.length <= this.maxEntries) return;
185
+ const entries = [];
186
+ for (const path of paths) {
187
+ try {
188
+ const stats = await (0, import_promises.stat)(path);
189
+ entries.push({
190
+ path,
191
+ usedAt: Math.max(stats.atimeMs, stats.mtimeMs)
192
+ });
193
+ } catch {}
194
+ }
195
+ entries.sort((left, right) => left.usedAt - right.usedAt);
196
+ for (const entry of entries.slice(0, Math.max(0, entries.length - this.maxEntries))) {
197
+ await (0, import_promises.unlink)(entry.path).catch(() => {});
198
+ }
199
+ }
200
+ };
201
+ var ModulePlanCache = class {
202
+ constructor(root) {
203
+ this.root = root;
204
+ this.#files = new JsonFileCache(root, PLAN_CACHE_SCHEMA_VERSION);
205
+ }
206
+ root;
207
+ #files;
208
+ get stats() {
209
+ return this.#files.stats;
210
+ }
211
+ resetStats() {
212
+ this.#files.resetStats();
213
+ }
214
+ read(key, id, closureDigest) {
215
+ return this.#files.read(key, (value) => {
216
+ const entry = value;
217
+ const plan = entry?.plan;
218
+ return entry?.schemaVersion === PLAN_CACHE_SCHEMA_VERSION && entry.moduleId === id && entry.closureDigest === closureDigest && plan && plan.version === import_lower.LOWERED_MODULE_PLAN_VERSION && plan.id === id && typeof plan.sourceHash === "string" && typeof plan.css === "string" && Array.isArray(plan.edits) && Array.isArray(plan.diagnostics) && Array.isArray(plan.dependencies) && !!plan.stats ? entry : null;
219
+ });
220
+ }
221
+ write(key, entry) {
222
+ return this.#files.write(key, entry);
223
+ }
224
+ };
@@ -0,0 +1,119 @@
1
+
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all) __defProp(target, name, {
8
+ get: all[name],
9
+ enumerable: true
10
+ });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var session_exports = {};
23
+ __export(session_exports, { CompilerSession: () => CompilerSession });
24
+ module.exports = __toCommonJS(session_exports);
25
+ var import_graph = require("./graph.cjs");
26
+ var import_lower = require("./lower.cjs");
27
+ var import_materialize = require("./materialize.cjs");
28
+ var import_output = require("./output.cjs");
29
+ var import_planCache = require("./planCache.cjs");
30
+ var import_yuku = require("./yuku.cjs");
31
+ function compareIds(left, right) {
32
+ return left < right ? -1 : left > right ? 1 : 0;
33
+ }
34
+ var CompilerSession = class {
35
+ #graph = new import_graph.ProjectGraph(import_yuku.yukuFactory, { modules: [] });
36
+ #queue = Promise.resolve();
37
+ compile(input) {
38
+ const operation = this.#queue.then(() => this.#compile(input));
39
+ this.#queue = operation.catch(() => void 0);
40
+ return operation;
41
+ }
42
+ update(module2) {
43
+ return this.#enqueue(() => this.#graph.updateModule(module2).invalidatedIds);
44
+ }
45
+ has(id) {
46
+ return this.#graph.contentHash(id) !== null;
47
+ }
48
+ dependentsOf(id) {
49
+ return this.#graph.dependentsOf(id);
50
+ }
51
+ remove(id) {
52
+ return this.#enqueue(() => this.#graph.removeModule(id));
53
+ }
54
+ parseCount(id) {
55
+ return this.#graph.parseCount(id);
56
+ }
57
+ #enqueue(operation) {
58
+ const queued = this.#queue.then(operation);
59
+ this.#queue = queued.catch(() => void 0);
60
+ return queued;
61
+ }
62
+ async #compile({ module: module2, adapter, structuralPass }) {
63
+ const invalidated = /* @__PURE__ */ new Set();
64
+ await this.#install(module2, adapter, /* @__PURE__ */ new Set(), invalidated);
65
+ const graph = this.#graph;
66
+ const cache = adapter.planCache?.stamp ? adapter.planCache : null;
67
+ const closureDigest = cache ? (0, import_planCache.moduleClosureDigest)(module2.id, (id) => {
68
+ const hash = graph.contentHash(id);
69
+ return hash ? {
70
+ contentHash: hash,
71
+ dependencies: graph.dependenciesOf(id)
72
+ } : null;
73
+ }) : null;
74
+ const entry = cache && closureDigest ? {
75
+ store: cache.store,
76
+ digest: closureDigest,
77
+ key: (0, import_planCache.planCacheKey)({
78
+ stamp: cache.stamp,
79
+ target: adapter.target,
80
+ structuralPassHash: structuralPass?.versionHash ?? `${adapter.target}-noop-v1`
81
+ }, module2.id, closureDigest)
82
+ } : null;
83
+ const cached = entry ? await entry.store.read(entry.key, module2.id, entry.digest) : null;
84
+ const plan = cached?.plan ?? (0, import_lower.lowerModule)({
85
+ module: (0, import_materialize.materializeModule)(graph, module2.id),
86
+ source: module2.source,
87
+ target: adapter.target,
88
+ host: adapter.host,
89
+ options: { projectGeneration: adapter.projectGeneration },
90
+ structuralPass
91
+ });
92
+ if (entry && !cached) {
93
+ await entry.store.write(entry.key, {
94
+ schemaVersion: import_planCache.PLAN_CACHE_SCHEMA_VERSION,
95
+ moduleId: module2.id,
96
+ closureDigest: entry.digest,
97
+ plan
98
+ });
99
+ }
100
+ return {
101
+ plan,
102
+ output: (0, import_output.applyLoweredModule)(module2.source, module2.id, plan),
103
+ invalidatedIds: [...invalidated].sort(compareIds)
104
+ };
105
+ }
106
+ async #install(module2, adapter, visited, invalidated) {
107
+ if (visited.has(module2.id)) return;
108
+ visited.add(module2.id);
109
+ for (const dependency of module2.imports) {
110
+ if (dependency.external || this.has(dependency.resolvedId)) continue;
111
+ const loaded = await adapter.load(dependency.resolvedId);
112
+ if (loaded) {
113
+ await this.#install(loaded, adapter, visited, invalidated);
114
+ }
115
+ }
116
+ const update = this.#graph.updateModule(module2);
117
+ for (const id of update.invalidatedIds) invalidated.add(id);
118
+ }
119
+ };
@@ -0,0 +1,159 @@
1
+
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all) __defProp(target, name, {
8
+ get: all[name],
9
+ enumerable: true
10
+ });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var yuku_exports = {};
23
+ __export(yuku_exports, {
24
+ parseModuleAst: () => parseModuleAst,
25
+ yukuFactory: () => yukuFactory
26
+ });
27
+ module.exports = __toCommonJS(yuku_exports);
28
+ var import_yuku_analyzer = require("yuku-analyzer");
29
+ var import_contracts = require("./contracts.cjs");
30
+ var import_ast = require("./ast.cjs");
31
+ var import_normalize = require("./normalize.cjs");
32
+ var YukuCandidate = class {
33
+ constructor(project) {
34
+ this.project = project;
35
+ this.#analyzer = new import_yuku_analyzer.Analyzer({ resolve: (specifier, importer) => (0, import_contracts.resolveFromHost)(this.project.resolutions, specifier, importer) });
36
+ for (const [id, source] of project.files) this.addFile(id, source);
37
+ }
38
+ project;
39
+ name = "yuku";
40
+ #analyzer;
41
+ #parseCounts = /* @__PURE__ */ new Map();
42
+ addFile(id, source) {
43
+ this.#analyzer.addFile(id, source, {
44
+ preserveParens: true,
45
+ sourceType: "module"
46
+ });
47
+ this.#parseCounts.set(id, (this.#parseCounts.get(id) ?? 0) + 1);
48
+ }
49
+ removeFile(id) {
50
+ return this.#analyzer.removeFile(id);
51
+ }
52
+ link() {
53
+ this.#analyzer.link();
54
+ }
55
+ #module(id) {
56
+ const module2 = this.#analyzer.module(id);
57
+ if (!module2) throw new Error(`yuku: unknown module ${id}`);
58
+ return module2;
59
+ }
60
+ #canonicalSymbol(id, localName) {
61
+ const symbol = this.#module(id).resolve(localName);
62
+ return symbol ? this.#analyzer.definitionOf(symbol)?.symbol ?? null : null;
63
+ }
64
+ definitionOf(id, localName) {
65
+ const symbol = this.#canonicalSymbol(id, localName);
66
+ if (!symbol) return null;
67
+ const declaration = symbol.declarations[0];
68
+ if (!declaration) return null;
69
+ const program = (0, import_normalize.asAstNode)(symbol.module.ast, `${symbol.module.path} program`);
70
+ const normalizedDeclaration = (0, import_normalize.declarationForName)(program, symbol.name) ?? (0, import_normalize.asAstNode)(declaration, `${symbol.name} declaration`);
71
+ return (0, import_normalize.definitionFromDeclaration)(symbol.module.path, symbol.name, program, normalizedDeclaration);
72
+ }
73
+ referencesOf(definition) {
74
+ const symbol = this.#canonicalSymbol(definition.id, definition.name);
75
+ if (!symbol) return [];
76
+ return this.#analyzer.referencesOf(symbol).map(({ module: module2, reference }) => ({
77
+ id: (0, import_contracts.resolvedModuleId)(module2.path),
78
+ name: reference.name,
79
+ start: reference.node.start,
80
+ end: reference.node.end
81
+ }));
82
+ }
83
+ dependenciesOf(id) {
84
+ return this.#module(id).dependencies.map((module2) => module2.path).sort();
85
+ }
86
+ dependentsOf(id) {
87
+ return this.#module(id).dependents.map((module2) => module2.path).sort();
88
+ }
89
+ affectedBy(id) {
90
+ const affected = /* @__PURE__ */ new Set([id]);
91
+ const queue = [id];
92
+ while (queue.length) {
93
+ const current = queue.shift();
94
+ for (const dependent of this.dependentsOf(current)) {
95
+ if (!affected.has(dependent)) {
96
+ affected.add(dependent);
97
+ queue.push(dependent);
98
+ }
99
+ }
100
+ }
101
+ return [...affected].sort();
102
+ }
103
+ programOf(id) {
104
+ return (0, import_normalize.asAstNode)(this.#module(id).ast, `${id} program`);
105
+ }
106
+ sourceOf(id) {
107
+ return this.#module(id).source;
108
+ }
109
+ parseCount(id) {
110
+ return this.#parseCounts.get(id) ?? 0;
111
+ }
112
+ diagnostics() {
113
+ const parseDiagnostics = [...this.#analyzer.modules.values()].flatMap((module2) => module2.diagnostics.map((diagnostic) => ({
114
+ kind: "parse",
115
+ id: (0, import_contracts.resolvedModuleId)(module2.path),
116
+ message: diagnostic.message
117
+ })));
118
+ return [...parseDiagnostics, ...this.#analyzer.diagnostics.map((diagnostic) => ({
119
+ kind: "link",
120
+ id: (0, import_contracts.resolvedModuleId)(diagnostic.module),
121
+ message: diagnostic.message
122
+ }))];
123
+ }
124
+ };
125
+ const yukuFactory = {
126
+ name: "yuku",
127
+ create: (project) => new YukuCandidate(project),
128
+ scanImports(id, source) {
129
+ const candidate = new YukuCandidate({
130
+ files: /* @__PURE__ */ new Map([[id, source]]),
131
+ resolutions: /* @__PURE__ */ new Map()
132
+ });
133
+ const found = [];
134
+ (0, import_ast.walkAst)(candidate.programOf(id), (node) => {
135
+ if (node.type !== "ImportDeclaration" && node.type !== "ExportNamedDeclaration" && node.type !== "ExportAllDeclaration" && node.type !== "ImportExpression") {
136
+ return;
137
+ }
138
+ const sourceNode = (0, import_ast.childNode)(node, "source");
139
+ if (sourceNode && typeof sourceNode.value === "string") {
140
+ found.push({
141
+ start: node.start,
142
+ specifier: sourceNode.value
143
+ });
144
+ }
145
+ });
146
+ return [...new Set(found.sort((left, right) => left.start - right.start).map(({ specifier }) => specifier))];
147
+ }
148
+ };
149
+ function parseModuleAst(source, id = "/tamagui/compiler-module.tsx") {
150
+ const candidate = yukuFactory.create({
151
+ files: /* @__PURE__ */ new Map([[id, source]]),
152
+ resolutions: /* @__PURE__ */ new Map()
153
+ });
154
+ const diagnostics = candidate.diagnostics().filter((diagnostic) => diagnostic.kind === "parse");
155
+ if (diagnostics.length) {
156
+ throw new Error(`Unable to parse ${id}: ${diagnostics.map(({ message }) => message).join("; ")}`);
157
+ }
158
+ return candidate.programOf(id);
159
+ }