@tamagui/babel-plugin 1.88.12 → 1.88.14
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/LICENSE +21 -0
- package/dist/esm/index.mjs +197 -0
- package/package.json +3 -3
- package/src/index.ts +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { basename } from "path";
|
|
2
|
+
import generator from "@babel/generator";
|
|
3
|
+
import { declare } from "@babel/helper-plugin-utils";
|
|
4
|
+
import template from "@babel/template";
|
|
5
|
+
import * as t from "@babel/types";
|
|
6
|
+
import { createExtractor, getPragmaOptions, isSimpleSpread, literalToAst } from "@tamagui/static";
|
|
7
|
+
const importNativeView = template(`
|
|
8
|
+
const __ReactNativeView = require('react-native').View;
|
|
9
|
+
const __ReactNativeText = require('react-native').Text;
|
|
10
|
+
`),
|
|
11
|
+
importStyleSheet = template(`
|
|
12
|
+
const __ReactNativeStyleSheet = require('react-native').StyleSheet;
|
|
13
|
+
`),
|
|
14
|
+
importWithTheme = template(`
|
|
15
|
+
const __internalWithTheme = require('@tamagui/core').internalWithTheme;
|
|
16
|
+
`),
|
|
17
|
+
extractor = createExtractor({
|
|
18
|
+
platform: "native"
|
|
19
|
+
});
|
|
20
|
+
var src_default = declare(function (api, options) {
|
|
21
|
+
return api.assertVersion(7), {
|
|
22
|
+
name: "tamagui",
|
|
23
|
+
visitor: {
|
|
24
|
+
Program: {
|
|
25
|
+
enter(root) {
|
|
26
|
+
let sourcePath = this.file.opts.filename;
|
|
27
|
+
process.env.SOURCE_ROOT?.endsWith("ios") && (sourcePath = sourcePath.replace("/ios", ""));
|
|
28
|
+
let hasImportedView = !1,
|
|
29
|
+
hasImportedViewWrapper = !1;
|
|
30
|
+
const sheetStyles = {},
|
|
31
|
+
sheetIdentifier = root.scope.generateUidIdentifier("sheet"),
|
|
32
|
+
firstComment = root.node.body[0]?.leadingComments?.[0]?.value?.trim() ?? "",
|
|
33
|
+
{
|
|
34
|
+
shouldPrintDebug,
|
|
35
|
+
shouldDisable
|
|
36
|
+
} = getPragmaOptions({
|
|
37
|
+
disableCommentCheck: !0,
|
|
38
|
+
source: firstComment,
|
|
39
|
+
path: sourcePath
|
|
40
|
+
});
|
|
41
|
+
if (shouldDisable) return;
|
|
42
|
+
function addSheetStyle(style, node) {
|
|
43
|
+
let key = `${`${Object.keys(sheetStyles).length}`}`;
|
|
44
|
+
if (process.env.NODE_ENV === "development") {
|
|
45
|
+
const lineNumbers = node.loc ? node.loc.start.line + (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : "") : "";
|
|
46
|
+
key += `:${basename(sourcePath)}:${lineNumbers}`;
|
|
47
|
+
}
|
|
48
|
+
return sheetStyles[key] = style, readStyleExpr(key);
|
|
49
|
+
}
|
|
50
|
+
function readStyleExpr(key) {
|
|
51
|
+
return template("SHEET['KEY']")({
|
|
52
|
+
SHEET: sheetIdentifier.name,
|
|
53
|
+
KEY: key
|
|
54
|
+
}).expression;
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
extractor.parseSync(root, {
|
|
58
|
+
// @ts-expect-error in case they leave it out
|
|
59
|
+
platform: "native",
|
|
60
|
+
shouldPrintDebug,
|
|
61
|
+
importsWhitelist: ["constants.js", "colors.js"],
|
|
62
|
+
extractStyledDefinitions: options.forceExtractStyleDefinitions,
|
|
63
|
+
excludeProps: /* @__PURE__ */new Set(["className", "userSelect", "whiteSpace", "textOverflow", "cursor", "contain"]),
|
|
64
|
+
...options,
|
|
65
|
+
// disable this extraction for now at least, need to figure out merging theme vs non-theme
|
|
66
|
+
// because theme need to stay in render(), whereas non-theme can be extracted
|
|
67
|
+
// for now just turn it off entirely at a small perf loss
|
|
68
|
+
disableExtractInlineMedia: !0,
|
|
69
|
+
// disable extracting variables as no native concept of them (only theme values)
|
|
70
|
+
disableExtractVariables: options.experimentalFlattenThemesOnNative ? !1 : "theme",
|
|
71
|
+
sourcePath,
|
|
72
|
+
// disabling flattening for now
|
|
73
|
+
// it's flattening a plain <Paragraph>hello</Paragraph> which breaks things because themes
|
|
74
|
+
// thinking it's not really worth the effort to do much compilation on native
|
|
75
|
+
// for now just disable flatten as it can only run in narrow places on native
|
|
76
|
+
// disableFlattening: 'styled',
|
|
77
|
+
getFlattenedNode({
|
|
78
|
+
isTextView
|
|
79
|
+
}) {
|
|
80
|
+
return hasImportedView || (hasImportedView = !0, root.unshiftContainer("body", importNativeView())), isTextView ? "__ReactNativeText" : "__ReactNativeView";
|
|
81
|
+
},
|
|
82
|
+
onExtractTag(props) {
|
|
83
|
+
const {
|
|
84
|
+
isFlattened
|
|
85
|
+
} = props;
|
|
86
|
+
if (!isFlattened) return;
|
|
87
|
+
assertValidTag(props.node);
|
|
88
|
+
const stylesExpr = t.arrayExpression([]),
|
|
89
|
+
expressions = [],
|
|
90
|
+
finalAttrs = [],
|
|
91
|
+
themeKeysUsed = /* @__PURE__ */new Set();
|
|
92
|
+
function getStyleExpression(style) {
|
|
93
|
+
if (!style) return;
|
|
94
|
+
const {
|
|
95
|
+
plain,
|
|
96
|
+
themed
|
|
97
|
+
} = splitThemeStyles(style);
|
|
98
|
+
if (themed && options.experimentalFlattenThemesOnNative) {
|
|
99
|
+
for (const key in themed) themeKeysUsed.add(themed[key].split("$")[1]);
|
|
100
|
+
return addThemedStyleExpression(themed);
|
|
101
|
+
}
|
|
102
|
+
return addSheetStyle(plain, props.node);
|
|
103
|
+
}
|
|
104
|
+
function addStyleExpression(expr) {
|
|
105
|
+
Array.isArray(expr) ? stylesExpr.elements.push(...expr) : stylesExpr.elements.push(expr);
|
|
106
|
+
}
|
|
107
|
+
function addThemedStyleExpression(styles) {
|
|
108
|
+
const themedStylesAst = literalToAst(styles);
|
|
109
|
+
return themedStylesAst.properties.forEach(_ => {
|
|
110
|
+
const prop = _;
|
|
111
|
+
prop.value.type === "StringLiteral" && (prop.value = t.memberExpression(t.identifier("theme"), t.identifier(prop.value.value.slice(1))));
|
|
112
|
+
}), themedStylesAst;
|
|
113
|
+
}
|
|
114
|
+
for (const attr of props.attrs) switch (attr.type) {
|
|
115
|
+
case "style":
|
|
116
|
+
{
|
|
117
|
+
addStyleExpression(getStyleExpression(attr.value));
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case "ternary":
|
|
121
|
+
{
|
|
122
|
+
const {
|
|
123
|
+
consequent,
|
|
124
|
+
alternate
|
|
125
|
+
} = attr.value;
|
|
126
|
+
options.experimentalFlattenThemesOnNative && expressions.push(attr.value.test);
|
|
127
|
+
const consExpr = getStyleExpression(consequent),
|
|
128
|
+
altExpr = getStyleExpression(alternate),
|
|
129
|
+
styleExpr = t.conditionalExpression(options.experimentalFlattenThemesOnNative ? t.identifier(`expressions[${expressions.length - 1}]`) : attr.value.test, consExpr || t.nullLiteral(), altExpr || t.nullLiteral());
|
|
130
|
+
addStyleExpression(styleExpr
|
|
131
|
+
// TODO: what is this for ?
|
|
132
|
+
// isFlattened ? simpleHash(JSON.stringify({ consequent, alternate })) : undefined
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case "dynamic-style":
|
|
138
|
+
{
|
|
139
|
+
expressions.push(attr.value), addStyleExpression(t.objectExpression([t.objectProperty(t.identifier(attr.name), t.identifier(`expressions[${expressions.length - 1}]`))]));
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case "attr":
|
|
143
|
+
{
|
|
144
|
+
t.isJSXSpreadAttribute(attr.value) && isSimpleSpread(attr.value) && stylesExpr.elements.push(t.memberExpression(attr.value.argument, t.identifier("style"))), finalAttrs.push(attr.value);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (props.node.attributes = finalAttrs, props.isFlattened) if (themeKeysUsed.size || expressions.length) {
|
|
149
|
+
hasImportedViewWrapper || (root.unshiftContainer("body", importWithTheme()), hasImportedViewWrapper = !0);
|
|
150
|
+
const name = props.node.name.name,
|
|
151
|
+
WrapperIdentifier = root.scope.generateUidIdentifier(name + "Wrapper");
|
|
152
|
+
root.pushContainer("body", t.variableDeclaration("const", [t.variableDeclarator(WrapperIdentifier, t.callExpression(t.identifier("__internalWithTheme"), [t.identifier(name), t.arrowFunctionExpression([t.identifier("theme"), t.identifier("expressions")], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.identifier("React"), t.identifier("useMemo")), [t.arrowFunctionExpression([], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), [...stylesExpr.elements]))])), t.arrayExpression([...[...themeKeysUsed].map(k => t.memberExpression(t.identifier("theme"), t.identifier(k))), t.spreadElement(t.identifier("expressions"))])]))]))]))])), props.node.name = WrapperIdentifier, expressions.length && props.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("expressions"), t.jsxExpressionContainer(t.arrayExpression(expressions))));
|
|
153
|
+
} else props.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("style"), t.jsxExpressionContainer(stylesExpr.elements.length === 1 ? stylesExpr.elements[0] : stylesExpr)));
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
} catch (err) {
|
|
157
|
+
if (err instanceof Error) {
|
|
158
|
+
let message = `${shouldPrintDebug === "verbose" ? err : err.message}`;
|
|
159
|
+
message.includes("Unexpected return value from visitor method") && (message = "Unexpected return value from visitor method"), console.warn("Error in Tamagui parse, skipping", message, err.stack);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (!Object.keys(sheetStyles).length) {
|
|
164
|
+
shouldPrintDebug && console.info("END no styles");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const sheetObject = literalToAst(sheetStyles),
|
|
168
|
+
sheetOuter = template("const SHEET = __ReactNativeStyleSheet.create(null)")({
|
|
169
|
+
SHEET: sheetIdentifier.name
|
|
170
|
+
});
|
|
171
|
+
sheetOuter.declarations[0].init.arguments[0] = sheetObject, root.unshiftContainer("body", sheetOuter), root.unshiftContainer("body", importStyleSheet()), shouldPrintDebug && (console.info(`
|
|
172
|
+
-------- output code -------
|
|
173
|
+
`), console.info(generator(root.parent).code.split(`
|
|
174
|
+
`).filter(x => !x.startsWith("//")).join(`
|
|
175
|
+
`)));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
function assertValidTag(node) {
|
|
182
|
+
node.attributes.find(x => x.type === "JSXAttribute" && x.name.name === "style") && process.env.DEBUG?.startsWith("tamagui") && console.warn("\u26A0\uFE0F Cannot pass style attribute to extracted style");
|
|
183
|
+
}
|
|
184
|
+
function splitThemeStyles(style) {
|
|
185
|
+
const themed = {},
|
|
186
|
+
plain = {};
|
|
187
|
+
let noTheme = !0;
|
|
188
|
+
for (const key in style) {
|
|
189
|
+
const val = style[key];
|
|
190
|
+
val && val[0] === "$" ? (themed[key] = val, noTheme = !1) : plain[key] = val;
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
themed: noTheme ? null : themed,
|
|
194
|
+
plain
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
export { src_default as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/babel-plugin",
|
|
3
|
-
"version": "1.88.
|
|
3
|
+
"version": "1.88.14",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"types": "./types/index.d.ts",
|
|
6
6
|
"main": "dist/cjs",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"@babel/helper-plugin-utils": "^7.22.5",
|
|
28
28
|
"@babel/template": "^7.22.15",
|
|
29
29
|
"@babel/traverse": "^7.23.3",
|
|
30
|
-
"@tamagui/static": "1.88.
|
|
30
|
+
"@tamagui/static": "1.88.14"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@babel/types": "^7.23.3",
|
|
34
|
-
"@tamagui/build": "1.88.
|
|
34
|
+
"@tamagui/build": "1.88.14"
|
|
35
35
|
},
|
|
36
36
|
"gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14"
|
|
37
37
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { basename } from 'path'
|
|
|
3
3
|
import generator from '@babel/generator'
|
|
4
4
|
import { declare } from '@babel/helper-plugin-utils'
|
|
5
5
|
import template from '@babel/template'
|
|
6
|
-
import { Visitor } from '@babel/traverse'
|
|
6
|
+
import type { Visitor } from '@babel/traverse'
|
|
7
7
|
import * as t from '@babel/types'
|
|
8
8
|
import type { TamaguiOptions } from '@tamagui/static'
|
|
9
9
|
import {
|