@wyw-in-js/processor-utils 1.1.0 → 2.0.0-alpha.1
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/esm/BaseProcessor.js +48 -76
- package/esm/BaseProcessor.js.map +1 -1
- package/esm/TaggedTemplateProcessor.js +32 -41
- package/esm/TaggedTemplateProcessor.js.map +1 -1
- package/esm/ast.js +72 -0
- package/esm/ast.js.map +1 -0
- package/esm/diagnostics.js +4 -4
- package/esm/diagnostics.js.map +1 -1
- package/esm/index.js +9 -8
- package/esm/index.js.map +1 -1
- package/esm/static.js +2 -0
- package/esm/static.js.map +1 -0
- package/esm/types.js +1 -1
- package/esm/types.js.map +1 -1
- package/esm/utils/buildSlug.js +2 -2
- package/esm/utils/buildSlug.js.map +1 -1
- package/esm/utils/getClassNameAndSlug.js +38 -41
- package/esm/utils/getClassNameAndSlug.js.map +1 -1
- package/esm/utils/getVariableName.js +7 -10
- package/esm/utils/getVariableName.js.map +1 -1
- package/esm/utils/stripLines.js +9 -11
- package/esm/utils/stripLines.js.map +1 -1
- package/esm/utils/templateProcessor.js +98 -118
- package/esm/utils/templateProcessor.js.map +1 -1
- package/esm/utils/throwIfInvalid.js +11 -12
- package/esm/utils/throwIfInvalid.js.map +1 -1
- package/esm/utils/toCSS.js +34 -43
- package/esm/utils/toCSS.js.map +1 -1
- package/esm/utils/toValidCSSIdentifier.js +2 -2
- package/esm/utils/toValidCSSIdentifier.js.map +1 -1
- package/esm/utils/types.js +1 -1
- package/esm/utils/types.js.map +1 -1
- package/esm/utils/units.js +79 -61
- package/esm/utils/units.js.map +1 -1
- package/esm/utils/validateParams.js +29 -39
- package/esm/utils/validateParams.js.map +1 -1
- package/package.json +15 -15
- package/types/BaseProcessor.d.ts +9 -12
- package/types/BaseProcessor.js +13 -22
- package/types/TaggedTemplateProcessor.d.ts +1 -1
- package/types/TaggedTemplateProcessor.js +9 -16
- package/types/ast.d.ts +95 -0
- package/types/ast.js +80 -0
- package/types/diagnostics.js +3 -8
- package/types/index.d.ts +4 -1
- package/types/index.js +8 -33
- package/types/static.d.ts +63 -0
- package/types/static.js +1 -0
- package/types/types.d.ts +1 -1
- package/types/types.js +1 -2
- package/types/utils/buildSlug.js +1 -4
- package/types/utils/getClassNameAndSlug.js +15 -18
- package/types/utils/getVariableName.js +1 -4
- package/types/utils/stripLines.js +1 -4
- package/types/utils/templateProcessor.d.ts +1 -1
- package/types/utils/templateProcessor.js +16 -55
- package/types/utils/throwIfInvalid.js +1 -3
- package/types/utils/toCSS.js +8 -13
- package/types/utils/toValidCSSIdentifier.js +1 -4
- package/types/utils/types.d.ts +4 -2
- package/types/utils/types.js +1 -2
- package/types/utils/units.js +2 -5
- package/types/utils/validateParams.js +2 -6
- package/lib/BaseProcessor.js +0 -87
- package/lib/BaseProcessor.js.map +0 -1
- package/lib/TaggedTemplateProcessor.js +0 -51
- package/lib/TaggedTemplateProcessor.js.map +0 -1
- package/lib/diagnostics.js +0 -12
- package/lib/diagnostics.js.map +0 -1
- package/lib/index.js +0 -89
- package/lib/index.js.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/lib/utils/buildSlug.js +0 -12
- package/lib/utils/buildSlug.js.map +0 -1
- package/lib/utils/getClassNameAndSlug.js +0 -50
- package/lib/utils/getClassNameAndSlug.js.map +0 -1
- package/lib/utils/getVariableName.js +0 -18
- package/lib/utils/getVariableName.js.map +0 -1
- package/lib/utils/stripLines.js +0 -22
- package/lib/utils/stripLines.js.map +0 -1
- package/lib/utils/templateProcessor.js +0 -129
- package/lib/utils/templateProcessor.js.map +0 -1
- package/lib/utils/throwIfInvalid.js +0 -35
- package/lib/utils/throwIfInvalid.js.map +0 -1
- package/lib/utils/toCSS.js +0 -58
- package/lib/utils/toCSS.js.map +0 -1
- package/lib/utils/toValidCSSIdentifier.js +0 -10
- package/lib/utils/toValidCSSIdentifier.js.map +0 -1
- package/lib/utils/types.js +0 -2
- package/lib/utils/types.js.map +0 -1
- package/lib/utils/units.js +0 -71
- package/lib/utils/units.js.map +0 -1
- package/lib/utils/validateParams.js +0 -54
- package/lib/utils/validateParams.js.map +0 -1
package/esm/utils/stripLines.js
CHANGED
|
@@ -2,15 +2,13 @@
|
|
|
2
2
|
// This is useful in case of tools such as the stylelint pre-processor
|
|
3
3
|
// This should be safe because strings cannot contain newline: https://www.w3.org/TR/CSS2/syndata.html#strings
|
|
4
4
|
export default function stripLines(loc, text) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
return result;
|
|
5
|
+
let result = String(text).replace(/[\r\n]+/g, " ").trim();
|
|
6
|
+
// If the start and end line numbers aren't same, add new lines to span the text across multiple lines
|
|
7
|
+
if (loc.start.line !== loc.end.line) {
|
|
8
|
+
result += "\n".repeat(loc.end.line - loc.start.line);
|
|
9
|
+
// Add extra spaces to offset the column
|
|
10
|
+
result += " ".repeat(loc.end.column);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
15
13
|
}
|
|
16
|
-
//# sourceMappingURL=stripLines.js.map
|
|
14
|
+
//# sourceMappingURL=stripLines.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":";;;AAKA,eAAe,SAAS,WACtB,KACA,MACA;CACA,IAAI,SAAS,OAAO,KAAK,CACtB,QAAQ,YAAY,IAAI,CACxB,MAAM;;AAGT,KAAI,IAAI,MAAM,SAAS,IAAI,IAAI,MAAM;AACnC,YAAU,KAAK,OAAO,IAAI,IAAI,OAAO,IAAI,MAAM,KAAK;;AAGpD,YAAU,IAAI,OAAO,IAAI,IAAI,OAAO;;AAGtC,QAAO","names":[],"sources":["../../src/utils/stripLines.ts"],"version":3,"sourcesContent":["import type { Location } from '@wyw-in-js/shared';\n\n// Stripping away the new lines ensures that we preserve line numbers\n// This is useful in case of tools such as the stylelint pre-processor\n// This should be safe because strings cannot contain newline: https://www.w3.org/TR/CSS2/syndata.html#strings\nexport default function stripLines(\n loc: { end: Location; start: Location },\n text: string | number\n) {\n let result = String(text)\n .replace(/[\\r\\n]+/g, ' ')\n .trim();\n\n // If the start and end line numbers aren't same, add new lines to span the text across multiple lines\n if (loc.start.line !== loc.end.line) {\n result += '\\n'.repeat(loc.end.line - loc.start.line);\n\n // Add extra spaces to offset the column\n result += ' '.repeat(loc.end.column);\n }\n\n return result;\n}\n"],"file":"stripLines.js"}
|
|
@@ -1,120 +1,100 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { hasEvalMeta, ValueType } from '@wyw-in-js/shared';
|
|
8
|
-
import { getVariableName } from './getVariableName';
|
|
9
|
-
import stripLines from './stripLines';
|
|
10
|
-
import throwIfInvalid from './throwIfInvalid';
|
|
11
|
-
import toCSS, { isCSSable } from './toCSS';
|
|
12
|
-
import { units } from './units';
|
|
13
|
-
|
|
1
|
+
import { hasEvalMeta, ValueType } from "@wyw-in-js/shared";
|
|
2
|
+
import { getVariableName } from "./getVariableName.js";
|
|
3
|
+
import stripLines from "./stripLines.js";
|
|
4
|
+
import throwIfInvalid from "./throwIfInvalid.js";
|
|
5
|
+
import toCSS, { isCSSable } from "./toCSS.js";
|
|
6
|
+
import { units } from "./units.js";
|
|
14
7
|
// Match any valid CSS unit not immediately followed by an alphanumeric character or underscore.
|
|
15
|
-
const unitRegex = new RegExp(`^(?:${units.join(
|
|
16
|
-
export default function templateProcessor(tagProcessor, [...template], valueCache, variableNameConfig) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
const rules = tagProcessor.extractRules(valueCache, cssText, tagProcessor.location);
|
|
111
|
-
|
|
112
|
-
// tagProcessor.doRuntimeReplacement(classes);
|
|
113
|
-
if (!isReferenced && !cssText.includes(':global')) {
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// eslint-disable-next-line no-param-reassign
|
|
118
|
-
return [rules, sourceMapReplacements];
|
|
8
|
+
const unitRegex = new RegExp(`^(?:${units.join("|")})(?![a-zA-Z0-9_])`);
|
|
9
|
+
export default function templateProcessor(tagProcessor, [ ...template], valueCache, variableNameConfig) {
|
|
10
|
+
const sourceMapReplacements = [];
|
|
11
|
+
// Check if the variable is referenced anywhere for basic DCE
|
|
12
|
+
// Only works when it's assigned to a variable
|
|
13
|
+
const { isReferenced } = tagProcessor;
|
|
14
|
+
// Serialize the tagged template literal to a string
|
|
15
|
+
let cssText = "";
|
|
16
|
+
let item;
|
|
17
|
+
// eslint-disable-next-line no-cond-assign
|
|
18
|
+
while (item = template.shift()) {
|
|
19
|
+
if ("type" in item) {
|
|
20
|
+
// It's a template element
|
|
21
|
+
cssText += item.value.cooked;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
// It's an expression
|
|
25
|
+
const { ex } = item;
|
|
26
|
+
const { end, start } = ex.loc;
|
|
27
|
+
const beforeLength = cssText.length;
|
|
28
|
+
// The location will be end of the current string to start of next string
|
|
29
|
+
const next = template[0];
|
|
30
|
+
const loc = {
|
|
31
|
+
start,
|
|
32
|
+
end: next ? {
|
|
33
|
+
line: next.loc.start.line,
|
|
34
|
+
column: next.loc.start.column
|
|
35
|
+
} : {
|
|
36
|
+
line: end.line,
|
|
37
|
+
column: end.column + 1
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const value = "value" in item ? item.value : valueCache.get(item.ex.name);
|
|
41
|
+
// Is it props based interpolation?
|
|
42
|
+
if (item.kind === ValueType.FUNCTION || typeof value === "function") {
|
|
43
|
+
// Check if previous expression was a CSS variable that we replaced
|
|
44
|
+
// If it has a unit after it, we need to move the unit into the interpolation
|
|
45
|
+
// e.g. `var(--size)px` should actually be `var(--size)`
|
|
46
|
+
// So we check if the current text starts with a unit, and add the unit to the previous interpolation
|
|
47
|
+
// Another approach would be `calc(var(--size) * 1px), but some browsers don't support all units
|
|
48
|
+
// https://bugzilla.mozilla.org/show_bug.cgi?id=956573
|
|
49
|
+
const matches = next.value.cooked?.match(unitRegex);
|
|
50
|
+
try {
|
|
51
|
+
if (matches) {
|
|
52
|
+
template.shift();
|
|
53
|
+
const [unit] = matches;
|
|
54
|
+
const varId = tagProcessor.addInterpolation(item.ex, cssText, item.source, unit);
|
|
55
|
+
cssText += getVariableName(varId, variableNameConfig);
|
|
56
|
+
cssText += next.value.cooked?.substring(unit?.length ?? 0) ?? "";
|
|
57
|
+
} else {
|
|
58
|
+
const varId = tagProcessor.addInterpolation(item.ex, cssText, item.source);
|
|
59
|
+
cssText += getVariableName(varId, variableNameConfig);
|
|
60
|
+
}
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (e instanceof Error) {
|
|
63
|
+
throw item.buildCodeFrameError(e.message);
|
|
64
|
+
}
|
|
65
|
+
throw e;
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
throwIfInvalid(tagProcessor.isValidValue.bind(tagProcessor), value, item, item.source);
|
|
69
|
+
if (value !== undefined && typeof value !== "function") {
|
|
70
|
+
// Skip the blank string instead of throw ing an error
|
|
71
|
+
if (value === "") {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (hasEvalMeta(value)) {
|
|
75
|
+
// If it's a React component wrapped in styled, get the class name
|
|
76
|
+
// Useful for interpolating components
|
|
77
|
+
cssText += `.${value.__wyw_meta.className}`;
|
|
78
|
+
} else if (isCSSable(value)) {
|
|
79
|
+
// If it's a plain object or an array, convert it to a CSS string
|
|
80
|
+
cssText += stripLines(loc, toCSS(value));
|
|
81
|
+
} else {
|
|
82
|
+
// For anything else, assume it'll be stringified
|
|
83
|
+
cssText += stripLines(loc, value);
|
|
84
|
+
}
|
|
85
|
+
sourceMapReplacements.push({
|
|
86
|
+
original: loc,
|
|
87
|
+
length: cssText.length - beforeLength
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const rules = tagProcessor.extractRules(valueCache, cssText, tagProcessor.location);
|
|
93
|
+
// tagProcessor.doRuntimeReplacement(classes);
|
|
94
|
+
if (!isReferenced && !cssText.includes(":global")) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
// eslint-disable-next-line no-param-reassign
|
|
98
|
+
return [rules, sourceMapReplacements];
|
|
119
99
|
}
|
|
120
|
-
//# sourceMappingURL=templateProcessor.js.map
|
|
100
|
+
//# sourceMappingURL=templateProcessor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAOA,SAAS,aAAa,iBAAiB;AAMvC,SAAS,uBAAuB;AAChC,OAAO,gBAAgB;AACvB,OAAO,oBAAoB;AAC3B,OAAO,SAAS,iBAAiB;AAEjC,SAAS,aAAa;;AAGtB,MAAM,YAAY,IAAI,OAAO,OAAO,MAAM,KAAK,IAAI,CAAC,mBAAmB;AAEvE,eAAe,SAAS,kBACtB,cACA,EAAC,GAAG,WACJ,YACA,oBAC4D;CAC5D,MAAM,wBAAsC,EAAE;;;CAG9C,MAAM,EAAE,iBAAiB;;CAGzB,IAAI,UAAU;CAEd,IAAI;;AAEJ,QAAQ,OAAO,SAAS,OAAO,EAAG;AAChC,MAAI,UAAU,MAAM;;AAElB,cAAW,KAAK,MAAM;AACtB;;;EAIF,MAAM,EAAE,OAAO;EAEf,MAAM,EAAE,KAAK,UAAU,GAAG;EAC1B,MAAM,eAAe,QAAQ;;EAG7B,MAAM,OAAO,SAAS;EACtB,MAAM,MAAM;GACV;GACA,KAAK,OACD;IAAE,MAAM,KAAK,IAAK,MAAM;IAAM,QAAQ,KAAK,IAAK,MAAM;IAAQ,GAC9D;IAAE,MAAM,IAAI;IAAM,QAAQ,IAAI,SAAS;IAAG;GAC/C;EAED,MAAM,QAAQ,WAAW,OAAO,KAAK,QAAQ,WAAW,IAAI,KAAK,GAAG,KAAK;;AAGzE,MAAI,KAAK,SAAS,UAAU,YAAY,OAAO,UAAU,YAAY;;;;;;;GAOnE,MAAM,UAAU,KAAK,MAAM,QAAQ,MAAM,UAAU;AAEnD,OAAI;AACF,QAAI,SAAS;AACX,cAAS,OAAO;KAChB,MAAM,CAAC,QAAQ;KAEf,MAAM,QAAQ,aAAa,iBACzB,KAAK,IACL,SACA,KAAK,QACL,KACD;AACD,gBAAW,gBAAgB,OAAO,mBAAmB;AAErD,gBAAW,KAAK,MAAM,QAAQ,UAAU,MAAM,UAAU,EAAE,IAAI;WACzD;KACL,MAAM,QAAQ,aAAa,iBACzB,KAAK,IACL,SACA,KAAK,OACN;AACD,gBAAW,gBAAgB,OAAO,mBAAmB;;YAEhD,GAAG;AACV,QAAI,aAAa,OAAO;AACtB,WAAM,KAAK,oBAAoB,EAAE,QAAQ;;AAG3C,UAAM;;SAEH;AACL,kBACE,aAAa,aAAa,KAAK,aAAa,EAC5C,OACA,MACA,KAAK,OACN;AAED,OAAI,UAAU,aAAa,OAAO,UAAU,YAAY;;AAEtD,QAAI,UAAU,IAAI;AAChB;;AAGF,QAAI,YAAY,MAAM,EAAE;;;AAGtB,gBAAW,IAAI,MAAM,WAAW;eACvB,UAAU,MAAM,EAAE;;AAE3B,gBAAW,WAAW,KAAK,MAAM,MAAM,CAAC;WACnC;;AAEL,gBAAW,WAAW,KAAK,MAAM;;AAGnC,0BAAsB,KAAK;KACzB,UAAU;KACV,QAAQ,QAAQ,SAAS;KAC1B,CAAC;;;;CAKR,MAAM,QAAQ,aAAa,aACzB,YACA,SACA,aAAa,SACd;;AAGD,KAAI,CAAC,gBAAgB,CAAC,QAAQ,SAAS,UAAU,EAAE;AACjD,SAAO;;;AAIT,QAAO,CAAC,OAAO,sBAAsB","names":[],"sources":["../../src/utils/templateProcessor.ts"],"version":3,"sourcesContent":["/* eslint-disable no-continue */\n/**\n * This file handles transforming template literals to class names or styled components and generates CSS content.\n * It uses CSS code from template literals and evaluated values of lazy dependencies stored in ValueCache.\n */\n\nimport type { ExpressionValue, Replacements } from '@wyw-in-js/shared';\nimport { hasEvalMeta, ValueType } from '@wyw-in-js/shared';\n\nimport type { TaggedTemplateProcessor } from '../TaggedTemplateProcessor';\nimport type { Expression, TemplateElement } from '../ast';\nimport type { ValueCache, Rules } from '../types';\n\nimport { getVariableName } from './getVariableName';\nimport stripLines from './stripLines';\nimport throwIfInvalid from './throwIfInvalid';\nimport toCSS, { isCSSable } from './toCSS';\nimport type { IOptions } from './types';\nimport { units } from './units';\n\n// Match any valid CSS unit not immediately followed by an alphanumeric character or underscore.\nconst unitRegex = new RegExp(`^(?:${units.join('|')})(?![a-zA-Z0-9_])`);\n\nexport default function templateProcessor(\n tagProcessor: TaggedTemplateProcessor,\n [...template]: (TemplateElement | ExpressionValue)[],\n valueCache: ValueCache,\n variableNameConfig: IOptions['variableNameConfig'] | undefined\n): [rules: Rules, sourceMapReplacements: Replacements] | null {\n const sourceMapReplacements: Replacements = [];\n // Check if the variable is referenced anywhere for basic DCE\n // Only works when it's assigned to a variable\n const { isReferenced } = tagProcessor;\n\n // Serialize the tagged template literal to a string\n let cssText = '';\n\n let item: TemplateElement | ExpressionValue | undefined;\n // eslint-disable-next-line no-cond-assign\n while ((item = template.shift())) {\n if ('type' in item) {\n // It's a template element\n cssText += item.value.cooked;\n continue;\n }\n\n // It's an expression\n const { ex } = item;\n\n const { end, start } = ex.loc!;\n const beforeLength = cssText.length;\n\n // The location will be end of the current string to start of next string\n const next = template[0] as TemplateElement; // template[0] is the next template element\n const loc = {\n start,\n end: next\n ? { line: next.loc!.start.line, column: next.loc!.start.column }\n : { line: end.line, column: end.column + 1 },\n };\n\n const value = 'value' in item ? item.value : valueCache.get(item.ex.name);\n\n // Is it props based interpolation?\n if (item.kind === ValueType.FUNCTION || typeof value === 'function') {\n // Check if previous expression was a CSS variable that we replaced\n // If it has a unit after it, we need to move the unit into the interpolation\n // e.g. `var(--size)px` should actually be `var(--size)`\n // So we check if the current text starts with a unit, and add the unit to the previous interpolation\n // Another approach would be `calc(var(--size) * 1px), but some browsers don't support all units\n // https://bugzilla.mozilla.org/show_bug.cgi?id=956573\n const matches = next.value.cooked?.match(unitRegex);\n\n try {\n if (matches) {\n template.shift();\n const [unit] = matches;\n\n const varId = tagProcessor.addInterpolation(\n item.ex as Expression,\n cssText,\n item.source,\n unit\n );\n cssText += getVariableName(varId, variableNameConfig);\n\n cssText += next.value.cooked?.substring(unit?.length ?? 0) ?? '';\n } else {\n const varId = tagProcessor.addInterpolation(\n item.ex as Expression,\n cssText,\n item.source\n );\n cssText += getVariableName(varId, variableNameConfig);\n }\n } catch (e) {\n if (e instanceof Error) {\n throw item.buildCodeFrameError(e.message);\n }\n\n throw e;\n }\n } else {\n throwIfInvalid(\n tagProcessor.isValidValue.bind(tagProcessor),\n value,\n item,\n item.source\n );\n\n if (value !== undefined && typeof value !== 'function') {\n // Skip the blank string instead of throw ing an error\n if (value === '') {\n continue;\n }\n\n if (hasEvalMeta(value)) {\n // If it's a React component wrapped in styled, get the class name\n // Useful for interpolating components\n cssText += `.${value.__wyw_meta.className}`;\n } else if (isCSSable(value)) {\n // If it's a plain object or an array, convert it to a CSS string\n cssText += stripLines(loc, toCSS(value));\n } else {\n // For anything else, assume it'll be stringified\n cssText += stripLines(loc, value);\n }\n\n sourceMapReplacements.push({\n original: loc,\n length: cssText.length - beforeLength,\n });\n }\n }\n }\n\n const rules = tagProcessor.extractRules(\n valueCache,\n cssText,\n tagProcessor.location\n );\n\n // tagProcessor.doRuntimeReplacement(classes);\n if (!isReferenced && !cssText.includes(':global')) {\n return null;\n }\n\n // eslint-disable-next-line no-param-reassign\n return [rules, sourceMapReplacements];\n}\n"],"file":"templateProcessor.js"}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
const isLikeError = value => typeof value ===
|
|
2
|
-
|
|
1
|
+
const isLikeError = (value) => typeof value === "object" && value !== null && "stack" in value && "message" in value;
|
|
3
2
|
// Throw if we can't handle the interpolated value
|
|
4
3
|
function throwIfInvalid(checker, value, ex, source) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
// We can't use instanceof here so let's use duck typing
|
|
5
|
+
if (isLikeError(value) && value.stack && value.message) {
|
|
6
|
+
throw ex.buildCodeFrameError(`An error occurred when evaluating the expression:
|
|
8
7
|
|
|
9
8
|
> ${value.message}.
|
|
10
9
|
|
|
@@ -18,12 +17,12 @@ function throwIfInvalid(checker, value, ex, source) {
|
|
|
18
17
|
because it cannot be statically determined in which context you use them.
|
|
19
18
|
That's why some variables may be not defined during evaluation.
|
|
20
19
|
`);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
}
|
|
21
|
+
if (checker(value)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const stringified = typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
25
|
+
throw ex.buildCodeFrameError(`The expression evaluated to '${stringified}', which is probably a mistake. If you want it to be inserted into CSS, explicitly cast or transform the value to a string, e.g. - 'String(${source})'.`);
|
|
27
26
|
}
|
|
28
27
|
export default throwIfInvalid;
|
|
29
|
-
//# sourceMappingURL=throwIfInvalid.js.map
|
|
28
|
+
//# sourceMappingURL=throwIfInvalid.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAEA,MAAM,eAAe,UACnB,OAAO,UAAU,YACjB,UAAU,QACV,WAAW,SACX,aAAa;;AAGf,SAAS,eACP,SACA,OACA,IACA,QACoB;;AAEpB,KAAI,YAAY,MAAM,IAAI,MAAM,SAAS,MAAM,SAAS;AACtD,QAAM,GAAG,oBACP;;MAEA,MAAM,QAAQ;;;;;;;;;;;QAYf;;AAGH,KAAI,QAAQ,MAAM,EAAE;AAClB;;CAGF,MAAM,cACJ,OAAO,UAAU,WAAW,KAAK,UAAU,MAAM,GAAG,OAAO,MAAM;AAEnE,OAAM,GAAG,oBACP,gCAAgC,YAAY,6IAA6I,OAAO,KACjM;;AAGH,eAAe","names":[],"sources":["../../src/utils/throwIfInvalid.ts"],"version":3,"sourcesContent":["import type { BuildCodeFrameErrorFn } from '@wyw-in-js/shared';\n\nconst isLikeError = (value: unknown): value is Error =>\n typeof value === 'object' &&\n value !== null &&\n 'stack' in value &&\n 'message' in value;\n\n// Throw if we can't handle the interpolated value\nfunction throwIfInvalid<T>(\n checker: (value: unknown) => value is T,\n value: Error | unknown,\n ex: { buildCodeFrameError: BuildCodeFrameErrorFn },\n source: string\n): asserts value is T {\n // We can't use instanceof here so let's use duck typing\n if (isLikeError(value) && value.stack && value.message) {\n throw ex.buildCodeFrameError(\n `An error occurred when evaluating the expression:\n\n > ${value.message}.\n\n Make sure you are not using a browser or Node specific API and all the variables are available in static context.\n Linaria have to extract pieces of your code to resolve the interpolated values.\n Defining styled component or class will not work inside:\n - function,\n - class,\n - method,\n - loop,\n because it cannot be statically determined in which context you use them.\n That's why some variables may be not defined during evaluation.\n `\n );\n }\n\n if (checker(value)) {\n return;\n }\n\n const stringified =\n typeof value === 'object' ? JSON.stringify(value) : String(value);\n\n throw ex.buildCodeFrameError(\n `The expression evaluated to '${stringified}', which is probably a mistake. If you want it to be inserted into CSS, explicitly cast or transform the value to a string, e.g. - 'String(${source})'.`\n );\n}\n\nexport default throwIfInvalid;\n"],"file":"throwIfInvalid.js"}
|
package/esm/utils/toCSS.js
CHANGED
|
@@ -1,50 +1,41 @@
|
|
|
1
|
-
import { isBoxedPrimitive } from
|
|
2
|
-
import { unitless } from
|
|
3
|
-
const isCSSPropertyValue = o => {
|
|
4
|
-
|
|
1
|
+
import { isBoxedPrimitive } from "@wyw-in-js/shared";
|
|
2
|
+
import { unitless } from "./units.js";
|
|
3
|
+
const isCSSPropertyValue = (o) => {
|
|
4
|
+
return isBoxedPrimitive(o) || typeof o === "string" || typeof o === "number" && Number.isFinite(o);
|
|
5
5
|
};
|
|
6
|
-
export const isCSSable = o => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
6
|
+
export const isCSSable = (o) => {
|
|
7
|
+
if (isCSSPropertyValue(o)) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
if (Array.isArray(o)) {
|
|
11
|
+
return o.every(isCSSable);
|
|
12
|
+
}
|
|
13
|
+
if (typeof o === "object") {
|
|
14
|
+
return o !== null && Object.values(o).every(isCSSable);
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
17
|
};
|
|
18
|
-
const hyphenate = s => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// Hyphenate CSS property names from camelCase version from JS string
|
|
25
|
-
.replace(/([A-Z])/g, (match, p1) => `-${p1.toLowerCase()}`)
|
|
26
|
-
// Special case for `-ms` because in JS it starts with `ms` unlike `Webkit`
|
|
27
|
-
.replace(/^ms-/, '-ms-');
|
|
18
|
+
const hyphenate = (s) => {
|
|
19
|
+
if (s.startsWith("--")) {
|
|
20
|
+
// It's a custom property which is already well formatted.
|
|
21
|
+
return s;
|
|
22
|
+
}
|
|
23
|
+
return s.replace(/([A-Z])/g, (match, p1) => `-${p1.toLowerCase()}`).replace(/^ms-/, "-ms-");
|
|
28
24
|
};
|
|
29
|
-
|
|
30
25
|
// Some tools such as polished.js output JS objects
|
|
31
26
|
// To support them transparently, we convert JS objects to CSS strings
|
|
32
27
|
export default function toCSS(o) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return `${hyphenate(key)}: ${typeof value === 'number' && value !== 0 &&
|
|
46
|
-
// Strip vendor prefixes when checking if the value is unitless
|
|
47
|
-
!(key.replace(/^(Webkit|Moz|O|ms)([A-Z])(.+)$/, (match, p1, p2, p3) => `${p2.toLowerCase()}${p3}`) in unitless) ? `${value}px` : value};`;
|
|
48
|
-
}).join(' ');
|
|
28
|
+
if (Array.isArray(o)) {
|
|
29
|
+
return o.map(toCSS).join("\n");
|
|
30
|
+
}
|
|
31
|
+
if (isCSSPropertyValue(o)) {
|
|
32
|
+
return o.valueOf().toString();
|
|
33
|
+
}
|
|
34
|
+
return Object.entries(o).filter(([, value]) => typeof value === "number" || value).map(([key, value]) => {
|
|
35
|
+
if (!isCSSPropertyValue(value)) {
|
|
36
|
+
return `${key} { ${toCSS(value)} }`;
|
|
37
|
+
}
|
|
38
|
+
return `${hyphenate(key)}: ${typeof value === "number" && value !== 0 && !(key.replace(/^(Webkit|Moz|O|ms)([A-Z])(.+)$/, (match, p1, p2, p3) => `${p2.toLowerCase()}${p3}`) in unitless) ? `${value}px` : value};`;
|
|
39
|
+
}).join(" ");
|
|
49
40
|
}
|
|
50
|
-
//# sourceMappingURL=toCSS.js.map
|
|
41
|
+
//# sourceMappingURL=toCSS.js.map
|
package/esm/utils/toCSS.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAAS,wBAAwB;AAIjC,SAAS,gBAAgB;AAEzB,MAAM,sBAAsB,MAAsC;AAChE,QACE,iBAAiB,EAAE,IACnB,OAAO,MAAM,YACZ,OAAO,MAAM,YAAY,OAAO,SAAS,EAAE;;AAIhD,OAAO,MAAM,aAAa,MAA6B;AACrD,KAAI,mBAAmB,EAAE,EAAE;AACzB,SAAO;;AAGT,KAAI,MAAM,QAAQ,EAAE,EAAE;AACpB,SAAO,EAAE,MAAM,UAAU;;AAG3B,KAAI,OAAO,MAAM,UAAU;AACzB,SAAO,MAAM,QAAQ,OAAO,OAAO,EAAE,CAAC,MAAM,UAAU;;AAGxD,QAAO;;AAGT,MAAM,aAAa,MAAc;AAC/B,KAAI,EAAE,WAAW,KAAK,EAAE;;AAEtB,SAAO;;AAET,QACE,EAEG,QAAQ,aAAa,OAAO,OAAO,IAAI,GAAG,aAAa,GAAG,CAE1D,QAAQ,QAAQ,OAAO;;;;AAM9B,eAAe,SAAS,MAAM,GAAoB;AAChD,KAAI,MAAM,QAAQ,EAAE,EAAE;AACpB,SAAO,EAAE,IAAI,MAAM,CAAC,KAAK,KAAK;;AAGhC,KAAI,mBAAmB,EAAE,EAAE;AACzB,SAAO,EAAE,SAAS,CAAC,UAAU;;AAG/B,QAAO,OAAO,QAAQ,EAAE,CACrB,QACE,GAAG,WAEF,OAAO,UAAU,YAAY,MAChC,CACA,KAAK,CAAC,KAAK,WAAW;AACrB,MAAI,CAAC,mBAAmB,MAAM,EAAE;AAC9B,UAAO,GAAG,IAAI,KAAK,MAAM,MAAM,CAAC;;AAGlC,SAAO,GAAG,UAAU,IAAI,CAAC,IACvB,OAAO,UAAU,YACjB,UAAU,KAEV,EACE,IAAI,QACF,mCACC,OAAO,IAAI,IAAI,OAAO,GAAG,GAAG,aAAa,GAAG,KAC9C,IAAI,YAEH,GAAG,MAAM,MACT,MACL;GACD,CACD,KAAK,IAAI","names":[],"sources":["../../src/utils/toCSS.ts"],"version":3,"sourcesContent":["import { isBoxedPrimitive } from '@wyw-in-js/shared';\n\nimport type { CSSPropertyValue, CSSable } from '../types';\n\nimport { unitless } from './units';\n\nconst isCSSPropertyValue = (o: unknown): o is CSSPropertyValue => {\n return (\n isBoxedPrimitive(o) ||\n typeof o === 'string' ||\n (typeof o === 'number' && Number.isFinite(o))\n );\n};\n\nexport const isCSSable = (o: unknown): o is CSSable => {\n if (isCSSPropertyValue(o)) {\n return true;\n }\n\n if (Array.isArray(o)) {\n return o.every(isCSSable);\n }\n\n if (typeof o === 'object') {\n return o !== null && Object.values(o).every(isCSSable);\n }\n\n return false;\n};\n\nconst hyphenate = (s: string) => {\n if (s.startsWith('--')) {\n // It's a custom property which is already well formatted.\n return s;\n }\n return (\n s\n // Hyphenate CSS property names from camelCase version from JS string\n .replace(/([A-Z])/g, (match, p1) => `-${p1.toLowerCase()}`)\n // Special case for `-ms` because in JS it starts with `ms` unlike `Webkit`\n .replace(/^ms-/, '-ms-')\n );\n};\n\n// Some tools such as polished.js output JS objects\n// To support them transparently, we convert JS objects to CSS strings\nexport default function toCSS(o: CSSable): string {\n if (Array.isArray(o)) {\n return o.map(toCSS).join('\\n');\n }\n\n if (isCSSPropertyValue(o)) {\n return o.valueOf().toString();\n }\n\n return Object.entries(o)\n .filter(\n ([, value]) =>\n // Ignore all falsy values except numbers\n typeof value === 'number' || value\n )\n .map(([key, value]) => {\n if (!isCSSPropertyValue(value)) {\n return `${key} { ${toCSS(value)} }`;\n }\n\n return `${hyphenate(key)}: ${\n typeof value === 'number' &&\n value !== 0 &&\n // Strip vendor prefixes when checking if the value is unitless\n !(\n key.replace(\n /^(Webkit|Moz|O|ms)([A-Z])(.+)$/,\n (match, p1, p2, p3) => `${p2.toLowerCase()}${p3}`\n ) in unitless\n )\n ? `${value}px`\n : value\n };`;\n })\n .join(' ');\n}\n"],"file":"toCSS.js"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export function toValidCSSIdentifier(s) {
|
|
2
|
-
|
|
2
|
+
return s.replace(/[^-_a-z0-9\u00A0-\uFFFF]/gi, "_").replace(/^\d/, "_");
|
|
3
3
|
}
|
|
4
|
-
//# sourceMappingURL=toValidCSSIdentifier.js.map
|
|
4
|
+
//# sourceMappingURL=toValidCSSIdentifier.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,OAAO,SAAS,qBAAqB,GAAW;AAC9C,QAAO,EAAE,QAAQ,8BAA8B,IAAI,CAAC,QAAQ,OAAO,IAAI","names":[],"sources":["../../src/utils/toValidCSSIdentifier.ts"],"version":3,"sourcesContent":["export function toValidCSSIdentifier(s: string) {\n return s.replace(/[^-_a-z0-9\\u00A0-\\uFFFF]/gi, '_').replace(/^\\d/, '_');\n}\n"],"file":"toValidCSSIdentifier.js"}
|
package/esm/utils/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=types.js.map
|
|
2
|
+
//# sourceMappingURL=types.js.map
|
package/esm/utils/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"","names":[],"sources":["../../src/utils/types.ts"],"version":3,"sourcesContent":["import type { ClassNameFn, VariableNameFn } from '@wyw-in-js/shared';\n\nexport interface IOptions {\n classNameSlug?: string | ClassNameFn;\n displayName: boolean;\n extensions?: string[];\n variableNameConfig?: 'var' | 'dashes' | 'raw';\n variableNameSlug?: string | VariableNameFn;\n}\n\nexport type IFileContext = {\n filename?: string | null;\n root?: string | null;\n};\n"],"file":"types.js"}
|