@wyw-in-js/processor-utils 1.1.0 → 2.0.0-alpha.0
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 +47 -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/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 +5 -12
- package/types/BaseProcessor.js +12 -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 +3 -1
- package/types/index.js +8 -33
- 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/BaseProcessor.js
CHANGED
|
@@ -1,78 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import { isCSSable } from './utils/toCSS';
|
|
8
|
-
import { validateParams } from './utils/validateParams';
|
|
1
|
+
import { hasEvalMeta } from "@wyw-in-js/shared";
|
|
2
|
+
import { expressionToCode } from "./ast.js";
|
|
3
|
+
import { createProcessorDiagnosticArtifact } from "./diagnostics.js";
|
|
4
|
+
import getClassNameAndSlug from "./utils/getClassNameAndSlug.js";
|
|
5
|
+
import { isCSSable } from "./utils/toCSS.js";
|
|
6
|
+
import { validateParams } from "./utils/validateParams.js";
|
|
9
7
|
export class BaseProcessor {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
isValidValue(value) {
|
|
52
|
-
return typeof value === 'function' || isCSSable(value) || hasEvalMeta(value);
|
|
53
|
-
}
|
|
54
|
-
toString() {
|
|
55
|
-
return this.tagSourceCode();
|
|
56
|
-
}
|
|
57
|
-
tagSourceCode() {
|
|
58
|
-
if (this.callee.type === 'Identifier') {
|
|
59
|
-
return this.callee.name;
|
|
60
|
-
}
|
|
61
|
-
return generator(this.callee).code;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Perform a replacement for the tag in evaluation time.
|
|
66
|
-
* For example, `css` tag will be replaced with its className,
|
|
67
|
-
* whereas `styled` tag will be replaced with an object with metadata.
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Perform a replacement for the tag with its runtime version.
|
|
72
|
-
* For example, `css` tag will be replaced with its className,
|
|
73
|
-
* whereas `styled` tag will be replaced with a component.
|
|
74
|
-
* If some parts require evaluated data for render,
|
|
75
|
-
* they will be replaced with placeholders.
|
|
76
|
-
*/
|
|
8
|
+
static SKIP = Symbol("skip");
|
|
9
|
+
artifacts = [];
|
|
10
|
+
className;
|
|
11
|
+
dependencies = [];
|
|
12
|
+
interpolations = [];
|
|
13
|
+
slug;
|
|
14
|
+
callee;
|
|
15
|
+
evaluated;
|
|
16
|
+
constructor(params, tagSource, astService, location, replacer, displayName, isReferenced, idx, options, context) {
|
|
17
|
+
this.tagSource = tagSource;
|
|
18
|
+
this.astService = astService;
|
|
19
|
+
this.location = location;
|
|
20
|
+
this.replacer = replacer;
|
|
21
|
+
this.displayName = displayName;
|
|
22
|
+
this.isReferenced = isReferenced;
|
|
23
|
+
this.idx = idx;
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.context = context;
|
|
26
|
+
validateParams(params, ["callee"], "Unknown error: a callee param is not specified");
|
|
27
|
+
const { className, slug } = getClassNameAndSlug(this.displayName, this.idx, this.options, this.context);
|
|
28
|
+
this.className = className;
|
|
29
|
+
this.slug = slug;
|
|
30
|
+
[[, this.callee]] = params;
|
|
31
|
+
}
|
|
32
|
+
addDiagnostic(diagnostic) {
|
|
33
|
+
this.artifacts.push(createProcessorDiagnosticArtifact({
|
|
34
|
+
...diagnostic,
|
|
35
|
+
end: diagnostic.end ?? this.location?.end ?? null,
|
|
36
|
+
start: diagnostic.start ?? this.location?.start ?? null
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
isValidValue(value) {
|
|
40
|
+
return typeof value === "function" || isCSSable(value) || hasEvalMeta(value);
|
|
41
|
+
}
|
|
42
|
+
toString() {
|
|
43
|
+
return this.tagSourceCode();
|
|
44
|
+
}
|
|
45
|
+
tagSourceCode() {
|
|
46
|
+
return expressionToCode(this.callee);
|
|
47
|
+
}
|
|
77
48
|
}
|
|
78
|
-
//# sourceMappingURL=BaseProcessor.js.map
|
|
49
|
+
//# sourceMappingURL=BaseProcessor.js.map
|
package/esm/BaseProcessor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAEA,SAAS,mBAAmB;AAS5B,SAAS,wBAAwB;AACjC,SAAS,yCAAyC;AAQlD,OAAO,yBAAyB;AAChC,SAAS,iBAAiB;AAE1B,SAAS,sBAAsB;AAc/B,OAAO,MAAe,cAAc;CAClC,OAAc,OAAO,OAAO,OAAO;CAEnC,AAAgB,YAAwB,EAAE;CAE1C,AAAgB;CAEhB,AAAgB,eAAkC,EAAE;CAEpD,AAAO,iBAAmC,EAAE;CAE5C,AAAgB;CAEhB,AAAU;CAEV,AAAU;CAIV,AAAO,YACL,QACA,AAAO,WACP,AAAmB,YACnB,AAAgB,UAChB,AAAmB,UAInB,AAAgB,aAChB,AAAgB,cAChB,AAAmB,KACnB,AAAmB,SACnB,AAAmB,SACnB;EAZO;EACY;EACH;EACG;EAIH;EACA;EACG;EACA;EACA;AAEnB,iBACE,QACA,CAAC,SAAS,EACV,iDACD;EAED,MAAM,EAAE,WAAW,SAAS,oBAC1B,KAAK,aACL,KAAK,KACL,KAAK,SACL,KAAK,QACN;AAED,OAAK,YAAY;AACjB,OAAK,OAAO;AAEZ,GAAC,GAAG,KAAK,WAAW;;CAetB,AAAO,cAAc,YAAuC;AAC1D,OAAK,UAAU,KACb,kCAAkC;GAChC,GAAG;GACH,KAAK,WAAW,OAAO,KAAK,UAAU,OAAO;GAC7C,OAAO,WAAW,SAAS,KAAK,UAAU,SAAS;GACpD,CAAC,CACH;;CAGH,AAAO,aAAa,OAAgC;AAClD,SACE,OAAO,UAAU,cAAc,UAAU,MAAM,IAAI,YAAY,MAAM;;CAIzE,AAAO,WAAmB;AACxB,SAAO,KAAK,eAAe;;CAG7B,AAAU,gBAAwB;AAChC,SAAO,iBAAiB,KAAK,OAAO","names":[],"sources":["../src/BaseProcessor.ts"],"version":3,"sourcesContent":["/* eslint-disable class-methods-use-this */\nimport type { Artifact, ExpressionValue } from '@wyw-in-js/shared';\nimport { hasEvalMeta } from '@wyw-in-js/shared';\n\nimport type {\n AstService,\n Expression,\n Identifier,\n MemberExpression,\n SourceLocation,\n} from './ast';\nimport { expressionToCode } from './ast';\nimport { createProcessorDiagnosticArtifact } from './diagnostics';\nimport type {\n IInterpolation,\n Params,\n ProcessorDiagnostic,\n Value,\n ValueCache,\n} from './types';\nimport getClassNameAndSlug from './utils/getClassNameAndSlug';\nimport { isCSSable } from './utils/toCSS';\nimport type { IFileContext, IOptions } from './utils/types';\nimport { validateParams } from './utils/validateParams';\n\nexport type { Expression };\n\nexport type ProcessorParams = ConstructorParameters<typeof BaseProcessor>;\nexport type TailProcessorParams = ProcessorParams extends [Params, ...infer T]\n ? T\n : never;\n\nexport type TagSource = {\n imported: string;\n source: string;\n};\n\nexport abstract class BaseProcessor {\n public static SKIP = Symbol('skip');\n\n public readonly artifacts: Artifact[] = [];\n\n public readonly className: string;\n\n public readonly dependencies: ExpressionValue[] = [];\n\n public interpolations: IInterpolation[] = [];\n\n public readonly slug: string;\n\n protected callee: Identifier | MemberExpression;\n\n protected evaluated:\n | Record<'dependencies' | 'expression', Value[]>\n | undefined;\n\n public constructor(\n params: Params,\n public tagSource: TagSource,\n protected readonly astService: AstService,\n public readonly location: SourceLocation | null,\n protected readonly replacer: (\n replacement: Expression | ((tagPath: unknown) => Expression),\n isPure: boolean\n ) => void,\n public readonly displayName: string,\n public readonly isReferenced: boolean,\n protected readonly idx: number,\n protected readonly options: IOptions,\n protected readonly context: IFileContext\n ) {\n validateParams(\n params,\n ['callee'],\n 'Unknown error: a callee param is not specified'\n );\n\n const { className, slug } = getClassNameAndSlug(\n this.displayName,\n this.idx,\n this.options,\n this.context\n );\n\n this.className = className;\n this.slug = slug;\n\n [[, this.callee]] = params;\n }\n\n /**\n * A replacement for tag referenced in a template literal.\n */\n public abstract get asSelector(): string;\n\n /**\n * A replacement for the tag in evaluation time.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with an object with metadata.\n */\n public abstract get value(): Expression;\n\n public addDiagnostic(diagnostic: ProcessorDiagnostic): void {\n this.artifacts.push(\n createProcessorDiagnosticArtifact({\n ...diagnostic,\n end: diagnostic.end ?? this.location?.end ?? null,\n start: diagnostic.start ?? this.location?.start ?? null,\n })\n );\n }\n\n public isValidValue(value: unknown): value is Value {\n return (\n typeof value === 'function' || isCSSable(value) || hasEvalMeta(value)\n );\n }\n\n public toString(): string {\n return this.tagSourceCode();\n }\n\n protected tagSourceCode(): string {\n return expressionToCode(this.callee);\n }\n\n public abstract build(values: ValueCache): void;\n\n /**\n * Perform a replacement for the tag in evaluation time.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with an object with metadata.\n */\n public abstract doEvaltimeReplacement(): void;\n\n /**\n * Perform a replacement for the tag with its runtime version.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with a component.\n * If some parts require evaluated data for render,\n * they will be replaced with placeholders.\n */\n public abstract doRuntimeReplacement(): void;\n}\n"],"file":"BaseProcessor.js"}
|
|
@@ -1,43 +1,34 @@
|
|
|
1
|
-
import { ValueType } from
|
|
2
|
-
import { BaseProcessor } from
|
|
3
|
-
import templateProcessor from
|
|
4
|
-
import { validateParams } from
|
|
1
|
+
import { ValueType } from "@wyw-in-js/shared";
|
|
2
|
+
import { BaseProcessor } from "./BaseProcessor.js";
|
|
3
|
+
import templateProcessor from "./utils/templateProcessor.js";
|
|
4
|
+
import { validateParams } from "./utils/validateParams.js";
|
|
5
5
|
export class TaggedTemplateProcessor extends BaseProcessor {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* It is called for each resolved expression in a template literal.
|
|
36
|
-
* @param node
|
|
37
|
-
* @param precedingCss
|
|
38
|
-
* @param source
|
|
39
|
-
* @param unit
|
|
40
|
-
* @return chunk of CSS that should be added to extracted CSS
|
|
41
|
-
*/
|
|
6
|
+
#template;
|
|
7
|
+
constructor(params, ...args) {
|
|
8
|
+
// Should have at least two params and the first one should be a callee.
|
|
9
|
+
validateParams(params, ["callee", "..."], TaggedTemplateProcessor.SKIP);
|
|
10
|
+
validateParams(params, ["callee", "template"], "Invalid usage of template tag");
|
|
11
|
+
const [tag, [, template]] = params;
|
|
12
|
+
super([tag], ...args);
|
|
13
|
+
template.forEach((element) => {
|
|
14
|
+
if ("kind" in element && element.kind !== ValueType.FUNCTION) {
|
|
15
|
+
this.dependencies.push(element);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
this.#template = template;
|
|
19
|
+
}
|
|
20
|
+
build(values) {
|
|
21
|
+
if (this.artifacts.length > 0) {
|
|
22
|
+
// FIXME: why it was called twice?
|
|
23
|
+
throw new Error("Tag is already built");
|
|
24
|
+
}
|
|
25
|
+
const artifact = templateProcessor(this, this.#template, values, this.options.variableNameConfig);
|
|
26
|
+
if (artifact) {
|
|
27
|
+
this.artifacts.push(["css", artifact]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
toString() {
|
|
31
|
+
return `${super.toString()}\`…\``;
|
|
32
|
+
}
|
|
42
33
|
}
|
|
43
|
-
//# sourceMappingURL=TaggedTemplateProcessor.js.map
|
|
34
|
+
//# sourceMappingURL=TaggedTemplateProcessor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AACA,SAAS,iBAAiB;AAI1B,SAAS,qBAAqB;AAE9B,OAAO,uBAAuB;AAC9B,SAAS,sBAAsB;AAE/B,OAAO,MAAe,gCAAgC,cAAc;CAClE,CAASA;CAET,AAAU,YAAY,QAAgB,GAAG,MAA2B;;AAElE,iBAAe,QAAQ,CAAC,UAAU,MAAM,EAAE,wBAAwB,KAAK;AAEvE,iBACE,QACA,CAAC,UAAU,WAAW,EACtB,gCACD;EACD,MAAM,CAAC,KAAK,GAAG,aAAa;AAE5B,QAAM,CAAC,IAAI,EAAE,GAAG,KAAK;AAErB,WAAS,SAAS,YAAY;AAC5B,OAAI,UAAU,WAAW,QAAQ,SAAS,UAAU,UAAU;AAC5D,SAAK,aAAa,KAAK,QAAQ;;IAEjC;AAEF,QAAKA,WAAY;;CAGnB,AAAgB,MAAM,QAAoB;AACxC,MAAI,KAAK,UAAU,SAAS,GAAG;;AAE7B,SAAM,IAAI,MAAM,uBAAuB;;EAGzC,MAAM,WAAW,kBACf,MACA,MAAKA,UACL,QACA,KAAK,QAAQ,mBACd;AACD,MAAI,UAAU;AACZ,QAAK,UAAU,KAAK,CAAC,OAAO,SAAS,CAAC;;;CAI1C,AAAgB,WAAmB;AACjC,SAAO,GAAG,MAAM,UAAU,CAAC","names":["#template"],"sources":["../src/TaggedTemplateProcessor.ts"],"version":3,"sourcesContent":["import type { ExpressionValue } from '@wyw-in-js/shared';\nimport { ValueType } from '@wyw-in-js/shared';\n\nimport type { Expression, SourceLocation, TemplateElement } from './ast';\nimport type { TailProcessorParams } from './BaseProcessor';\nimport { BaseProcessor } from './BaseProcessor';\nimport type { ValueCache, Rules, Params } from './types';\nimport templateProcessor from './utils/templateProcessor';\nimport { validateParams } from './utils/validateParams';\n\nexport abstract class TaggedTemplateProcessor extends BaseProcessor {\n readonly #template: (TemplateElement | ExpressionValue)[];\n\n protected constructor(params: Params, ...args: TailProcessorParams) {\n // Should have at least two params and the first one should be a callee.\n validateParams(params, ['callee', '...'], TaggedTemplateProcessor.SKIP);\n\n validateParams(\n params,\n ['callee', 'template'],\n 'Invalid usage of template tag'\n );\n const [tag, [, template]] = params;\n\n super([tag], ...args);\n\n template.forEach((element) => {\n if ('kind' in element && element.kind !== ValueType.FUNCTION) {\n this.dependencies.push(element);\n }\n });\n\n this.#template = template;\n }\n\n public override build(values: ValueCache) {\n if (this.artifacts.length > 0) {\n // FIXME: why it was called twice?\n throw new Error('Tag is already built');\n }\n\n const artifact = templateProcessor(\n this,\n this.#template,\n values,\n this.options.variableNameConfig\n );\n if (artifact) {\n this.artifacts.push(['css', artifact]);\n }\n }\n\n public override toString(): string {\n return `${super.toString()}\\`…\\``;\n }\n\n /**\n * It is called for each resolved expression in a template literal.\n * @param node\n * @param precedingCss\n * @param source\n * @param unit\n * @return chunk of CSS that should be added to extracted CSS\n */\n public abstract addInterpolation(\n node: Expression,\n precedingCss: string,\n source: string,\n unit?: string\n ): string;\n\n public abstract extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): Rules;\n}\n"],"file":"TaggedTemplateProcessor.js"}
|
package/esm/ast.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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" || expression.type === "BooleanLiteral") {
|
|
17
|
+
return String(expression.value);
|
|
18
|
+
}
|
|
19
|
+
if (expression.type === "NullLiteral") {
|
|
20
|
+
return "null";
|
|
21
|
+
}
|
|
22
|
+
if (expression.type === "MemberExpression") {
|
|
23
|
+
const memberExpression = expression;
|
|
24
|
+
const object = expressionToCodeWithContext(memberExpression.object, context);
|
|
25
|
+
const property = expressionToCodeWithContext(memberExpression.property, context);
|
|
26
|
+
return memberExpression.computed ? `${object}[${property}]` : `${object}.${property}`;
|
|
27
|
+
}
|
|
28
|
+
if (expression.type === "CallExpression") {
|
|
29
|
+
const callExpression = expression;
|
|
30
|
+
const callee = expressionToCodeWithContext(callExpression.callee, context);
|
|
31
|
+
const args = callExpression.arguments.map((arg) => expressionToCodeWithContext(arg, {
|
|
32
|
+
...context,
|
|
33
|
+
quote: arg.type === "StringLiteral" ? "single" : context.quote
|
|
34
|
+
}));
|
|
35
|
+
return `${callee}(${args.join(", ")})`;
|
|
36
|
+
}
|
|
37
|
+
if (expression.type === "ArrowFunctionExpression") {
|
|
38
|
+
const arrow = expression;
|
|
39
|
+
return `(${arrow.params.map((param) => expressionToCodeWithContext(param, context)).join(", ")}) => ${expressionToCodeWithContext(arrow.body, context)}`;
|
|
40
|
+
}
|
|
41
|
+
if (expression.type === "ArrayExpression") {
|
|
42
|
+
return `[${expression.elements.map((item) => item ? expressionToCodeWithContext(item, context) : "").join(", ")}]`;
|
|
43
|
+
}
|
|
44
|
+
if (expression.type === "BlockStatement") {
|
|
45
|
+
return "{ }";
|
|
46
|
+
}
|
|
47
|
+
if (expression.type === "ObjectExpression") {
|
|
48
|
+
const objectExpression = expression;
|
|
49
|
+
if (objectExpression.properties.length === 0) {
|
|
50
|
+
return "{}";
|
|
51
|
+
}
|
|
52
|
+
const nextIndent = context.indent + 1;
|
|
53
|
+
const properties = objectExpression.properties.map((property) => {
|
|
54
|
+
const key = expressionToCodeWithContext(property.key, {
|
|
55
|
+
...context,
|
|
56
|
+
quote: "double"
|
|
57
|
+
});
|
|
58
|
+
const value = expressionToCodeWithContext(property.value, {
|
|
59
|
+
indent: nextIndent,
|
|
60
|
+
quote: "double"
|
|
61
|
+
});
|
|
62
|
+
return `${indent(nextIndent)}${key}: ${value}`;
|
|
63
|
+
}).join(",\n");
|
|
64
|
+
return `{\n${properties}\n${indent(context.indent)}}`;
|
|
65
|
+
}
|
|
66
|
+
return expression.type;
|
|
67
|
+
};
|
|
68
|
+
export const expressionToCode = (expression) => expressionToCodeWithContext(expression, {
|
|
69
|
+
indent: 0,
|
|
70
|
+
quote: "double"
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=ast.js.map
|
package/esm/ast.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AA4HA,MAAM,qBACJ,OACA,QAA6B,aAClB;CACX,MAAM,OAAO,KAAK,UAAU,MAAM;AAClC,KAAI,UAAU,UAAU;AACtB,SAAO;;AAGT,QAAO,IAAI,KAAK,MAAM,GAAG,CAAC,EAAE,CAAC,QAAQ,MAAM,MAAM,CAAC;;AAGpD,MAAM,UAAU,UAA0B,KAAK,OAAO,MAAM;AAE5D,MAAM,+BACJ,YACA,YACW;AACX,KAAI,WAAW,SAAS,cAAc;AACpC,SAAQ,WAA0B;;AAGpC,KAAI,WAAW,SAAS,iBAAiB;AACvC,SAAO,kBAAmB,WAA6B,OAAO,QAAQ,MAAM;;AAG9E,KACE,WAAW,SAAS,oBACpB,WAAW,SAAS,kBACpB;AACA,SAAO,OAAQ,WAA+C,MAAM;;AAGtE,KAAI,WAAW,SAAS,eAAe;AACrC,SAAO;;AAGT,KAAI,WAAW,SAAS,oBAAoB;EAC1C,MAAM,mBAAmB;EACzB,MAAM,SAAS,4BAA4B,iBAAiB,QAAQ,QAAQ;EAC5E,MAAM,WAAW,4BACf,iBAAiB,UACjB,QACD;AACD,SAAO,iBAAiB,WACpB,GAAG,OAAO,GAAG,SAAS,KACtB,GAAG,OAAO,GAAG;;AAGnB,KAAI,WAAW,SAAS,kBAAkB;EACxC,MAAM,iBAAiB;EACvB,MAAM,SAAS,4BAA4B,eAAe,QAAQ,QAAQ;EAC1E,MAAM,OAAO,eAAe,UAAU,KAAK,QACzC,4BAA4B,KAAK;GAC/B,GAAG;GACH,OAAO,IAAI,SAAS,kBAAkB,WAAW,QAAQ;GAC1D,CAAC,CACH;AAED,SAAO,GAAG,OAAO,GAAG,KAAK,KAAK,KAAK,CAAC;;AAGtC,KAAI,WAAW,SAAS,2BAA2B;EACjD,MAAM,QAAQ;AACd,SAAO,IAAI,MAAM,OACd,KAAK,UAAU,4BAA4B,OAAO,QAAQ,CAAC,CAC3D,KAAK,KAAK,CAAC,OAAO,4BAA4B,MAAM,MAAM,QAAQ;;AAGvE,KAAI,WAAW,SAAS,mBAAmB;AACzC,SAAO,IAAK,WAA+B,SACxC,KAAK,SACJ,OAAO,4BAA4B,MAAM,QAAQ,GAAG,GACrD,CACA,KAAK,KAAK,CAAC;;AAGhB,KAAI,WAAW,SAAS,kBAAkB;AACxC,SAAO;;AAGT,KAAI,WAAW,SAAS,oBAAoB;EAC1C,MAAM,mBAAmB;AACzB,MAAI,iBAAiB,WAAW,WAAW,GAAG;AAC5C,UAAO;;EAGT,MAAM,aAAa,QAAQ,SAAS;EACpC,MAAM,aAAa,iBAAiB,WACjC,KAAK,aAAa;GACjB,MAAM,MAAM,4BAA4B,SAAS,KAAK;IACpD,GAAG;IACH,OAAO;IACR,CAAC;GACF,MAAM,QAAQ,4BAA4B,SAAS,OAAO;IACxD,QAAQ;IACR,OAAO;IACR,CAAC;AACF,UAAO,GAAG,OAAO,WAAW,GAAG,IAAI,IAAI;IACvC,CACD,KAAK,MAAM;AAEd,SAAO,MAAM,WAAW,IAAI,OAAO,QAAQ,OAAO,CAAC;;AAGrD,QAAO,WAAW;;AAGpB,OAAO,MAAM,oBAAoB,eAC/B,4BAA4B,YAAY;CACtC,QAAQ;CACR,OAAO;CACR,CAAC","names":[],"sources":["../src/ast.ts"],"version":3,"sourcesContent":["import type { Location } from '@wyw-in-js/shared';\n\nexport type SourceLocation = {\n end: Location;\n filename?: string;\n identifierName?: string | null;\n start: Location;\n};\n\nexport type BaseAstNode = {\n end?: number | null;\n loc?: unknown;\n start?: number | null;\n type: string;\n};\n\nexport type Expression = BaseAstNode;\n\nexport type Identifier = Expression & {\n name: string;\n type: 'Identifier';\n};\n\nexport type StringLiteral = Expression & {\n type: 'StringLiteral';\n value: string;\n};\n\nexport type NumericLiteral = Expression & {\n type: 'NumericLiteral';\n value: number;\n};\n\nexport type BooleanLiteral = Expression & {\n type: 'BooleanLiteral';\n value: boolean;\n};\n\nexport type NullLiteral = Expression & {\n type: 'NullLiteral';\n};\n\nexport type BlockStatement = BaseAstNode & {\n body: BaseAstNode[];\n type: 'BlockStatement';\n};\n\nexport type TemplateElement = BaseAstNode & {\n loc?: SourceLocation | null;\n tail: boolean;\n type: 'TemplateElement';\n value: {\n cooked?: string | null;\n raw: string;\n };\n};\n\nexport type MemberExpression = Expression & {\n computed?: boolean;\n object: Expression;\n property: Expression;\n type: 'MemberExpression';\n};\n\nexport type ObjectProperty = BaseAstNode & {\n computed?: boolean;\n key: Expression;\n shorthand?: boolean;\n type: 'ObjectProperty';\n value: Expression;\n};\n\nexport type ObjectExpression = Expression & {\n properties: ObjectProperty[];\n type: 'ObjectExpression';\n};\n\nexport type ArrayExpression = Expression & {\n elements: (Expression | null)[];\n type: 'ArrayExpression';\n};\n\nexport type CallExpression = Expression & {\n arguments: Expression[];\n callee: Expression;\n type: 'CallExpression';\n};\n\nexport type ArrowFunctionExpression = Expression & {\n async?: boolean;\n body: BlockStatement | Expression;\n params: Identifier[];\n type: 'ArrowFunctionExpression';\n};\n\nexport type AstService = {\n addDefaultImport(source: string, nameHint?: string): Identifier;\n addNamedImport(name: string, source: string, nameHint?: string): Identifier;\n arrayExpression(elements: (Expression | null)[]): ArrayExpression;\n arrowFunctionExpression(\n params: Identifier[],\n body: BlockStatement | Expression\n ): ArrowFunctionExpression;\n blockStatement(body: BaseAstNode[]): BlockStatement;\n booleanLiteral(value: boolean): BooleanLiteral;\n callExpression(callee: Expression, args: Expression[]): CallExpression;\n identifier(name: string): Identifier;\n memberExpression(\n object: Expression,\n property: Expression,\n computed?: boolean\n ): MemberExpression;\n nullLiteral(): NullLiteral;\n numericLiteral(value: number): NumericLiteral;\n objectExpression(properties: ObjectProperty[]): ObjectExpression;\n objectProperty(key: Expression, value: Expression): ObjectProperty;\n stringLiteral(value: string): StringLiteral;\n};\n\ntype PrintContext = {\n indent: number;\n quote?: 'double' | 'single';\n};\n\nconst stringLiteralCode = (\n value: string,\n quote: 'double' | 'single' = 'double'\n): string => {\n const json = JSON.stringify(value);\n if (quote === 'double') {\n return json;\n }\n\n return `'${json.slice(1, -1).replace(/'/g, \"\\\\'\")}'`;\n};\n\nconst indent = (level: number): string => ' '.repeat(level);\n\nconst expressionToCodeWithContext = (\n expression: Expression,\n context: PrintContext\n): string => {\n if (expression.type === 'Identifier') {\n return (expression as Identifier).name;\n }\n\n if (expression.type === 'StringLiteral') {\n return stringLiteralCode((expression as StringLiteral).value, context.quote);\n }\n\n if (\n expression.type === 'NumericLiteral' ||\n expression.type === 'BooleanLiteral'\n ) {\n return String((expression as NumericLiteral | BooleanLiteral).value);\n }\n\n if (expression.type === 'NullLiteral') {\n return 'null';\n }\n\n if (expression.type === 'MemberExpression') {\n const memberExpression = expression as MemberExpression;\n const object = expressionToCodeWithContext(memberExpression.object, context);\n const property = expressionToCodeWithContext(\n memberExpression.property,\n context\n );\n return memberExpression.computed\n ? `${object}[${property}]`\n : `${object}.${property}`;\n }\n\n if (expression.type === 'CallExpression') {\n const callExpression = expression as CallExpression;\n const callee = expressionToCodeWithContext(callExpression.callee, context);\n const args = callExpression.arguments.map((arg) =>\n expressionToCodeWithContext(arg, {\n ...context,\n quote: arg.type === 'StringLiteral' ? 'single' : context.quote,\n })\n );\n\n return `${callee}(${args.join(', ')})`;\n }\n\n if (expression.type === 'ArrowFunctionExpression') {\n const arrow = expression as ArrowFunctionExpression;\n return `(${arrow.params\n .map((param) => expressionToCodeWithContext(param, context))\n .join(', ')}) => ${expressionToCodeWithContext(arrow.body, context)}`;\n }\n\n if (expression.type === 'ArrayExpression') {\n return `[${(expression as ArrayExpression).elements\n .map((item) =>\n item ? expressionToCodeWithContext(item, context) : ''\n )\n .join(', ')}]`;\n }\n\n if (expression.type === 'BlockStatement') {\n return '{ }';\n }\n\n if (expression.type === 'ObjectExpression') {\n const objectExpression = expression as ObjectExpression;\n if (objectExpression.properties.length === 0) {\n return '{}';\n }\n\n const nextIndent = context.indent + 1;\n const properties = objectExpression.properties\n .map((property) => {\n const key = expressionToCodeWithContext(property.key, {\n ...context,\n quote: 'double',\n });\n const value = expressionToCodeWithContext(property.value, {\n indent: nextIndent,\n quote: 'double',\n });\n return `${indent(nextIndent)}${key}: ${value}`;\n })\n .join(',\\n');\n\n return `{\\n${properties}\\n${indent(context.indent)}}`;\n }\n\n return expression.type;\n};\n\nexport const expressionToCode = (expression: Expression): string =>\n expressionToCodeWithContext(expression, {\n indent: 0,\n quote: 'double',\n });\n"],"file":"ast.js"}
|
package/esm/diagnostics.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const PROCESSOR_DIAGNOSTIC_ARTIFACT =
|
|
2
|
-
export const createProcessorDiagnosticArtifact = diagnostic => [PROCESSOR_DIAGNOSTIC_ARTIFACT, diagnostic];
|
|
3
|
-
export const isProcessorDiagnosticArtifact = artifact => artifact[0] === PROCESSOR_DIAGNOSTIC_ARTIFACT;
|
|
4
|
-
//# sourceMappingURL=diagnostics.js.map
|
|
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;
|
|
4
|
+
//# sourceMappingURL=diagnostics.js.map
|
package/esm/diagnostics.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAIA,OAAO,MAAM,gCAAgC;AAO7C,OAAO,MAAM,qCACX,eACgC,CAAC,+BAA+B,WAAW;AAE7E,OAAO,MAAM,iCACX,aAEA,SAAS,OAAO","names":[],"sources":["../src/diagnostics.ts"],"version":3,"sourcesContent":["import type { Artifact } from '@wyw-in-js/shared';\n\nimport type { ProcessorDiagnostic } from './types';\n\nexport const PROCESSOR_DIAGNOSTIC_ARTIFACT = 'wyw-in-js:diagnostic' as const;\n\nexport type ProcessorDiagnosticArtifact = [\n name: typeof PROCESSOR_DIAGNOSTIC_ARTIFACT,\n data: ProcessorDiagnostic,\n];\n\nexport const createProcessorDiagnosticArtifact = (\n diagnostic: ProcessorDiagnostic\n): ProcessorDiagnosticArtifact => [PROCESSOR_DIAGNOSTIC_ARTIFACT, diagnostic];\n\nexport const isProcessorDiagnosticArtifact = (\n artifact: Artifact\n): artifact is ProcessorDiagnosticArtifact =>\n artifact[0] === PROCESSOR_DIAGNOSTIC_ARTIFACT;\n"],"file":"diagnostics.js"}
|
package/esm/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export { BaseProcessor } from
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
|
|
1
|
+
export { BaseProcessor } from "./BaseProcessor.js";
|
|
2
|
+
export { expressionToCode } from "./ast.js";
|
|
3
|
+
export { createProcessorDiagnosticArtifact, isProcessorDiagnosticArtifact, PROCESSOR_DIAGNOSTIC_ARTIFACT } from "./diagnostics.js";
|
|
4
|
+
export * from "./types.js";
|
|
5
|
+
export { buildSlug } from "./utils/buildSlug.js";
|
|
6
|
+
export { isValidParams, validateParams } from "./utils/validateParams.js";
|
|
7
|
+
export { TaggedTemplateProcessor } from "./TaggedTemplateProcessor.js";
|
|
8
|
+
export { toValidCSSIdentifier } from "./utils/toValidCSSIdentifier.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SACE,mCACA,+BACA,qCACK;AA0BP,cAAc;AACd,SAAS,iBAAiB;AAE1B,SAAS,eAAe,sBAAsB;AAE9C,SAAS,+BAA+B;AACxC,SAAS,4BAA4B","names":[],"sources":["../src/index.ts"],"version":3,"sourcesContent":["export { BaseProcessor } from './BaseProcessor';\nexport { expressionToCode } from './ast';\nexport {\n createProcessorDiagnosticArtifact,\n isProcessorDiagnosticArtifact,\n PROCESSOR_DIAGNOSTIC_ARTIFACT,\n} from './diagnostics';\nexport type { ProcessorDiagnosticArtifact } from './diagnostics';\nexport type {\n ArrayExpression,\n ArrowFunctionExpression,\n AstService,\n BaseAstNode,\n BlockStatement,\n BooleanLiteral,\n CallExpression,\n Expression,\n Identifier,\n MemberExpression,\n NullLiteral,\n NumericLiteral,\n ObjectExpression,\n ObjectProperty,\n SourceLocation,\n StringLiteral,\n TemplateElement,\n} from './ast';\nexport type {\n ProcessorParams,\n TagSource,\n TailProcessorParams,\n} from './BaseProcessor';\nexport * from './types';\nexport { buildSlug } from './utils/buildSlug';\nexport type { IOptions, IFileContext } from './utils/types';\nexport { isValidParams, validateParams } from './utils/validateParams';\nexport type { MapParams, ParamConstraints } from './utils/validateParams';\nexport { TaggedTemplateProcessor } from './TaggedTemplateProcessor';\nexport { toValidCSSIdentifier } from './utils/toValidCSSIdentifier';\n"],"file":"index.js"}
|
package/esm/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=types.js.map
|
|
2
|
+
//# sourceMappingURL=types.js.map
|
package/esm/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"","names":[],"sources":["../src/types.ts"],"version":3,"sourcesContent":["import type { ExpressionValue, Location, WYWEvalMeta } from '@wyw-in-js/shared';\n\nimport type {\n Expression,\n Identifier,\n MemberExpression,\n TemplateElement,\n} from './ast';\n\nexport type CSSPropertyValue = string | number;\n\nexport type ObjectWithSelectors = {\n [key: string]:\n | ObjectWithSelectors\n | CSSPropertyValue\n | (ObjectWithSelectors | CSSPropertyValue)[];\n};\n\nexport type CSSable = ObjectWithSelectors[string];\n\nexport type Value = (() => void) | WYWEvalMeta | CSSable;\n\nexport type ValueCache = Map<string | number | boolean | null, unknown>;\n\nexport interface ICSSRule {\n atom?: boolean;\n className: string;\n cssText: string;\n displayName: string;\n start: Location | null | undefined;\n}\n\nexport interface IInterpolation {\n id: string;\n node: Expression;\n source: string;\n unit: string;\n}\n\nexport type ProcessorDiagnosticSeverity = 'error' | 'warning';\n\nexport type ProcessorDiagnostic = {\n category: string;\n end?: Location | null;\n message: string;\n severity: ProcessorDiagnosticSeverity;\n start?: Location | null;\n};\n\nexport type Rules = Record<string, ICSSRule>;\n\nexport type CalleeParam = readonly ['callee', Identifier | MemberExpression];\nexport type CallParam = readonly ['call', ...ExpressionValue[]];\nexport type MemberParam = readonly ['member', string];\nexport type TemplateParam = readonly [\n 'template',\n (TemplateElement | ExpressionValue)[],\n];\n\nexport type Param = CalleeParam | CallParam | MemberParam | TemplateParam;\nexport type Params = readonly Param[];\n"],"file":"types.js"}
|
package/esm/utils/buildSlug.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const PLACEHOLDER = /\[(.*?)]/g;
|
|
2
2
|
const isValidArgName = (key, args) => key in args;
|
|
3
3
|
export function buildSlug(pattern, args) {
|
|
4
|
-
|
|
4
|
+
return pattern.replace(PLACEHOLDER, (_, name) => isValidArgName(name, args) ? args[name].toString() : "");
|
|
5
5
|
}
|
|
6
|
-
//# sourceMappingURL=buildSlug.js.map
|
|
6
|
+
//# sourceMappingURL=buildSlug.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,MAAM,cAAc;AAEpB,MAAM,kBACJ,KACA,SACuB,OAAO;AAEhC,OAAO,SAAS,UACd,SACA,MACA;AACA,QAAO,QAAQ,QAAQ,cAAc,GAAG,SACtC,eAAe,MAAM,KAAK,GAAG,KAAK,MAAM,UAAU,GAAG,GACtD","names":[],"sources":["../../src/utils/buildSlug.ts"],"version":3,"sourcesContent":["const PLACEHOLDER = /\\[(.*?)]/g;\n\nconst isValidArgName = <TArgs extends Record<string, { toString(): string }>>(\n key: string | number | symbol,\n args: TArgs\n): key is keyof TArgs => key in args;\n\nexport function buildSlug<TArgs extends Record<string, { toString(): string }>>(\n pattern: string,\n args: TArgs\n) {\n return pattern.replace(PLACEHOLDER, (_, name: string) =>\n isValidArgName(name, args) ? args[name].toString() : ''\n );\n}\n"],"file":"buildSlug.js"}
|
|
@@ -1,43 +1,40 @@
|
|
|
1
|
-
import { basename, dirname, extname, relative, sep, posix } from
|
|
2
|
-
import { logger, slugify } from
|
|
3
|
-
import { buildSlug } from
|
|
4
|
-
import { toValidCSSIdentifier } from
|
|
1
|
+
import { basename, dirname, extname, relative, sep, posix } from "path";
|
|
2
|
+
import { logger, slugify } from "@wyw-in-js/shared";
|
|
3
|
+
import { buildSlug } from "./buildSlug.js";
|
|
4
|
+
import { toValidCSSIdentifier } from "./toValidCSSIdentifier.js";
|
|
5
5
|
export default function getClassNameAndSlug(displayName, idx, options, context) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
className,
|
|
40
|
-
slug
|
|
41
|
-
};
|
|
6
|
+
const relativeFilename = (context.root && context.filename ? relative(context.root, context.filename) : context.filename ?? "unknown").replace(/\\/g, posix.sep);
|
|
7
|
+
// Custom properties need to start with a letter, so we prefix the slug
|
|
8
|
+
// Also use append the index of the class to the filename for uniqueness in the file
|
|
9
|
+
const slug = toValidCSSIdentifier(`${displayName.charAt(0).toLowerCase()}${slugify(`${relativeFilename}:${idx}`)}`);
|
|
10
|
+
// Collect some useful replacement patterns from the filename
|
|
11
|
+
// Available variables for the square brackets used in `classNameSlug` options
|
|
12
|
+
const ext = extname(relativeFilename);
|
|
13
|
+
const slugVars = {
|
|
14
|
+
hash: slug,
|
|
15
|
+
title: displayName,
|
|
16
|
+
index: idx,
|
|
17
|
+
file: relativeFilename,
|
|
18
|
+
ext,
|
|
19
|
+
name: basename(relativeFilename, ext),
|
|
20
|
+
dir: dirname(relativeFilename).split(sep).pop()
|
|
21
|
+
};
|
|
22
|
+
let className = options.displayName ? `${toValidCSSIdentifier(displayName)}_${slug}` : slug;
|
|
23
|
+
// The className can be defined by the user either as fn or a string
|
|
24
|
+
if (typeof options.classNameSlug === "function") {
|
|
25
|
+
try {
|
|
26
|
+
className = toValidCSSIdentifier(options.classNameSlug(slug, displayName, slugVars));
|
|
27
|
+
} catch {
|
|
28
|
+
throw new Error("classNameSlug option must return a string");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (typeof options.classNameSlug === "string") {
|
|
32
|
+
className = toValidCSSIdentifier(buildSlug(options.classNameSlug, slugVars));
|
|
33
|
+
}
|
|
34
|
+
logger.extend("template-parse:generated-meta")(`slug: ${slug}, displayName: ${displayName}, className: ${className}`);
|
|
35
|
+
return {
|
|
36
|
+
className,
|
|
37
|
+
slug
|
|
38
|
+
};
|
|
42
39
|
}
|
|
43
|
-
//# sourceMappingURL=getClassNameAndSlug.js.map
|
|
40
|
+
//# sourceMappingURL=getClassNameAndSlug.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAAS,UAAU,SAAS,SAAS,UAAU,KAAK,aAAa;AAGjE,SAAS,QAAQ,eAAe;AAEhC,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AAGrC,eAAe,SAAS,oBACtB,aACA,KACA,SACA,SACqC;CACrC,MAAM,oBACJ,QAAQ,QAAQ,QAAQ,WACpB,SAAS,QAAQ,MAAM,QAAQ,SAAS,GACxC,QAAQ,YAAY,WACxB,QAAQ,OAAO,MAAM,IAAI;;;CAI3B,MAAM,OAAO,qBACX,GAAG,YAAY,OAAO,EAAE,CAAC,aAAa,GAAG,QACvC,GAAG,iBAAiB,GAAG,MACxB,GACF;;;CAID,MAAM,MAAM,QAAQ,iBAAiB;CACrC,MAAM,WAA8B;EAClC,MAAM;EACN,OAAO;EACP,OAAO;EACP,MAAM;EACN;EACA,MAAM,SAAS,kBAAkB,IAAI;EACrC,KAAK,QAAQ,iBAAiB,CAAC,MAAM,IAAI,CAAC,KAAK;EAChD;CAED,IAAI,YAAY,QAAQ,cACpB,GAAG,qBAAqB,YAAa,CAAC,GAAG,SACzC;;AAGJ,KAAI,OAAO,QAAQ,kBAAkB,YAAY;AAC/C,MAAI;AACF,eAAY,qBACV,QAAQ,cAAc,MAAM,aAAa,SAAS,CACnD;UACK;AACN,SAAM,IAAI,MAAM,4CAA4C;;;AAIhE,KAAI,OAAO,QAAQ,kBAAkB,UAAU;AAC7C,cAAY,qBACV,UAAU,QAAQ,eAAe,SAAS,CAC3C;;AAGH,QAAO,OAAO,gCAAgC,CAC5C,SAAS,KAAK,iBAAiB,YAAY,eAAe,YAC3D;AAED,QAAO;EAAE;EAAW;EAAM","names":[],"sources":["../../src/utils/getClassNameAndSlug.ts"],"version":3,"sourcesContent":["import { basename, dirname, extname, relative, sep, posix } from 'path';\n\nimport type { ClassNameSlugVars } from '@wyw-in-js/shared';\nimport { logger, slugify } from '@wyw-in-js/shared';\n\nimport { buildSlug } from './buildSlug';\nimport { toValidCSSIdentifier } from './toValidCSSIdentifier';\nimport type { IFileContext, IOptions } from './types';\n\nexport default function getClassNameAndSlug(\n displayName: string,\n idx: number,\n options: IOptions,\n context: IFileContext\n): { className: string; slug: string } {\n const relativeFilename = (\n context.root && context.filename\n ? relative(context.root, context.filename)\n : context.filename ?? 'unknown'\n ).replace(/\\\\/g, posix.sep);\n\n // Custom properties need to start with a letter, so we prefix the slug\n // Also use append the index of the class to the filename for uniqueness in the file\n const slug = toValidCSSIdentifier(\n `${displayName.charAt(0).toLowerCase()}${slugify(\n `${relativeFilename}:${idx}`\n )}`\n );\n\n // Collect some useful replacement patterns from the filename\n // Available variables for the square brackets used in `classNameSlug` options\n const ext = extname(relativeFilename);\n const slugVars: ClassNameSlugVars = {\n hash: slug,\n title: displayName,\n index: idx,\n file: relativeFilename,\n ext,\n name: basename(relativeFilename, ext),\n dir: dirname(relativeFilename).split(sep).pop() as string,\n };\n\n let className = options.displayName\n ? `${toValidCSSIdentifier(displayName!)}_${slug!}`\n : slug!;\n\n // The className can be defined by the user either as fn or a string\n if (typeof options.classNameSlug === 'function') {\n try {\n className = toValidCSSIdentifier(\n options.classNameSlug(slug, displayName, slugVars)\n );\n } catch {\n throw new Error('classNameSlug option must return a string');\n }\n }\n\n if (typeof options.classNameSlug === 'string') {\n className = toValidCSSIdentifier(\n buildSlug(options.classNameSlug, slugVars)\n );\n }\n\n logger.extend('template-parse:generated-meta')(\n `slug: ${slug}, displayName: ${displayName}, className: ${className}`\n );\n\n return { className, slug };\n}\n"],"file":"getClassNameAndSlug.js"}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
export function getVariableName(varId, rawVariableName) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
default:
|
|
9
|
-
return `var(--${varId})`;
|
|
10
|
-
}
|
|
2
|
+
switch (rawVariableName) {
|
|
3
|
+
case "raw": return varId;
|
|
4
|
+
case "dashes": return `--${varId}`;
|
|
5
|
+
case "var":
|
|
6
|
+
default: return `var(--${varId})`;
|
|
7
|
+
}
|
|
11
8
|
}
|
|
12
|
-
//# sourceMappingURL=getVariableName.js.map
|
|
9
|
+
//# sourceMappingURL=getVariableName.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAEA,OAAO,SAAS,gBACd,OACA,iBACA;AACA,SAAQ,iBAAR;EACE,KAAK,MACH,QAAO;EACT,KAAK,SACH,QAAO,KAAK;EACd,KAAK;EACL,QACE,QAAO,SAAS,MAAM","names":[],"sources":["../../src/utils/getVariableName.ts"],"version":3,"sourcesContent":["import type { IOptions } from './types';\n\nexport function getVariableName(\n varId: string,\n rawVariableName: IOptions['variableNameConfig'] | undefined\n) {\n switch (rawVariableName) {\n case 'raw':\n return varId;\n case 'dashes':\n return `--${varId}`;\n case 'var':\n default:\n return `var(--${varId})`;\n }\n}\n"],"file":"getVariableName.js"}
|
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
|