@wyw-in-js/processor-utils 0.1.1 → 0.2.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/utils/templateProcessor.js +3 -8
- package/esm/utils/templateProcessor.js.map +1 -1
- package/esm/utils/validateParams.js +3 -0
- package/esm/utils/validateParams.js.map +1 -1
- package/lib/utils/templateProcessor.js +5 -10
- package/lib/utils/templateProcessor.js.map +1 -1
- package/package.json +10 -10
- package/types/utils/templateProcessor.js +2 -8
|
@@ -24,13 +24,11 @@ export default function templateProcessor(tagProcessor, [...template], valueCach
|
|
|
24
24
|
// Serialize the tagged template literal to a string
|
|
25
25
|
let cssText = '';
|
|
26
26
|
let item;
|
|
27
|
-
let lastTemplateElementLocation;
|
|
28
27
|
// eslint-disable-next-line no-cond-assign
|
|
29
28
|
while (item = template.shift()) {
|
|
30
29
|
if ('type' in item) {
|
|
31
30
|
// It's a template element
|
|
32
31
|
cssText += item.value.cooked;
|
|
33
|
-
lastTemplateElementLocation = item.loc;
|
|
34
32
|
continue;
|
|
35
33
|
}
|
|
36
34
|
|
|
@@ -39,18 +37,15 @@ export default function templateProcessor(tagProcessor, [...template], valueCach
|
|
|
39
37
|
ex
|
|
40
38
|
} = item;
|
|
41
39
|
const {
|
|
42
|
-
end
|
|
40
|
+
end,
|
|
41
|
+
start
|
|
43
42
|
} = ex.loc;
|
|
44
43
|
const beforeLength = cssText.length;
|
|
45
44
|
|
|
46
45
|
// The location will be end of the current string to start of next string
|
|
47
46
|
const next = template[0]; // template[0] is the next template element
|
|
48
47
|
const loc = {
|
|
49
|
-
|
|
50
|
-
start: {
|
|
51
|
-
line: lastTemplateElementLocation.end.line,
|
|
52
|
-
column: lastTemplateElementLocation.end.column + 1
|
|
53
|
-
},
|
|
48
|
+
start,
|
|
54
49
|
end: next ? {
|
|
55
50
|
line: next.loc.start.line,
|
|
56
51
|
column: next.loc.start.column
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templateProcessor.js","names":["hasEvalMeta","ValueType","getVariableName","stripLines","throwIfInvalid","toCSS","isCSSable","units","unitRegex","RegExp","join","templateProcessor","tagProcessor","template","valueCache","variableNameConfig","sourceMapReplacements","isReferenced","cssText","item","
|
|
1
|
+
{"version":3,"file":"templateProcessor.js","names":["hasEvalMeta","ValueType","getVariableName","stripLines","throwIfInvalid","toCSS","isCSSable","units","unitRegex","RegExp","join","templateProcessor","tagProcessor","template","valueCache","variableNameConfig","sourceMapReplacements","isReferenced","cssText","item","shift","value","cooked","ex","end","start","loc","beforeLength","length","next","line","column","get","name","kind","FUNCTION","matches","match","unit","varId","addInterpolation","source","substring","e","Error","buildCodeFrameError","message","isValidValue","bind","undefined","__wyw_meta","className","push","original","rules","extractRules","location","includes"],"sources":["../../src/utils/templateProcessor.ts"],"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 { TemplateElement } from '@babel/types';\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 { 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 units followed by a separator such as ;, newline etc.\nconst unitRegex = new RegExp(`^(?:${units.join('|')})\\\\b`);\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,\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,\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"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAKA,SAASA,WAAW,EAAEC,SAAS,QAAQ,mBAAmB;AAK1D,SAASC,eAAe,QAAQ,mBAAmB;AACnD,OAAOC,UAAU,MAAM,cAAc;AACrC,OAAOC,cAAc,MAAM,kBAAkB;AAC7C,OAAOC,KAAK,IAAIC,SAAS,QAAQ,SAAS;AAE1C,SAASC,KAAK,QAAQ,SAAS;;AAE/B;AACA,MAAMC,SAAS,GAAG,IAAIC,MAAM,CAAE,OAAMF,KAAK,CAACG,IAAI,CAAC,GAAG,CAAE,MAAK,CAAC;AAE1D,eAAe,SAASC,iBAAiBA,CACvCC,YAAqC,EACrC,CAAC,GAAGC,QAAQ,CAAwC,EACpDC,UAAsB,EACtBC,kBAA8D,EACF;EAC5D,MAAMC,qBAAmC,GAAG,EAAE;EAC9C;EACA;EACA,MAAM;IAAEC;EAAa,CAAC,GAAGL,YAAY;;EAErC;EACA,IAAIM,OAAO,GAAG,EAAE;EAEhB,IAAIC,IAAmD;EACvD;EACA,OAAQA,IAAI,GAAGN,QAAQ,CAACO,KAAK,CAAC,CAAC,EAAG;IAChC,IAAI,MAAM,IAAID,IAAI,EAAE;MAClB;MACAD,OAAO,IAAIC,IAAI,CAACE,KAAK,CAACC,MAAM;MAC5B;IACF;;IAEA;IACA,MAAM;MAAEC;IAAG,CAA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B,KAAK,GAAG1C,YAAY,CAAC2C,YAAY,CACrCzC,UAAU,EACVI,OAAO,EACPN,YAAY,CAAC4C,QACf,CAAC;;EAED;EACA,IAAI,CAACvC,YAAY,IAAI,CAACC,OAAO,CAACuC,QAAQ,CAAC,SAAS,CAAC,EAAE;IACjD,OAAO,IAAI;EACb;;EAEA;EACA,OAAO,CAACH,KAAK,EAAEtC,qBAAqB,CAAC;AACvC"}
|
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
// GetParamByName returns the Param type based on the input type T.
|
|
4
4
|
|
|
5
5
|
// If T is none of the above, return never.
|
|
6
|
+
|
|
6
7
|
// MapParams iteratively maps the input ParamConstraints to their corresponding Param types.
|
|
8
|
+
|
|
7
9
|
// If TNames is an empty tuple, return the result tuple.
|
|
10
|
+
|
|
8
11
|
export function isValidParams(params, constraints) {
|
|
9
12
|
const length = Math.max(params.length, constraints.length);
|
|
10
13
|
for (let i = 0; i < length; i++) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateParams.js","names":["isValidParams","params","constraints","length","Math","max","i","undefined","constraint","Array","isArray","every","c","validateParams","messageOrError","Error"],"sources":["../../src/utils/validateParams.ts"],"sourcesContent":["import type { Param, Params } from '../types';\n\ntype ParamName = Param[0];\ntype ParamConstraint = ParamName | [...ParamName[]] | '*';\n\nexport type ParamConstraints =\n | [...ParamConstraint[]]\n | [...ParamConstraint[], '...'];\n\n// ParamMapping maps each ParamName to its corresponding Param type.\ntype ParamMapping = {\n [K in ParamName]: Extract<Param, readonly [K, ...unknown[]]>; // For each ParamName K, extract the corresponding Param type.\n};\n\n// GetParamByName returns the Param type based on the input type T.\ntype GetParamByName<T> = T extends '*'\n ? Param // If T is '*', return Param type.\n : T extends keyof ParamMapping // If T is a key in ParamMapping (i.e., a ParamName).\n ? ParamMapping[T] // Return the corresponding Param type from ParamMapping.\n : T extends Array<infer TNames> // If T is an array of names.\n ? TNames extends ParamName // If TNames is a ParamName.\n ? Extract<Param, readonly [TNames, ...unknown[]]> // Return the corresponding Param type.\n : never // If TNames is not a ParamName, return never.\n : never; // If T is none of the above, return never.\n\n// MapParams iteratively maps the input ParamConstraints to their corresponding Param types.\nexport type MapParams<\n TNames extends ParamConstraints,\n TRes extends Param[] = [],\n> = TNames extends [infer THead, ...infer TTail] // If TNames is a non-empty tuple.\n ? THead extends '...' // If the first element in the tuple is '...'.\n ? [...TRes, ...Params] // Append all Params to the result tuple.\n : MapParams<\n Extract<TTail, ParamConstraints>, // Extract the remaining ParamConstraints.\n [...TRes, GetParamByName<Extract<THead, ParamName | '*' | ParamName[]>>] // Append the mapped Param to the result tuple and recurse.\n >\n : TRes; // If TNames is an empty tuple, return the result tuple.\n\nexport function isValidParams<T extends ParamConstraints>(\n params: Params,\n constraints: T\n): params is MapParams<T> {\n const length = Math.max(params.length, constraints.length);\n for (let i = 0; i < length; i++) {\n if (params[i] === undefined || constraints[i] === undefined) {\n return false;\n }\n\n const constraint = constraints[i];\n if (constraint === '...') {\n return true;\n }\n\n if (constraint === '*') {\n if (params[i] === undefined) {\n return false;\n }\n } else if (Array.isArray(constraint)) {\n if (constraint.every((c) => c !== params[i]?.[0])) {\n return false;\n }\n } else if (constraint !== params[i]?.[0]) {\n return false;\n }\n }\n\n return true;\n}\n\nexport function validateParams<T extends ParamConstraints>(\n params: Params,\n constraints: T,\n messageOrError: unknown\n): asserts params is MapParams<T> {\n if (!isValidParams(params, constraints)) {\n if (typeof messageOrError === 'string') {\n throw new Error(messageOrError);\n }\n\n throw messageOrError;\n }\n}\n"],"mappings":"AASA;;AAKA;;AASW
|
|
1
|
+
{"version":3,"file":"validateParams.js","names":["isValidParams","params","constraints","length","Math","max","i","undefined","constraint","Array","isArray","every","c","validateParams","messageOrError","Error"],"sources":["../../src/utils/validateParams.ts"],"sourcesContent":["import type { Param, Params } from '../types';\n\ntype ParamName = Param[0];\ntype ParamConstraint = ParamName | [...ParamName[]] | '*';\n\nexport type ParamConstraints =\n | [...ParamConstraint[]]\n | [...ParamConstraint[], '...'];\n\n// ParamMapping maps each ParamName to its corresponding Param type.\ntype ParamMapping = {\n [K in ParamName]: Extract<Param, readonly [K, ...unknown[]]>; // For each ParamName K, extract the corresponding Param type.\n};\n\n// GetParamByName returns the Param type based on the input type T.\ntype GetParamByName<T> = T extends '*'\n ? Param // If T is '*', return Param type.\n : T extends keyof ParamMapping // If T is a key in ParamMapping (i.e., a ParamName).\n ? ParamMapping[T] // Return the corresponding Param type from ParamMapping.\n : T extends Array<infer TNames> // If T is an array of names.\n ? TNames extends ParamName // If TNames is a ParamName.\n ? Extract<Param, readonly [TNames, ...unknown[]]> // Return the corresponding Param type.\n : never // If TNames is not a ParamName, return never.\n : never; // If T is none of the above, return never.\n\n// MapParams iteratively maps the input ParamConstraints to their corresponding Param types.\nexport type MapParams<\n TNames extends ParamConstraints,\n TRes extends Param[] = [],\n> = TNames extends [infer THead, ...infer TTail] // If TNames is a non-empty tuple.\n ? THead extends '...' // If the first element in the tuple is '...'.\n ? [...TRes, ...Params] // Append all Params to the result tuple.\n : MapParams<\n Extract<TTail, ParamConstraints>, // Extract the remaining ParamConstraints.\n [...TRes, GetParamByName<Extract<THead, ParamName | '*' | ParamName[]>>] // Append the mapped Param to the result tuple and recurse.\n >\n : TRes; // If TNames is an empty tuple, return the result tuple.\n\nexport function isValidParams<T extends ParamConstraints>(\n params: Params,\n constraints: T\n): params is MapParams<T> {\n const length = Math.max(params.length, constraints.length);\n for (let i = 0; i < length; i++) {\n if (params[i] === undefined || constraints[i] === undefined) {\n return false;\n }\n\n const constraint = constraints[i];\n if (constraint === '...') {\n return true;\n }\n\n if (constraint === '*') {\n if (params[i] === undefined) {\n return false;\n }\n } else if (Array.isArray(constraint)) {\n if (constraint.every((c) => c !== params[i]?.[0])) {\n return false;\n }\n } else if (constraint !== params[i]?.[0]) {\n return false;\n }\n }\n\n return true;\n}\n\nexport function validateParams<T extends ParamConstraints>(\n params: Params,\n constraints: T,\n messageOrError: unknown\n): asserts params is MapParams<T> {\n if (!isValidParams(params, constraints)) {\n if (typeof messageOrError === 'string') {\n throw new Error(messageOrError);\n }\n\n throw messageOrError;\n }\n}\n"],"mappings":"AASA;;AAKA;;AASW;;AAEX;;AAWU;;AAEV,OAAO,SAASA,aAAaA,CAC3BC,MAAc,EACdC,WAAc,EACU;EACxB,MAAMC,MAAM,GAAGC,IAAI,CAACC,GAAG,CAACJ,MAAM,CAACE,MAAM,EAAED,WAAW,CAACC,MAAM,CAAC;EAC1D,KAAK,IAAIG,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGH,MAAM,EAAEG,CAAC,EAAE,EAAE;IAC/B,IAAIL,MAAM,CAACK,CAAC,CAAC,KAAKC,SAAS,IAAIL,WAAW,CAACI,CAAC,CAAC,KAAKC,SAAS,EAAE;MAC3D,OAAO,KAAK;IACd;IAEA,MAAMC,UAAU,GAAGN,WAAW,CAACI,CAAC,CAAC;IACjC,IAAIE,UAAU,KAAK,KAAK,EAAE;MACxB,OAAO,IAAI;IACb;IAEA,IAAIA,UAAU,KAAK,GAAG,EAAE;MACtB,IAAIP,MAAM,CAACK,CAAC,CAAC,KAAKC,SAAS,EAAE;QAC3B,OAAO,KAAK;MACd;IACF,CAAC,MAAM,IAAIE,KAAK,CAACC,OAAO,CAACF,UAAU,CAAC,EAAE;MACpC,IAAIA,UAAU,CAACG,KAAK,CAAEC,CAAC,IAAKA,CAAC,KAAKX,MAAM,CAACK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;QACjD,OAAO,KAAK;MACd;IACF,CAAC,MAAM,IAAIE,UAAU,KAAKP,MAAM,CAACK,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE;MACxC,OAAO,KAAK;IACd;EACF;EAEA,OAAO,IAAI;AACb;AAEA,OAAO,SAASO,cAAcA,CAC5BZ,MAAc,EACdC,WAAc,EACdY,cAAuB,EACS;EAChC,IAAI,CAACd,aAAa,CAACC,MAAM,EAAEC,WAAW,CAAC,EAAE;IACvC,IAAI,OAAOY,cAAc,KAAK,QAAQ,EAAE;MACtC,MAAM,IAAIC,KAAK,CAACD,cAAc,CAAC;IACjC;IAEA,MAAMA,cAAc;EACtB;AACF"}
|
|
@@ -10,8 +10,8 @@ var _stripLines = _interopRequireDefault(require("./stripLines"));
|
|
|
10
10
|
var _throwIfInvalid = _interopRequireDefault(require("./throwIfInvalid"));
|
|
11
11
|
var _toCSS = _interopRequireWildcard(require("./toCSS"));
|
|
12
12
|
var _units = require("./units");
|
|
13
|
-
function _getRequireWildcardCache(
|
|
14
|
-
function _interopRequireWildcard(
|
|
13
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
14
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
15
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
16
|
/* eslint-disable no-continue */
|
|
17
17
|
/**
|
|
@@ -32,13 +32,11 @@ function templateProcessor(tagProcessor, [...template], valueCache, variableName
|
|
|
32
32
|
// Serialize the tagged template literal to a string
|
|
33
33
|
let cssText = '';
|
|
34
34
|
let item;
|
|
35
|
-
let lastTemplateElementLocation;
|
|
36
35
|
// eslint-disable-next-line no-cond-assign
|
|
37
36
|
while (item = template.shift()) {
|
|
38
37
|
if ('type' in item) {
|
|
39
38
|
// It's a template element
|
|
40
39
|
cssText += item.value.cooked;
|
|
41
|
-
lastTemplateElementLocation = item.loc;
|
|
42
40
|
continue;
|
|
43
41
|
}
|
|
44
42
|
|
|
@@ -47,18 +45,15 @@ function templateProcessor(tagProcessor, [...template], valueCache, variableName
|
|
|
47
45
|
ex
|
|
48
46
|
} = item;
|
|
49
47
|
const {
|
|
50
|
-
end
|
|
48
|
+
end,
|
|
49
|
+
start
|
|
51
50
|
} = ex.loc;
|
|
52
51
|
const beforeLength = cssText.length;
|
|
53
52
|
|
|
54
53
|
// The location will be end of the current string to start of next string
|
|
55
54
|
const next = template[0]; // template[0] is the next template element
|
|
56
55
|
const loc = {
|
|
57
|
-
|
|
58
|
-
start: {
|
|
59
|
-
line: lastTemplateElementLocation.end.line,
|
|
60
|
-
column: lastTemplateElementLocation.end.column + 1
|
|
61
|
-
},
|
|
56
|
+
start,
|
|
62
57
|
end: next ? {
|
|
63
58
|
line: next.loc.start.line,
|
|
64
59
|
column: next.loc.start.column
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templateProcessor.js","names":["_shared","require","_getVariableName","_stripLines","_interopRequireDefault","_throwIfInvalid","_toCSS","_interopRequireWildcard","_units","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","unitRegex","RegExp","units","join","templateProcessor","tagProcessor","template","valueCache","variableNameConfig","sourceMapReplacements","isReferenced","cssText","item","lastTemplateElementLocation","shift","value","cooked","loc","ex","end","beforeLength","length","next","start","line","column","name","kind","ValueType","FUNCTION","_next$value$cooked","matches","match","_next$value$cooked$su","_next$value$cooked2","_unit$length","unit","varId","addInterpolation","source","getVariableName","substring","e","Error","buildCodeFrameError","message","throwIfInvalid","isValidValue","bind","undefined","hasEvalMeta","__wyw_meta","className","isCSSable","stripLines","toCSS","push","original","rules","extractRules","location","includes"],"sources":["../../src/utils/templateProcessor.ts"],"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 { TemplateElement, SourceLocation } from '@babel/types';\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 { 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 units followed by a separator such as ;, newline etc.\nconst unitRegex = new RegExp(`^(?:${units.join('|')})\\\\b`);\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 let lastTemplateElementLocation: SourceLocation | null | 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 lastTemplateElementLocation = item.loc;\n continue;\n }\n\n // It's an expression\n const { ex } = item;\n\n const { end } = 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 // +1 because an expression location always shows 1 column before\n start: {\n line: lastTemplateElementLocation!.end.line,\n column: lastTemplateElementLocation!.end.column + 1,\n },\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,\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,\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"],"m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QAAQ,EAAExC,GAAG;UACbI,MAAM,EAAEV,OAAO,CAACU,MAAM,GAAGD;QAC3B,CAAC,CAAC;MACJ;IACF;EACF;EAEA,MAAMsC,KAAK,GAAGrD,YAAY,CAACsD,YAAY,CACrCpD,UAAU,EACVI,OAAO,EACPN,YAAY,CAACuD,QACf,CAAC;;EAED;EACA,IAAI,CAAClD,YAAY,IAAI,CAACC,OAAO,CAACkD,QAAQ,CAAC,SAAS,CAAC,EAAE;IACjD,OAAO,IAAI;EACb;;EAEA;EACA,OAAO,CAACH,KAAK,EAAEjD,qBAAqB,CAAC;AACvC"}
|
|
1
|
+
{"version":3,"file":"templateProcessor.js","names":["_shared","require","_getVariableName","_stripLines","_interopRequireDefault","_throwIfInvalid","_toCSS","_interopRequireWildcard","_units","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","obj","unitRegex","RegExp","units","join","templateProcessor","tagProcessor","template","valueCache","variableNameConfig","sourceMapReplacements","isReferenced","cssText","item","shift","value","cooked","ex","end","start","loc","beforeLength","length","next","line","column","name","kind","ValueType","FUNCTION","_next$value$cooked","matches","match","_next$value$cooked$su","_next$value$cooked2","_unit$length","unit","varId","addInterpolation","source","getVariableName","substring","Error","buildCodeFrameError","message","throwIfInvalid","isValidValue","bind","undefined","hasEvalMeta","__wyw_meta","className","isCSSable","stripLines","toCSS","push","original","rules","extractRules","location","includes"],"sources":["../../src/utils/templateProcessor.ts"],"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 { TemplateElement } from '@babel/types';\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 { 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 units followed by a separator such as ;, newline etc.\nconst unitRegex = new RegExp(`^(?:${units.join('|')})\\\\b`);\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,\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,\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"],"mappings":";;;;;;AASA,IAAAA,OAAA,GAAAC,OAAA;AAKA,IAAAC,gBAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,eAAA,GAAAD,sBAAA,CAAAH,OAAA;AACA,IAAAK,MAAA,GAAAC,uBAAA,CAAAN,OAAA;AAEA,IAAAO,MAAA,GAAAP,OAAA;AAAgC,SAAAQ,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAA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CAACwC,YAAY,CAACC,IAAI,CAACzC,YAAY,CAAC,EAC5CS,KAAK,EACLF,IAAI,EACJA,IAAI,CAAC0B,MACP,CAAC;MAED,IAAIxB,KAAK,KAAKiC,SAAS,IAAI,OAAOjC,KAAK,KAAK,UAAU,EAAE;QACtD;QACA,IAAIA,KAAK,KAAK,EAAE,EAAE;UAChB;QACF;QAEA,IAAI,IAAAkC,mBAAW,EAAClC,KAAK,CAAC,EAAE;UACtB;UACA;UACAH,OAAO,IAAK,IAAGG,KAAK,CAACmC,UAAU,CAACC,SAAU,EAAC;QAC7C,CAAC,MAAM,IAAI,IAAAC,gBAAS,EAACrC,KAAK,CAAC,EAAE;UAC3B;UACAH,OAAO,IAAI,IAAAyC,mBAAU,EAACjC,GAAG,EAAE,IAAAkC,cAAK,EAACvC,KAAK,CAAC,CAAC;QAC1C,CAAC,MAAM;UACL;UACAH,OAAO,IAAI,IAAAyC,mBAAU,EAACjC,GAAG,EAAEL,KAAK,CAAC;QACnC;QAEAL,qBAAqB,CAAC6C,IAAI,CAAC;UACzBC,QAAQ,EAAEpC,GAAG;UACbE,MAAM,EAAEV,OAAO,CAACU,MAAM,GAAGD;QAC3B,CAAC,CAAC;MACJ;IACF;EACF;EAEA,MAAMoC,KAAK,GAAGnD,YAAY,CAACoD,YAAY,CACrClD,UAAU,EACVI,OAAO,EACPN,YAAY,CAACqD,QACf,CAAC;;EAED;EACA,IAAI,CAAChD,YAAY,IAAI,CAACC,OAAO,CAACgD,QAAQ,CAAC,SAAS,CAAC,EAAE;IACjD,OAAO,IAAI;EACb;;EAEA;EACA,OAAO,CAACH,KAAK,EAAE/C,qBAAqB,CAAC;AACvC"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wyw-in-js/processor-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@babel/generator": "^7.23.
|
|
6
|
-
"@wyw-in-js/shared": "0.
|
|
5
|
+
"@babel/generator": "^7.23.5",
|
|
6
|
+
"@wyw-in-js/shared": "0.2.1"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@babel/types": "^7.23.
|
|
10
|
-
"@types/babel__core": "^7.20.
|
|
11
|
-
"@types/babel__generator": "^7.6.
|
|
9
|
+
"@babel/types": "^7.23.5",
|
|
10
|
+
"@types/babel__core": "^7.20.5",
|
|
11
|
+
"@types/babel__generator": "^7.6.7",
|
|
12
12
|
"@types/node": "^16.18.55",
|
|
13
13
|
"typescript": "^5.2.2",
|
|
14
|
-
"@wyw-in-js/babel-config": "0.
|
|
15
|
-
"@wyw-in-js/eslint-config": "0.
|
|
16
|
-
"@wyw-in-js/jest-preset": "0.
|
|
17
|
-
"@wyw-in-js/ts-config": "0.
|
|
14
|
+
"@wyw-in-js/babel-config": "0.2.1",
|
|
15
|
+
"@wyw-in-js/eslint-config": "0.2.1",
|
|
16
|
+
"@wyw-in-js/jest-preset": "0.2.1",
|
|
17
|
+
"@wyw-in-js/ts-config": "0.2.1"
|
|
18
18
|
},
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">=16.0.0"
|
|
@@ -47,27 +47,21 @@ function templateProcessor(tagProcessor, [...template], valueCache, variableName
|
|
|
47
47
|
// Serialize the tagged template literal to a string
|
|
48
48
|
let cssText = '';
|
|
49
49
|
let item;
|
|
50
|
-
let lastTemplateElementLocation;
|
|
51
50
|
// eslint-disable-next-line no-cond-assign
|
|
52
51
|
while ((item = template.shift())) {
|
|
53
52
|
if ('type' in item) {
|
|
54
53
|
// It's a template element
|
|
55
54
|
cssText += item.value.cooked;
|
|
56
|
-
lastTemplateElementLocation = item.loc;
|
|
57
55
|
continue;
|
|
58
56
|
}
|
|
59
57
|
// It's an expression
|
|
60
58
|
const { ex } = item;
|
|
61
|
-
const { end } = ex.loc;
|
|
59
|
+
const { end, start } = ex.loc;
|
|
62
60
|
const beforeLength = cssText.length;
|
|
63
61
|
// The location will be end of the current string to start of next string
|
|
64
62
|
const next = template[0]; // template[0] is the next template element
|
|
65
63
|
const loc = {
|
|
66
|
-
|
|
67
|
-
start: {
|
|
68
|
-
line: lastTemplateElementLocation.end.line,
|
|
69
|
-
column: lastTemplateElementLocation.end.column + 1,
|
|
70
|
-
},
|
|
64
|
+
start,
|
|
71
65
|
end: next
|
|
72
66
|
? { line: next.loc.start.line, column: next.loc.start.column }
|
|
73
67
|
: { line: end.line, column: end.column + 1 },
|