@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/types/ast.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const stringLiteralCode = (value, quote = 'double') => {
|
|
2
|
+
const json = JSON.stringify(value);
|
|
3
|
+
if (quote === 'double') {
|
|
4
|
+
return json;
|
|
5
|
+
}
|
|
6
|
+
return `'${json.slice(1, -1).replace(/'/g, "\\'")}'`;
|
|
7
|
+
};
|
|
8
|
+
const indent = (level) => ' '.repeat(level);
|
|
9
|
+
const expressionToCodeWithContext = (expression, context) => {
|
|
10
|
+
if (expression.type === 'Identifier') {
|
|
11
|
+
return expression.name;
|
|
12
|
+
}
|
|
13
|
+
if (expression.type === 'StringLiteral') {
|
|
14
|
+
return stringLiteralCode(expression.value, context.quote);
|
|
15
|
+
}
|
|
16
|
+
if (expression.type === 'NumericLiteral' ||
|
|
17
|
+
expression.type === 'BooleanLiteral') {
|
|
18
|
+
return String(expression.value);
|
|
19
|
+
}
|
|
20
|
+
if (expression.type === 'NullLiteral') {
|
|
21
|
+
return 'null';
|
|
22
|
+
}
|
|
23
|
+
if (expression.type === 'MemberExpression') {
|
|
24
|
+
const memberExpression = expression;
|
|
25
|
+
const object = expressionToCodeWithContext(memberExpression.object, context);
|
|
26
|
+
const property = expressionToCodeWithContext(memberExpression.property, context);
|
|
27
|
+
return memberExpression.computed
|
|
28
|
+
? `${object}[${property}]`
|
|
29
|
+
: `${object}.${property}`;
|
|
30
|
+
}
|
|
31
|
+
if (expression.type === 'CallExpression') {
|
|
32
|
+
const callExpression = expression;
|
|
33
|
+
const callee = expressionToCodeWithContext(callExpression.callee, context);
|
|
34
|
+
const args = callExpression.arguments.map((arg) => expressionToCodeWithContext(arg, {
|
|
35
|
+
...context,
|
|
36
|
+
quote: arg.type === 'StringLiteral' ? 'single' : context.quote,
|
|
37
|
+
}));
|
|
38
|
+
return `${callee}(${args.join(', ')})`;
|
|
39
|
+
}
|
|
40
|
+
if (expression.type === 'ArrowFunctionExpression') {
|
|
41
|
+
const arrow = expression;
|
|
42
|
+
return `(${arrow.params
|
|
43
|
+
.map((param) => expressionToCodeWithContext(param, context))
|
|
44
|
+
.join(', ')}) => ${expressionToCodeWithContext(arrow.body, context)}`;
|
|
45
|
+
}
|
|
46
|
+
if (expression.type === 'ArrayExpression') {
|
|
47
|
+
return `[${expression.elements
|
|
48
|
+
.map((item) => (item ? expressionToCodeWithContext(item, context) : ''))
|
|
49
|
+
.join(', ')}]`;
|
|
50
|
+
}
|
|
51
|
+
if (expression.type === 'BlockStatement') {
|
|
52
|
+
return '{ }';
|
|
53
|
+
}
|
|
54
|
+
if (expression.type === 'ObjectExpression') {
|
|
55
|
+
const objectExpression = expression;
|
|
56
|
+
if (objectExpression.properties.length === 0) {
|
|
57
|
+
return '{}';
|
|
58
|
+
}
|
|
59
|
+
const nextIndent = context.indent + 1;
|
|
60
|
+
const properties = objectExpression.properties
|
|
61
|
+
.map((property) => {
|
|
62
|
+
const key = expressionToCodeWithContext(property.key, {
|
|
63
|
+
...context,
|
|
64
|
+
quote: 'double',
|
|
65
|
+
});
|
|
66
|
+
const value = expressionToCodeWithContext(property.value, {
|
|
67
|
+
indent: nextIndent,
|
|
68
|
+
quote: 'double',
|
|
69
|
+
});
|
|
70
|
+
return `${indent(nextIndent)}${key}: ${value}`;
|
|
71
|
+
})
|
|
72
|
+
.join(',\n');
|
|
73
|
+
return `{\n${properties}\n${indent(context.indent)}}`;
|
|
74
|
+
}
|
|
75
|
+
return expression.type;
|
|
76
|
+
};
|
|
77
|
+
export const expressionToCode = (expression) => expressionToCodeWithContext(expression, {
|
|
78
|
+
indent: 0,
|
|
79
|
+
quote: 'double',
|
|
80
|
+
});
|
package/types/diagnostics.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.PROCESSOR_DIAGNOSTIC_ARTIFACT = 'wyw-in-js:diagnostic';
|
|
5
|
-
const createProcessorDiagnosticArtifact = (diagnostic) => [exports.PROCESSOR_DIAGNOSTIC_ARTIFACT, diagnostic];
|
|
6
|
-
exports.createProcessorDiagnosticArtifact = createProcessorDiagnosticArtifact;
|
|
7
|
-
const isProcessorDiagnosticArtifact = (artifact) => artifact[0] === exports.PROCESSOR_DIAGNOSTIC_ARTIFACT;
|
|
8
|
-
exports.isProcessorDiagnosticArtifact = isProcessorDiagnosticArtifact;
|
|
1
|
+
export const PROCESSOR_DIAGNOSTIC_ARTIFACT = 'wyw-in-js:diagnostic';
|
|
2
|
+
export const createProcessorDiagnosticArtifact = (diagnostic) => [PROCESSOR_DIAGNOSTIC_ARTIFACT, diagnostic];
|
|
3
|
+
export const isProcessorDiagnosticArtifact = (artifact) => artifact[0] === PROCESSOR_DIAGNOSTIC_ARTIFACT;
|
package/types/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export { BaseProcessor } from './BaseProcessor';
|
|
2
|
+
export { expressionToCode } from './ast';
|
|
2
3
|
export { createProcessorDiagnosticArtifact, isProcessorDiagnosticArtifact, PROCESSOR_DIAGNOSTIC_ARTIFACT, } from './diagnostics';
|
|
3
4
|
export type { ProcessorDiagnosticArtifact } from './diagnostics';
|
|
4
|
-
export type { Expression,
|
|
5
|
+
export type { ArrayExpression, ArrowFunctionExpression, AstService, BaseAstNode, BlockStatement, BooleanLiteral, CallExpression, Expression, Identifier, MemberExpression, NullLiteral, NumericLiteral, ObjectExpression, ObjectProperty, SourceLocation, StringLiteral, TemplateElement, } from './ast';
|
|
6
|
+
export type { ProcessorParams, TagSource, TailProcessorParams, } from './BaseProcessor';
|
|
7
|
+
export type { ProcessorStaticClassNameValue, ProcessorStaticContext, ProcessorStaticDebugReason, ProcessorStaticDependency, ProcessorStaticInterpolationResolver, ProcessorStaticMetadata, ProcessorStaticOpaqueComponentValue, ProcessorStaticRuntimeCallbackValue, ProcessorStaticSelectorChainValue, ProcessorStaticSerializableValue, ProcessorStaticTagTargetResolver, ProcessorStaticUnresolvedValue, ProcessorStaticValue, } from './static';
|
|
5
8
|
export * from './types';
|
|
6
9
|
export { buildSlug } from './utils/buildSlug';
|
|
7
10
|
export type { IOptions, IFileContext } from './utils/types';
|
package/types/index.js
CHANGED
|
@@ -1,33 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.toValidCSSIdentifier = exports.TaggedTemplateProcessor = exports.validateParams = exports.isValidParams = exports.buildSlug = exports.PROCESSOR_DIAGNOSTIC_ARTIFACT = exports.isProcessorDiagnosticArtifact = exports.createProcessorDiagnosticArtifact = exports.BaseProcessor = void 0;
|
|
18
|
-
var BaseProcessor_1 = require("./BaseProcessor");
|
|
19
|
-
Object.defineProperty(exports, "BaseProcessor", { enumerable: true, get: function () { return BaseProcessor_1.BaseProcessor; } });
|
|
20
|
-
var diagnostics_1 = require("./diagnostics");
|
|
21
|
-
Object.defineProperty(exports, "createProcessorDiagnosticArtifact", { enumerable: true, get: function () { return diagnostics_1.createProcessorDiagnosticArtifact; } });
|
|
22
|
-
Object.defineProperty(exports, "isProcessorDiagnosticArtifact", { enumerable: true, get: function () { return diagnostics_1.isProcessorDiagnosticArtifact; } });
|
|
23
|
-
Object.defineProperty(exports, "PROCESSOR_DIAGNOSTIC_ARTIFACT", { enumerable: true, get: function () { return diagnostics_1.PROCESSOR_DIAGNOSTIC_ARTIFACT; } });
|
|
24
|
-
__exportStar(require("./types"), exports);
|
|
25
|
-
var buildSlug_1 = require("./utils/buildSlug");
|
|
26
|
-
Object.defineProperty(exports, "buildSlug", { enumerable: true, get: function () { return buildSlug_1.buildSlug; } });
|
|
27
|
-
var validateParams_1 = require("./utils/validateParams");
|
|
28
|
-
Object.defineProperty(exports, "isValidParams", { enumerable: true, get: function () { return validateParams_1.isValidParams; } });
|
|
29
|
-
Object.defineProperty(exports, "validateParams", { enumerable: true, get: function () { return validateParams_1.validateParams; } });
|
|
30
|
-
var TaggedTemplateProcessor_1 = require("./TaggedTemplateProcessor");
|
|
31
|
-
Object.defineProperty(exports, "TaggedTemplateProcessor", { enumerable: true, get: function () { return TaggedTemplateProcessor_1.TaggedTemplateProcessor; } });
|
|
32
|
-
var toValidCSSIdentifier_1 = require("./utils/toValidCSSIdentifier");
|
|
33
|
-
Object.defineProperty(exports, "toValidCSSIdentifier", { enumerable: true, get: function () { return toValidCSSIdentifier_1.toValidCSSIdentifier; } });
|
|
1
|
+
export { BaseProcessor } from './BaseProcessor';
|
|
2
|
+
export { expressionToCode } from './ast';
|
|
3
|
+
export { createProcessorDiagnosticArtifact, isProcessorDiagnosticArtifact, PROCESSOR_DIAGNOSTIC_ARTIFACT, } from './diagnostics';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export { buildSlug } from './utils/buildSlug';
|
|
6
|
+
export { isValidParams, validateParams } from './utils/validateParams';
|
|
7
|
+
export { TaggedTemplateProcessor } from './TaggedTemplateProcessor';
|
|
8
|
+
export { toValidCSSIdentifier } from './utils/toValidCSSIdentifier';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { SourceLocation } from './ast';
|
|
2
|
+
import type { IFileContext, IOptions } from './utils/types';
|
|
3
|
+
import type { IInterpolation } from './types';
|
|
4
|
+
import type { TagSource } from './BaseProcessor';
|
|
5
|
+
export type ProcessorStaticSerializableValue = {
|
|
6
|
+
kind: 'serializable';
|
|
7
|
+
value: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type ProcessorStaticClassNameValue = {
|
|
10
|
+
className: string;
|
|
11
|
+
kind: 'class-name';
|
|
12
|
+
value?: unknown;
|
|
13
|
+
};
|
|
14
|
+
export type ProcessorStaticSelectorChainValue = {
|
|
15
|
+
className: string;
|
|
16
|
+
kind: 'selector-chain';
|
|
17
|
+
selectors: string[];
|
|
18
|
+
value?: unknown;
|
|
19
|
+
};
|
|
20
|
+
export type ProcessorStaticRuntimeCallbackValue = {
|
|
21
|
+
kind: 'runtime-callback';
|
|
22
|
+
source?: string;
|
|
23
|
+
value?: unknown;
|
|
24
|
+
};
|
|
25
|
+
export type ProcessorStaticOpaqueComponentValue = {
|
|
26
|
+
className?: string;
|
|
27
|
+
kind: 'opaque-component';
|
|
28
|
+
value?: unknown;
|
|
29
|
+
};
|
|
30
|
+
export type ProcessorStaticUnresolvedValue = {
|
|
31
|
+
details?: Readonly<Record<string, unknown>>;
|
|
32
|
+
kind: 'unresolved';
|
|
33
|
+
reason: string;
|
|
34
|
+
};
|
|
35
|
+
export type ProcessorStaticValue = ProcessorStaticClassNameValue | ProcessorStaticOpaqueComponentValue | ProcessorStaticRuntimeCallbackValue | ProcessorStaticSelectorChainValue | ProcessorStaticSerializableValue | ProcessorStaticUnresolvedValue;
|
|
36
|
+
export type ProcessorStaticDependency = Readonly<{
|
|
37
|
+
imported?: string;
|
|
38
|
+
local?: string;
|
|
39
|
+
reason?: string;
|
|
40
|
+
source?: string;
|
|
41
|
+
}>;
|
|
42
|
+
export type ProcessorStaticDebugReason = Readonly<{
|
|
43
|
+
details?: Readonly<Record<string, unknown>>;
|
|
44
|
+
reason: string;
|
|
45
|
+
}>;
|
|
46
|
+
export type ProcessorStaticMetadata = Readonly<{
|
|
47
|
+
className: string;
|
|
48
|
+
displayName: string;
|
|
49
|
+
isReferenced: boolean;
|
|
50
|
+
location: SourceLocation | null;
|
|
51
|
+
slug: string;
|
|
52
|
+
tagSource: TagSource;
|
|
53
|
+
}>;
|
|
54
|
+
export type ProcessorStaticContext = Readonly<{
|
|
55
|
+
fileContext: Readonly<IFileContext>;
|
|
56
|
+
metadata: ProcessorStaticMetadata;
|
|
57
|
+
options: Readonly<IOptions>;
|
|
58
|
+
addDependency(dependency: ProcessorStaticDependency): void;
|
|
59
|
+
debug(reason: ProcessorStaticDebugReason): void;
|
|
60
|
+
unresolved(reason: string, details?: Readonly<Record<string, unknown>>): ProcessorStaticUnresolvedValue;
|
|
61
|
+
}>;
|
|
62
|
+
export type ProcessorStaticInterpolationResolver = (interpolation: IInterpolation, value: ProcessorStaticValue, context: ProcessorStaticContext) => ProcessorStaticValue | null | undefined;
|
|
63
|
+
export type ProcessorStaticTagTargetResolver = (target: ProcessorStaticValue, context: ProcessorStaticContext) => ProcessorStaticValue | null | undefined;
|
package/types/static.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Expression, Identifier, TemplateElement, MemberExpression } from '@babel/types';
|
|
2
1
|
import type { ExpressionValue, Location, WYWEvalMeta } from '@wyw-in-js/shared';
|
|
2
|
+
import type { Expression, Identifier, MemberExpression, TemplateElement } from './ast';
|
|
3
3
|
export type CSSPropertyValue = string | number;
|
|
4
4
|
export type ObjectWithSelectors = {
|
|
5
5
|
[key: string]: ObjectWithSelectors | CSSPropertyValue | (ObjectWithSelectors | CSSPropertyValue)[];
|
package/types/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/types/utils/buildSlug.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildSlug = buildSlug;
|
|
4
1
|
const PLACEHOLDER = /\[(.*?)]/g;
|
|
5
2
|
const isValidArgName = (key, args) => key in args;
|
|
6
|
-
function buildSlug(pattern, args) {
|
|
3
|
+
export function buildSlug(pattern, args) {
|
|
7
4
|
return pattern.replace(PLACEHOLDER, (_, name) => isValidArgName(name, args) ? args[name].toString() : '');
|
|
8
5
|
}
|
|
@@ -1,44 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const buildSlug_1 = require("./buildSlug");
|
|
7
|
-
const toValidCSSIdentifier_1 = require("./toValidCSSIdentifier");
|
|
8
|
-
function getClassNameAndSlug(displayName, idx, options, context) {
|
|
1
|
+
import { basename, dirname, extname, relative, sep, posix } from 'path';
|
|
2
|
+
import { logger, slugify } from '@wyw-in-js/shared';
|
|
3
|
+
import { buildSlug } from './buildSlug';
|
|
4
|
+
import { toValidCSSIdentifier } from './toValidCSSIdentifier';
|
|
5
|
+
export default function getClassNameAndSlug(displayName, idx, options, context) {
|
|
9
6
|
const relativeFilename = (context.root && context.filename
|
|
10
|
-
?
|
|
11
|
-
: context.filename ?? 'unknown').replace(/\\/g,
|
|
7
|
+
? relative(context.root, context.filename)
|
|
8
|
+
: context.filename ?? 'unknown').replace(/\\/g, posix.sep);
|
|
12
9
|
// Custom properties need to start with a letter, so we prefix the slug
|
|
13
10
|
// Also use append the index of the class to the filename for uniqueness in the file
|
|
14
|
-
const slug =
|
|
11
|
+
const slug = toValidCSSIdentifier(`${displayName.charAt(0).toLowerCase()}${slugify(`${relativeFilename}:${idx}`)}`);
|
|
15
12
|
// Collect some useful replacement patterns from the filename
|
|
16
13
|
// Available variables for the square brackets used in `classNameSlug` options
|
|
17
|
-
const ext =
|
|
14
|
+
const ext = extname(relativeFilename);
|
|
18
15
|
const slugVars = {
|
|
19
16
|
hash: slug,
|
|
20
17
|
title: displayName,
|
|
21
18
|
index: idx,
|
|
22
19
|
file: relativeFilename,
|
|
23
20
|
ext,
|
|
24
|
-
name:
|
|
25
|
-
dir:
|
|
21
|
+
name: basename(relativeFilename, ext),
|
|
22
|
+
dir: dirname(relativeFilename).split(sep).pop(),
|
|
26
23
|
};
|
|
27
24
|
let className = options.displayName
|
|
28
|
-
? `${
|
|
25
|
+
? `${toValidCSSIdentifier(displayName)}_${slug}`
|
|
29
26
|
: slug;
|
|
30
27
|
// The className can be defined by the user either as fn or a string
|
|
31
28
|
if (typeof options.classNameSlug === 'function') {
|
|
32
29
|
try {
|
|
33
|
-
className =
|
|
30
|
+
className = toValidCSSIdentifier(options.classNameSlug(slug, displayName, slugVars));
|
|
34
31
|
}
|
|
35
32
|
catch {
|
|
36
33
|
throw new Error('classNameSlug option must return a string');
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
if (typeof options.classNameSlug === 'string') {
|
|
40
|
-
className =
|
|
37
|
+
className = toValidCSSIdentifier(buildSlug(options.classNameSlug, slugVars));
|
|
41
38
|
}
|
|
42
|
-
|
|
39
|
+
logger.extend('template-parse:generated-meta')(`slug: ${slug}, displayName: ${displayName}, className: ${className}`);
|
|
43
40
|
return { className, slug };
|
|
44
41
|
}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getVariableName = getVariableName;
|
|
4
|
-
function getVariableName(varId, rawVariableName) {
|
|
1
|
+
export function getVariableName(varId, rawVariableName) {
|
|
5
2
|
switch (rawVariableName) {
|
|
6
3
|
case 'raw':
|
|
7
4
|
return varId;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = stripLines;
|
|
4
1
|
// Stripping away the new lines ensures that we preserve line numbers
|
|
5
2
|
// This is useful in case of tools such as the stylelint pre-processor
|
|
6
3
|
// This should be safe because strings cannot contain newline: https://www.w3.org/TR/CSS2/syndata.html#strings
|
|
7
|
-
function stripLines(loc, text) {
|
|
4
|
+
export default function stripLines(loc, text) {
|
|
8
5
|
let result = String(text)
|
|
9
6
|
.replace(/[\r\n]+/g, ' ')
|
|
10
7
|
.trim();
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* This file handles transforming template literals to class names or styled components and generates CSS content.
|
|
3
3
|
* It uses CSS code from template literals and evaluated values of lazy dependencies stored in ValueCache.
|
|
4
4
|
*/
|
|
5
|
-
import type { TemplateElement } from '@babel/types';
|
|
6
5
|
import type { ExpressionValue, Replacements } from '@wyw-in-js/shared';
|
|
7
6
|
import type { TaggedTemplateProcessor } from '../TaggedTemplateProcessor';
|
|
7
|
+
import type { TemplateElement } from '../ast';
|
|
8
8
|
import type { ValueCache, Rules } from '../types';
|
|
9
9
|
import type { IOptions } from './types';
|
|
10
10
|
export default function templateProcessor(tagProcessor: TaggedTemplateProcessor, [...template]: (TemplateElement | ExpressionValue)[], valueCache: ValueCache, variableNameConfig: IOptions['variableNameConfig'] | undefined): [rules: Rules, sourceMapReplacements: Replacements] | null;
|
|
@@ -1,56 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/* eslint-disable no-continue */
|
|
3
2
|
/**
|
|
4
3
|
* This file handles transforming template literals to class names or styled components and generates CSS content.
|
|
5
4
|
* It uses CSS code from template literals and evaluated values of lazy dependencies stored in ValueCache.
|
|
6
5
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Object.defineProperty(o, k2, desc);
|
|
14
|
-
}) : (function(o, m, k, k2) {
|
|
15
|
-
if (k2 === undefined) k2 = k;
|
|
16
|
-
o[k2] = m[k];
|
|
17
|
-
}));
|
|
18
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
-
}) : function(o, v) {
|
|
21
|
-
o["default"] = v;
|
|
22
|
-
});
|
|
23
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
-
var ownKeys = function(o) {
|
|
25
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
-
var ar = [];
|
|
27
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
-
return ar;
|
|
29
|
-
};
|
|
30
|
-
return ownKeys(o);
|
|
31
|
-
};
|
|
32
|
-
return function (mod) {
|
|
33
|
-
if (mod && mod.__esModule) return mod;
|
|
34
|
-
var result = {};
|
|
35
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
-
__setModuleDefault(result, mod);
|
|
37
|
-
return result;
|
|
38
|
-
};
|
|
39
|
-
})();
|
|
40
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
-
};
|
|
43
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.default = templateProcessor;
|
|
45
|
-
const shared_1 = require("@wyw-in-js/shared");
|
|
46
|
-
const getVariableName_1 = require("./getVariableName");
|
|
47
|
-
const stripLines_1 = __importDefault(require("./stripLines"));
|
|
48
|
-
const throwIfInvalid_1 = __importDefault(require("./throwIfInvalid"));
|
|
49
|
-
const toCSS_1 = __importStar(require("./toCSS"));
|
|
50
|
-
const units_1 = require("./units");
|
|
6
|
+
import { hasEvalMeta, ValueType } from '@wyw-in-js/shared';
|
|
7
|
+
import { getVariableName } from './getVariableName';
|
|
8
|
+
import stripLines from './stripLines';
|
|
9
|
+
import throwIfInvalid from './throwIfInvalid';
|
|
10
|
+
import toCSS, { isCSSable } from './toCSS';
|
|
11
|
+
import { units } from './units';
|
|
51
12
|
// Match any valid CSS unit not immediately followed by an alphanumeric character or underscore.
|
|
52
|
-
const unitRegex = new RegExp(`^(?:${
|
|
53
|
-
function templateProcessor(tagProcessor, [...template], valueCache, variableNameConfig) {
|
|
13
|
+
const unitRegex = new RegExp(`^(?:${units.join('|')})(?![a-zA-Z0-9_])`);
|
|
14
|
+
export default function templateProcessor(tagProcessor, [...template], valueCache, variableNameConfig) {
|
|
54
15
|
const sourceMapReplacements = [];
|
|
55
16
|
// Check if the variable is referenced anywhere for basic DCE
|
|
56
17
|
// Only works when it's assigned to a variable
|
|
@@ -79,7 +40,7 @@ function templateProcessor(tagProcessor, [...template], valueCache, variableName
|
|
|
79
40
|
};
|
|
80
41
|
const value = 'value' in item ? item.value : valueCache.get(item.ex.name);
|
|
81
42
|
// Is it props based interpolation?
|
|
82
|
-
if (item.kind ===
|
|
43
|
+
if (item.kind === ValueType.FUNCTION || typeof value === 'function') {
|
|
83
44
|
// Check if previous expression was a CSS variable that we replaced
|
|
84
45
|
// If it has a unit after it, we need to move the unit into the interpolation
|
|
85
46
|
// e.g. `var(--size)px` should actually be `var(--size)`
|
|
@@ -92,12 +53,12 @@ function templateProcessor(tagProcessor, [...template], valueCache, variableName
|
|
|
92
53
|
template.shift();
|
|
93
54
|
const [unit] = matches;
|
|
94
55
|
const varId = tagProcessor.addInterpolation(item.ex, cssText, item.source, unit);
|
|
95
|
-
cssText +=
|
|
56
|
+
cssText += getVariableName(varId, variableNameConfig);
|
|
96
57
|
cssText += next.value.cooked?.substring(unit?.length ?? 0) ?? '';
|
|
97
58
|
}
|
|
98
59
|
else {
|
|
99
60
|
const varId = tagProcessor.addInterpolation(item.ex, cssText, item.source);
|
|
100
|
-
cssText +=
|
|
61
|
+
cssText += getVariableName(varId, variableNameConfig);
|
|
101
62
|
}
|
|
102
63
|
}
|
|
103
64
|
catch (e) {
|
|
@@ -108,24 +69,24 @@ function templateProcessor(tagProcessor, [...template], valueCache, variableName
|
|
|
108
69
|
}
|
|
109
70
|
}
|
|
110
71
|
else {
|
|
111
|
-
(
|
|
72
|
+
throwIfInvalid(tagProcessor.isValidValue.bind(tagProcessor), value, item, item.source);
|
|
112
73
|
if (value !== undefined && typeof value !== 'function') {
|
|
113
74
|
// Skip the blank string instead of throw ing an error
|
|
114
75
|
if (value === '') {
|
|
115
76
|
continue;
|
|
116
77
|
}
|
|
117
|
-
if (
|
|
78
|
+
if (hasEvalMeta(value)) {
|
|
118
79
|
// If it's a React component wrapped in styled, get the class name
|
|
119
80
|
// Useful for interpolating components
|
|
120
81
|
cssText += `.${value.__wyw_meta.className}`;
|
|
121
82
|
}
|
|
122
|
-
else if (
|
|
83
|
+
else if (isCSSable(value)) {
|
|
123
84
|
// If it's a plain object or an array, convert it to a CSS string
|
|
124
|
-
cssText += (
|
|
85
|
+
cssText += stripLines(loc, toCSS(value));
|
|
125
86
|
}
|
|
126
87
|
else {
|
|
127
88
|
// For anything else, assume it'll be stringified
|
|
128
|
-
cssText += (
|
|
89
|
+
cssText += stripLines(loc, value);
|
|
129
90
|
}
|
|
130
91
|
sourceMapReplacements.push({
|
|
131
92
|
original: loc,
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
const isLikeError = (value) => typeof value === 'object' &&
|
|
4
2
|
value !== null &&
|
|
5
3
|
'stack' in value &&
|
|
@@ -29,4 +27,4 @@ function throwIfInvalid(checker, value, ex, source) {
|
|
|
29
27
|
const stringified = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
30
28
|
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})'.`);
|
|
31
29
|
}
|
|
32
|
-
|
|
30
|
+
export default throwIfInvalid;
|
package/types/utils/toCSS.js
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.isCSSable = void 0;
|
|
4
|
-
exports.default = toCSS;
|
|
5
|
-
const shared_1 = require("@wyw-in-js/shared");
|
|
6
|
-
const units_1 = require("./units");
|
|
1
|
+
import { isBoxedPrimitive } from '@wyw-in-js/shared';
|
|
2
|
+
import { unitless } from './units';
|
|
7
3
|
const isCSSPropertyValue = (o) => {
|
|
8
|
-
return (
|
|
4
|
+
return (isBoxedPrimitive(o) ||
|
|
9
5
|
typeof o === 'string' ||
|
|
10
6
|
(typeof o === 'number' && Number.isFinite(o)));
|
|
11
7
|
};
|
|
12
|
-
const isCSSable = (o) => {
|
|
8
|
+
export const isCSSable = (o) => {
|
|
13
9
|
if (isCSSPropertyValue(o)) {
|
|
14
10
|
return true;
|
|
15
11
|
}
|
|
16
12
|
if (Array.isArray(o)) {
|
|
17
|
-
return o.every(
|
|
13
|
+
return o.every(isCSSable);
|
|
18
14
|
}
|
|
19
15
|
if (typeof o === 'object') {
|
|
20
|
-
return o !== null && Object.values(o).every(
|
|
16
|
+
return o !== null && Object.values(o).every(isCSSable);
|
|
21
17
|
}
|
|
22
18
|
return false;
|
|
23
19
|
};
|
|
24
|
-
exports.isCSSable = isCSSable;
|
|
25
20
|
const hyphenate = (s) => {
|
|
26
21
|
if (s.startsWith('--')) {
|
|
27
22
|
// It's a custom property which is already well formatted.
|
|
@@ -35,7 +30,7 @@ const hyphenate = (s) => {
|
|
|
35
30
|
};
|
|
36
31
|
// Some tools such as polished.js output JS objects
|
|
37
32
|
// To support them transparently, we convert JS objects to CSS strings
|
|
38
|
-
function toCSS(o) {
|
|
33
|
+
export default function toCSS(o) {
|
|
39
34
|
if (Array.isArray(o)) {
|
|
40
35
|
return o.map(toCSS).join('\n');
|
|
41
36
|
}
|
|
@@ -53,7 +48,7 @@ function toCSS(o) {
|
|
|
53
48
|
return `${hyphenate(key)}: ${typeof value === 'number' &&
|
|
54
49
|
value !== 0 &&
|
|
55
50
|
// Strip vendor prefixes when checking if the value is unitless
|
|
56
|
-
!(key.replace(/^(Webkit|Moz|O|ms)([A-Z])(.+)$/, (match, p1, p2, p3) => `${p2.toLowerCase()}${p3}`) in
|
|
51
|
+
!(key.replace(/^(Webkit|Moz|O|ms)([A-Z])(.+)$/, (match, p1, p2, p3) => `${p2.toLowerCase()}${p3}`) in unitless)
|
|
57
52
|
? `${value}px`
|
|
58
53
|
: value};`;
|
|
59
54
|
})
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toValidCSSIdentifier = toValidCSSIdentifier;
|
|
4
|
-
function toValidCSSIdentifier(s) {
|
|
1
|
+
export function toValidCSSIdentifier(s) {
|
|
5
2
|
return s.replace(/[^-_a-z0-9\u00A0-\uFFFF]/gi, '_').replace(/^\d/, '_');
|
|
6
3
|
}
|
package/types/utils/types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { TransformOptions } from '@babel/core';
|
|
2
1
|
import type { ClassNameFn, VariableNameFn } from '@wyw-in-js/shared';
|
|
3
2
|
export interface IOptions {
|
|
4
3
|
classNameSlug?: string | ClassNameFn;
|
|
@@ -7,4 +6,7 @@ export interface IOptions {
|
|
|
7
6
|
variableNameConfig?: 'var' | 'dashes' | 'raw';
|
|
8
7
|
variableNameSlug?: string | VariableNameFn;
|
|
9
8
|
}
|
|
10
|
-
export type IFileContext =
|
|
9
|
+
export type IFileContext = {
|
|
10
|
+
filename?: string | null;
|
|
11
|
+
root?: string | null;
|
|
12
|
+
};
|
package/types/utils/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/types/utils/units.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unitless = exports.units = void 0;
|
|
4
1
|
// https://www.w3.org/TR/css-values-4/
|
|
5
|
-
|
|
2
|
+
export const units = [
|
|
6
3
|
// font relative lengths
|
|
7
4
|
'em',
|
|
8
5
|
'ex',
|
|
@@ -48,7 +45,7 @@ exports.units = [
|
|
|
48
45
|
// percentages
|
|
49
46
|
'%',
|
|
50
47
|
];
|
|
51
|
-
|
|
48
|
+
export const unitless = {
|
|
52
49
|
animationIterationCount: true,
|
|
53
50
|
borderImageOutset: true,
|
|
54
51
|
borderImageSlice: true,
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isValidParams = isValidParams;
|
|
4
|
-
exports.validateParams = validateParams;
|
|
5
|
-
function isValidParams(params, constraints) {
|
|
1
|
+
export function isValidParams(params, constraints) {
|
|
6
2
|
const length = Math.max(params.length, constraints.length);
|
|
7
3
|
for (let i = 0; i < length; i++) {
|
|
8
4
|
if (params[i] === undefined || constraints[i] === undefined) {
|
|
@@ -28,7 +24,7 @@ function isValidParams(params, constraints) {
|
|
|
28
24
|
}
|
|
29
25
|
return true;
|
|
30
26
|
}
|
|
31
|
-
function validateParams(params, constraints, messageOrError) {
|
|
27
|
+
export function validateParams(params, constraints, messageOrError) {
|
|
32
28
|
if (!isValidParams(params, constraints)) {
|
|
33
29
|
if (typeof messageOrError === 'string') {
|
|
34
30
|
throw new Error(messageOrError);
|