next-yak 0.0.36 → 0.0.39
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/loaders/cssloader.cjs +649 -0
- package/dist/loaders/cssloader.cjs.map +1 -0
- package/dist/loaders/cssloader.d.cts +3 -0
- package/dist/loaders/cssloader.d.ts +3 -0
- package/dist/loaders/cssloader.js +616 -0
- package/dist/loaders/cssloader.js.map +1 -0
- package/dist/loaders/tsloader.cjs +844 -0
- package/dist/loaders/tsloader.cjs.map +1 -0
- package/dist/loaders/tsloader.d.cts +6 -0
- package/dist/loaders/tsloader.d.ts +6 -0
- package/dist/loaders/tsloader.js +820 -0
- package/dist/loaders/tsloader.js.map +1 -0
- package/{withYak → dist/withYak}/index.cjs +14 -7
- package/dist/withYak/index.cjs.map +1 -0
- package/{withYak → dist/withYak}/index.d.cts +2 -2
- package/dist/withYak/index.d.ts +37 -0
- package/dist/withYak/index.js +68 -0
- package/dist/withYak/index.js.map +1 -0
- package/loaders/__tests__/classifier.test.ts +99 -93
- package/loaders/__tests__/cssloader.test.ts +1 -1
- package/loaders/__tests__/getCssName.test.ts +1 -1
- package/loaders/__tests__/tsloader.test.ts +1 -1
- package/loaders/{babel-yak-plugin.cjs → babel-yak-plugin.ts} +69 -68
- package/loaders/{cssloader.cjs → cssloader.ts} +73 -97
- package/loaders/lib/{appendCssUnitToExpressionValue.cjs → appendCssUnitToExpressionValue.ts} +7 -10
- package/loaders/lib/{getConstantValues.cjs → getConstantValues.ts} +10 -17
- package/loaders/lib/{getCssName.cjs → getCssName.ts} +19 -31
- package/loaders/lib/{getStyledComponentName.cjs → getStyledComponentName.ts} +7 -10
- package/loaders/lib/{getYakImports.cjs → getYakImports.ts} +4 -10
- package/loaders/lib/{hash.cjs → hash.ts} +3 -5
- package/loaders/lib/{localIdent.cjs → localIdent.ts} +5 -7
- package/loaders/lib/{quasiClassifier.cjs → quasiClassifier.ts} +7 -16
- package/loaders/lib/{replaceQuasiExpressionTokens.cjs → replaceQuasiExpressionTokens.ts} +27 -26
- package/loaders/lib/{stripCssComments.cjs → stripCssComments.ts} +3 -9
- package/loaders/{tsloader.cjs → tsloader.ts} +11 -15
- package/package.json +12 -21
- package/withYak/index.ts +43 -34
- package/withYak/index.cjs.map +0 -1
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res) => function __init() {
|
|
4
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
+
};
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// loaders/lib/stripCssComments.ts
|
|
12
|
+
function stripCssComments(cssString) {
|
|
13
|
+
let isInsideString = false;
|
|
14
|
+
let currentCharacter = "";
|
|
15
|
+
let comment = "";
|
|
16
|
+
let returnValue = "";
|
|
17
|
+
for (let index = 0; index < cssString.length; index++) {
|
|
18
|
+
currentCharacter = cssString[index];
|
|
19
|
+
if (cssString[index - 1] !== "\\" && (currentCharacter === '"' || currentCharacter === "'")) {
|
|
20
|
+
if (isInsideString === currentCharacter) {
|
|
21
|
+
isInsideString = false;
|
|
22
|
+
} else if (!isInsideString) {
|
|
23
|
+
isInsideString = currentCharacter;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!isInsideString && currentCharacter === "/" && cssString[index + 1] === "*") {
|
|
27
|
+
let index2 = index + 2;
|
|
28
|
+
for (; index2 < cssString.length; index2++) {
|
|
29
|
+
if (cssString[index2] === "*" && cssString[index2 + 1] === "/") {
|
|
30
|
+
if (cssString[index2 + 2] === "\n") {
|
|
31
|
+
index2++;
|
|
32
|
+
} else if (cssString[index2 + 2] + cssString[index2 + 3] === "\r\n") {
|
|
33
|
+
index2 += 2;
|
|
34
|
+
}
|
|
35
|
+
comment = "";
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
comment += cssString[index2];
|
|
39
|
+
}
|
|
40
|
+
index = index2 + 1;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
returnValue += currentCharacter;
|
|
44
|
+
}
|
|
45
|
+
return returnValue;
|
|
46
|
+
}
|
|
47
|
+
var init_stripCssComments = __esm({
|
|
48
|
+
"loaders/lib/stripCssComments.ts"() {
|
|
49
|
+
"use strict";
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// loaders/lib/quasiClassifier.ts
|
|
54
|
+
function quasiClassifier(quasiValue, currentNestingScopes) {
|
|
55
|
+
const trimmedCssString = stripCssComments(quasiValue).trim();
|
|
56
|
+
if (trimmedCssString === "") {
|
|
57
|
+
return {
|
|
58
|
+
empty: true,
|
|
59
|
+
unknownSelector: false,
|
|
60
|
+
insideCssValue: false,
|
|
61
|
+
currentNestingScopes
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
let isInsideString = false;
|
|
65
|
+
let currentCharacter = "";
|
|
66
|
+
let newNestingLevel = [...currentNestingScopes];
|
|
67
|
+
let currentSelector = "";
|
|
68
|
+
for (let index = 0; index < trimmedCssString.length; index++) {
|
|
69
|
+
currentCharacter = trimmedCssString[index];
|
|
70
|
+
if (trimmedCssString[index - 1] !== "\\" && (currentCharacter === '"' || currentCharacter === "'")) {
|
|
71
|
+
if (isInsideString === currentCharacter) {
|
|
72
|
+
isInsideString = false;
|
|
73
|
+
} else if (!isInsideString) {
|
|
74
|
+
isInsideString = currentCharacter;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (isInsideString) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (currentCharacter === "{") {
|
|
81
|
+
const selector = currentSelector.trim();
|
|
82
|
+
if (selector !== "") {
|
|
83
|
+
newNestingLevel.push(selector);
|
|
84
|
+
}
|
|
85
|
+
currentSelector = "";
|
|
86
|
+
} else if (currentCharacter === "}") {
|
|
87
|
+
newNestingLevel.pop();
|
|
88
|
+
currentSelector = "";
|
|
89
|
+
} else if (currentCharacter === ";") {
|
|
90
|
+
currentSelector = "";
|
|
91
|
+
} else {
|
|
92
|
+
currentSelector += currentCharacter;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
empty: false,
|
|
97
|
+
unknownSelector: trimmedCssString[0] === "{" || trimmedCssString[0] === ",",
|
|
98
|
+
insideCssValue: currentCharacter !== "{" && currentCharacter !== "}" && currentCharacter !== ";",
|
|
99
|
+
currentNestingScopes: newNestingLevel
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
var init_quasiClassifier = __esm({
|
|
103
|
+
"loaders/lib/quasiClassifier.ts"() {
|
|
104
|
+
"use strict";
|
|
105
|
+
init_stripCssComments();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// loaders/lib/replaceQuasiExpressionTokens.ts
|
|
110
|
+
function replaceTokensInQuasiExpressions(quasi, replacer, t) {
|
|
111
|
+
for (let i = quasi.expressions.length - 1; i >= 0; i--) {
|
|
112
|
+
const expression = quasi.expressions[i];
|
|
113
|
+
const parts = getExpressionParts(expression, t);
|
|
114
|
+
const replacement = parts && replacer(parts[0]);
|
|
115
|
+
const replacementValue = replacement && getReplacementValue(replacement, parts);
|
|
116
|
+
if (replacementValue !== false && replacementValue !== null) {
|
|
117
|
+
replaceExpressionAndMergeQuasis(quasi, i, replacementValue);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function replaceExpressionAndMergeQuasis(quasi, expressionIndex, replacement) {
|
|
122
|
+
const stringReplacement = typeof replacement === "string" ? replacement : replacement == null ? "" : JSON.stringify(replacement);
|
|
123
|
+
quasi.expressions.splice(expressionIndex, 1);
|
|
124
|
+
quasi.quasis[expressionIndex].value.raw += stringReplacement + quasi.quasis[expressionIndex + 1].value.raw;
|
|
125
|
+
quasi.quasis[expressionIndex].value.cooked += stringReplacement + quasi.quasis[expressionIndex + 1].value.cooked;
|
|
126
|
+
quasi.quasis.splice(expressionIndex + 1, 1);
|
|
127
|
+
}
|
|
128
|
+
function getExpressionParts(expression, t) {
|
|
129
|
+
let currentExpression = expression;
|
|
130
|
+
const tokens = [];
|
|
131
|
+
while (currentExpression) {
|
|
132
|
+
if (t.isIdentifier(currentExpression)) {
|
|
133
|
+
tokens.unshift(currentExpression.name);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
if (t.isMemberExpression(currentExpression)) {
|
|
137
|
+
if (currentExpression.computed === false && t.isIdentifier(currentExpression.property)) {
|
|
138
|
+
tokens.unshift(currentExpression.property.name);
|
|
139
|
+
} else if (t.isStringLiteral(currentExpression.property)) {
|
|
140
|
+
tokens.unshift(currentExpression.property.value);
|
|
141
|
+
} else if (t.isNumericLiteral(currentExpression.property)) {
|
|
142
|
+
tokens.unshift(String(currentExpression.property.value));
|
|
143
|
+
} else {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
currentExpression = currentExpression.object;
|
|
147
|
+
} else if (t.isCallExpression(currentExpression)) {
|
|
148
|
+
if (!t.isExpression(currentExpression.callee)) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
currentExpression = currentExpression.callee;
|
|
152
|
+
} else {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return tokens;
|
|
157
|
+
}
|
|
158
|
+
function getReplacementValue(replacement, parts) {
|
|
159
|
+
let currentReplacement = replacement;
|
|
160
|
+
for (let i = 1; i < parts.length; i++) {
|
|
161
|
+
const part = parts[i];
|
|
162
|
+
if (currentReplacement == null || typeof currentReplacement !== "object") {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
currentReplacement = currentReplacement[part];
|
|
166
|
+
}
|
|
167
|
+
return currentReplacement;
|
|
168
|
+
}
|
|
169
|
+
var init_replaceQuasiExpressionTokens = __esm({
|
|
170
|
+
"loaders/lib/replaceQuasiExpressionTokens.ts"() {
|
|
171
|
+
"use strict";
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// loaders/lib/hash.ts
|
|
176
|
+
function murmurhash2_32_gc(str) {
|
|
177
|
+
let l = str.length;
|
|
178
|
+
let h = l;
|
|
179
|
+
let i = 0;
|
|
180
|
+
let k;
|
|
181
|
+
while (l >= 4) {
|
|
182
|
+
k = str.charCodeAt(i) & 255 | (str.charCodeAt(++i) & 255) << 8 | (str.charCodeAt(++i) & 255) << 16 | (str.charCodeAt(++i) & 255) << 24;
|
|
183
|
+
k = (k & 65535) * 1540483477 + (((k >>> 16) * 1540483477 & 65535) << 16);
|
|
184
|
+
k ^= k >>> 24;
|
|
185
|
+
k = (k & 65535) * 1540483477 + (((k >>> 16) * 1540483477 & 65535) << 16);
|
|
186
|
+
h = (h & 65535) * 1540483477 + (((h >>> 16) * 1540483477 & 65535) << 16) ^ k;
|
|
187
|
+
l -= 4;
|
|
188
|
+
++i;
|
|
189
|
+
}
|
|
190
|
+
switch (l) {
|
|
191
|
+
case 3:
|
|
192
|
+
h ^= (str.charCodeAt(i + 2) & 255) << 16;
|
|
193
|
+
case 2:
|
|
194
|
+
h ^= (str.charCodeAt(i + 1) & 255) << 8;
|
|
195
|
+
case 1:
|
|
196
|
+
h ^= str.charCodeAt(i) & 255;
|
|
197
|
+
h = (h & 65535) * 1540483477 + (((h >>> 16) * 1540483477 & 65535) << 16);
|
|
198
|
+
}
|
|
199
|
+
h ^= h >>> 13;
|
|
200
|
+
h = (h & 65535) * 1540483477 + (((h >>> 16) * 1540483477 & 65535) << 16);
|
|
201
|
+
h ^= h >>> 15;
|
|
202
|
+
return (h >>> 0).toString(36);
|
|
203
|
+
}
|
|
204
|
+
var init_hash = __esm({
|
|
205
|
+
"loaders/lib/hash.ts"() {
|
|
206
|
+
"use strict";
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// loaders/lib/localIdent.ts
|
|
211
|
+
function localIdent(variableName, i, kind) {
|
|
212
|
+
switch (kind) {
|
|
213
|
+
case "selector":
|
|
214
|
+
return `.${variableName}${i === null ? "" : `_${i}`}`;
|
|
215
|
+
case "className":
|
|
216
|
+
case "animation":
|
|
217
|
+
return `${variableName}${i === null ? "" : `_${i}`}`;
|
|
218
|
+
case "keyframes":
|
|
219
|
+
return `@keyframes ${variableName}${i === null ? "" : `_${i}`}`;
|
|
220
|
+
default:
|
|
221
|
+
throw new Error("unknown kind");
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
var init_localIdent = __esm({
|
|
225
|
+
"loaders/lib/localIdent.ts"() {
|
|
226
|
+
"use strict";
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// loaders/lib/getStyledComponentName.ts
|
|
231
|
+
var getStyledComponentName, getStyledComponentName_default;
|
|
232
|
+
var init_getStyledComponentName = __esm({
|
|
233
|
+
"loaders/lib/getStyledComponentName.ts"() {
|
|
234
|
+
"use strict";
|
|
235
|
+
getStyledComponentName = (taggedTemplateExpressionPath) => {
|
|
236
|
+
const variableDeclaratorPath = taggedTemplateExpressionPath.findParent(
|
|
237
|
+
(path) => path.isVariableDeclarator()
|
|
238
|
+
);
|
|
239
|
+
if (!variableDeclaratorPath || !("id" in variableDeclaratorPath.node) || variableDeclaratorPath.node.id?.type !== "Identifier") {
|
|
240
|
+
throw new Error(
|
|
241
|
+
"Could not find variable declaration for styled component at " + taggedTemplateExpressionPath.node.loc
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
return variableDeclaratorPath.node.id.name;
|
|
245
|
+
};
|
|
246
|
+
getStyledComponentName_default = getStyledComponentName;
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// loaders/lib/appendCssUnitToExpressionValue.ts
|
|
251
|
+
var appendCssUnitToExpressionValue, appendCssUnitToExpressionValue_default;
|
|
252
|
+
var init_appendCssUnitToExpressionValue = __esm({
|
|
253
|
+
"loaders/lib/appendCssUnitToExpressionValue.ts"() {
|
|
254
|
+
"use strict";
|
|
255
|
+
appendCssUnitToExpressionValue = (cssUnit, expression, runtimeInternalHelpers, t) => {
|
|
256
|
+
if (expression.type === "ArrowFunctionExpression") {
|
|
257
|
+
if (expression.body.type !== "BlockStatement") {
|
|
258
|
+
const newBody = t.binaryExpression(
|
|
259
|
+
"+",
|
|
260
|
+
t.parenthesizedExpression(expression.body),
|
|
261
|
+
t.stringLiteral(cssUnit)
|
|
262
|
+
);
|
|
263
|
+
const newArrowFunction = t.arrowFunctionExpression(
|
|
264
|
+
expression.params,
|
|
265
|
+
newBody
|
|
266
|
+
);
|
|
267
|
+
return newArrowFunction;
|
|
268
|
+
}
|
|
269
|
+
} else if (expression.type === "NumericLiteral" || expression.type === "BinaryExpression" || expression.type === "Identifier") {
|
|
270
|
+
const cssUnitLiteral = t.stringLiteral(cssUnit);
|
|
271
|
+
const binaryExpression = t.binaryExpression(
|
|
272
|
+
"+",
|
|
273
|
+
expression,
|
|
274
|
+
cssUnitLiteral
|
|
275
|
+
);
|
|
276
|
+
return binaryExpression;
|
|
277
|
+
}
|
|
278
|
+
const callExpression = t.callExpression(t.identifier("__yak_unitPostFix"), [
|
|
279
|
+
expression,
|
|
280
|
+
t.stringLiteral(cssUnit)
|
|
281
|
+
]);
|
|
282
|
+
runtimeInternalHelpers.add("__yak_unitPostFix");
|
|
283
|
+
return callExpression;
|
|
284
|
+
};
|
|
285
|
+
appendCssUnitToExpressionValue_default = appendCssUnitToExpressionValue;
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// loaders/lib/getCssName.ts
|
|
290
|
+
function extractConditions(path) {
|
|
291
|
+
const conditions = [];
|
|
292
|
+
const visitedNodes = /* @__PURE__ */ new Set();
|
|
293
|
+
const getConditions = (node, previousNode, isNegated = false) => {
|
|
294
|
+
if (visitedNodes.has(node))
|
|
295
|
+
return;
|
|
296
|
+
visitedNodes.add(node);
|
|
297
|
+
if (node.type === "LogicalExpression") {
|
|
298
|
+
if (node.operator === "&&") {
|
|
299
|
+
getConditions(node.right, previousNode, isNegated);
|
|
300
|
+
conditions.push("and");
|
|
301
|
+
getConditions(node.left, previousNode, isNegated);
|
|
302
|
+
} else if (node.operator === "||") {
|
|
303
|
+
getConditions(node.right, previousNode, isNegated);
|
|
304
|
+
conditions.push("or");
|
|
305
|
+
getConditions(node.left, previousNode, isNegated);
|
|
306
|
+
}
|
|
307
|
+
} else if (node.type === "ConditionalExpression") {
|
|
308
|
+
conditions.push("and");
|
|
309
|
+
getConditions(node.test, previousNode, node.alternate === previousNode);
|
|
310
|
+
} else if (node.type === "UnaryExpression" && node.operator === "!") {
|
|
311
|
+
getConditions(node.argument, previousNode, !isNegated);
|
|
312
|
+
} else if (node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "Boolean") {
|
|
313
|
+
getConditions(node.arguments[0], previousNode, isNegated);
|
|
314
|
+
} else if (node.type === "Identifier") {
|
|
315
|
+
conditions.push((isNegated ? "not_" : "") + node.name);
|
|
316
|
+
} else if (node.type === "MemberExpression") {
|
|
317
|
+
conditions.push(
|
|
318
|
+
(isNegated ? "not_" : "") + getMemberExpressionName(node)
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
let currentPath = path;
|
|
323
|
+
let previousPath = path;
|
|
324
|
+
while (currentPath) {
|
|
325
|
+
getConditions(currentPath.node, previousPath.node);
|
|
326
|
+
previousPath = currentPath;
|
|
327
|
+
currentPath = currentPath.parentPath;
|
|
328
|
+
}
|
|
329
|
+
if (conditions[0] === "or" || conditions[0] === "and") {
|
|
330
|
+
conditions.shift();
|
|
331
|
+
}
|
|
332
|
+
return conditions.reverse();
|
|
333
|
+
}
|
|
334
|
+
function getMemberExpressionName(node) {
|
|
335
|
+
if (!node.object || !node.property || node.object.type !== "Identifier" && node.object.type !== "MemberExpression") {
|
|
336
|
+
return "";
|
|
337
|
+
}
|
|
338
|
+
const objectName = node.object.type === "Identifier" ? node.object.name : getMemberExpressionName(node.object);
|
|
339
|
+
const property = node.property;
|
|
340
|
+
let propertyName = "";
|
|
341
|
+
if (property.type === "Identifier") {
|
|
342
|
+
propertyName = property.name;
|
|
343
|
+
} else if (property.type === "StringLiteral") {
|
|
344
|
+
propertyName = property.value;
|
|
345
|
+
}
|
|
346
|
+
if (!propertyName) {
|
|
347
|
+
return "";
|
|
348
|
+
}
|
|
349
|
+
return objectName + propertyName[0].toUpperCase() + propertyName.slice(1);
|
|
350
|
+
}
|
|
351
|
+
function getCssName(literal) {
|
|
352
|
+
const conditions = extractConditions(literal);
|
|
353
|
+
if (conditions.length === 0) {
|
|
354
|
+
const mixinName = getStyledComponentName2(literal);
|
|
355
|
+
return mixinName ? mixinName : "yak";
|
|
356
|
+
}
|
|
357
|
+
return conditions.join("_").replace(/\$/g, "");
|
|
358
|
+
}
|
|
359
|
+
var getStyledComponentName2;
|
|
360
|
+
var init_getCssName = __esm({
|
|
361
|
+
"loaders/lib/getCssName.ts"() {
|
|
362
|
+
"use strict";
|
|
363
|
+
getStyledComponentName2 = (taggedTemplateExpressionPath) => {
|
|
364
|
+
const variableDeclaratorPath = taggedTemplateExpressionPath.findParent(
|
|
365
|
+
(path) => path.isVariableDeclarator()
|
|
366
|
+
);
|
|
367
|
+
if (!variableDeclaratorPath || !("id" in variableDeclaratorPath.node) || variableDeclaratorPath.node.id?.type !== "Identifier") {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
return variableDeclaratorPath.node.id.name;
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// loaders/lib/getConstantValues.ts
|
|
376
|
+
function getConstantValues(path, t) {
|
|
377
|
+
const topLevelConstBindings = /* @__PURE__ */ new Map();
|
|
378
|
+
const bindings = Object.entries(path.scope.bindings);
|
|
379
|
+
for (const [name, binding] of bindings) {
|
|
380
|
+
if (binding.kind === "module") {
|
|
381
|
+
topLevelConstBindings.set(name, null);
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (binding.kind === "let" || binding.kind === "var" || binding.kind === "const") {
|
|
385
|
+
if (!("init" in binding.path.node) || t.isFunctionDeclaration(binding.path.node.init) || t.isArrowFunctionExpression(binding.path.node.init)) {
|
|
386
|
+
topLevelConstBindings.set(name, null);
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const value = binding.path.node.init;
|
|
390
|
+
topLevelConstBindings.set(
|
|
391
|
+
name,
|
|
392
|
+
t.isStringLiteral(value) || t.isNumericLiteral(value) ? value.value : null
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return topLevelConstBindings;
|
|
397
|
+
}
|
|
398
|
+
var getConstantName;
|
|
399
|
+
var init_getConstantValues = __esm({
|
|
400
|
+
"loaders/lib/getConstantValues.ts"() {
|
|
401
|
+
"use strict";
|
|
402
|
+
getConstantName = (expression, t) => {
|
|
403
|
+
if (t.isIdentifier(expression)) {
|
|
404
|
+
return expression.name;
|
|
405
|
+
} else if (t.isMemberExpression(expression) && t.isIdentifier(expression.object)) {
|
|
406
|
+
return expression.object.name;
|
|
407
|
+
} else if (t.isCallExpression(expression) && t.isIdentifier(expression.callee)) {
|
|
408
|
+
return expression.callee.name;
|
|
409
|
+
} else if (t.isCallExpression(expression) && t.isMemberExpression(expression.callee) && t.isIdentifier(expression.callee.object)) {
|
|
410
|
+
return expression.callee.object.name;
|
|
411
|
+
} else {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// loaders/babel-yak-plugin.ts
|
|
419
|
+
var babel_yak_plugin_exports = {};
|
|
420
|
+
__export(babel_yak_plugin_exports, {
|
|
421
|
+
InvalidPositionError: () => InvalidPositionError,
|
|
422
|
+
default: () => babel_yak_plugin_default
|
|
423
|
+
});
|
|
424
|
+
import { relative, resolve, basename } from "path";
|
|
425
|
+
function babel_yak_plugin_default(babel2, options) {
|
|
426
|
+
const { replaces } = options;
|
|
427
|
+
const rootContext = options.rootContext || process.cwd();
|
|
428
|
+
const { types: t } = babel2;
|
|
429
|
+
const hashedFilePaths = /* @__PURE__ */ new WeakMap();
|
|
430
|
+
const getHashedFilePath = (file) => {
|
|
431
|
+
const fromCache = hashedFilePaths.get(file);
|
|
432
|
+
if (fromCache) {
|
|
433
|
+
return fromCache;
|
|
434
|
+
}
|
|
435
|
+
const resourcePath = file.opts.filename;
|
|
436
|
+
if (!resourcePath) {
|
|
437
|
+
throw new Error("resourcePath is undefined");
|
|
438
|
+
}
|
|
439
|
+
const relativePath = relative(
|
|
440
|
+
rootContext,
|
|
441
|
+
resolve(rootContext, resourcePath)
|
|
442
|
+
);
|
|
443
|
+
const hashedFilePath = murmurhash2_32_gc(relativePath);
|
|
444
|
+
hashedFilePaths.set(file, hashedFilePath);
|
|
445
|
+
return hashedFilePath;
|
|
446
|
+
};
|
|
447
|
+
const getYakExpressionType = (tag, localVarNames) => {
|
|
448
|
+
if (t.isIdentifier(tag)) {
|
|
449
|
+
if (tag.name === localVarNames.css) {
|
|
450
|
+
return "cssLiteral";
|
|
451
|
+
}
|
|
452
|
+
if (tag.name === localVarNames.keyframes) {
|
|
453
|
+
return "keyframesLiteral";
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (t.isMemberExpression(tag) && t.isIdentifier(tag.object) && tag.object.name === localVarNames.styled) {
|
|
457
|
+
return "styledLiteral";
|
|
458
|
+
}
|
|
459
|
+
if (t.isCallExpression(tag) && t.isIdentifier(tag.callee) && tag.callee.name === localVarNames.styled) {
|
|
460
|
+
return "styledCall";
|
|
461
|
+
}
|
|
462
|
+
if (t.isCallExpression(tag) && t.isMemberExpression(tag.callee) && t.isIdentifier(tag.callee.property) && tag.callee.property.name === "attrs") {
|
|
463
|
+
return "attrsCall";
|
|
464
|
+
}
|
|
465
|
+
return "unknown";
|
|
466
|
+
};
|
|
467
|
+
return {
|
|
468
|
+
name: "next-yak",
|
|
469
|
+
pre() {
|
|
470
|
+
this.localVarNames = {
|
|
471
|
+
css: void 0,
|
|
472
|
+
styled: void 0,
|
|
473
|
+
keyframes: void 0
|
|
474
|
+
};
|
|
475
|
+
this.isImportedInCurrentFile = false;
|
|
476
|
+
this.classNameCount = 0;
|
|
477
|
+
this.varIndex = 0;
|
|
478
|
+
this.variableNameToStyledCall = /* @__PURE__ */ new Map();
|
|
479
|
+
this.topLevelConstBindings = /* @__PURE__ */ new Map();
|
|
480
|
+
this.runtimeInternalHelpers = /* @__PURE__ */ new Set();
|
|
481
|
+
},
|
|
482
|
+
visitor: {
|
|
483
|
+
Program: {
|
|
484
|
+
enter(path, state) {
|
|
485
|
+
this.topLevelConstBindings = getConstantValues(path, t);
|
|
486
|
+
},
|
|
487
|
+
exit(path, state) {
|
|
488
|
+
if (this.runtimeInternalHelpers.size && this.yakImportPath) {
|
|
489
|
+
const newImport = t.importDeclaration(
|
|
490
|
+
[...this.runtimeInternalHelpers].map(
|
|
491
|
+
(helper) => t.importSpecifier(t.identifier(helper), t.identifier(helper))
|
|
492
|
+
),
|
|
493
|
+
t.stringLiteral("next-yak/runtime-internals")
|
|
494
|
+
);
|
|
495
|
+
this.yakImportPath.insertAfter(newImport);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
/**
|
|
500
|
+
* Store the name of the imported 'css' and 'styled' variables e.g.:
|
|
501
|
+
* - `import { css, styled } from 'next-yak'` -> { css: 'css', styled: 'styled' }
|
|
502
|
+
* - `import { css as yakCss, styled as yakStyled } from 'next-yak'` -> { css: 'yakCss', styled: 'yakStyled' }
|
|
503
|
+
*
|
|
504
|
+
* Inject the import to the css-module (with .yak.module.css extension)
|
|
505
|
+
* e.g. `import './App.yak.module.css!=!./App?./App.yak.module.css'`
|
|
506
|
+
*/
|
|
507
|
+
ImportDeclaration(path, state) {
|
|
508
|
+
const node = path.node;
|
|
509
|
+
if (node.source.value !== "next-yak") {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const filePath = state.file.opts.filename;
|
|
513
|
+
if (!filePath) {
|
|
514
|
+
throw new Error("filePath is undefined");
|
|
515
|
+
}
|
|
516
|
+
const fileName = basename(filePath).replace(/\.tsx?/, "");
|
|
517
|
+
path.insertAfter(
|
|
518
|
+
t.importDeclaration(
|
|
519
|
+
[t.importDefaultSpecifier(t.identifier("__styleYak"))],
|
|
520
|
+
t.stringLiteral(
|
|
521
|
+
`./${fileName}.yak.module.css!=!./${fileName}?./${fileName}.yak.module.css`
|
|
522
|
+
)
|
|
523
|
+
)
|
|
524
|
+
);
|
|
525
|
+
this.yakImportPath = path;
|
|
526
|
+
node.specifiers.forEach((specifier) => {
|
|
527
|
+
if (!("imported" in specifier) || !specifier.imported || !t.isIdentifier(specifier.imported)) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const importSpecifier = specifier.imported;
|
|
531
|
+
const localSpecifier = specifier.local || importSpecifier;
|
|
532
|
+
if (importSpecifier.name === "styled" || importSpecifier.name === "css" || importSpecifier.name === "keyframes") {
|
|
533
|
+
this.localVarNames[importSpecifier.name] = localSpecifier.name;
|
|
534
|
+
this.isImportedInCurrentFile = true;
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
},
|
|
538
|
+
/**
|
|
539
|
+
* Replace the tagged template expression
|
|
540
|
+
* - css`...`
|
|
541
|
+
* - styled.div`...`
|
|
542
|
+
* - styled(Component)`...`
|
|
543
|
+
* - styled.div.attrs({})`...`
|
|
544
|
+
* - keyframes`...`
|
|
545
|
+
*/
|
|
546
|
+
TaggedTemplateExpression(path, state) {
|
|
547
|
+
if (!this.isImportedInCurrentFile) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const tag = path.node.tag;
|
|
551
|
+
const expressionType = getYakExpressionType(tag, this.localVarNames);
|
|
552
|
+
if (expressionType === "unknown") {
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
const styledApi = expressionType === "styledLiteral" || expressionType === "styledCall" || expressionType === "attrsCall";
|
|
556
|
+
replaceTokensInQuasiExpressions(
|
|
557
|
+
path.node.quasi,
|
|
558
|
+
(name) => {
|
|
559
|
+
if (name in replaces) {
|
|
560
|
+
return replaces[name];
|
|
561
|
+
}
|
|
562
|
+
const styledCall2 = this.variableNameToStyledCall.get(name);
|
|
563
|
+
if (styledCall2) {
|
|
564
|
+
const { wasAdded, className, astNode } = styledCall2;
|
|
565
|
+
if (!wasAdded) {
|
|
566
|
+
styledCall2.wasAdded = true;
|
|
567
|
+
astNode.arguments.unshift(
|
|
568
|
+
t.memberExpression(
|
|
569
|
+
t.identifier("__styleYak"),
|
|
570
|
+
t.identifier(className)
|
|
571
|
+
)
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
return className;
|
|
575
|
+
}
|
|
576
|
+
return false;
|
|
577
|
+
},
|
|
578
|
+
t
|
|
579
|
+
);
|
|
580
|
+
const variableName = styledApi || expressionType === "keyframesLiteral" ? getStyledComponentName_default(path) : expressionType === "cssLiteral" ? getCssName(path) : null;
|
|
581
|
+
const identifier = localIdent(
|
|
582
|
+
variableName || "_yak",
|
|
583
|
+
variableName && expressionType !== "cssLiteral" ? null : this.classNameCount++,
|
|
584
|
+
expressionType === "keyframesLiteral" ? "animation" : "className"
|
|
585
|
+
);
|
|
586
|
+
let literalSelectorWasUsed = false;
|
|
587
|
+
const classNameExpression = t.memberExpression(
|
|
588
|
+
t.identifier("__styleYak"),
|
|
589
|
+
t.identifier(identifier)
|
|
590
|
+
);
|
|
591
|
+
const newArguments = /* @__PURE__ */ new Set();
|
|
592
|
+
const quasis = path.node.quasi.quasis;
|
|
593
|
+
let currentNestingScopes = [];
|
|
594
|
+
const quasiTypes = quasis.map((quasi) => {
|
|
595
|
+
const classification = quasiClassifier(
|
|
596
|
+
quasi.value.raw,
|
|
597
|
+
currentNestingScopes
|
|
598
|
+
);
|
|
599
|
+
currentNestingScopes = classification.currentNestingScopes;
|
|
600
|
+
return classification;
|
|
601
|
+
});
|
|
602
|
+
const expressions = path.node.quasi.expressions.filter(
|
|
603
|
+
(expression) => t.isExpression(expression)
|
|
604
|
+
);
|
|
605
|
+
let cssVariablesInlineStyle;
|
|
606
|
+
if (quasiTypes.length > 1 || quasiTypes.length === 1 && !quasiTypes[0].empty) {
|
|
607
|
+
newArguments.add(classNameExpression);
|
|
608
|
+
literalSelectorWasUsed = true;
|
|
609
|
+
}
|
|
610
|
+
let wasInsideCssValue = false;
|
|
611
|
+
for (let i = 0; i < quasis.length; i++) {
|
|
612
|
+
const expression = expressions[i];
|
|
613
|
+
const type = quasiTypes[i];
|
|
614
|
+
if (type.unknownSelector) {
|
|
615
|
+
const expression2 = expressions[i - 1];
|
|
616
|
+
if (!expression2) {
|
|
617
|
+
throw new Error(`Invalid css "${quasis[i].value.raw}"`);
|
|
618
|
+
}
|
|
619
|
+
throw new InvalidPositionError(
|
|
620
|
+
"Expressions are not allowed as selectors",
|
|
621
|
+
expression2,
|
|
622
|
+
this.file
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
if (expression && (type.unknownSelector || type.insideCssValue || type.empty && wasInsideCssValue)) {
|
|
626
|
+
wasInsideCssValue = true;
|
|
627
|
+
const variableName2 = getConstantName(expression, t);
|
|
628
|
+
if (variableName2 && this.topLevelConstBindings.has(variableName2)) {
|
|
629
|
+
const value = this.topLevelConstBindings.get(variableName2);
|
|
630
|
+
if (value !== null) {
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
throw new InvalidPositionError(
|
|
634
|
+
"Possible constant used as runtime value for a css variable\nPlease move the constant to a .yak import or use an arrow function\ne.g.:\n| import { primaryColor } from './foo.yak'\n| const MyStyledDiv = styled.div`color: ${primaryColor};`",
|
|
635
|
+
expression,
|
|
636
|
+
this.file
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
if (!cssVariablesInlineStyle) {
|
|
640
|
+
cssVariablesInlineStyle = t.objectExpression([]);
|
|
641
|
+
}
|
|
642
|
+
const cssVariableName = `--\u{1F9AC}${getHashedFilePath(state.file)}${this.varIndex++}`;
|
|
643
|
+
const cssUnit = quasis[i + 1]?.value.raw.match(/^([a-z]+|%)/i)?.[0];
|
|
644
|
+
const transformedExpression = cssUnit ? appendCssUnitToExpressionValue_default(
|
|
645
|
+
cssUnit,
|
|
646
|
+
expression,
|
|
647
|
+
this.runtimeInternalHelpers,
|
|
648
|
+
t
|
|
649
|
+
) : expression;
|
|
650
|
+
cssVariablesInlineStyle.properties.push(
|
|
651
|
+
t.objectProperty(
|
|
652
|
+
t.stringLiteral(cssVariableName),
|
|
653
|
+
transformedExpression
|
|
654
|
+
)
|
|
655
|
+
);
|
|
656
|
+
} else {
|
|
657
|
+
wasInsideCssValue = false;
|
|
658
|
+
if (expression) {
|
|
659
|
+
if (quasiTypes[i].currentNestingScopes.length > 0) {
|
|
660
|
+
const isReferenceToMixin = t.isIdentifier(expression) || t.isCallExpression(expression);
|
|
661
|
+
if (isReferenceToMixin) {
|
|
662
|
+
throw new InvalidPositionError(
|
|
663
|
+
`Mixins are not allowed inside nested selectors`,
|
|
664
|
+
expression,
|
|
665
|
+
this.file,
|
|
666
|
+
"Use an inline css literal instead or move the selector into the mixin"
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
newArguments.add(expression);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
if (cssVariablesInlineStyle) {
|
|
675
|
+
newArguments.add(
|
|
676
|
+
t.objectExpression([
|
|
677
|
+
t.objectProperty(
|
|
678
|
+
t.stringLiteral(`style`),
|
|
679
|
+
cssVariablesInlineStyle
|
|
680
|
+
)
|
|
681
|
+
])
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
const styledCall = t.callExpression(tag, [...newArguments]);
|
|
685
|
+
path.replaceWith(styledCall);
|
|
686
|
+
if (styledApi && variableName) {
|
|
687
|
+
this.variableNameToStyledCall.set(variableName, {
|
|
688
|
+
wasAdded: literalSelectorWasUsed,
|
|
689
|
+
className: identifier,
|
|
690
|
+
astNode: styledCall
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
var InvalidPositionError;
|
|
698
|
+
var init_babel_yak_plugin = __esm({
|
|
699
|
+
"loaders/babel-yak-plugin.ts"() {
|
|
700
|
+
"use strict";
|
|
701
|
+
init_quasiClassifier();
|
|
702
|
+
init_replaceQuasiExpressionTokens();
|
|
703
|
+
init_hash();
|
|
704
|
+
init_localIdent();
|
|
705
|
+
init_getStyledComponentName();
|
|
706
|
+
init_appendCssUnitToExpressionValue();
|
|
707
|
+
init_getCssName();
|
|
708
|
+
init_getConstantValues();
|
|
709
|
+
InvalidPositionError = class extends Error {
|
|
710
|
+
/**
|
|
711
|
+
* Add the expression code that caused the error to the message
|
|
712
|
+
* for better debugging
|
|
713
|
+
*/
|
|
714
|
+
constructor(message, expression, file, recommendedFix) {
|
|
715
|
+
let errorText = message;
|
|
716
|
+
const line = expression.loc?.start.line ?? -1;
|
|
717
|
+
if (line !== -1) {
|
|
718
|
+
errorText = `line ${line}: ${errorText}`;
|
|
719
|
+
}
|
|
720
|
+
if (expression.start && expression.end) {
|
|
721
|
+
errorText += `
|
|
722
|
+
found: \${${file.code.slice(
|
|
723
|
+
expression.start,
|
|
724
|
+
expression.end
|
|
725
|
+
)}}`;
|
|
726
|
+
}
|
|
727
|
+
if (recommendedFix) {
|
|
728
|
+
errorText += `
|
|
729
|
+
${recommendedFix}`;
|
|
730
|
+
}
|
|
731
|
+
super(errorText);
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
// loaders/tsloader.ts
|
|
738
|
+
import babel from "@babel/core";
|
|
739
|
+
|
|
740
|
+
// loaders/lib/getYakImports.ts
|
|
741
|
+
var getYakImports = (code) => {
|
|
742
|
+
const codeWithoutComments = code.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
743
|
+
const allImports = codeWithoutComments.matchAll(
|
|
744
|
+
/(^|\n|;)\s*import\s+(?:(\w+(?:\s+as\s+\w+)?)\s*,?\s*)?(?:{([^}]*)})?\s+from\s+["']([^'"]+\.yak)["'](;|\n)/g
|
|
745
|
+
);
|
|
746
|
+
return [...allImports].map(([, , defaultImport, namedImports, from]) => {
|
|
747
|
+
const imports = namedImports?.split(",").map((namedImport) => {
|
|
748
|
+
const [importedName, localName = importedName] = namedImport.replace(/^type\s+/, "").trim().split(/\s+as\s+/);
|
|
749
|
+
return { localName, importedName };
|
|
750
|
+
}) ?? [];
|
|
751
|
+
if (defaultImport) {
|
|
752
|
+
imports.push(parseDefaultImport(defaultImport));
|
|
753
|
+
}
|
|
754
|
+
return { imports, from };
|
|
755
|
+
});
|
|
756
|
+
};
|
|
757
|
+
function parseDefaultImport(defaultImport) {
|
|
758
|
+
const defaultImportArray = defaultImport.split(/\s+as\s+/);
|
|
759
|
+
return {
|
|
760
|
+
localName: defaultImportArray[1] ?? defaultImportArray[0],
|
|
761
|
+
importedName: defaultImportArray[0]
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
var getYakImports_default = getYakImports;
|
|
765
|
+
|
|
766
|
+
// loaders/tsloader.ts
|
|
767
|
+
init_babel_yak_plugin();
|
|
768
|
+
async function tsloader(source) {
|
|
769
|
+
if (!source.includes("next-yak")) {
|
|
770
|
+
return source;
|
|
771
|
+
}
|
|
772
|
+
const callback = this.async();
|
|
773
|
+
const { rootContext, resourcePath } = this;
|
|
774
|
+
const isYakFile = /\.yak\.(j|t)sx?$/.test(resourcePath.matches);
|
|
775
|
+
const importedYakConstants = isYakFile ? [] : getYakImports_default(source);
|
|
776
|
+
const replaces = {};
|
|
777
|
+
await Promise.all(
|
|
778
|
+
importedYakConstants.map(async ({ imports, from }) => {
|
|
779
|
+
const constantValues = await this.importModule(from, {
|
|
780
|
+
layer: "yak-importModule"
|
|
781
|
+
});
|
|
782
|
+
imports.forEach(({ localName, importedName }) => {
|
|
783
|
+
replaces[localName] = constantValues[importedName];
|
|
784
|
+
});
|
|
785
|
+
})
|
|
786
|
+
);
|
|
787
|
+
let result = null;
|
|
788
|
+
try {
|
|
789
|
+
result = babel.transformSync(source, {
|
|
790
|
+
filename: resourcePath,
|
|
791
|
+
configFile: false,
|
|
792
|
+
plugins: [
|
|
793
|
+
[
|
|
794
|
+
"@babel/plugin-syntax-typescript",
|
|
795
|
+
{ isTSX: this.resourcePath.endsWith(".tsx") }
|
|
796
|
+
],
|
|
797
|
+
[
|
|
798
|
+
await Promise.resolve().then(() => (init_babel_yak_plugin(), babel_yak_plugin_exports)).then((m) => m.default),
|
|
799
|
+
{
|
|
800
|
+
replaces,
|
|
801
|
+
rootContext
|
|
802
|
+
}
|
|
803
|
+
]
|
|
804
|
+
]
|
|
805
|
+
});
|
|
806
|
+
} catch (error) {
|
|
807
|
+
if (error instanceof InvalidPositionError) {
|
|
808
|
+
return callback(new Error(error.message));
|
|
809
|
+
}
|
|
810
|
+
return callback(new Error("babel transform failed"));
|
|
811
|
+
}
|
|
812
|
+
if (!result?.code) {
|
|
813
|
+
return callback(new Error("babel transform failed"));
|
|
814
|
+
}
|
|
815
|
+
return callback(null, result.code, result.map);
|
|
816
|
+
}
|
|
817
|
+
export {
|
|
818
|
+
tsloader as default
|
|
819
|
+
};
|
|
820
|
+
//# sourceMappingURL=tsloader.js.map
|