@tamagui/static 1.13.2 → 1.13.4
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.
- package/dist/cjs/constants.js +1 -52
- package/dist/cjs/constants.js.map +2 -2
- package/dist/cjs/extractor/accessSafe.js +1 -52
- package/dist/cjs/extractor/accessSafe.js.map +2 -2
- package/dist/cjs/extractor/babelParse.js +1 -66
- package/dist/cjs/extractor/babelParse.js.map +2 -2
- package/dist/cjs/extractor/buildClassName.js +1 -99
- package/dist/cjs/extractor/buildClassName.js.map +2 -2
- package/dist/cjs/extractor/bundle.js +1 -126
- package/dist/cjs/extractor/bundle.js.map +2 -2
- package/dist/cjs/extractor/createEvaluator.js +1 -86
- package/dist/cjs/extractor/createEvaluator.js.map +2 -2
- package/dist/cjs/extractor/createExtractor.js +15 -1630
- package/dist/cjs/extractor/createExtractor.js.map +2 -2
- package/dist/cjs/extractor/ensureImportingConcat.js +1 -64
- package/dist/cjs/extractor/ensureImportingConcat.js.map +2 -2
- package/dist/cjs/extractor/evaluateAstNode.js +1 -130
- package/dist/cjs/extractor/evaluateAstNode.js.map +2 -2
- package/dist/cjs/extractor/extractHelpers.js +1 -207
- package/dist/cjs/extractor/extractHelpers.js.map +2 -2
- package/dist/cjs/extractor/extractMediaStyle.js +1 -193
- package/dist/cjs/extractor/extractMediaStyle.js.map +2 -2
- package/dist/cjs/extractor/extractToClassNames.js +13 -413
- package/dist/cjs/extractor/extractToClassNames.js.map +2 -2
- package/dist/cjs/extractor/findTopmostFunction.js +1 -43
- package/dist/cjs/extractor/findTopmostFunction.js.map +2 -2
- package/dist/cjs/extractor/generatedUid.js +1 -59
- package/dist/cjs/extractor/generatedUid.js.map +2 -2
- package/dist/cjs/extractor/getPrefixLogs.js +1 -31
- package/dist/cjs/extractor/getPrefixLogs.js.map +2 -2
- package/dist/cjs/extractor/getPropValueFromAttributes.js +1 -89
- package/dist/cjs/extractor/getPropValueFromAttributes.js.map +2 -2
- package/dist/cjs/extractor/getSourceModule.js +1 -103
- package/dist/cjs/extractor/getSourceModule.js.map +2 -2
- package/dist/cjs/extractor/getStaticBindingsForScope.js +1 -225
- package/dist/cjs/extractor/getStaticBindingsForScope.js.map +2 -2
- package/dist/cjs/extractor/hoistClassNames.js +1 -74
- package/dist/cjs/extractor/hoistClassNames.js.map +2 -2
- package/dist/cjs/extractor/literalToAst.js +1 -115
- package/dist/cjs/extractor/literalToAst.js.map +2 -2
- package/dist/cjs/extractor/loadFile.js +2 -20
- package/dist/cjs/extractor/loadFile.js.map +2 -2
- package/dist/cjs/extractor/loadTamagui.js +15 -432
- package/dist/cjs/extractor/loadTamagui.js.map +2 -2
- package/dist/cjs/extractor/logLines.js +3 -43
- package/dist/cjs/extractor/logLines.js.map +2 -2
- package/dist/cjs/extractor/normalizeTernaries.js +1 -85
- package/dist/cjs/extractor/normalizeTernaries.js.map +2 -2
- package/dist/cjs/extractor/removeUnusedHooks.js +1 -109
- package/dist/cjs/extractor/removeUnusedHooks.js.map +2 -2
- package/dist/cjs/extractor/timer.js +1 -52
- package/dist/cjs/extractor/timer.js.map +2 -2
- package/dist/cjs/extractor/validHTMLAttributes.js +1 -126
- package/dist/cjs/extractor/validHTMLAttributes.js.map +2 -2
- package/dist/cjs/getPragmaOptions.js +2 -67
- package/dist/cjs/getPragmaOptions.js.map +2 -2
- package/dist/cjs/helpers/memoize.js +1 -45
- package/dist/cjs/helpers/memoize.js.map +2 -2
- package/dist/cjs/index.js +1 -42
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/require.js +5 -81
- package/dist/cjs/require.js.map +2 -2
- package/dist/cjs/types.js +1 -16
- package/dist/cjs/types.js.map +2 -2
- package/dist/cjs/webpackPlugin.js +1 -34
- package/dist/cjs/webpackPlugin.js.map +2 -2
- package/dist/esm/constants.js +1 -14
- package/dist/esm/constants.js.map +2 -2
- package/dist/esm/constants.mjs +1 -14
- package/dist/esm/constants.mjs.map +2 -2
- package/dist/esm/extractor/accessSafe.js +1 -18
- package/dist/esm/extractor/accessSafe.js.map +2 -2
- package/dist/esm/extractor/accessSafe.mjs +1 -18
- package/dist/esm/extractor/accessSafe.mjs.map +2 -2
- package/dist/esm/extractor/babelParse.js +1 -31
- package/dist/esm/extractor/babelParse.js.map +2 -2
- package/dist/esm/extractor/babelParse.mjs +1 -31
- package/dist/esm/extractor/babelParse.mjs.map +2 -2
- package/dist/esm/extractor/buildClassName.js +1 -64
- package/dist/esm/extractor/buildClassName.js.map +2 -2
- package/dist/esm/extractor/buildClassName.mjs +1 -64
- package/dist/esm/extractor/buildClassName.mjs.map +2 -2
- package/dist/esm/extractor/bundle.js +1 -91
- package/dist/esm/extractor/bundle.js.map +2 -2
- package/dist/esm/extractor/bundle.mjs +1 -91
- package/dist/esm/extractor/bundle.mjs.map +2 -2
- package/dist/esm/extractor/createEvaluator.js +1 -51
- package/dist/esm/extractor/createEvaluator.js.map +2 -2
- package/dist/esm/extractor/createEvaluator.mjs +1 -51
- package/dist/esm/extractor/createEvaluator.mjs.map +2 -2
- package/dist/esm/extractor/createExtractor.js +15 -1613
- package/dist/esm/extractor/createExtractor.js.map +2 -2
- package/dist/esm/extractor/createExtractor.mjs +15 -1613
- package/dist/esm/extractor/createExtractor.mjs.map +2 -2
- package/dist/esm/extractor/ensureImportingConcat.js +1 -30
- package/dist/esm/extractor/ensureImportingConcat.js.map +2 -2
- package/dist/esm/extractor/ensureImportingConcat.mjs +1 -30
- package/dist/esm/extractor/ensureImportingConcat.mjs.map +2 -2
- package/dist/esm/extractor/evaluateAstNode.js +1 -96
- package/dist/esm/extractor/evaluateAstNode.js.map +2 -2
- package/dist/esm/extractor/evaluateAstNode.mjs +1 -96
- package/dist/esm/extractor/evaluateAstNode.mjs.map +2 -2
- package/dist/esm/extractor/extractHelpers.js +1 -161
- package/dist/esm/extractor/extractHelpers.js.map +2 -2
- package/dist/esm/extractor/extractHelpers.mjs +1 -161
- package/dist/esm/extractor/extractHelpers.mjs.map +2 -2
- package/dist/esm/extractor/extractMediaStyle.js +1 -158
- package/dist/esm/extractor/extractMediaStyle.js.map +2 -2
- package/dist/esm/extractor/extractMediaStyle.mjs +1 -158
- package/dist/esm/extractor/extractMediaStyle.mjs.map +2 -2
- package/dist/esm/extractor/extractToClassNames.js +13 -379
- package/dist/esm/extractor/extractToClassNames.js.map +2 -2
- package/dist/esm/extractor/extractToClassNames.mjs +13 -379
- package/dist/esm/extractor/extractToClassNames.mjs.map +2 -2
- package/dist/esm/extractor/findTopmostFunction.js +1 -19
- package/dist/esm/extractor/findTopmostFunction.js.map +2 -2
- package/dist/esm/extractor/findTopmostFunction.mjs +1 -19
- package/dist/esm/extractor/findTopmostFunction.mjs.map +2 -2
- package/dist/esm/extractor/generatedUid.js +1 -25
- package/dist/esm/extractor/generatedUid.js.map +2 -2
- package/dist/esm/extractor/generatedUid.mjs +1 -25
- package/dist/esm/extractor/generatedUid.mjs.map +2 -2
- package/dist/esm/extractor/getPrefixLogs.js +1 -7
- package/dist/esm/extractor/getPrefixLogs.js.map +2 -2
- package/dist/esm/extractor/getPrefixLogs.mjs +1 -7
- package/dist/esm/extractor/getPrefixLogs.mjs.map +2 -2
- package/dist/esm/extractor/getPropValueFromAttributes.js +1 -55
- package/dist/esm/extractor/getPropValueFromAttributes.js.map +2 -2
- package/dist/esm/extractor/getPropValueFromAttributes.mjs +1 -55
- package/dist/esm/extractor/getPropValueFromAttributes.mjs.map +2 -2
- package/dist/esm/extractor/getSourceModule.js +1 -69
- package/dist/esm/extractor/getSourceModule.js.map +2 -2
- package/dist/esm/extractor/getSourceModule.mjs +1 -69
- package/dist/esm/extractor/getSourceModule.mjs.map +2 -2
- package/dist/esm/extractor/getStaticBindingsForScope.js +1 -190
- package/dist/esm/extractor/getStaticBindingsForScope.js.map +2 -2
- package/dist/esm/extractor/getStaticBindingsForScope.mjs +1 -190
- package/dist/esm/extractor/getStaticBindingsForScope.mjs.map +2 -2
- package/dist/esm/extractor/hoistClassNames.js +1 -40
- package/dist/esm/extractor/hoistClassNames.js.map +2 -2
- package/dist/esm/extractor/hoistClassNames.mjs +1 -40
- package/dist/esm/extractor/hoistClassNames.mjs.map +2 -2
- package/dist/esm/extractor/literalToAst.js +1 -80
- package/dist/esm/extractor/literalToAst.js.map +2 -2
- package/dist/esm/extractor/literalToAst.mjs +1 -80
- package/dist/esm/extractor/literalToAst.mjs.map +2 -2
- package/dist/esm/extractor/loadFile.js +2 -19
- package/dist/esm/extractor/loadFile.js.map +2 -2
- package/dist/esm/extractor/loadFile.mjs +2 -19
- package/dist/esm/extractor/loadFile.mjs.map +2 -2
- package/dist/esm/extractor/loadTamagui.js +15 -396
- package/dist/esm/extractor/loadTamagui.js.map +2 -2
- package/dist/esm/extractor/loadTamagui.mjs +15 -396
- package/dist/esm/extractor/loadTamagui.mjs.map +2 -2
- package/dist/esm/extractor/logLines.js +3 -19
- package/dist/esm/extractor/logLines.js.map +2 -2
- package/dist/esm/extractor/logLines.mjs +3 -19
- package/dist/esm/extractor/logLines.mjs.map +2 -2
- package/dist/esm/extractor/normalizeTernaries.js +1 -51
- package/dist/esm/extractor/normalizeTernaries.js.map +2 -2
- package/dist/esm/extractor/normalizeTernaries.mjs +1 -51
- package/dist/esm/extractor/normalizeTernaries.mjs.map +2 -2
- package/dist/esm/extractor/removeUnusedHooks.js +1 -75
- package/dist/esm/extractor/removeUnusedHooks.js.map +2 -2
- package/dist/esm/extractor/removeUnusedHooks.mjs +1 -75
- package/dist/esm/extractor/removeUnusedHooks.mjs.map +2 -2
- package/dist/esm/extractor/timer.js +1 -28
- package/dist/esm/extractor/timer.js.map +2 -2
- package/dist/esm/extractor/timer.mjs +1 -28
- package/dist/esm/extractor/timer.mjs.map +2 -2
- package/dist/esm/extractor/validHTMLAttributes.js +1 -101
- package/dist/esm/extractor/validHTMLAttributes.js.map +2 -2
- package/dist/esm/extractor/validHTMLAttributes.mjs +1 -101
- package/dist/esm/extractor/validHTMLAttributes.mjs.map +2 -2
- package/dist/esm/getPragmaOptions.js +2 -43
- package/dist/esm/getPragmaOptions.js.map +2 -2
- package/dist/esm/getPragmaOptions.mjs +2 -43
- package/dist/esm/getPragmaOptions.mjs.map +2 -2
- package/dist/esm/helpers/memoize.js +1 -21
- package/dist/esm/helpers/memoize.js.map +2 -2
- package/dist/esm/helpers/memoize.mjs +1 -21
- package/dist/esm/helpers/memoize.mjs.map +2 -2
- package/dist/esm/index.js +1 -15
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/index.mjs +1 -15
- package/dist/esm/index.mjs.map +2 -2
- package/dist/esm/require.js +5 -55
- package/dist/esm/require.js.map +2 -2
- package/dist/esm/require.mjs +5 -55
- package/dist/esm/require.mjs.map +2 -2
- package/dist/esm/webpackPlugin.js +1 -10
- package/dist/esm/webpackPlugin.js.map +2 -2
- package/dist/esm/webpackPlugin.mjs +1 -10
- package/dist/esm/webpackPlugin.mjs.map +2 -2
- package/package.json +14 -14
|
@@ -1,191 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirname, extname, join, resolve } from "path";
|
|
3
|
-
import * as t from "@babel/types";
|
|
4
|
-
import { evaluateAstNode } from "./evaluateAstNode.js";
|
|
5
|
-
import { getSourceModule } from "./getSourceModule.js";
|
|
6
|
-
const isLocalImport = (path) => path.startsWith(".") || path.startsWith("/");
|
|
7
|
-
function resolveImportPath(sourcePath, path) {
|
|
8
|
-
const sourceDir = dirname(sourcePath);
|
|
9
|
-
if (isLocalImport(path)) {
|
|
10
|
-
if (extname(path) === "") {
|
|
11
|
-
path += ".js";
|
|
12
|
-
}
|
|
13
|
-
return resolve(sourceDir, path);
|
|
14
|
-
}
|
|
15
|
-
return path;
|
|
16
|
-
}
|
|
17
|
-
const cache = /* @__PURE__ */ new Map();
|
|
18
|
-
const pending = /* @__PURE__ */ new Map();
|
|
19
|
-
const loadCmd = `${join(__dirname, "loadFile.js")}`;
|
|
20
|
-
let exited = false;
|
|
21
|
-
let child = null;
|
|
22
|
-
function forkChild() {
|
|
23
|
-
child = fork(loadCmd, [], {
|
|
24
|
-
execArgv: ["-r", "esbuild-register"],
|
|
25
|
-
detached: false,
|
|
26
|
-
stdio: "ignore"
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
function cleanupBeforeExit() {
|
|
30
|
-
if (exited)
|
|
31
|
-
return;
|
|
32
|
-
if (!child)
|
|
33
|
-
return;
|
|
34
|
-
child.removeAllListeners();
|
|
35
|
-
child.unref();
|
|
36
|
-
child.disconnect();
|
|
37
|
-
child.kill();
|
|
38
|
-
exited = true;
|
|
39
|
-
}
|
|
40
|
-
process.once("SIGTERM", cleanupBeforeExit);
|
|
41
|
-
process.once("SIGINT", cleanupBeforeExit);
|
|
42
|
-
process.once("beforeExit", cleanupBeforeExit);
|
|
43
|
-
function importModule(path) {
|
|
44
|
-
if (!child) {
|
|
45
|
-
forkChild();
|
|
46
|
-
}
|
|
47
|
-
if (pending.has(path)) {
|
|
48
|
-
return pending.get(path);
|
|
49
|
-
}
|
|
50
|
-
const promise = new Promise((res, rej) => {
|
|
51
|
-
if (!child)
|
|
52
|
-
return;
|
|
53
|
-
if (cache.size > 2e3) {
|
|
54
|
-
cache.clear();
|
|
55
|
-
}
|
|
56
|
-
if (cache.has(path)) {
|
|
57
|
-
return cache.get(path);
|
|
58
|
-
}
|
|
59
|
-
const listener = (msg) => {
|
|
60
|
-
if (!child)
|
|
61
|
-
return;
|
|
62
|
-
if (!msg)
|
|
63
|
-
return;
|
|
64
|
-
if (typeof msg !== "string")
|
|
65
|
-
return;
|
|
66
|
-
if (msg[0] === "-") {
|
|
67
|
-
rej(new Error(msg.slice(1)));
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
child.removeListener("message", listener);
|
|
71
|
-
const val = JSON.parse(msg);
|
|
72
|
-
cache.set(path, val);
|
|
73
|
-
res(val);
|
|
74
|
-
};
|
|
75
|
-
child.once("message", listener);
|
|
76
|
-
child.send(`${path.replace(".js", "")}`);
|
|
77
|
-
});
|
|
78
|
-
pending.set(path, promise);
|
|
79
|
-
return promise;
|
|
80
|
-
}
|
|
81
|
-
async function getStaticBindingsForScope(scope, whitelist = [], sourcePath, bindingCache, shouldPrintDebug) {
|
|
82
|
-
var _a;
|
|
83
|
-
const bindings = scope.getAllBindings();
|
|
84
|
-
const ret = {};
|
|
85
|
-
if (shouldPrintDebug) {
|
|
86
|
-
}
|
|
87
|
-
const program = scope.getProgramParent().block;
|
|
88
|
-
for (const node of program.body) {
|
|
89
|
-
if (t.isImportDeclaration(node)) {
|
|
90
|
-
const importPath = node.source.value;
|
|
91
|
-
if (!node.specifiers.length)
|
|
92
|
-
continue;
|
|
93
|
-
if (!isLocalImport(importPath)) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const moduleName = resolveImportPath(sourcePath, importPath);
|
|
97
|
-
const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test));
|
|
98
|
-
if (!isOnWhitelist)
|
|
99
|
-
continue;
|
|
100
|
-
try {
|
|
101
|
-
const src = await importModule(moduleName);
|
|
102
|
-
if (!src)
|
|
103
|
-
continue;
|
|
104
|
-
for (const specifier of node.specifiers) {
|
|
105
|
-
if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported)) {
|
|
106
|
-
if (typeof src[specifier.imported.name] !== "undefined") {
|
|
107
|
-
const val = src[specifier.local.name];
|
|
108
|
-
ret[specifier.local.name] = val;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
} catch (err) {
|
|
113
|
-
if (shouldPrintDebug) {
|
|
114
|
-
console.warn(
|
|
115
|
-
` | Skipping partial evaluation of constant file: ${moduleName} (DEBUG=tamagui for more)`
|
|
116
|
-
);
|
|
117
|
-
} else if ((_a = process.env.DEBUG) == null ? void 0 : _a.startsWith("tamagui")) {
|
|
118
|
-
console.log(`Error in partial evaluation`, err.message, err.stack);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (!bindingCache) {
|
|
124
|
-
throw new Error("BindingCache is a required param");
|
|
125
|
-
}
|
|
126
|
-
for (const k in bindings) {
|
|
127
|
-
const binding = bindings[k];
|
|
128
|
-
const sourceModule = getSourceModule(k, binding);
|
|
129
|
-
if (sourceModule) {
|
|
130
|
-
if (!sourceModule.sourceModule) {
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
const moduleName = resolveImportPath(sourcePath, sourceModule.sourceModule);
|
|
134
|
-
const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test));
|
|
135
|
-
if (isOnWhitelist) {
|
|
136
|
-
const src = importModule(moduleName);
|
|
137
|
-
if (!src) {
|
|
138
|
-
console.log(
|
|
139
|
-
` | \u26A0\uFE0F Missing file ${moduleName} via ${sourcePath} import ${sourceModule.sourceModule}?`
|
|
140
|
-
);
|
|
141
|
-
return {};
|
|
142
|
-
}
|
|
143
|
-
if (sourceModule.destructured) {
|
|
144
|
-
if (sourceModule.imported) {
|
|
145
|
-
ret[k] = src[sourceModule.imported];
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
ret[k] = src;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const { parent } = binding.path;
|
|
154
|
-
if (!t.isVariableDeclaration(parent) || parent.kind !== "const") {
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
const dec = parent.declarations.find((d) => t.isIdentifier(d.id) && d.id.name === k);
|
|
158
|
-
if (!dec || !dec.init) {
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
if (typeof dec.id.start !== "number" || typeof dec.id.end !== "number") {
|
|
162
|
-
console.error("dec.id.start/end is not a number");
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (!t.isIdentifier(dec.id)) {
|
|
166
|
-
console.error("dec is not an identifier");
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
const cacheKey = `${dec.id.name}_${dec.id.start}-${dec.id.end}`;
|
|
170
|
-
if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {
|
|
171
|
-
ret[k] = bindingCache[cacheKey];
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {
|
|
175
|
-
ret[k] = bindingCache[cacheKey];
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
try {
|
|
179
|
-
ret[k] = evaluateAstNode(dec.init, void 0, shouldPrintDebug);
|
|
180
|
-
bindingCache[cacheKey] = ret[k];
|
|
181
|
-
continue;
|
|
182
|
-
} catch {
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return ret;
|
|
186
|
-
}
|
|
187
|
-
export {
|
|
188
|
-
cleanupBeforeExit,
|
|
189
|
-
getStaticBindingsForScope
|
|
190
|
-
};
|
|
1
|
+
import{fork as W}from"child_process";import{dirname as $,extname as k,join as B,resolve as b}from"path";import*as m from"@babel/types";import{evaluateAstNode as x}from"./evaluateAstNode.js";import{getSourceModule as N}from"./getSourceModule.js";const h=i=>i.startsWith(".")||i.startsWith("/");function w(i,t){const d=$(i);return h(t)?(k(t)===""&&(t+=".js"),b(d,t)):t}const y=new Map,I=new Map,j=`${B(__dirname,"loadFile.js")}`;let E=!1,r=null;function R(){r=W(j,[],{execArgv:["-r","esbuild-register"],detached:!1,stdio:"ignore"})}function M(){E||r&&(r.removeAllListeners(),r.unref(),r.disconnect(),r.kill(),E=!0)}process.once("SIGTERM",M),process.once("SIGINT",M),process.once("beforeExit",M);function O(i){if(r||R(),I.has(i))return I.get(i);const t=new Promise((d,a)=>{if(!r)return;if(y.size>2e3&&y.clear(),y.has(i))return y.get(i);const u=f=>{if(!r||!f||typeof f!="string")return;if(f[0]==="-"){a(new Error(f.slice(1)));return}r.removeListener("message",u);const o=JSON.parse(f);y.set(i,o),d(o)};r.once("message",u),r.send(`${i.replace(".js","")}`)});return I.set(i,t),t}async function q(i,t=[],d,a,u){var P;const f=i.getAllBindings(),o={},S=i.getProgramParent().block;for(const n of S.body)if(m.isImportDeclaration(n)){const p=n.source.value;if(!n.specifiers.length||!h(p))continue;const s=w(d,p);if(!t.some(e=>s.endsWith(e)))continue;try{const e=await O(s);if(!e)continue;for(const c of n.specifiers)if(m.isImportSpecifier(c)&&m.isIdentifier(c.imported)&&typeof e[c.imported.name]<"u"){const l=e[c.local.name];o[c.local.name]=l}}catch(e){u?console.warn(` | Skipping partial evaluation of constant file: ${s} (DEBUG=tamagui for more)`):(P=process.env.DEBUG)!=null&&P.startsWith("tamagui")&&console.log("Error in partial evaluation",e.message,e.stack)}}if(!a)throw new Error("BindingCache is a required param");for(const n in f){const p=f[n],s=N(n,p);if(s){if(!s.sourceModule)continue;const l=w(d,s.sourceModule);if(t.some(g=>l.endsWith(g))){const g=O(l);if(!g)return console.log(` | \u26A0\uFE0F Missing file ${l} via ${d} import ${s.sourceModule}?`),{};s.destructured?s.imported&&(o[n]=g[s.imported]):o[n]=g}continue}const{parent:v}=p.path;if(!m.isVariableDeclaration(v)||v.kind!=="const")continue;const e=v.declarations.find(l=>m.isIdentifier(l.id)&&l.id.name===n);if(!e||!e.init)continue;if(typeof e.id.start!="number"||typeof e.id.end!="number"){console.error("dec.id.start/end is not a number");continue}if(!m.isIdentifier(e.id)){console.error("dec is not an identifier");continue}const c=`${e.id.name}_${e.id.start}-${e.id.end}`;if(Object.hasOwnProperty.call(a,c)){o[n]=a[c];continue}if(Object.hasOwnProperty.call(a,c)){o[n]=a[c];continue}try{o[n]=x(e.init,void 0,u),a[c]=o[n];continue}catch{}}return o}export{M as cleanupBeforeExit,q as getStaticBindingsForScope};
|
|
191
2
|
//# sourceMappingURL=getStaticBindingsForScope.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/extractor/getStaticBindingsForScope.ts"],
|
|
4
4
|
"sourcesContent": ["import { ChildProcess, fork } from 'child_process'\nimport { dirname, extname, join, resolve } from 'path'\n\nimport { Binding, NodePath } from '@babel/traverse'\nimport * as t from '@babel/types'\n\nimport { evaluateAstNode } from './evaluateAstNode.js'\nimport { getSourceModule } from './getSourceModule.js'\n\nconst isLocalImport = (path: string) => path.startsWith('.') || path.startsWith('/')\n\nfunction resolveImportPath(sourcePath: string, path: string) {\n const sourceDir = dirname(sourcePath)\n if (isLocalImport(path)) {\n if (extname(path) === '') {\n path += '.js'\n }\n return resolve(sourceDir, path)\n }\n return path\n}\n\nconst cache = new Map()\nconst pending = new Map<string, Promise<any>>()\n\nconst loadCmd = `${join(__dirname, 'loadFile.js')}`\n\nlet exited = false\n\nlet child: ChildProcess | null = null\n\nfunction forkChild() {\n child = fork(loadCmd, [], {\n execArgv: ['-r', 'esbuild-register'],\n detached: false,\n stdio: 'ignore',\n })\n}\n\nexport function cleanupBeforeExit() {\n if (exited) return\n if (!child) return\n child.removeAllListeners()\n child.unref()\n child.disconnect()\n child.kill()\n exited = true\n}\n\nprocess.once('SIGTERM', cleanupBeforeExit)\nprocess.once('SIGINT', cleanupBeforeExit)\nprocess.once('beforeExit', cleanupBeforeExit)\n\nfunction importModule(path: string) {\n if (!child) {\n forkChild()\n }\n if (pending.has(path)) {\n return pending.get(path)\n }\n const promise = new Promise((res, rej) => {\n if (!child) return\n if (cache.size > 2000) {\n cache.clear()\n }\n if (cache.has(path)) {\n return cache.get(path)\n }\n const listener = (msg: any) => {\n if (!child) return\n if (!msg) return\n if (typeof msg !== 'string') return\n if (msg[0] === '-') {\n rej(new Error(msg.slice(1)))\n return\n }\n child.removeListener('message', listener)\n const val = JSON.parse(msg)\n cache.set(path, val)\n res(val)\n }\n child.once('message', listener)\n child.send(`${path.replace('.js', '')}`)\n })\n pending.set(path, promise)\n return promise\n}\n\nexport async function getStaticBindingsForScope(\n scope: NodePath<t.JSXElement>['scope'],\n whitelist: string[] = [],\n sourcePath: string,\n bindingCache: Record<string, string | null>,\n shouldPrintDebug: boolean | 'verbose'\n): Promise<Record<string, any>> {\n const bindings: Record<string, Binding> = scope.getAllBindings() as any\n const ret: Record<string, any> = {}\n\n if (\n shouldPrintDebug\n ) {\n // prettier-ignore\n // console.log(' ', Object.keys(bindings).length, 'variables in scope')\n // .map(x => bindings[x].identifier?.name).join(', ')\n }\n\n // on react native at least it doesnt find some bindings? not sure why\n // lets add in whitelisted imports if they exist\n const program = scope.getProgramParent().block as t.Program\n for (const node of program.body) {\n if (t.isImportDeclaration(node)) {\n const importPath = node.source.value\n if (!node.specifiers.length) continue\n if (!isLocalImport(importPath)) {\n continue\n }\n const moduleName = resolveImportPath(sourcePath, importPath)\n const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test))\n if (!isOnWhitelist) continue\n try {\n const src = await importModule(moduleName)\n if (!src) continue\n for (const specifier of node.specifiers) {\n if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported)) {\n if (typeof src[specifier.imported.name] !== 'undefined') {\n const val = src[specifier.local.name]\n ret[specifier.local.name] = val\n }\n }\n }\n } catch (err: any) {\n if (shouldPrintDebug) {\n // eslint-disable-next-line no-console\n console.warn(\n ` | Skipping partial evaluation of constant file: ${moduleName} (DEBUG=tamagui for more)`\n )\n } else if (process.env.DEBUG?.startsWith('tamagui')) {\n // eslint-disable-next-line no-console\n console.log(`Error in partial evaluation`, err.message, err.stack)\n }\n }\n }\n }\n\n if (!bindingCache) {\n throw new Error('BindingCache is a required param')\n }\n\n for (const k in bindings) {\n const binding = bindings[k]\n\n // check to see if the item is a module\n const sourceModule = getSourceModule(k, binding)\n if (sourceModule) {\n if (!sourceModule.sourceModule) {\n continue\n }\n\n const moduleName = resolveImportPath(sourcePath, sourceModule.sourceModule)\n const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test))\n\n // TODO we could cache this at the file level.. and check if its been touched since\n\n if (isOnWhitelist) {\n const src = importModule(moduleName)\n if (!src) {\n // eslint-disable-next-line no-console\n console.log(\n ` | \u26A0\uFE0F Missing file ${moduleName} via ${sourcePath} import ${sourceModule.sourceModule}?`\n )\n return {}\n }\n if (sourceModule.destructured) {\n if (sourceModule.imported) {\n ret[k] = src[sourceModule.imported]\n }\n } else {\n ret[k] = src\n }\n }\n continue\n }\n\n const { parent } = binding.path\n\n if (!t.isVariableDeclaration(parent) || parent.kind !== 'const') {\n continue\n }\n\n // pick out the right variable declarator\n const dec = parent.declarations.find((d) => t.isIdentifier(d.id) && d.id.name === k)\n\n // if init is not set, there's nothing to evaluate\n // TODO: handle spread syntax\n if (!dec || !dec.init) {\n continue\n }\n\n // missing start/end will break caching\n if (typeof dec.id.start !== 'number' || typeof dec.id.end !== 'number') {\n // eslint-disable-next-line no-console\n console.error('dec.id.start/end is not a number')\n continue\n }\n\n if (!t.isIdentifier(dec.id)) {\n // eslint-disable-next-line no-console\n console.error('dec is not an identifier')\n continue\n }\n\n const cacheKey = `${dec.id.name}_${dec.id.start}-${dec.id.end}`\n\n // retrieve value from cache\n if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {\n ret[k] = bindingCache[cacheKey]\n continue\n }\n // retrieve value from cache\n if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {\n ret[k] = bindingCache[cacheKey]\n continue\n }\n\n // evaluate\n try {\n ret[k] = evaluateAstNode(dec.init, undefined, shouldPrintDebug)\n bindingCache[cacheKey] = ret[k]\n continue\n } catch {\n // skip\n }\n }\n\n return ret\n}\n"],
|
|
5
|
-
"mappings": "AAAA,
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "AAAA,OAAuB,QAAAA,MAAY,gBACnC,OAAS,WAAAC,EAAS,WAAAC,EAAS,QAAAC,EAAM,WAAAC,MAAe,OAGhD,UAAYC,MAAO,eAEnB,OAAS,mBAAAC,MAAuB,uBAChC,OAAS,mBAAAC,MAAuB,uBAEhC,MAAMC,EAAiBC,GAAiBA,EAAK,WAAW,GAAG,GAAKA,EAAK,WAAW,GAAG,EAEnF,SAASC,EAAkBC,EAAoBF,EAAc,CAC3D,MAAMG,EAAYX,EAAQU,CAAU,EACpC,OAAIH,EAAcC,CAAI,GAChBP,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AamB,CAAQ,EAAIf,EAAIS,CAAC,EAC9B,QACF,MAAE,CAEF,CACF,CAEA,OAAOT,CACT",
|
|
6
|
+
"names": ["fork", "dirname", "extname", "join", "resolve", "t", "evaluateAstNode", "getSourceModule", "isLocalImport", "path", "resolveImportPath", "sourcePath", "sourceDir", "cache", "pending", "loadCmd", "exited", "child", "forkChild", "cleanupBeforeExit", "importModule", "promise", "res", "rej", "listener", "msg", "val", "getStaticBindingsForScope", "scope", "whitelist", "bindingCache", "shouldPrintDebug", "_a", "bindings", "ret", "program", "node", "importPath", "moduleName", "test", "src", "specifier", "err", "k", "binding", "sourceModule", "parent", "dec", "d", "cacheKey"]
|
|
7
7
|
}
|
|
@@ -1,191 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirname, extname, join, resolve } from "path";
|
|
3
|
-
import * as t from "@babel/types";
|
|
4
|
-
import { evaluateAstNode } from "./evaluateAstNode.js";
|
|
5
|
-
import { getSourceModule } from "./getSourceModule.js";
|
|
6
|
-
const isLocalImport = (path) => path.startsWith(".") || path.startsWith("/");
|
|
7
|
-
function resolveImportPath(sourcePath, path) {
|
|
8
|
-
const sourceDir = dirname(sourcePath);
|
|
9
|
-
if (isLocalImport(path)) {
|
|
10
|
-
if (extname(path) === "") {
|
|
11
|
-
path += ".js";
|
|
12
|
-
}
|
|
13
|
-
return resolve(sourceDir, path);
|
|
14
|
-
}
|
|
15
|
-
return path;
|
|
16
|
-
}
|
|
17
|
-
const cache = /* @__PURE__ */ new Map();
|
|
18
|
-
const pending = /* @__PURE__ */ new Map();
|
|
19
|
-
const loadCmd = `${join(__dirname, "loadFile.js")}`;
|
|
20
|
-
let exited = false;
|
|
21
|
-
let child = null;
|
|
22
|
-
function forkChild() {
|
|
23
|
-
child = fork(loadCmd, [], {
|
|
24
|
-
execArgv: ["-r", "esbuild-register"],
|
|
25
|
-
detached: false,
|
|
26
|
-
stdio: "ignore"
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
function cleanupBeforeExit() {
|
|
30
|
-
if (exited)
|
|
31
|
-
return;
|
|
32
|
-
if (!child)
|
|
33
|
-
return;
|
|
34
|
-
child.removeAllListeners();
|
|
35
|
-
child.unref();
|
|
36
|
-
child.disconnect();
|
|
37
|
-
child.kill();
|
|
38
|
-
exited = true;
|
|
39
|
-
}
|
|
40
|
-
process.once("SIGTERM", cleanupBeforeExit);
|
|
41
|
-
process.once("SIGINT", cleanupBeforeExit);
|
|
42
|
-
process.once("beforeExit", cleanupBeforeExit);
|
|
43
|
-
function importModule(path) {
|
|
44
|
-
if (!child) {
|
|
45
|
-
forkChild();
|
|
46
|
-
}
|
|
47
|
-
if (pending.has(path)) {
|
|
48
|
-
return pending.get(path);
|
|
49
|
-
}
|
|
50
|
-
const promise = new Promise((res, rej) => {
|
|
51
|
-
if (!child)
|
|
52
|
-
return;
|
|
53
|
-
if (cache.size > 2e3) {
|
|
54
|
-
cache.clear();
|
|
55
|
-
}
|
|
56
|
-
if (cache.has(path)) {
|
|
57
|
-
return cache.get(path);
|
|
58
|
-
}
|
|
59
|
-
const listener = (msg) => {
|
|
60
|
-
if (!child)
|
|
61
|
-
return;
|
|
62
|
-
if (!msg)
|
|
63
|
-
return;
|
|
64
|
-
if (typeof msg !== "string")
|
|
65
|
-
return;
|
|
66
|
-
if (msg[0] === "-") {
|
|
67
|
-
rej(new Error(msg.slice(1)));
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
child.removeListener("message", listener);
|
|
71
|
-
const val = JSON.parse(msg);
|
|
72
|
-
cache.set(path, val);
|
|
73
|
-
res(val);
|
|
74
|
-
};
|
|
75
|
-
child.once("message", listener);
|
|
76
|
-
child.send(`${path.replace(".js", "")}`);
|
|
77
|
-
});
|
|
78
|
-
pending.set(path, promise);
|
|
79
|
-
return promise;
|
|
80
|
-
}
|
|
81
|
-
async function getStaticBindingsForScope(scope, whitelist = [], sourcePath, bindingCache, shouldPrintDebug) {
|
|
82
|
-
var _a;
|
|
83
|
-
const bindings = scope.getAllBindings();
|
|
84
|
-
const ret = {};
|
|
85
|
-
if (shouldPrintDebug) {
|
|
86
|
-
}
|
|
87
|
-
const program = scope.getProgramParent().block;
|
|
88
|
-
for (const node of program.body) {
|
|
89
|
-
if (t.isImportDeclaration(node)) {
|
|
90
|
-
const importPath = node.source.value;
|
|
91
|
-
if (!node.specifiers.length)
|
|
92
|
-
continue;
|
|
93
|
-
if (!isLocalImport(importPath)) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const moduleName = resolveImportPath(sourcePath, importPath);
|
|
97
|
-
const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test));
|
|
98
|
-
if (!isOnWhitelist)
|
|
99
|
-
continue;
|
|
100
|
-
try {
|
|
101
|
-
const src = await importModule(moduleName);
|
|
102
|
-
if (!src)
|
|
103
|
-
continue;
|
|
104
|
-
for (const specifier of node.specifiers) {
|
|
105
|
-
if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported)) {
|
|
106
|
-
if (typeof src[specifier.imported.name] !== "undefined") {
|
|
107
|
-
const val = src[specifier.local.name];
|
|
108
|
-
ret[specifier.local.name] = val;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
} catch (err) {
|
|
113
|
-
if (shouldPrintDebug) {
|
|
114
|
-
console.warn(
|
|
115
|
-
` | Skipping partial evaluation of constant file: ${moduleName} (DEBUG=tamagui for more)`
|
|
116
|
-
);
|
|
117
|
-
} else if ((_a = process.env.DEBUG) == null ? void 0 : _a.startsWith("tamagui")) {
|
|
118
|
-
console.log(`Error in partial evaluation`, err.message, err.stack);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (!bindingCache) {
|
|
124
|
-
throw new Error("BindingCache is a required param");
|
|
125
|
-
}
|
|
126
|
-
for (const k in bindings) {
|
|
127
|
-
const binding = bindings[k];
|
|
128
|
-
const sourceModule = getSourceModule(k, binding);
|
|
129
|
-
if (sourceModule) {
|
|
130
|
-
if (!sourceModule.sourceModule) {
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
const moduleName = resolveImportPath(sourcePath, sourceModule.sourceModule);
|
|
134
|
-
const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test));
|
|
135
|
-
if (isOnWhitelist) {
|
|
136
|
-
const src = importModule(moduleName);
|
|
137
|
-
if (!src) {
|
|
138
|
-
console.log(
|
|
139
|
-
` | \u26A0\uFE0F Missing file ${moduleName} via ${sourcePath} import ${sourceModule.sourceModule}?`
|
|
140
|
-
);
|
|
141
|
-
return {};
|
|
142
|
-
}
|
|
143
|
-
if (sourceModule.destructured) {
|
|
144
|
-
if (sourceModule.imported) {
|
|
145
|
-
ret[k] = src[sourceModule.imported];
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
ret[k] = src;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const { parent } = binding.path;
|
|
154
|
-
if (!t.isVariableDeclaration(parent) || parent.kind !== "const") {
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
const dec = parent.declarations.find((d) => t.isIdentifier(d.id) && d.id.name === k);
|
|
158
|
-
if (!dec || !dec.init) {
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
if (typeof dec.id.start !== "number" || typeof dec.id.end !== "number") {
|
|
162
|
-
console.error("dec.id.start/end is not a number");
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (!t.isIdentifier(dec.id)) {
|
|
166
|
-
console.error("dec is not an identifier");
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
const cacheKey = `${dec.id.name}_${dec.id.start}-${dec.id.end}`;
|
|
170
|
-
if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {
|
|
171
|
-
ret[k] = bindingCache[cacheKey];
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {
|
|
175
|
-
ret[k] = bindingCache[cacheKey];
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
try {
|
|
179
|
-
ret[k] = evaluateAstNode(dec.init, void 0, shouldPrintDebug);
|
|
180
|
-
bindingCache[cacheKey] = ret[k];
|
|
181
|
-
continue;
|
|
182
|
-
} catch {
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return ret;
|
|
186
|
-
}
|
|
187
|
-
export {
|
|
188
|
-
cleanupBeforeExit,
|
|
189
|
-
getStaticBindingsForScope
|
|
190
|
-
};
|
|
1
|
+
import{fork as W}from"child_process";import{dirname as $,extname as k,join as B,resolve as b}from"path";import*as m from"@babel/types";import{evaluateAstNode as x}from"./evaluateAstNode.js";import{getSourceModule as N}from"./getSourceModule.js";const h=i=>i.startsWith(".")||i.startsWith("/");function w(i,t){const d=$(i);return h(t)?(k(t)===""&&(t+=".js"),b(d,t)):t}const y=new Map,I=new Map,j=`${B(__dirname,"loadFile.js")}`;let E=!1,r=null;function R(){r=W(j,[],{execArgv:["-r","esbuild-register"],detached:!1,stdio:"ignore"})}function M(){E||r&&(r.removeAllListeners(),r.unref(),r.disconnect(),r.kill(),E=!0)}process.once("SIGTERM",M),process.once("SIGINT",M),process.once("beforeExit",M);function O(i){if(r||R(),I.has(i))return I.get(i);const t=new Promise((d,a)=>{if(!r)return;if(y.size>2e3&&y.clear(),y.has(i))return y.get(i);const u=f=>{if(!r||!f||typeof f!="string")return;if(f[0]==="-"){a(new Error(f.slice(1)));return}r.removeListener("message",u);const o=JSON.parse(f);y.set(i,o),d(o)};r.once("message",u),r.send(`${i.replace(".js","")}`)});return I.set(i,t),t}async function q(i,t=[],d,a,u){var P;const f=i.getAllBindings(),o={},S=i.getProgramParent().block;for(const n of S.body)if(m.isImportDeclaration(n)){const p=n.source.value;if(!n.specifiers.length||!h(p))continue;const s=w(d,p);if(!t.some(e=>s.endsWith(e)))continue;try{const e=await O(s);if(!e)continue;for(const c of n.specifiers)if(m.isImportSpecifier(c)&&m.isIdentifier(c.imported)&&typeof e[c.imported.name]<"u"){const l=e[c.local.name];o[c.local.name]=l}}catch(e){u?console.warn(` | Skipping partial evaluation of constant file: ${s} (DEBUG=tamagui for more)`):(P=process.env.DEBUG)!=null&&P.startsWith("tamagui")&&console.log("Error in partial evaluation",e.message,e.stack)}}if(!a)throw new Error("BindingCache is a required param");for(const n in f){const p=f[n],s=N(n,p);if(s){if(!s.sourceModule)continue;const l=w(d,s.sourceModule);if(t.some(g=>l.endsWith(g))){const g=O(l);if(!g)return console.log(` | \u26A0\uFE0F Missing file ${l} via ${d} import ${s.sourceModule}?`),{};s.destructured?s.imported&&(o[n]=g[s.imported]):o[n]=g}continue}const{parent:v}=p.path;if(!m.isVariableDeclaration(v)||v.kind!=="const")continue;const e=v.declarations.find(l=>m.isIdentifier(l.id)&&l.id.name===n);if(!e||!e.init)continue;if(typeof e.id.start!="number"||typeof e.id.end!="number"){console.error("dec.id.start/end is not a number");continue}if(!m.isIdentifier(e.id)){console.error("dec is not an identifier");continue}const c=`${e.id.name}_${e.id.start}-${e.id.end}`;if(Object.hasOwnProperty.call(a,c)){o[n]=a[c];continue}if(Object.hasOwnProperty.call(a,c)){o[n]=a[c];continue}try{o[n]=x(e.init,void 0,u),a[c]=o[n];continue}catch{}}return o}export{M as cleanupBeforeExit,q as getStaticBindingsForScope};
|
|
191
2
|
//# sourceMappingURL=getStaticBindingsForScope.mjs.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/extractor/getStaticBindingsForScope.ts"],
|
|
4
4
|
"sourcesContent": ["import { ChildProcess, fork } from 'child_process'\nimport { dirname, extname, join, resolve } from 'path'\n\nimport { Binding, NodePath } from '@babel/traverse'\nimport * as t from '@babel/types'\n\nimport { evaluateAstNode } from './evaluateAstNode.js'\nimport { getSourceModule } from './getSourceModule.js'\n\nconst isLocalImport = (path: string) => path.startsWith('.') || path.startsWith('/')\n\nfunction resolveImportPath(sourcePath: string, path: string) {\n const sourceDir = dirname(sourcePath)\n if (isLocalImport(path)) {\n if (extname(path) === '') {\n path += '.js'\n }\n return resolve(sourceDir, path)\n }\n return path\n}\n\nconst cache = new Map()\nconst pending = new Map<string, Promise<any>>()\n\nconst loadCmd = `${join(__dirname, 'loadFile.js')}`\n\nlet exited = false\n\nlet child: ChildProcess | null = null\n\nfunction forkChild() {\n child = fork(loadCmd, [], {\n execArgv: ['-r', 'esbuild-register'],\n detached: false,\n stdio: 'ignore',\n })\n}\n\nexport function cleanupBeforeExit() {\n if (exited) return\n if (!child) return\n child.removeAllListeners()\n child.unref()\n child.disconnect()\n child.kill()\n exited = true\n}\n\nprocess.once('SIGTERM', cleanupBeforeExit)\nprocess.once('SIGINT', cleanupBeforeExit)\nprocess.once('beforeExit', cleanupBeforeExit)\n\nfunction importModule(path: string) {\n if (!child) {\n forkChild()\n }\n if (pending.has(path)) {\n return pending.get(path)\n }\n const promise = new Promise((res, rej) => {\n if (!child) return\n if (cache.size > 2000) {\n cache.clear()\n }\n if (cache.has(path)) {\n return cache.get(path)\n }\n const listener = (msg: any) => {\n if (!child) return\n if (!msg) return\n if (typeof msg !== 'string') return\n if (msg[0] === '-') {\n rej(new Error(msg.slice(1)))\n return\n }\n child.removeListener('message', listener)\n const val = JSON.parse(msg)\n cache.set(path, val)\n res(val)\n }\n child.once('message', listener)\n child.send(`${path.replace('.js', '')}`)\n })\n pending.set(path, promise)\n return promise\n}\n\nexport async function getStaticBindingsForScope(\n scope: NodePath<t.JSXElement>['scope'],\n whitelist: string[] = [],\n sourcePath: string,\n bindingCache: Record<string, string | null>,\n shouldPrintDebug: boolean | 'verbose'\n): Promise<Record<string, any>> {\n const bindings: Record<string, Binding> = scope.getAllBindings() as any\n const ret: Record<string, any> = {}\n\n if (\n shouldPrintDebug\n ) {\n // prettier-ignore\n // console.log(' ', Object.keys(bindings).length, 'variables in scope')\n // .map(x => bindings[x].identifier?.name).join(', ')\n }\n\n // on react native at least it doesnt find some bindings? not sure why\n // lets add in whitelisted imports if they exist\n const program = scope.getProgramParent().block as t.Program\n for (const node of program.body) {\n if (t.isImportDeclaration(node)) {\n const importPath = node.source.value\n if (!node.specifiers.length) continue\n if (!isLocalImport(importPath)) {\n continue\n }\n const moduleName = resolveImportPath(sourcePath, importPath)\n const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test))\n if (!isOnWhitelist) continue\n try {\n const src = await importModule(moduleName)\n if (!src) continue\n for (const specifier of node.specifiers) {\n if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported)) {\n if (typeof src[specifier.imported.name] !== 'undefined') {\n const val = src[specifier.local.name]\n ret[specifier.local.name] = val\n }\n }\n }\n } catch (err: any) {\n if (shouldPrintDebug) {\n // eslint-disable-next-line no-console\n console.warn(\n ` | Skipping partial evaluation of constant file: ${moduleName} (DEBUG=tamagui for more)`\n )\n } else if (process.env.DEBUG?.startsWith('tamagui')) {\n // eslint-disable-next-line no-console\n console.log(`Error in partial evaluation`, err.message, err.stack)\n }\n }\n }\n }\n\n if (!bindingCache) {\n throw new Error('BindingCache is a required param')\n }\n\n for (const k in bindings) {\n const binding = bindings[k]\n\n // check to see if the item is a module\n const sourceModule = getSourceModule(k, binding)\n if (sourceModule) {\n if (!sourceModule.sourceModule) {\n continue\n }\n\n const moduleName = resolveImportPath(sourcePath, sourceModule.sourceModule)\n const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test))\n\n // TODO we could cache this at the file level.. and check if its been touched since\n\n if (isOnWhitelist) {\n const src = importModule(moduleName)\n if (!src) {\n // eslint-disable-next-line no-console\n console.log(\n ` | \u26A0\uFE0F Missing file ${moduleName} via ${sourcePath} import ${sourceModule.sourceModule}?`\n )\n return {}\n }\n if (sourceModule.destructured) {\n if (sourceModule.imported) {\n ret[k] = src[sourceModule.imported]\n }\n } else {\n ret[k] = src\n }\n }\n continue\n }\n\n const { parent } = binding.path\n\n if (!t.isVariableDeclaration(parent) || parent.kind !== 'const') {\n continue\n }\n\n // pick out the right variable declarator\n const dec = parent.declarations.find((d) => t.isIdentifier(d.id) && d.id.name === k)\n\n // if init is not set, there's nothing to evaluate\n // TODO: handle spread syntax\n if (!dec || !dec.init) {\n continue\n }\n\n // missing start/end will break caching\n if (typeof dec.id.start !== 'number' || typeof dec.id.end !== 'number') {\n // eslint-disable-next-line no-console\n console.error('dec.id.start/end is not a number')\n continue\n }\n\n if (!t.isIdentifier(dec.id)) {\n // eslint-disable-next-line no-console\n console.error('dec is not an identifier')\n continue\n }\n\n const cacheKey = `${dec.id.name}_${dec.id.start}-${dec.id.end}`\n\n // retrieve value from cache\n if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {\n ret[k] = bindingCache[cacheKey]\n continue\n }\n // retrieve value from cache\n if (Object.hasOwnProperty.call(bindingCache, cacheKey)) {\n ret[k] = bindingCache[cacheKey]\n continue\n }\n\n // evaluate\n try {\n ret[k] = evaluateAstNode(dec.init, undefined, shouldPrintDebug)\n bindingCache[cacheKey] = ret[k]\n continue\n } catch {\n // skip\n }\n }\n\n return ret\n}\n"],
|
|
5
|
-
"mappings": "AAAA,
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": "AAAA,OAAuB,QAAAA,MAAY,gBACnC,OAAS,WAAAC,EAAS,WAAAC,EAAS,QAAAC,EAAM,WAAAC,MAAe,OAGhD,UAAYC,MAAO,eAEnB,OAAS,mBAAAC,MAAuB,uBAChC,OAAS,mBAAAC,MAAuB,uBAEhC,MAAMC,EAAiBC,GAAiBA,EAAK,WAAW,GAAG,GAAKA,EAAK,WAAW,GAAG,EAEnF,SAASC,EAAkBC,EAAoBF,EAAc,CAC3D,MAAMG,EAAYX,EAAQU,CAAU,EACpC,OAAIH,EAAcC,CAAI,GAChBP,EAAQO,CAAI,IAAM,KACpBA,GAAQ,OAEHL,EAAQQ,EAAWH,CAAI,GAEzBA,CACT,CAEA,MAAMI,EAAQ,IAAI,IACZC,EAAU,IAAI,IAE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t", "evaluateAstNode", "getSourceModule", "isLocalImport", "path", "resolveImportPath", "sourcePath", "sourceDir", "cache", "pending", "loadCmd", "exited", "child", "forkChild", "cleanupBeforeExit", "importModule", "promise", "res", "rej", "listener", "msg", "val", "getStaticBindingsForScope", "scope", "whitelist", "bindingCache", "shouldPrintDebug", "_a", "bindings", "ret", "program", "node", "importPath", "moduleName", "test", "src", "specifier", "err", "k", "binding", "sourceModule", "parent", "dec", "d", "cacheKey"]
|
|
7
7
|
}
|
|
@@ -1,41 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
function hoistClassNames(path, existing, expr) {
|
|
3
|
-
const hoist = hoistClassNames.bind(null, path, existing);
|
|
4
|
-
if (t.isStringLiteral(expr)) {
|
|
5
|
-
if (expr.value.trim() === "") {
|
|
6
|
-
return expr;
|
|
7
|
-
}
|
|
8
|
-
if (existing[expr.value]) {
|
|
9
|
-
return existing[expr.value];
|
|
10
|
-
}
|
|
11
|
-
const identifier = replaceStringWithVariable(expr);
|
|
12
|
-
existing[expr.value] = identifier;
|
|
13
|
-
return identifier;
|
|
14
|
-
}
|
|
15
|
-
if (t.isBinaryExpression(expr)) {
|
|
16
|
-
return t.binaryExpression(expr.operator, hoist(expr.left), hoist(expr.right));
|
|
17
|
-
}
|
|
18
|
-
if (t.isLogicalExpression(expr)) {
|
|
19
|
-
return t.logicalExpression(expr.operator, hoist(expr.left), hoist(expr.right));
|
|
20
|
-
}
|
|
21
|
-
if (t.isConditionalExpression(expr)) {
|
|
22
|
-
return t.conditionalExpression(expr.test, hoist(expr.consequent), hoist(expr.alternate));
|
|
23
|
-
}
|
|
24
|
-
return expr;
|
|
25
|
-
function replaceStringWithVariable(str) {
|
|
26
|
-
const uid = path.scope.generateUidIdentifier("cn");
|
|
27
|
-
const parent = path.findParent((path2) => path2.isProgram());
|
|
28
|
-
if (!parent)
|
|
29
|
-
throw new Error(`no program?`);
|
|
30
|
-
const variable = t.variableDeclaration("const", [
|
|
31
|
-
// adding a space for extra safety
|
|
32
|
-
t.variableDeclarator(uid, t.stringLiteral(` ${str.value}`))
|
|
33
|
-
]);
|
|
34
|
-
parent.unshiftContainer("body", variable);
|
|
35
|
-
return uid;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export {
|
|
39
|
-
hoistClassNames
|
|
40
|
-
};
|
|
1
|
+
import*as i from"@babel/types";function c(e,r,t){const n=c.bind(null,e,r);if(i.isStringLiteral(t)){if(t.value.trim()==="")return t;if(r[t.value])return r[t.value];const o=l(t);return r[t.value]=o,o}if(i.isBinaryExpression(t))return i.binaryExpression(t.operator,n(t.left),n(t.right));if(i.isLogicalExpression(t))return i.logicalExpression(t.operator,n(t.left),n(t.right));if(i.isConditionalExpression(t))return i.conditionalExpression(t.test,n(t.consequent),n(t.alternate));return t;function l(o){const a=e.scope.generateUidIdentifier("cn"),s=e.findParent(u=>u.isProgram());if(!s)throw new Error("no program?");const f=i.variableDeclaration("const",[i.variableDeclarator(a,i.stringLiteral(` ${o.value}`))]);return s.unshiftContainer("body",f),a}}export{c as hoistClassNames};
|
|
41
2
|
//# sourceMappingURL=hoistClassNames.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/extractor/hoistClassNames.ts"],
|
|
4
4
|
"sourcesContent": ["import { NodePath } from '@babel/traverse'\nimport * as t from '@babel/types'\n\nexport function hoistClassNames(\n path: NodePath<t.JSXElement>,\n existing: { [key: string]: t.Identifier },\n expr: t.Expression\n) {\n const hoist = hoistClassNames.bind(null, path, existing)\n if (t.isStringLiteral(expr)) {\n if (expr.value.trim() === '') {\n return expr\n }\n if (existing[expr.value]) {\n return existing[expr.value]\n }\n const identifier = replaceStringWithVariable(expr)\n existing[expr.value] = identifier\n return identifier\n }\n if (t.isBinaryExpression(expr)) {\n return t.binaryExpression(expr.operator, hoist(expr.left), hoist(expr.right))\n }\n if (t.isLogicalExpression(expr)) {\n return t.logicalExpression(expr.operator, hoist(expr.left), hoist(expr.right))\n }\n if (t.isConditionalExpression(expr)) {\n return t.conditionalExpression(expr.test, hoist(expr.consequent), hoist(expr.alternate))\n }\n return expr\n\n function replaceStringWithVariable(str: t.StringLiteral): t.Identifier {\n // hoist outside fn!\n const uid = path.scope.generateUidIdentifier('cn')\n const parent = path.findParent((path) => path.isProgram())\n if (!parent) throw new Error(`no program?`)\n const variable = t.variableDeclaration('const', [\n // adding a space for extra safety\n t.variableDeclarator(uid, t.stringLiteral(` ${str.value}`)),\n ])\n // @ts-ignore\n parent.unshiftContainer('body', variable)\n return uid\n }\n}\n"],
|
|
5
|
-
"mappings": "AACA,
|
|
6
|
-
"names": ["path"]
|
|
5
|
+
"mappings": "AACA,UAAYA,MAAO,eAEZ,SAASC,EACdC,EACAC,EACAC,EACA,CACA,MAAMC,EAAQJ,EAAgB,KAAK,KAAMC,EAAMC,CAAQ,EACvD,GAAIH,EAAE,gBAAgBI,CAAI,EAAG,CAC3B,GAAIA,EAAK,MAAM,KAAK,IAAM,GACxB,OAAOA,EAET,GAAID,EAASC,EAAK,KAAK,EACrB,OAAOD,EAASC,EAAK,KAAK,EAE5B,MAAME,EAAaC,EAA0BH,CAAI,EACjD,OAAAD,EAASC,EAAK,KAAK,EAAIE,EAChBA,CACT,CACA,GAAIN,EAAE,mBAAmBI,CAAI,EAC3B,OAAOJ,EAAE,iBAAiBI,EAAK,SAAUC,EAAMD,EAAK,IAAI,EAAGC,EAAMD,EAAK,KAAK,CAAC,EAE9E,GAAIJ,EAAE,oBAAoBI,CAAI,EAC5B,OAAOJ,EAAE,kBAAkBI,EAAK,SAAUC,EAAMD,EAAK,IAAI,EAAGC,EAAMD,EAAK,KAAK,CAAC,EAE/E,GAAIJ,EAAE,wBAAwBI,CAAI,EAChC,OAAOJ,EAAE,sBAAsBI,EAAK,KAAMC,EAAMD,EAAK,UAAU,EAAGC,EAAMD,EAAK,SAAS,CAAC,EAEzF,OAAOA,EAEP,SAASG,EAA0BC,EAAoC,CAErE,MAAMC,EAAMP,EAAK,MAAM,sBAAsB,IAAI,EAC3CQ,EAASR,EAAK,WAAYA,GAASA,EAAK,UAAU,CAAC,EACzD,GAAI,CAACQ,EAAQ,MAAM,IAAI,MAAM,aAAa,EAC1C,MAAMC,EAAWX,EAAE,oBAAoB,QAAS,CAE9CA,EAAE,mBAAmBS,EAAKT,EAAE,cAAc,IAAIQ,EAAI,OAAO,CAAC,CAC5D,CAAC,EAED,OAAAE,EAAO,iBAAiB,OAAQC,CAAQ,EACjCF,CACT,CACF",
|
|
6
|
+
"names": ["t", "hoistClassNames", "path", "existing", "expr", "hoist", "identifier", "replaceStringWithVariable", "str", "uid", "parent", "variable"]
|
|
7
7
|
}
|
|
@@ -1,41 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
function hoistClassNames(path, existing, expr) {
|
|
3
|
-
const hoist = hoistClassNames.bind(null, path, existing);
|
|
4
|
-
if (t.isStringLiteral(expr)) {
|
|
5
|
-
if (expr.value.trim() === "") {
|
|
6
|
-
return expr;
|
|
7
|
-
}
|
|
8
|
-
if (existing[expr.value]) {
|
|
9
|
-
return existing[expr.value];
|
|
10
|
-
}
|
|
11
|
-
const identifier = replaceStringWithVariable(expr);
|
|
12
|
-
existing[expr.value] = identifier;
|
|
13
|
-
return identifier;
|
|
14
|
-
}
|
|
15
|
-
if (t.isBinaryExpression(expr)) {
|
|
16
|
-
return t.binaryExpression(expr.operator, hoist(expr.left), hoist(expr.right));
|
|
17
|
-
}
|
|
18
|
-
if (t.isLogicalExpression(expr)) {
|
|
19
|
-
return t.logicalExpression(expr.operator, hoist(expr.left), hoist(expr.right));
|
|
20
|
-
}
|
|
21
|
-
if (t.isConditionalExpression(expr)) {
|
|
22
|
-
return t.conditionalExpression(expr.test, hoist(expr.consequent), hoist(expr.alternate));
|
|
23
|
-
}
|
|
24
|
-
return expr;
|
|
25
|
-
function replaceStringWithVariable(str) {
|
|
26
|
-
const uid = path.scope.generateUidIdentifier("cn");
|
|
27
|
-
const parent = path.findParent((path2) => path2.isProgram());
|
|
28
|
-
if (!parent)
|
|
29
|
-
throw new Error(`no program?`);
|
|
30
|
-
const variable = t.variableDeclaration("const", [
|
|
31
|
-
// adding a space for extra safety
|
|
32
|
-
t.variableDeclarator(uid, t.stringLiteral(` ${str.value}`))
|
|
33
|
-
]);
|
|
34
|
-
parent.unshiftContainer("body", variable);
|
|
35
|
-
return uid;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export {
|
|
39
|
-
hoistClassNames
|
|
40
|
-
};
|
|
1
|
+
import*as i from"@babel/types";function c(e,r,t){const n=c.bind(null,e,r);if(i.isStringLiteral(t)){if(t.value.trim()==="")return t;if(r[t.value])return r[t.value];const o=l(t);return r[t.value]=o,o}if(i.isBinaryExpression(t))return i.binaryExpression(t.operator,n(t.left),n(t.right));if(i.isLogicalExpression(t))return i.logicalExpression(t.operator,n(t.left),n(t.right));if(i.isConditionalExpression(t))return i.conditionalExpression(t.test,n(t.consequent),n(t.alternate));return t;function l(o){const a=e.scope.generateUidIdentifier("cn"),s=e.findParent(u=>u.isProgram());if(!s)throw new Error("no program?");const f=i.variableDeclaration("const",[i.variableDeclarator(a,i.stringLiteral(` ${o.value}`))]);return s.unshiftContainer("body",f),a}}export{c as hoistClassNames};
|
|
41
2
|
//# sourceMappingURL=hoistClassNames.mjs.map
|