@tamagui/static-worker 2.0.0-rc.4 → 2.0.0-rc.40

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.
@@ -2,33 +2,35 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var index_exports = {};
33
35
  __export(index_exports, {
34
36
  clearWorkerCache: () => clearWorkerCache,
@@ -42,37 +44,46 @@ __export(index_exports, {
42
44
  watchTamaguiConfig: () => watchTamaguiConfig
43
45
  });
44
46
  module.exports = __toCommonJS(index_exports);
45
- var import_node_url = require("node:url"),
46
- import_piscina = __toESM(require("piscina"), 1);
47
- const import_meta = {},
48
- getPragmaOptions = async props => {
49
- const {
50
- default: Static
51
- } = await import("@tamagui/static");
52
- return Static.getPragmaOptions(props);
53
- },
54
- getWorkerPath = () => typeof import_meta < "u" && import_meta.url ? (0, import_node_url.fileURLToPath)(import_meta.resolve("@tamagui/static/worker")).replace(/\.mjs$/, ".js") : require.resolve("@tamagui/static/worker").replace(/\.mjs$/, ".js"),
55
- POOL_KEY = "__tamagui_piscina_pool__",
56
- CLOSING_KEY = "__tamagui_piscina_closing__",
57
- TASK_COUNT_KEY = "__tamagui_piscina_task_count__",
58
- RECYCLING_KEY = "__tamagui_piscina_recycling__",
59
- MAX_TASKS_BEFORE_RECYCLE = 200;
47
+ var import_node_url = require("node:url");
48
+ var import_piscina = __toESM(require("piscina"), 1);
49
+ const import_meta = {};
50
+ const getPragmaOptions = async props => {
51
+ const {
52
+ default: Static
53
+ } = await import("@tamagui/static");
54
+ return Static.getPragmaOptions(props);
55
+ };
56
+ const getWorkerPath = () => {
57
+ if (typeof import_meta !== "undefined" && import_meta.url) {
58
+ const workerPath = (0, import_node_url.fileURLToPath)(import_meta.resolve("@tamagui/static/worker"));
59
+ return workerPath.replace(/\.mjs$/, ".js");
60
+ }
61
+ return require.resolve("@tamagui/static/worker").replace(/\.mjs$/, ".js");
62
+ };
63
+ const POOL_KEY = "__tamagui_piscina_pool__";
64
+ const CLOSING_KEY = "__tamagui_piscina_closing__";
65
+ const TASK_COUNT_KEY = "__tamagui_piscina_task_count__";
66
+ const RECYCLING_KEY = "__tamagui_piscina_recycling__";
67
+ const MAX_TASKS_BEFORE_RECYCLE = 1e3;
60
68
  function getSharedPool() {
61
69
  return globalThis[POOL_KEY] ?? null;
62
70
  }
63
71
  function setSharedPool(pool) {
72
+ ;
64
73
  globalThis[POOL_KEY] = pool;
65
74
  }
66
75
  function isClosing() {
67
- return globalThis[CLOSING_KEY] === !0;
76
+ return globalThis[CLOSING_KEY] === true;
68
77
  }
69
78
  function setClosing(value) {
79
+ ;
70
80
  globalThis[CLOSING_KEY] = value;
71
81
  }
72
82
  function isRecycling() {
73
- return globalThis[RECYCLING_KEY] === !0;
83
+ return globalThis[RECYCLING_KEY] === true;
74
84
  }
75
85
  function setRecycling(value) {
86
+ ;
76
87
  globalThis[RECYCLING_KEY] = value;
77
88
  }
78
89
  function getTaskCount() {
@@ -80,64 +91,87 @@ function getTaskCount() {
80
91
  }
81
92
  function incrementTaskCount() {
82
93
  const count = getTaskCount() + 1;
83
- return globalThis[TASK_COUNT_KEY] = count, count;
94
+ globalThis[TASK_COUNT_KEY] = count;
95
+ return count;
84
96
  }
85
97
  function resetTaskCount() {
98
+ ;
86
99
  globalThis[TASK_COUNT_KEY] = 0;
87
100
  }
88
101
  function createPool() {
89
102
  const pool = new import_piscina.default({
90
103
  filename: getWorkerPath(),
91
- // Single worker for state consistency and simpler caching
92
- minThreads: 1,
93
- maxThreads: 1,
104
+ // each worker loads and caches config independently
105
+ minThreads: 2,
106
+ maxThreads: 2,
94
107
  // Never terminate due to idle - worker stays alive until close() or process exit
95
108
  // This prevents "Terminating worker thread" errors from Piscina during idle
96
109
  idleTimeout: Number.POSITIVE_INFINITY
97
110
  // no resourceLimits - we rely on task-based recycling instead
98
111
  // V8 resourceLimits cause "Terminating worker thread" messages when hit
99
112
  });
100
- return pool.on("error", err => {
101
- isClosing() || isRecycling() || (err && typeof err == "object" && "message" in err ? String(err.message) : "").includes("Terminating worker thread") || console.error("[tamagui] Worker pool error:", err);
102
- }), pool;
113
+ pool.on("error", err => {
114
+ if (isClosing() || isRecycling()) return;
115
+ const message = err && typeof err === "object" && "message" in err ? String(err.message) : "";
116
+ if (message.includes("Terminating worker thread")) return;
117
+ console.error("[tamagui] Worker pool error:", err);
118
+ });
119
+ return pool;
103
120
  }
104
121
  function getPool() {
105
122
  let pool = getSharedPool();
106
- return pool || (pool = createPool(), setSharedPool(pool)), pool;
123
+ if (!pool) {
124
+ pool = createPool();
125
+ setSharedPool(pool);
126
+ }
127
+ return pool;
107
128
  }
108
129
  async function loadTamagui(options) {
109
- const pool = getPool(),
110
- task = {
111
- type: "extractToClassNames",
112
- source: "// dummy",
113
- sourcePath: "__dummy__.tsx",
114
- options: {
115
- components: ["tamagui"],
116
- ...options
117
- },
118
- shouldPrintDebug: !1
119
- };
130
+ const pool = getPool();
131
+ const task = {
132
+ type: "extractToClassNames",
133
+ source: "// dummy",
134
+ sourcePath: "__dummy__.tsx",
135
+ options: {
136
+ components: ["tamagui"],
137
+ ...options
138
+ },
139
+ shouldPrintDebug: false
140
+ };
120
141
  try {
121
- return await pool.run(task, {
142
+ await pool.run(task, {
122
143
  name: "runTask"
123
- }), {
124
- success: !0
144
+ });
145
+ return {
146
+ success: true
125
147
  };
126
148
  } catch (error) {
127
- throw console.error("[static-worker] Error loading Tamagui config:", error), error;
149
+ console.error("[static-worker] Error loading Tamagui config:", error);
150
+ throw error;
128
151
  }
129
152
  }
130
153
  async function recyclePool(options) {
131
154
  if (isClosing() || isRecycling()) return;
132
155
  const oldPool = getSharedPool();
133
156
  if (!oldPool) return;
134
- setRecycling(!0);
157
+ setRecycling(true);
135
158
  const start = Date.now();
136
159
  try {
137
- const originalStderr = process.stderr.write.bind(process.stderr),
138
- originalStdout = process.stdout.write.bind(process.stdout),
139
- filter = (chunk, ...args) => !!(typeof chunk == "string" ? chunk : chunk?.toString?.() || "").includes("Terminating worker thread");
140
- process.stderr.write = (chunk, ...args) => filter(chunk) ? !0 : originalStderr(chunk, ...args), process.stdout.write = (chunk, ...args) => filter(chunk) ? !0 : originalStdout(chunk, ...args);
160
+ const originalStderr = process.stderr.write.bind(process.stderr);
161
+ const originalStdout = process.stdout.write.bind(process.stdout);
162
+ const filter = (chunk, ...args) => {
163
+ const str = typeof chunk === "string" ? chunk : chunk?.toString?.() || "";
164
+ if (str.includes("Terminating worker thread")) return true;
165
+ return false;
166
+ };
167
+ process.stderr.write = (chunk, ...args) => {
168
+ if (filter(chunk)) return true;
169
+ return originalStderr(chunk, ...args);
170
+ };
171
+ process.stdout.write = (chunk, ...args) => {
172
+ if (filter(chunk)) return true;
173
+ return originalStdout(chunk, ...args);
174
+ };
141
175
  const newPool = createPool();
142
176
  setSharedPool(newPool);
143
177
  const warmupTask = {
@@ -147,17 +181,22 @@ async function recyclePool(options) {
147
181
  options: {
148
182
  ...options,
149
183
  // skip the "built config" log on warmup since it's a recycle
150
- _skipBuildLog: !0
184
+ _skipBuildLog: true
151
185
  },
152
- shouldPrintDebug: !1
186
+ shouldPrintDebug: false
153
187
  };
154
188
  await newPool.run(warmupTask, {
155
189
  name: "runTask"
156
- }), oldPool.removeAllListeners(), oldPool.destroy().catch(() => {}), setTimeout(() => {
157
- process.stderr.write = originalStderr, process.stdout.write = originalStdout;
158
- }), console.log(` \u267B\uFE0F [tamagui] recycled worker pool (${Date.now() - start}ms)`);
190
+ });
191
+ oldPool.removeAllListeners();
192
+ oldPool.destroy().catch(() => {});
193
+ setTimeout(() => {
194
+ process.stderr.write = originalStderr;
195
+ process.stdout.write = originalStdout;
196
+ });
197
+ console.log(` \u267B\uFE0F [tamagui] recycled worker pool (${Date.now() - start}ms)`);
159
198
  } finally {
160
- setRecycling(!1);
199
+ setRecycling(false);
161
200
  }
162
201
  }
163
202
  async function loadTamaguiBuildConfig(tamaguiOptions) {
@@ -171,55 +210,72 @@ async function extractToClassNames(params) {
171
210
  source,
172
211
  sourcePath = "",
173
212
  options,
174
- shouldPrintDebug = !1
213
+ shouldPrintDebug = false
175
214
  } = params;
176
- if (typeof source != "string") throw new Error("`source` must be a string of javascript");
215
+ if (typeof source !== "string") {
216
+ throw new Error("`source` must be a string of javascript");
217
+ }
177
218
  const task = {
178
- type: "extractToClassNames",
179
- source,
180
- sourcePath,
181
- options,
182
- shouldPrintDebug
183
- },
184
- result = await getPool().run(task, {
185
- name: "runTask"
186
- });
219
+ type: "extractToClassNames",
220
+ source,
221
+ sourcePath,
222
+ options,
223
+ shouldPrintDebug
224
+ };
225
+ const pool = getPool();
226
+ const result = await pool.run(task, {
227
+ name: "runTask"
228
+ });
187
229
  if (!result.success) {
188
- const errorMessage = [`[tamagui-extract] Error processing file: ${sourcePath || "(unknown)"}`, "", result.error, result.stack ? `
189
- ${result.stack}` : ""].filter(Boolean).join(`
190
- `);
230
+ const errorMessage = [`[tamagui-extract] Error processing file: ${sourcePath || "(unknown)"}`, ``, result.error, result.stack ? `
231
+ ${result.stack}` : ""].filter(Boolean).join("\n");
191
232
  throw new Error(errorMessage);
192
233
  }
193
- return incrementTaskCount() >= MAX_TASKS_BEFORE_RECYCLE && (resetTaskCount(), recyclePool(options).catch(() => {})), result.data;
234
+ const count = incrementTaskCount();
235
+ if (count >= MAX_TASKS_BEFORE_RECYCLE) {
236
+ resetTaskCount();
237
+ recyclePool(options).catch(() => {});
238
+ }
239
+ return result.data;
194
240
  }
195
241
  async function extractToNative(sourceFileName, sourceCode, options) {
196
242
  const task = {
197
- type: "extractToNative",
198
- sourceFileName,
199
- sourceCode,
200
- options
201
- },
202
- result = await getPool().run(task, {
203
- name: "runTask"
204
- });
243
+ type: "extractToNative",
244
+ sourceFileName,
245
+ sourceCode,
246
+ options
247
+ };
248
+ const pool = getPool();
249
+ const result = await pool.run(task, {
250
+ name: "runTask"
251
+ });
205
252
  if (!result.success) {
206
- const errorMessage = [`[tamagui-extract] Error processing file: ${sourceFileName || "(unknown)"}`, "", result.error, result.stack ? `
207
- ${result.stack}` : ""].filter(Boolean).join(`
208
- `);
253
+ const errorMessage = [`[tamagui-extract] Error processing file: ${sourceFileName || "(unknown)"}`, ``, result.error, result.stack ? `
254
+ ${result.stack}` : ""].filter(Boolean).join("\n");
209
255
  throw new Error(errorMessage);
210
256
  }
211
- return incrementTaskCount() >= MAX_TASKS_BEFORE_RECYCLE && (resetTaskCount(), recyclePool(options).catch(() => {})), result.data;
257
+ const count = incrementTaskCount();
258
+ if (count >= MAX_TASKS_BEFORE_RECYCLE) {
259
+ resetTaskCount();
260
+ recyclePool(options).catch(() => {});
261
+ }
262
+ return result.data;
212
263
  }
213
264
  async function watchTamaguiConfig(options) {
214
265
  const {
215
- default: Static
216
- } = await import("@tamagui/static"),
217
- watcher = await Static.watchTamaguiConfig(options);
218
- if (!watcher) return;
266
+ default: Static
267
+ } = await import("@tamagui/static");
268
+ const watcher = await Static.watchTamaguiConfig(options);
269
+ if (!watcher) {
270
+ return;
271
+ }
219
272
  const originalDispose = watcher.dispose;
220
273
  return {
221
274
  dispose: () => {
222
- originalDispose(), getSharedPool() && clearWorkerCache();
275
+ originalDispose();
276
+ if (getSharedPool()) {
277
+ clearWorkerCache();
278
+ }
223
279
  }
224
280
  };
225
281
  }
@@ -236,21 +292,25 @@ async function clearWorkerCache() {
236
292
  async function destroyPool() {
237
293
  const pool = getSharedPool();
238
294
  if (pool) {
239
- setClosing(!0);
295
+ setClosing(true);
240
296
  try {
241
297
  await pool.close();
242
298
  } finally {
243
- setSharedPool(null), setClosing(!1);
299
+ setSharedPool(null);
300
+ setClosing(false);
244
301
  }
245
302
  }
246
303
  }
247
304
  function getPoolStats() {
248
305
  const pool = getSharedPool();
249
- return pool ? {
306
+ if (!pool) {
307
+ return null;
308
+ }
309
+ return {
250
310
  threads: pool.threads.length,
251
311
  queueSize: pool.queueSize,
252
312
  completed: pool.completed,
253
313
  duration: pool.duration,
254
314
  utilization: pool.utilization
255
- } : null;
315
+ };
256
316
  }