@tamagui/static 1.0.0-alpha.29 → 1.0.0-alpha.32

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 (52) hide show
  1. package/dist/constants.js +10 -0
  2. package/dist/constants.js.map +7 -0
  3. package/dist/extractor/accessSafe.js +11 -0
  4. package/dist/extractor/accessSafe.js.map +7 -0
  5. package/dist/extractor/babelParse.js +30 -0
  6. package/dist/extractor/babelParse.js.map +7 -0
  7. package/dist/extractor/buildClassName.js +40 -0
  8. package/dist/extractor/buildClassName.js.map +7 -0
  9. package/dist/extractor/createEvaluator.js +51 -0
  10. package/dist/extractor/createEvaluator.js.map +7 -0
  11. package/dist/extractor/createExtractor.js +827 -0
  12. package/dist/extractor/createExtractor.js.map +7 -0
  13. package/dist/extractor/ensureImportingConcat.js +24 -0
  14. package/dist/extractor/ensureImportingConcat.js.map +7 -0
  15. package/dist/extractor/evaluateAstNode.js +94 -0
  16. package/dist/extractor/evaluateAstNode.js.map +7 -0
  17. package/dist/extractor/extractHelpers.js +94 -0
  18. package/dist/extractor/extractHelpers.js.map +7 -0
  19. package/dist/extractor/extractMediaStyle.js +151 -0
  20. package/dist/extractor/extractMediaStyle.js.map +7 -0
  21. package/dist/extractor/extractToClassNames.js +235 -0
  22. package/dist/extractor/extractToClassNames.js.map +7 -0
  23. package/dist/extractor/findTopmostFunction.js +23 -0
  24. package/dist/extractor/findTopmostFunction.js.map +7 -0
  25. package/dist/extractor/generatedUid.js +28 -0
  26. package/dist/extractor/generatedUid.js.map +7 -0
  27. package/dist/extractor/getPropValueFromAttributes.js +49 -0
  28. package/dist/extractor/getPropValueFromAttributes.js.map +7 -0
  29. package/dist/extractor/getSourceModule.js +69 -0
  30. package/dist/extractor/getSourceModule.js.map +7 -0
  31. package/dist/extractor/getStaticBindingsForScope.js +128 -0
  32. package/dist/extractor/getStaticBindingsForScope.js.map +7 -0
  33. package/dist/extractor/hoistClassNames.js +44 -0
  34. package/dist/extractor/hoistClassNames.js.map +7 -0
  35. package/dist/extractor/literalToAst.js +34 -0
  36. package/dist/extractor/literalToAst.js.map +7 -0
  37. package/dist/extractor/loadTamagui.js +41 -0
  38. package/dist/extractor/loadTamagui.js.map +7 -0
  39. package/dist/extractor/normalizeTernaries.js +52 -0
  40. package/dist/extractor/normalizeTernaries.js.map +7 -0
  41. package/dist/extractor/removeUnusedHooks.js +78 -0
  42. package/dist/extractor/removeUnusedHooks.js.map +7 -0
  43. package/dist/index.cjs +2116 -0
  44. package/dist/index.cjs.map +7 -0
  45. package/dist/index.js +14 -0
  46. package/dist/index.js.map +7 -0
  47. package/dist/patchReactNativeWeb.js +107 -0
  48. package/dist/patchReactNativeWeb.js.map +7 -0
  49. package/dist/types.js +1 -0
  50. package/dist/types.js.map +7 -0
  51. package/package.json +7 -7
  52. package/types/extractor/createExtractor.d.ts.map +1 -1
@@ -0,0 +1,44 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import * as t from "@babel/types";
4
+ function hoistClassNames(path, existing, expr) {
5
+ const hoist = hoistClassNames.bind(null, path, existing);
6
+ if (t.isStringLiteral(expr)) {
7
+ if (expr.value.trim() === "") {
8
+ return expr;
9
+ }
10
+ if (existing[expr.value]) {
11
+ return existing[expr.value];
12
+ }
13
+ const identifier = replaceStringWithVariable(expr);
14
+ existing[expr.value] = identifier;
15
+ return identifier;
16
+ }
17
+ if (t.isBinaryExpression(expr)) {
18
+ return t.binaryExpression(expr.operator, hoist(expr.left), hoist(expr.right));
19
+ }
20
+ if (t.isLogicalExpression(expr)) {
21
+ return t.logicalExpression(expr.operator, hoist(expr.left), hoist(expr.right));
22
+ }
23
+ if (t.isConditionalExpression(expr)) {
24
+ return t.conditionalExpression(expr.test, hoist(expr.consequent), hoist(expr.alternate));
25
+ }
26
+ return expr;
27
+ function replaceStringWithVariable(str) {
28
+ const uid = path.scope.generateUidIdentifier("cn");
29
+ const parent = path.findParent((path2) => path2.isProgram());
30
+ if (!parent)
31
+ throw new Error(`no program?`);
32
+ const variable = t.variableDeclaration("const", [
33
+ t.variableDeclarator(uid, t.stringLiteral(` ${str.value}`))
34
+ ]);
35
+ parent.unshiftContainer("body", variable);
36
+ return uid;
37
+ }
38
+ __name(replaceStringWithVariable, "replaceStringWithVariable");
39
+ }
40
+ __name(hoistClassNames, "hoistClassNames");
41
+ export {
42
+ hoistClassNames
43
+ };
44
+ //# sourceMappingURL=hoistClassNames.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/extractor/hoistClassNames.ts"],
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;AAEO,yBACL,MACA,UACA,MACA;AACA,QAAM,QAAQ,gBAAgB,KAAK,MAAM,MAAM;AAC/C,MAAI,EAAE,gBAAgB,OAAO;AAC3B,QAAI,KAAK,MAAM,WAAW,IAAI;AAC5B,aAAO;AAAA;AAET,QAAI,SAAS,KAAK,QAAQ;AACxB,aAAO,SAAS,KAAK;AAAA;AAEvB,UAAM,aAAa,0BAA0B;AAC7C,aAAS,KAAK,SAAS;AACvB,WAAO;AAAA;AAET,MAAI,EAAE,mBAAmB,OAAO;AAC9B,WAAO,EAAE,iBAAiB,KAAK,UAAU,MAAM,KAAK,OAAO,MAAM,KAAK;AAAA;AAExE,MAAI,EAAE,oBAAoB,OAAO;AAC/B,WAAO,EAAE,kBAAkB,KAAK,UAAU,MAAM,KAAK,OAAO,MAAM,KAAK;AAAA;AAEzE,MAAI,EAAE,wBAAwB,OAAO;AACnC,WAAO,EAAE,sBAAsB,KAAK,MAAM,MAAM,KAAK,aAAa,MAAM,KAAK;AAAA;AAE/E,SAAO;AAEP,qCAAmC,KAAoC;AAErE,UAAM,MAAM,KAAK,MAAM,sBAAsB;AAC7C,UAAM,SAAS,KAAK,WAAW,CAAC,UAAS,MAAK;AAC9C,QAAI,CAAC;AAAQ,YAAM,IAAI,MAAM;AAC7B,UAAM,WAAW,EAAE,oBAAoB,SAAS;AAAA,MAE9C,EAAE,mBAAmB,KAAK,EAAE,cAAc,IAAI,IAAI;AAAA;AAGpD,WAAO,iBAAiB,QAAQ;AAChC,WAAO;AAAA;AAXA;AAAA;AA5BK;",
6
+ "names": []
7
+ }
@@ -0,0 +1,34 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import * as t from "@babel/types";
4
+ function literalToAst(literal) {
5
+ if (literal === null) {
6
+ return t.nullLiteral();
7
+ }
8
+ switch (typeof literal) {
9
+ case "function":
10
+ throw new Error("Unsupported");
11
+ case "number":
12
+ return t.numericLiteral(literal);
13
+ case "string":
14
+ return t.stringLiteral(literal);
15
+ case "boolean":
16
+ return t.booleanLiteral(literal);
17
+ case "undefined":
18
+ return t.unaryExpression("void", t.numericLiteral(0), true);
19
+ default:
20
+ if (Array.isArray(literal)) {
21
+ return t.arrayExpression(literal.map(literalToAst));
22
+ }
23
+ return t.objectExpression(Object.keys(literal).filter((k) => {
24
+ return typeof literal[k] !== "undefined";
25
+ }).map((k) => {
26
+ return t.objectProperty(t.stringLiteral(k), literalToAst(literal[k]));
27
+ }));
28
+ }
29
+ }
30
+ __name(literalToAst, "literalToAst");
31
+ export {
32
+ literalToAst
33
+ };
34
+ //# sourceMappingURL=literalToAst.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/extractor/literalToAst.ts"],
4
+ "sourcesContent": ["import * as t from '@babel/types'\n\nexport function literalToAst(literal: any): t.Expression {\n if (literal === null) {\n return t.nullLiteral()\n }\n switch (typeof literal) {\n case 'function':\n throw new Error('Unsupported')\n case 'number':\n return t.numericLiteral(literal)\n case 'string':\n return t.stringLiteral(literal)\n case 'boolean':\n return t.booleanLiteral(literal)\n case 'undefined':\n return t.unaryExpression('void', t.numericLiteral(0), true)\n default:\n if (Array.isArray(literal)) {\n return t.arrayExpression(literal.map(literalToAst))\n }\n return t.objectExpression(\n Object.keys(literal)\n .filter((k) => {\n return typeof literal[k] !== 'undefined'\n })\n .map((k) => {\n return t.objectProperty(t.stringLiteral(k), literalToAst(literal[k]))\n })\n )\n }\n}\n"],
5
+ "mappings": ";;AAAA;AAEO,sBAAsB,SAA4B;AACvD,MAAI,YAAY,MAAM;AACpB,WAAO,EAAE;AAAA;AAEX,UAAQ,OAAO;AAAA,SACR;AACH,YAAM,IAAI,MAAM;AAAA,SACb;AACH,aAAO,EAAE,eAAe;AAAA,SACrB;AACH,aAAO,EAAE,cAAc;AAAA,SACpB;AACH,aAAO,EAAE,eAAe;AAAA,SACrB;AACH,aAAO,EAAE,gBAAgB,QAAQ,EAAE,eAAe,IAAI;AAAA;AAEtD,UAAI,MAAM,QAAQ,UAAU;AAC1B,eAAO,EAAE,gBAAgB,QAAQ,IAAI;AAAA;AAEvC,aAAO,EAAE,iBACP,OAAO,KAAK,SACT,OAAO,CAAC,MAAM;AACb,eAAO,OAAO,QAAQ,OAAO;AAAA,SAE9B,IAAI,CAAC,MAAM;AACV,eAAO,EAAE,eAAe,EAAE,cAAc,IAAI,aAAa,QAAQ;AAAA;AAAA;AAAA;AAzB7D;",
6
+ "names": []
7
+ }
@@ -0,0 +1,41 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import { join } from "path";
4
+ import { createTamagui } from "@tamagui/core-node";
5
+ let loadedTamagui = null;
6
+ function loadTamagui(props) {
7
+ if (loadedTamagui) {
8
+ return loadedTamagui;
9
+ }
10
+ process.env.IS_STATIC = "is_static";
11
+ const proxyWorm = require("@tamagui/fake-react-native");
12
+ const Mod = require("module");
13
+ const og = Mod.prototype.require;
14
+ Mod.prototype.require = function(path) {
15
+ if (path.startsWith("react-native") && !path.startsWith("react-native-web/dist/cjs/exports")) {
16
+ return proxyWorm;
17
+ }
18
+ return og.apply(this, arguments);
19
+ };
20
+ const configPath = join(process.cwd(), props.config);
21
+ const tamaguiConfigExport = require(configPath);
22
+ const tamaguiConfig = tamaguiConfigExport["default"] || tamaguiConfigExport;
23
+ const components = {};
24
+ for (const module of props.components) {
25
+ const exported = require(module);
26
+ Object.assign(components, exported);
27
+ }
28
+ process.env.IS_STATIC = void 0;
29
+ Mod.prototype.require = og;
30
+ createTamagui(tamaguiConfig);
31
+ loadedTamagui = {
32
+ components,
33
+ tamaguiConfig
34
+ };
35
+ return loadedTamagui;
36
+ }
37
+ __name(loadTamagui, "loadTamagui");
38
+ export {
39
+ loadTamagui
40
+ };
41
+ //# sourceMappingURL=loadTamagui.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/extractor/loadTamagui.ts"],
4
+ "sourcesContent": ["import { join } from 'path'\n\nimport type { StaticComponent, TamaguiInternalConfig } from '@tamagui/core'\nimport { createTamagui } from '@tamagui/core-node'\n\nlet loadedTamagui: any = null\n\nexport function loadTamagui(props: { components: string[]; config: string }): {\n components: Record<string, StaticComponent>\n tamaguiConfig: TamaguiInternalConfig\n} {\n if (loadedTamagui) {\n return loadedTamagui\n }\n\n // lets shim require and avoid importing react-native + react-native-web\n // we just need to read the config around them\n process.env.IS_STATIC = 'is_static'\n const proxyWorm = require('@tamagui/fake-react-native')\n const Mod = require('module')\n const og = Mod.prototype.require\n Mod.prototype.require = function (path: string) {\n if (\n path.startsWith('react-native') &&\n // allow our rnw.tsx imports through\n !path.startsWith('react-native-web/dist/cjs/exports')\n ) {\n return proxyWorm\n }\n return og.apply(this, arguments)\n }\n\n // import config\n const configPath = join(process.cwd(), props.config)\n const tamaguiConfigExport = require(configPath)\n // TODO validate its the right config\n const tamaguiConfig = (tamaguiConfigExport['default'] ||\n tamaguiConfigExport) as TamaguiInternalConfig\n\n // import components\n const components = {}\n for (const module of props.components) {\n const exported = require(module)\n // TODO check if overwriting and warn\n Object.assign(components, exported)\n }\n\n // undo shims\n process.env.IS_STATIC = undefined\n Mod.prototype.require = og\n\n // set up core-node\n createTamagui(tamaguiConfig)\n\n loadedTamagui = {\n components,\n tamaguiConfig,\n }\n\n return loadedTamagui\n}\n"],
5
+ "mappings": ";;AAAA;AAGA;AAEA,IAAI,gBAAqB;AAElB,qBAAqB,OAG1B;AACA,MAAI,eAAe;AACjB,WAAO;AAAA;AAKT,UAAQ,IAAI,YAAY;AACxB,QAAM,YAAY,QAAQ;AAC1B,QAAM,MAAM,QAAQ;AACpB,QAAM,KAAK,IAAI,UAAU;AACzB,MAAI,UAAU,UAAU,SAAU,MAAc;AAC9C,QACE,KAAK,WAAW,mBAEhB,CAAC,KAAK,WAAW,sCACjB;AACA,aAAO;AAAA;AAET,WAAO,GAAG,MAAM,MAAM;AAAA;AAIxB,QAAM,aAAa,KAAK,QAAQ,OAAO,MAAM;AAC7C,QAAM,sBAAsB,QAAQ;AAEpC,QAAM,gBAAiB,oBAAoB,cACzC;AAGF,QAAM,aAAa;AACnB,aAAW,UAAU,MAAM,YAAY;AACrC,UAAM,WAAW,QAAQ;AAEzB,WAAO,OAAO,YAAY;AAAA;AAI5B,UAAQ,IAAI,YAAY;AACxB,MAAI,UAAU,UAAU;AAGxB,gBAAc;AAEd,kBAAgB;AAAA,IACd;AAAA,IACA;AAAA;AAGF,SAAO;AAAA;AApDO;",
6
+ "names": []
7
+ }
@@ -0,0 +1,52 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import generate from "@babel/generator";
4
+ import * as t from "@babel/types";
5
+ import invariant from "invariant";
6
+ function normalizeTernaries(ternaries) {
7
+ invariant(Array.isArray(ternaries), "extractStaticTernaries expects param 1 to be an array of ternaries");
8
+ if (ternaries.length === 0) {
9
+ return [];
10
+ }
11
+ const ternariesByKey = {};
12
+ for (let idx = -1, len = ternaries.length; ++idx < len; ) {
13
+ const { test, consequent, alternate, remove, ...rest } = ternaries[idx];
14
+ let ternaryTest = test;
15
+ if (t.isExpressionStatement(test)) {
16
+ ternaryTest = test.expression;
17
+ }
18
+ let shouldSwap = false;
19
+ if (t.isUnaryExpression(test) && test.operator === "!") {
20
+ ternaryTest = test.argument;
21
+ shouldSwap = true;
22
+ } else if (t.isBinaryExpression(test)) {
23
+ if (test.operator === "!==" || test.operator === "!=") {
24
+ ternaryTest = t.binaryExpression(test.operator, test.left, test.right);
25
+ shouldSwap = true;
26
+ }
27
+ }
28
+ const key = generate(ternaryTest).code;
29
+ if (!ternariesByKey[key]) {
30
+ ternariesByKey[key] = {
31
+ ...rest,
32
+ alternate: {},
33
+ consequent: {},
34
+ test: ternaryTest,
35
+ remove
36
+ };
37
+ }
38
+ const altStyle = (shouldSwap ? consequent : alternate) ?? {};
39
+ const consStyle = (shouldSwap ? alternate : consequent) ?? {};
40
+ Object.assign(ternariesByKey[key].alternate, altStyle);
41
+ Object.assign(ternariesByKey[key].consequent, consStyle);
42
+ }
43
+ const ternaryExpression = Object.keys(ternariesByKey).map((key) => {
44
+ return ternariesByKey[key];
45
+ });
46
+ return ternaryExpression;
47
+ }
48
+ __name(normalizeTernaries, "normalizeTernaries");
49
+ export {
50
+ normalizeTernaries
51
+ };
52
+ //# sourceMappingURL=normalizeTernaries.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/extractor/normalizeTernaries.ts"],
4
+ "sourcesContent": ["import generate from '@babel/generator'\nimport * as t from '@babel/types'\nimport invariant from 'invariant'\n\nimport { Ternary } from '../types'\n\nexport function normalizeTernaries(ternaries: Ternary[]) {\n invariant(Array.isArray(ternaries), 'extractStaticTernaries expects param 1 to be an array of ternaries')\n\n if (ternaries.length === 0) {\n return []\n }\n\n const ternariesByKey: { [key: string]: Ternary } = {}\n\n for (let idx = -1, len = ternaries.length; ++idx < len; ) {\n const { test, consequent, alternate, remove, ...rest } = ternaries[idx]\n\n let ternaryTest = test\n\n // strip parens\n if (t.isExpressionStatement(test)) {\n ternaryTest = (test as any).expression\n }\n\n // convert `!thing` to `thing` with swapped consequent and alternate\n let shouldSwap = false\n if (t.isUnaryExpression(test) && test.operator === '!') {\n ternaryTest = test.argument\n shouldSwap = true\n } else if (t.isBinaryExpression(test)) {\n if (test.operator === '!==' || test.operator === '!=') {\n ternaryTest = t.binaryExpression(test.operator, test.left, test.right)\n shouldSwap = true\n }\n }\n\n const key = generate(ternaryTest).code\n\n if (!ternariesByKey[key]) {\n ternariesByKey[key] = {\n ...rest,\n alternate: {},\n consequent: {},\n test: ternaryTest,\n remove,\n }\n }\n const altStyle = (shouldSwap ? consequent : alternate) ?? {}\n const consStyle = (shouldSwap ? alternate : consequent) ?? {}\n Object.assign(ternariesByKey[key].alternate, altStyle)\n Object.assign(ternariesByKey[key].consequent, consStyle)\n }\n\n const ternaryExpression = Object.keys(ternariesByKey).map((key) => {\n return ternariesByKey[key]\n })\n\n return ternaryExpression\n}\n"],
5
+ "mappings": ";;AAAA;AACA;AACA;AAIO,4BAA4B,WAAsB;AACvD,YAAU,MAAM,QAAQ,YAAY;AAEpC,MAAI,UAAU,WAAW,GAAG;AAC1B,WAAO;AAAA;AAGT,QAAM,iBAA6C;AAEnD,WAAS,MAAM,IAAI,MAAM,UAAU,QAAQ,EAAE,MAAM,OAAO;AACxD,UAAM,EAAE,MAAM,YAAY,WAAW,WAAW,SAAS,UAAU;AAEnE,QAAI,cAAc;AAGlB,QAAI,EAAE,sBAAsB,OAAO;AACjC,oBAAe,KAAa;AAAA;AAI9B,QAAI,aAAa;AACjB,QAAI,EAAE,kBAAkB,SAAS,KAAK,aAAa,KAAK;AACtD,oBAAc,KAAK;AACnB,mBAAa;AAAA,eACJ,EAAE,mBAAmB,OAAO;AACrC,UAAI,KAAK,aAAa,SAAS,KAAK,aAAa,MAAM;AACrD,sBAAc,EAAE,iBAAiB,KAAK,UAAU,KAAK,MAAM,KAAK;AAChE,qBAAa;AAAA;AAAA;AAIjB,UAAM,MAAM,SAAS,aAAa;AAElC,QAAI,CAAC,eAAe,MAAM;AACxB,qBAAe,OAAO;AAAA,WACjB;AAAA,QACH,WAAW;AAAA,QACX,YAAY;AAAA,QACZ,MAAM;AAAA,QACN;AAAA;AAAA;AAGJ,UAAM,WAAY,cAAa,aAAa,cAAc;AAC1D,UAAM,YAAa,cAAa,YAAY,eAAe;AAC3D,WAAO,OAAO,eAAe,KAAK,WAAW;AAC7C,WAAO,OAAO,eAAe,KAAK,YAAY;AAAA;AAGhD,QAAM,oBAAoB,OAAO,KAAK,gBAAgB,IAAI,CAAC,QAAQ;AACjE,WAAO,eAAe;AAAA;AAGxB,SAAO;AAAA;AApDO;",
6
+ "names": []
7
+ }
@@ -0,0 +1,78 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import * as t from "@babel/types";
4
+ const hooks = {
5
+ useMedia: true,
6
+ useTheme: true
7
+ };
8
+ function removeUnusedHooks(compFn, shouldPrintDebug) {
9
+ compFn.scope.crawl();
10
+ let bodyStatements = compFn?.get("body");
11
+ if (!bodyStatements) {
12
+ console.log("no body statemnts?", compFn);
13
+ return;
14
+ }
15
+ if (!Array.isArray(bodyStatements)) {
16
+ if (bodyStatements.isFunctionExpression()) {
17
+ bodyStatements = bodyStatements.scope.path.get("body");
18
+ } else {
19
+ bodyStatements = bodyStatements.get("body");
20
+ }
21
+ }
22
+ if (!bodyStatements || !Array.isArray(bodyStatements)) {
23
+ return;
24
+ }
25
+ const statements = bodyStatements;
26
+ for (const statement of statements) {
27
+ if (!statement.isVariableDeclaration()) {
28
+ continue;
29
+ }
30
+ const declarations = statement.get("declarations");
31
+ if (!Array.isArray(declarations)) {
32
+ continue;
33
+ }
34
+ const isBindingReferenced = /* @__PURE__ */ __name((name) => {
35
+ return !!statement.scope.getBinding(name)?.referenced;
36
+ }, "isBindingReferenced");
37
+ for (const declarator of declarations) {
38
+ const id = declarator.get("id");
39
+ const init = declarator.node.init;
40
+ if (Array.isArray(id) || Array.isArray(init)) {
41
+ continue;
42
+ }
43
+ const shouldRemove = (() => {
44
+ const isHook = t.isCallExpression(init) && t.isIdentifier(init.callee) && hooks[init.callee.name];
45
+ if (!isHook) {
46
+ return false;
47
+ }
48
+ if (t.isIdentifier(id.node)) {
49
+ const name = id.node.name;
50
+ return !isBindingReferenced(name);
51
+ } else if (t.isObjectPattern(id.node)) {
52
+ const propPaths = id.get("properties");
53
+ return propPaths.every((prop) => {
54
+ if (!prop.isObjectProperty())
55
+ return false;
56
+ const value = prop.get("value");
57
+ if (Array.isArray(value) || !value.isIdentifier())
58
+ return false;
59
+ const name = value.node.name;
60
+ return !isBindingReferenced(name);
61
+ });
62
+ }
63
+ return false;
64
+ })();
65
+ if (shouldRemove) {
66
+ declarator.remove();
67
+ if (shouldPrintDebug) {
68
+ console.log(` [\u{1FA9D}] removed ${id.node["name"] ?? ""}`);
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ __name(removeUnusedHooks, "removeUnusedHooks");
75
+ export {
76
+ removeUnusedHooks
77
+ };
78
+ //# sourceMappingURL=removeUnusedHooks.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/extractor/removeUnusedHooks.ts"],
4
+ "sourcesContent": ["import { NodePath } from '@babel/traverse'\nimport * as t from '@babel/types'\n\nconst hooks = {\n useMedia: true,\n useTheme: true,\n}\n\nexport function removeUnusedHooks(compFn: NodePath<any>, shouldPrintDebug: boolean) {\n compFn.scope.crawl()\n // check the top level statements\n let bodyStatements = compFn?.get('body')\n if (!bodyStatements) {\n console.log('no body statemnts?', compFn)\n return\n }\n if (!Array.isArray(bodyStatements)) {\n if (bodyStatements.isFunctionExpression()) {\n bodyStatements = bodyStatements.scope.path.get('body')\n } else {\n bodyStatements = bodyStatements.get('body')\n }\n }\n if (!bodyStatements || !Array.isArray(bodyStatements)) {\n return\n }\n const statements = bodyStatements as NodePath<any>[]\n for (const statement of statements) {\n if (!statement.isVariableDeclaration()) {\n continue\n }\n const declarations = statement.get('declarations')\n if (!Array.isArray(declarations)) {\n continue\n }\n const isBindingReferenced = (name: string) => {\n return !!statement.scope.getBinding(name)?.referenced\n }\n for (const declarator of declarations) {\n const id = declarator.get('id')\n const init = declarator.node.init\n if (Array.isArray(id) || Array.isArray(init)) {\n continue\n }\n const shouldRemove = (() => {\n const isHook =\n t.isCallExpression(init) && t.isIdentifier(init.callee) && hooks[init.callee.name]\n if (!isHook) {\n return false\n }\n if (t.isIdentifier(id.node)) {\n // remove \"const media = useMedia()\"\n const name = id.node.name\n return !isBindingReferenced(name)\n } else if (t.isObjectPattern(id.node)) {\n // remove \"const { sm } = useMedia()\"\n const propPaths = id.get('properties') as NodePath<any>[]\n return propPaths.every((prop) => {\n if (!prop.isObjectProperty()) return false\n const value = prop.get('value')\n if (Array.isArray(value) || !value.isIdentifier()) return false\n const name = value.node.name\n return !isBindingReferenced(name)\n })\n }\n return false\n })()\n if (shouldRemove) {\n declarator.remove()\n if (shouldPrintDebug) {\n console.log(` [\uD83E\uDE9D] removed ${id.node['name'] ?? ''}`)\n }\n }\n }\n }\n}\n"],
5
+ "mappings": ";;AACA;AAEA,MAAM,QAAQ;AAAA,EACZ,UAAU;AAAA,EACV,UAAU;AAAA;AAGL,2BAA2B,QAAuB,kBAA2B;AAClF,SAAO,MAAM;AAEb,MAAI,iBAAiB,QAAQ,IAAI;AACjC,MAAI,CAAC,gBAAgB;AACnB,YAAQ,IAAI,sBAAsB;AAClC;AAAA;AAEF,MAAI,CAAC,MAAM,QAAQ,iBAAiB;AAClC,QAAI,eAAe,wBAAwB;AACzC,uBAAiB,eAAe,MAAM,KAAK,IAAI;AAAA,WAC1C;AACL,uBAAiB,eAAe,IAAI;AAAA;AAAA;AAGxC,MAAI,CAAC,kBAAkB,CAAC,MAAM,QAAQ,iBAAiB;AACrD;AAAA;AAEF,QAAM,aAAa;AACnB,aAAW,aAAa,YAAY;AAClC,QAAI,CAAC,UAAU,yBAAyB;AACtC;AAAA;AAEF,UAAM,eAAe,UAAU,IAAI;AACnC,QAAI,CAAC,MAAM,QAAQ,eAAe;AAChC;AAAA;AAEF,UAAM,sBAAsB,wBAAC,SAAiB;AAC5C,aAAO,CAAC,CAAC,UAAU,MAAM,WAAW,OAAO;AAAA,OADjB;AAG5B,eAAW,cAAc,cAAc;AACrC,YAAM,KAAK,WAAW,IAAI;AAC1B,YAAM,OAAO,WAAW,KAAK;AAC7B,UAAI,MAAM,QAAQ,OAAO,MAAM,QAAQ,OAAO;AAC5C;AAAA;AAEF,YAAM,eAAgB,OAAM;AAC1B,cAAM,SACJ,EAAE,iBAAiB,SAAS,EAAE,aAAa,KAAK,WAAW,MAAM,KAAK,OAAO;AAC/E,YAAI,CAAC,QAAQ;AACX,iBAAO;AAAA;AAET,YAAI,EAAE,aAAa,GAAG,OAAO;AAE3B,gBAAM,OAAO,GAAG,KAAK;AACrB,iBAAO,CAAC,oBAAoB;AAAA,mBACnB,EAAE,gBAAgB,GAAG,OAAO;AAErC,gBAAM,YAAY,GAAG,IAAI;AACzB,iBAAO,UAAU,MAAM,CAAC,SAAS;AAC/B,gBAAI,CAAC,KAAK;AAAoB,qBAAO;AACrC,kBAAM,QAAQ,KAAK,IAAI;AACvB,gBAAI,MAAM,QAAQ,UAAU,CAAC,MAAM;AAAgB,qBAAO;AAC1D,kBAAM,OAAO,MAAM,KAAK;AACxB,mBAAO,CAAC,oBAAoB;AAAA;AAAA;AAGhC,eAAO;AAAA;AAET,UAAI,cAAc;AAChB,mBAAW;AACX,YAAI,kBAAkB;AACpB,kBAAQ,IAAI,yBAAkB,GAAG,KAAK,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AA9D3C;",
6
+ "names": []
7
+ }