Skip to content

Commit ccc738f

Browse files
committed
start css modules implementation #116
1 parent 2b2ef2a commit ccc738f

File tree

7 files changed

+603
-33
lines changed

7 files changed

+603
-33
lines changed

CHANGELOG.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
# Changelog
22

3-
## v1.3.4
3+
## v1.3.5
44

55
- [x] fix \<integer\> syntax validation #115
66

7+
### CSS Module
8+
9+
- [x] scoped name generation
10+
- [ ] @values
11+
- [ ] :import
12+
- [ ] composes: https://github.com/css-modules/css-modules/blob/master/docs/composition.md
13+
- [ ] compose from local selector
14+
- [ ] compose from other files
15+
- [ ] compose from global selector
16+
- [x] :local
17+
- [x] :global
18+
- [x] :local()
19+
- [x] :global()
20+
- [x] selector
21+
- [ ] keyframe
22+
- [x] grid-template-areas
23+
- [x] dashed ident
24+
- [ ] animations
25+
- [ ] pure
26+
- [ ] default mode
27+
- [ ] global
28+
- [ ] local
29+
730
## v1.3.4
831

932
- [x] make streaming optional #109

src/@types/index.d.ts

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,64 @@ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptio
286286
* @private
287287
*/
288288
cache?: WeakMap<AstNode, string>;
289+
290+
/**
291+
* module options
292+
*/
293+
module?: {
294+
295+
296+
/**
297+
* use global scope
298+
*/
299+
globalScope?: boolean;
300+
301+
/**
302+
* module output file path
303+
*/
304+
filePath?: string;
305+
306+
/**
307+
* module hash length
308+
*/
309+
hashLength?: number;
310+
311+
/**
312+
* scoped name pattern. the supported placeholders are:
313+
* - name: the file base name without the extension
314+
* - hash: the file path hash
315+
* - local: the local name
316+
* - path: the file path
317+
* - folder: the file folder
318+
* - ext: the file extension
319+
*
320+
* pattern can optionally have a maximum number of characters: `pattern: '[name:2]-[hash:5]'`.
321+
* the hash pattern can take an algorithm and a maximum number of characters: `pattern: 'name-[hash:base64:5]'` or `pattern: 'name-[hash:5]'`.
322+
* supported hash algorithms are:
323+
* - base64
324+
* - hex
325+
* - base64url
326+
* - sha1
327+
* - sha256
328+
* - sha384
329+
* - sha512
330+
*/
331+
pattern?: string;
332+
333+
/**
334+
* optional custom function to generate scoped name
335+
* @param localName
336+
* @param filePath
337+
* @param hashLength
338+
* @param pattern see {@link ParserOptions.module.pattern}
339+
*/
340+
generateScopedName?: (
341+
localName: string,
342+
filePath: string,
343+
hashLength?: number,
344+
pattern?: string
345+
) => string;
346+
}
289347
}
290348

291349
/**
@@ -462,13 +520,17 @@ export declare interface ParseResultStats {
462520
*/
463521
importedBytesIn: number;
464522
/**
465-
* parse time
523+
* parse processing time
466524
*/
467525
parse: string;
468526
/**
469-
* minify time
527+
* minify processing time
470528
*/
471529
minify: string;
530+
/**
531+
* module processing time
532+
*/
533+
module?: string;
472534
/**
473535
* total time
474536
*/
@@ -493,6 +555,37 @@ export declare interface ParseResultStats {
493555
* parse result object
494556
*/
495557
export declare interface ParseResult {
558+
/**
559+
* parsed ast tree
560+
*/
561+
ast: AstStyleSheet;
562+
/**
563+
* parse errors
564+
*/
565+
errors: ErrorDescription[];
566+
/**
567+
* parse stats
568+
*/
569+
stats: ParseResultStats;
570+
571+
/**
572+
* module mapping
573+
*/
574+
mapping?: Record<string, string>;
575+
576+
}
577+
578+
export declare interface ParseModuleResult {
579+
580+
/**
581+
* module mapping
582+
*/
583+
mapping: Record<string, string>;
584+
/**
585+
* module code
586+
*/
587+
code: string;
588+
496589
/**
497590
* parsed ast tree
498591
*/

src/@types/walker.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export declare type WalkerFilter = (node: AstNode) => WalkerOption;
2020
/**
2121
* filter nod
2222
*/
23-
export declare type WalkerValueFilter = (node: AstNode | Token, parent?: AstNode | Token | null, event?: WalkerEvent) => WalkerOption | null;
23+
export declare type WalkerValueFilter = (node: AstNode | Token, parent?: AstNode | Token | AstNode[] | Token[] | null, event?: WalkerEvent) => WalkerOption | null;
2424

2525
export declare interface WalkResult {
2626
node: AstNode;

src/lib/ast/walk.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,18 @@ export function* walkValues(values: Token[], root: AstNode | Token | null = null
277277
}
278278

279279
// @ts-ignore
280-
if (option != null && typeof option == 'object' && 'typ' in option) {
280+
if (option != null && typeof option == 'object' && ('typ' in option || Array.isArray(option))) {
281+
282+
const op = Array.isArray(option) ? option : [option];
283+
284+
for (const o of op) {
285+
286+
map.set(o as Token, map.get(value) ?? root as FunctionToken | ParensToken);
287+
}
288+
289+
stack[reverse ? 'push' : 'unshift'](...op);
290+
console.error({op, s: stack[0]});
281291

282-
map.set(option as Token, map.get(value) ?? root as FunctionToken | ParensToken);
283292
}
284293
}
285294
}
@@ -360,9 +369,16 @@ export function* walkValues(values: Token[], root: AstNode | Token | null = null
360369
option = filter.fn(value, <FunctionToken | ParensToken>map.get(value), WalkerEvent.Leave);
361370

362371
// @ts-ignore
363-
if (option != null && 'typ' in option) {
372+
if (option != null && ('typ' in option || Array.isArray(option))) {
373+
374+
const op = Array.isArray(option) ? option : [option];
375+
376+
for (const o of op) {
377+
378+
map.set(o as Token, map.get(value) ?? root as FunctionToken | ParensToken);
379+
}
364380

365-
map.set(option as Token, map.get(value) ?? root as FunctionToken | ParensToken);
381+
stack[reverse ? 'push' : 'unshift'](...op);
366382
}
367383
}
368384
}

0 commit comments

Comments
 (0)