Skip to content

Commit fdd30a3

Browse files
committed
add shortest scope #123
1 parent 02160d7 commit fdd30a3

File tree

225 files changed

+3456
-3113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+3456
-3113
lines changed

dist/index-umd-web.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,26 @@
715715
* export using ICSS module format
716716
*/
717717
ModuleScopeEnumOptions[ModuleScopeEnumOptions["ICSS"] = 256] = "ICSS";
718+
/**
719+
* use the shortest name possible. pattern is ignored.
720+
* it will produce names such as
721+
*
722+
* ```css
723+
* .a {
724+
* content: 'a';
725+
* }
726+
*
727+
* .b {
728+
* content: 'b';
729+
* }
730+
*
731+
* .c {
732+
* content: 'c';
733+
* }
734+
* ...
735+
* ```
736+
*/
737+
ModuleScopeEnumOptions[ModuleScopeEnumOptions["Shortest"] = 512] = "Shortest";
718738
})(exports.ModuleScopeEnumOptions || (exports.ModuleScopeEnumOptions = {}));
719739

720740
// from https://www.w3.org/TR/css-color-4/multiply-matrices.js
@@ -17931,6 +17951,24 @@
1793117951
}
1793217952
return key;
1793317953
}
17954+
let keyNameCounter = 0;
17955+
let keyNameCache = {};
17956+
function getShortNameGenerator() {
17957+
const forbidden = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].map(c => c.charCodeAt(0));
17958+
return (localName, filePath, pattern, hashLength = 5) => {
17959+
const key = `${localName}_${filePath}_${pattern}_${hashLength}`;
17960+
if (key in keyNameCache) {
17961+
return keyNameCache[key];
17962+
}
17963+
let value = keyNameCounter.toString(36);
17964+
keyNameCounter++;
17965+
while (forbidden.includes(value.charCodeAt(0))) {
17966+
value = keyNameCounter.toString(36);
17967+
keyNameCounter++;
17968+
}
17969+
return keyNameCache[key] ?? (keyNameCache[key] = value);
17970+
};
17971+
}
1793417972
/**
1793517973
* generate scoped name
1793617974
* @param localName
@@ -18583,6 +18621,10 @@
1858318621
// @ts-ignore
1858418622
moduleSettings.scoped |= exports.ModuleScopeEnumOptions.Pure;
1858518623
}
18624+
if (options.module & exports.ModuleScopeEnumOptions.Shortest) {
18625+
// @ts-ignore
18626+
moduleSettings.scoped |= exports.ModuleScopeEnumOptions.Shortest;
18627+
}
1858618628
if (options.module & exports.ModuleScopeEnumOptions.ICSS) {
1858718629
// @ts-ignore
1858818630
moduleSettings.scoped |= exports.ModuleScopeEnumOptions.ICSS;
@@ -18591,6 +18633,9 @@
1859118633
if (typeof moduleSettings.scoped == 'boolean') {
1859218634
moduleSettings.scoped = moduleSettings.scoped ? exports.ModuleScopeEnumOptions.Local : exports.ModuleScopeEnumOptions.Global;
1859318635
}
18636+
if (moduleSettings.scoped & exports.ModuleScopeEnumOptions.Shortest) {
18637+
moduleSettings.generateScopedName = getShortNameGenerator();
18638+
}
1859418639
moduleSettings.filePath = filePath;
1859518640
moduleSettings.pattern = pattern != null && pattern !== '' ? pattern : (filePath === '' ? `[local]_[hash]` : `[local]_[hash]_[name]`);
1859618641
for (const { node, parent } of walk(ast)) {
@@ -21134,10 +21179,9 @@
2113421179
}
2113521180
}
2113621181
run(ast, options = {}, parent, context) {
21137-
// if (!('chi' in ast)) {
21138-
//
21139-
// return null;
21140-
// }
21182+
if (!('chi' in ast)) {
21183+
return null;
21184+
}
2114121185
if (!('variableScope' in context)) {
2114221186
context.variableScope = new Map;
2114321187
}
@@ -21183,10 +21227,9 @@
2118321227
}
2118421228
cleanup(ast, options = {}, context) {
2118521229
const variableScope = context.variableScope;
21186-
// if (variableScope == null) {
21187-
//
21188-
// return;
21189-
// }
21230+
if (variableScope == null) {
21231+
return;
21232+
}
2119021233
for (const info of variableScope.values()) {
2119121234
if (info.replaceable) {
2119221235
let i;

dist/index.cjs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,26 @@ exports.ModuleScopeEnumOptions = void 0;
716716
* export using ICSS module format
717717
*/
718718
ModuleScopeEnumOptions[ModuleScopeEnumOptions["ICSS"] = 256] = "ICSS";
719+
/**
720+
* use the shortest name possible. pattern is ignored.
721+
* it will produce names such as
722+
*
723+
* ```css
724+
* .a {
725+
* content: 'a';
726+
* }
727+
*
728+
* .b {
729+
* content: 'b';
730+
* }
731+
*
732+
* .c {
733+
* content: 'c';
734+
* }
735+
* ...
736+
* ```
737+
*/
738+
ModuleScopeEnumOptions[ModuleScopeEnumOptions["Shortest"] = 512] = "Shortest";
719739
})(exports.ModuleScopeEnumOptions || (exports.ModuleScopeEnumOptions = {}));
720740

721741
// from https://www.w3.org/TR/css-color-4/multiply-matrices.js
@@ -18134,6 +18154,24 @@ function getKeyName(key, how) {
1813418154
}
1813518155
return key;
1813618156
}
18157+
let keyNameCounter = 0;
18158+
let keyNameCache = {};
18159+
function getShortNameGenerator() {
18160+
const forbidden = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].map(c => c.charCodeAt(0));
18161+
return (localName, filePath, pattern, hashLength = 5) => {
18162+
const key = `${localName}_${filePath}_${pattern}_${hashLength}`;
18163+
if (key in keyNameCache) {
18164+
return keyNameCache[key];
18165+
}
18166+
let value = keyNameCounter.toString(36);
18167+
keyNameCounter++;
18168+
while (forbidden.includes(value.charCodeAt(0))) {
18169+
value = keyNameCounter.toString(36);
18170+
keyNameCounter++;
18171+
}
18172+
return keyNameCache[key] ?? (keyNameCache[key] = value);
18173+
};
18174+
}
1813718175
/**
1813818176
* generate scoped name
1813918177
* @param localName
@@ -18786,6 +18824,10 @@ async function doParse(iter, options = {}) {
1878618824
// @ts-ignore
1878718825
moduleSettings.scoped |= exports.ModuleScopeEnumOptions.Pure;
1878818826
}
18827+
if (options.module & exports.ModuleScopeEnumOptions.Shortest) {
18828+
// @ts-ignore
18829+
moduleSettings.scoped |= exports.ModuleScopeEnumOptions.Shortest;
18830+
}
1878918831
if (options.module & exports.ModuleScopeEnumOptions.ICSS) {
1879018832
// @ts-ignore
1879118833
moduleSettings.scoped |= exports.ModuleScopeEnumOptions.ICSS;
@@ -18794,6 +18836,9 @@ async function doParse(iter, options = {}) {
1879418836
if (typeof moduleSettings.scoped == 'boolean') {
1879518837
moduleSettings.scoped = moduleSettings.scoped ? exports.ModuleScopeEnumOptions.Local : exports.ModuleScopeEnumOptions.Global;
1879618838
}
18839+
if (moduleSettings.scoped & exports.ModuleScopeEnumOptions.Shortest) {
18840+
moduleSettings.generateScopedName = getShortNameGenerator();
18841+
}
1879718842
moduleSettings.filePath = filePath;
1879818843
moduleSettings.pattern = pattern != null && pattern !== '' ? pattern : (filePath === '' ? `[local]_[hash]` : `[local]_[hash]_[name]`);
1879918844
for (const { node, parent } of walk(ast)) {
@@ -21320,10 +21365,9 @@ class InlineCssVariablesFeature {
2132021365
}
2132121366
}
2132221367
run(ast, options = {}, parent, context) {
21323-
// if (!('chi' in ast)) {
21324-
//
21325-
// return null;
21326-
// }
21368+
if (!('chi' in ast)) {
21369+
return null;
21370+
}
2132721371
if (!('variableScope' in context)) {
2132821372
context.variableScope = new Map;
2132921373
}
@@ -21369,10 +21413,9 @@ class InlineCssVariablesFeature {
2136921413
}
2137021414
cleanup(ast, options = {}, context) {
2137121415
const variableScope = context.variableScope;
21372-
// if (variableScope == null) {
21373-
//
21374-
// return;
21375-
// }
21416+
if (variableScope == null) {
21417+
return;
21418+
}
2137621419
for (const info of variableScope.values()) {
2137721420
if (info.replaceable) {
2137821421
let i;

dist/index.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,27 @@ declare enum ModuleScopeEnumOptions {
698698
/**
699699
* export using ICSS module format
700700
*/
701-
ICSS = 256
701+
ICSS = 256,
702+
/**
703+
* use the shortest name possible. pattern is ignored.
704+
* it will produce names such as
705+
*
706+
* ```css
707+
* .a {
708+
* content: 'a';
709+
* }
710+
*
711+
* .b {
712+
* content: 'b';
713+
* }
714+
*
715+
* .c {
716+
* content: 'c';
717+
* }
718+
* ...
719+
* ```
720+
*/
721+
Shortest = 512
702722
}
703723

704724
/**

dist/lib/ast/features/inlinecssvariables.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ class InlineCssVariablesFeature {
6666
}
6767
}
6868
run(ast, options = {}, parent, context) {
69-
// if (!('chi' in ast)) {
70-
//
71-
// return null;
72-
// }
69+
if (!('chi' in ast)) {
70+
return null;
71+
}
7372
if (!('variableScope' in context)) {
7473
context.variableScope = new Map;
7574
}
@@ -115,10 +114,9 @@ class InlineCssVariablesFeature {
115114
}
116115
cleanup(ast, options = {}, context) {
117116
const variableScope = context.variableScope;
118-
// if (variableScope == null) {
119-
//
120-
// return;
121-
// }
117+
if (variableScope == null) {
118+
return;
119+
}
122120
for (const info of variableScope.values()) {
123121
if (info.replaceable) {
124122
let i;

dist/lib/ast/types.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,26 @@ var ModuleScopeEnumOptions;
709709
* export using ICSS module format
710710
*/
711711
ModuleScopeEnumOptions[ModuleScopeEnumOptions["ICSS"] = 256] = "ICSS";
712+
/**
713+
* use the shortest name possible. pattern is ignored.
714+
* it will produce names such as
715+
*
716+
* ```css
717+
* .a {
718+
* content: 'a';
719+
* }
720+
*
721+
* .b {
722+
* content: 'b';
723+
* }
724+
*
725+
* .c {
726+
* content: 'c';
727+
* }
728+
* ...
729+
* ```
730+
*/
731+
ModuleScopeEnumOptions[ModuleScopeEnumOptions["Shortest"] = 512] = "Shortest";
712732
})(ModuleScopeEnumOptions || (ModuleScopeEnumOptions = {}));
713733

714734
export { ColorType, EnumToken, ModuleCaseTransformEnum, ModuleScopeEnumOptions, SyntaxValidationResult, ValidationLevel };

dist/lib/parser/parse.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ function getKeyName(key, how) {
9292
}
9393
return key;
9494
}
95+
let keyNameCounter = 0;
96+
let keyNameCache = {};
97+
function getShortNameGenerator() {
98+
const forbidden = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].map(c => c.charCodeAt(0));
99+
return (localName, filePath, pattern, hashLength = 5) => {
100+
const key = `${localName}_${filePath}_${pattern}_${hashLength}`;
101+
if (key in keyNameCache) {
102+
return keyNameCache[key];
103+
}
104+
let value = keyNameCounter.toString(36);
105+
keyNameCounter++;
106+
while (forbidden.includes(value.charCodeAt(0))) {
107+
value = keyNameCounter.toString(36);
108+
keyNameCounter++;
109+
}
110+
return keyNameCache[key] ?? (keyNameCache[key] = value);
111+
};
112+
}
95113
/**
96114
* generate scoped name
97115
* @param localName
@@ -744,6 +762,10 @@ async function doParse(iter, options = {}) {
744762
// @ts-ignore
745763
moduleSettings.scoped |= ModuleScopeEnumOptions.Pure;
746764
}
765+
if (options.module & ModuleScopeEnumOptions.Shortest) {
766+
// @ts-ignore
767+
moduleSettings.scoped |= ModuleScopeEnumOptions.Shortest;
768+
}
747769
if (options.module & ModuleScopeEnumOptions.ICSS) {
748770
// @ts-ignore
749771
moduleSettings.scoped |= ModuleScopeEnumOptions.ICSS;
@@ -752,6 +774,9 @@ async function doParse(iter, options = {}) {
752774
if (typeof moduleSettings.scoped == 'boolean') {
753775
moduleSettings.scoped = moduleSettings.scoped ? ModuleScopeEnumOptions.Local : ModuleScopeEnumOptions.Global;
754776
}
777+
if (moduleSettings.scoped & ModuleScopeEnumOptions.Shortest) {
778+
moduleSettings.generateScopedName = getShortNameGenerator();
779+
}
755780
moduleSettings.filePath = filePath;
756781
moduleSettings.pattern = pattern != null && pattern !== '' ? pattern : (filePath === '' ? `[local]_[hash]` : `[local]_[hash]_[name]`);
757782
for (const { node, parent } of walk(ast)) {
@@ -2757,4 +2782,4 @@ function parseTokens(tokens, options = {}) {
27572782
return tokens;
27582783
}
27592784

2760-
export { doParse, generateScopedName, getKeyName, getTokenType, parseAtRulePrelude, parseDeclarations, parseSelector, parseString, parseTokens, replaceToken, urlTokenMatcher };
2785+
export { doParse, generateScopedName, getKeyName, getShortNameGenerator, getTokenType, parseAtRulePrelude, parseDeclarations, parseSelector, parseString, parseTokens, replaceToken, urlTokenMatcher };

dist/lib/parser/utils/hash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ async function hash(input, length = 6, algo) {
8383
return hashId(input, length);
8484
}
8585

86-
export { hash, hashAlgorithms, hashId };
86+
export { DIGITS, FIRST_ALPHABET, FULL_ALPHABET, LOWER, hash, hashAlgorithms, hashId };

docs/assets/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)