@terrazzo/parser 2.0.0-alpha.0 → 2.0.0-alpha.2
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/CHANGELOG.md +76 -0
- package/dist/index.d.ts +31 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/build/index.ts +11 -2
- package/src/parse/token.ts +4 -1
- package/src/types.ts +30 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrazzo/parser",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
4
4
|
"description": "Parser/validator for the Design Tokens Community Group (DTCG) standard.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"picocolors": "^1.1.1",
|
|
43
43
|
"scule": "^1.3.0",
|
|
44
44
|
"wildcard-match": "^5.1.4",
|
|
45
|
-
"@terrazzo/
|
|
46
|
-
"@terrazzo/
|
|
45
|
+
"@terrazzo/json-schema-tools": "^0.0.1",
|
|
46
|
+
"@terrazzo/token-tools": "^2.0.0-alpha.2"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"yaml-to-momoa": "0.0.6"
|
package/src/build/index.ts
CHANGED
|
@@ -9,7 +9,6 @@ export interface BuildRunnerOptions {
|
|
|
9
9
|
config: ConfigInit;
|
|
10
10
|
logger?: Logger;
|
|
11
11
|
}
|
|
12
|
-
|
|
13
12
|
export const SINGLE_VALUE = 'SINGLE_VALUE';
|
|
14
13
|
export const MULTI_VALUE = 'MULTI_VALUE';
|
|
15
14
|
|
|
@@ -87,6 +86,7 @@ export default async function build(
|
|
|
87
86
|
for (const plugin of config.plugins) {
|
|
88
87
|
if (typeof plugin.transform === 'function') {
|
|
89
88
|
await plugin.transform({
|
|
89
|
+
context: { logger },
|
|
90
90
|
tokens,
|
|
91
91
|
sources,
|
|
92
92
|
getTransforms,
|
|
@@ -151,6 +151,7 @@ export default async function build(
|
|
|
151
151
|
if (typeof plugin.build === 'function') {
|
|
152
152
|
const pluginBuildStart = performance.now();
|
|
153
153
|
await plugin.build({
|
|
154
|
+
context: { logger },
|
|
154
155
|
tokens,
|
|
155
156
|
sources,
|
|
156
157
|
getTransforms,
|
|
@@ -184,7 +185,15 @@ export default async function build(
|
|
|
184
185
|
// buildEnd()
|
|
185
186
|
const startBuildEnd = performance.now();
|
|
186
187
|
await Promise.all(
|
|
187
|
-
config.plugins.map(async (plugin) =>
|
|
188
|
+
config.plugins.map(async (plugin) =>
|
|
189
|
+
plugin.buildEnd?.({
|
|
190
|
+
context: { logger },
|
|
191
|
+
tokens,
|
|
192
|
+
getTransforms,
|
|
193
|
+
sources,
|
|
194
|
+
outputFiles: structuredClone(result.outputFiles),
|
|
195
|
+
}),
|
|
196
|
+
),
|
|
188
197
|
);
|
|
189
198
|
logger.debug({
|
|
190
199
|
group: 'parser',
|
package/src/parse/token.ts
CHANGED
|
@@ -284,7 +284,10 @@ export function graphAliases(refMap: RefMap, { tokens, logger, sources }: GraphA
|
|
|
284
284
|
}
|
|
285
285
|
// last node: apply partial alias
|
|
286
286
|
if (i === partial.length - 1) {
|
|
287
|
-
|
|
287
|
+
// important: we want to get only the immediate alias [0], not the final one [.length - 1].
|
|
288
|
+
// if we resolve this too far, we could get incorrect values especially in plugin-css if a
|
|
289
|
+
// user is applying cascades to the intermediate aliases but not the final one
|
|
290
|
+
const aliasedID = getTokenRef(refChain[0]!);
|
|
288
291
|
if (!(aliasedID in tokens)) {
|
|
289
292
|
logger.error({
|
|
290
293
|
group: 'parser',
|
package/src/types.ts
CHANGED
|
@@ -3,7 +3,13 @@ import type { TokenNormalized } from '@terrazzo/token-tools';
|
|
|
3
3
|
import type ytm from 'yaml-to-momoa';
|
|
4
4
|
import type Logger from './logger.js';
|
|
5
5
|
|
|
6
|
+
export interface PluginHookContext {
|
|
7
|
+
logger: Logger;
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
export interface BuildHookOptions {
|
|
11
|
+
/** Plugin hook context (provides access to shared logger) */
|
|
12
|
+
context: PluginHookContext;
|
|
7
13
|
/** Map of tokens */
|
|
8
14
|
tokens: Record<string, TokenNormalized>;
|
|
9
15
|
/** Query transformed values */
|
|
@@ -19,10 +25,12 @@ export interface BuildHookOptions {
|
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
export interface BuildRunnerResult {
|
|
22
|
-
outputFiles:
|
|
28
|
+
outputFiles: OutputFileExpanded[];
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
export interface BuildEndHookOptions {
|
|
32
|
+
/** Plugin hook context (provides access to shared logger) */
|
|
33
|
+
context: PluginHookContext;
|
|
26
34
|
/** Map of tokens */
|
|
27
35
|
tokens: Record<string, TokenNormalized>;
|
|
28
36
|
/** Query transformed values */
|
|
@@ -299,17 +307,14 @@ export interface Plugin {
|
|
|
299
307
|
lint?(): Record<string, LintRule<any, any, any>>;
|
|
300
308
|
transform?(options: TransformHookOptions): Promise<void>;
|
|
301
309
|
build?(options: BuildHookOptions): Promise<void>;
|
|
302
|
-
buildEnd?(
|
|
310
|
+
buildEnd?(options: BuildEndHookOptions): Promise<void>;
|
|
303
311
|
}
|
|
304
312
|
|
|
305
|
-
|
|
306
|
-
export interface TokenTransformedSingleValue {
|
|
313
|
+
interface TokenTransformedBase {
|
|
307
314
|
/** Original Token ID */
|
|
308
315
|
id: string;
|
|
309
316
|
/** ID unique to this format. */
|
|
310
317
|
localID?: string;
|
|
311
|
-
type: 'SINGLE_VALUE';
|
|
312
|
-
value: string;
|
|
313
318
|
/**
|
|
314
319
|
* The mode of this value
|
|
315
320
|
* @default "."
|
|
@@ -317,23 +322,27 @@ export interface TokenTransformedSingleValue {
|
|
|
317
322
|
mode: string;
|
|
318
323
|
/** The original token. */
|
|
319
324
|
token: TokenNormalized;
|
|
325
|
+
/** Arbitrary metadata set by plugins. */
|
|
326
|
+
meta?: Record<string | number | symbol, unknown> & {
|
|
327
|
+
/**
|
|
328
|
+
* Metadata for the token-listing plugin. Plugins can
|
|
329
|
+
* set this to be the name of a token as it appears in code,
|
|
330
|
+
* and the token-listing plugin will pick it up and use it.
|
|
331
|
+
*/
|
|
332
|
+
'token-listing'?: { name: string | undefined };
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Transformed token with a single value. Note that this may be any type! */
|
|
337
|
+
export interface TokenTransformedSingleValue extends TokenTransformedBase {
|
|
338
|
+
type: 'SINGLE_VALUE';
|
|
339
|
+
value: string;
|
|
320
340
|
}
|
|
321
341
|
|
|
322
342
|
/** Transformed token with multiple values. Note that this may be any type! */
|
|
323
|
-
export interface TokenTransformedMultiValue {
|
|
324
|
-
/** Original Token ID */
|
|
325
|
-
id: string;
|
|
326
|
-
/** ID unique to this format.*/
|
|
327
|
-
localID?: string;
|
|
343
|
+
export interface TokenTransformedMultiValue extends TokenTransformedBase {
|
|
328
344
|
type: 'MULTI_VALUE';
|
|
329
345
|
value: Record<string, string>;
|
|
330
|
-
/**
|
|
331
|
-
* The mode of this value
|
|
332
|
-
* @default "."
|
|
333
|
-
*/
|
|
334
|
-
mode: string;
|
|
335
|
-
/** The original token */
|
|
336
|
-
token: TokenNormalized;
|
|
337
346
|
}
|
|
338
347
|
|
|
339
348
|
export type TokenTransformed = TokenTransformedSingleValue | TokenTransformedMultiValue;
|
|
@@ -353,6 +362,8 @@ export interface TransformParams {
|
|
|
353
362
|
}
|
|
354
363
|
|
|
355
364
|
export interface TransformHookOptions {
|
|
365
|
+
/** Plugin hook context (provides access to shared logger) */
|
|
366
|
+
context: PluginHookContext;
|
|
356
367
|
/** Map of tokens */
|
|
357
368
|
tokens: Record<string, TokenNormalized>;
|
|
358
369
|
/** Query transformed values */
|
|
@@ -365,6 +376,7 @@ export interface TransformHookOptions {
|
|
|
365
376
|
localID?: string;
|
|
366
377
|
value: string | Record<string, string>; // allow looser type for input (`undefined` will just get stripped)
|
|
367
378
|
mode?: string;
|
|
379
|
+
meta?: TokenTransformedBase['meta'];
|
|
368
380
|
},
|
|
369
381
|
): void;
|
|
370
382
|
/** Momoa documents */
|