Skip to content

Commit c4a2a1b

Browse files
authored
Merge pull request #68 from tbela99/feature/minification_passes
minification passes #66
2 parents 127085a + 298ce63 commit c4a2a1b

File tree

16 files changed

+139
-47
lines changed

16 files changed

+139
-47
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![playground](https://img.shields.io/badge/playground-try%20it%20now-%230a7398
22
)](https://tbela99.github.io/css-parser/playground/) [![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Ftbela99%2Fcss-parser%2Fmaster%2Fpackage.json&query=version&logo=npm&label=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser) [![npm](https://img.shields.io/jsr/v/%40tbela99/css-parser?link=https%3A%2F%2Fjsr.io%2F%40tbela99%2Fcss-parser
3-
)](https://jsr.io/@tbela99/css-parser) [![cov](https://tbela99.github.io/css-parser/badges/coverage.svg)](https://github.com/tbela99/css-parser/actions) [![NPM Downloads](https://img.shields.io/npm/dm/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser)
3+
)](https://jsr.io/@tbela99/css-parser) [![cov](https://tbela99.github.io/css-parser/badges/coverage.svg)](https://github.com/tbela99/css-parser/actions) [![NPM Downloads](https://img.shields.io/npm/dm/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser) [![bundle size](https://img.shields.io/bundlejs/size/%40tbela99/css-parser%400.9.0?exports=cjs)](https://www.npmjs.com/package/@tbela99/css-parser)
44

55
# css-parser
66

@@ -65,7 +65,7 @@ import as a module
6565

6666
```javascript
6767

68-
import {transform} from 'npm:@tbela99/css-parser';
68+
import {transform} from '@tbela99/css-parser';
6969

7070
// ...
7171
```
@@ -96,7 +96,7 @@ Javascript module from cdn
9696

9797
<script type="module">
9898
99-
import {transform} from 'https://esm.sh/@tbela99/css-parser@0.4.0/web';
99+
import {transform} from 'https://esm.sh/@tbela99/css-parser@0.9.0/web';
100100
101101
102102
const css = `
@@ -154,6 +154,7 @@ Include ParseOptions and RenderOptions
154154
> Minify Options
155155
156156
- minify: boolean, optional. default to _true_. optimize ast.
157+
- pass: number, optional. minification pass. default to 1
157158
- nestingRules: boolean, optional. automatically generated nested rules.
158159
- expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules
159160
to false.

dist/index-umd-web.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15910,6 +15910,7 @@
1591015910
src: '',
1591115911
sourcemap: false,
1591215912
minify: true,
15913+
pass: 1,
1591315914
parseColor: true,
1591415915
nestingRules: false,
1591515916
resolveImport: false,
@@ -16089,7 +16090,10 @@
1608916090
}
1609016091
if (options.minify) {
1609116092
if (ast.chi.length > 0) {
16092-
minify(ast, options, true, errors, false);
16093+
let passes = options.pass ?? 1;
16094+
while (passes--) {
16095+
minify(ast, options, true, errors, false);
16096+
}
1609316097
}
1609416098
}
1609516099
const endTime = performance.now();

dist/index.cjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15909,6 +15909,7 @@ async function doParse(iterator, options = {}) {
1590915909
src: '',
1591015910
sourcemap: false,
1591115911
minify: true,
15912+
pass: 1,
1591215913
parseColor: true,
1591315914
nestingRules: false,
1591415915
resolveImport: false,
@@ -16088,7 +16089,10 @@ async function doParse(iterator, options = {}) {
1608816089
}
1608916090
if (options.minify) {
1609016091
if (ast.chi.length > 0) {
16091-
minify(ast, options, true, errors, false);
16092+
let passes = options.pass ?? 1;
16093+
while (passes--) {
16094+
minify(ast, options, true, errors, false);
16095+
}
1609216096
}
1609316097
}
1609416098
const endTime = performance.now();

dist/index.d.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ declare enum EnumToken {
129129
* @param nestingContent
130130
* @param context
131131
*/
132-
declare function minify(ast: AstNode, options?: ParserOptions | MinifyOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean, context?: {
132+
declare function minify(ast: AstNode, options?: ParserOptions | MinifyFeatureOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean, context?: {
133133
[key: string]: any;
134134
}): AstNode;
135135

@@ -1035,24 +1035,29 @@ interface ValidationOptions {
10351035
lenient?: boolean;
10361036
}
10371037

1038-
export declare interface ParserOptions extends ValidationOptions, PropertyListOptions {
1038+
interface MinifyOptions {
10391039

10401040
minify?: boolean;
1041-
src?: string;
1042-
sourcemap?: boolean;
10431041
nestingRules?: boolean;
10441042
expandNestingRules?: boolean;
1045-
removeCharset?: boolean;
1043+
removeDuplicateDeclarations?: boolean;
1044+
computeShorthand?: boolean;
1045+
computeCalcExpression?: boolean;
1046+
inlineCssVariables?: boolean;
10461047
removeEmpty?: boolean;
1048+
pass?: number;
1049+
}
1050+
1051+
export declare interface ParserOptions extends MinifyOptions, ValidationOptions, PropertyListOptions {
1052+
1053+
src?: string;
1054+
sourcemap?: boolean;
1055+
removeCharset?: boolean;
10471056
resolveUrls?: boolean;
10481057
resolveImport?: boolean;
10491058
cwd?: string;
10501059
parseColor?: boolean;
1051-
removeDuplicateDeclarations?: boolean;
1052-
computeShorthand?: boolean;
10531060
removePrefix?: boolean;
1054-
inlineCssVariables?: boolean;
1055-
computeCalcExpression?: boolean;
10561061
load?: (url: string, currentUrl: string) => Promise<string>;
10571062
dirname?: (path: string) => string;
10581063
resolve?: (url: string, currentUrl: string, currentWorkingDirectory?: string) => {
@@ -1064,7 +1069,7 @@ export declare interface ParserOptions extends ValidationOptions, PropertyListOp
10641069
setParent?: boolean;
10651070
}
10661071

1067-
export declare interface MinifyOptions extends ParserOptions {
1072+
export declare interface MinifyFeatureOptions extends ParserOptions {
10681073

10691074
features: MinifyFeature[];
10701075
}
@@ -1073,7 +1078,7 @@ export declare interface MinifyFeature {
10731078

10741079
ordering: number;
10751080

1076-
register(options: MinifyOptions | ParserOptions): void;
1081+
register(options: MinifyFeatureOptions | ParserOptions): void;
10771082

10781083
// run(ast: AstRule | AstAtRule, options: ParserOptions = {}, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: { [key: string]: any }): void;
10791084

@@ -1083,7 +1088,7 @@ export declare interface MinifyFeature {
10831088
export declare interface MinifyFeature {
10841089

10851090
ordering: number;
1086-
register: (options: MinifyOptions | ParserOptions) => void;
1091+
register: (options: MinifyFeatureOptions | ParserOptions) => void;
10871092
run: (ast: AstRule | AstAtRule, options: ParserOptions, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
10881093
[key: string]: any
10891094
}) => void;

dist/lib/parser/parse.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async function doParse(iterator, options = {}) {
4646
src: '',
4747
sourcemap: false,
4848
minify: true,
49+
pass: 1,
4950
parseColor: true,
5051
nestingRules: false,
5152
resolveImport: false,
@@ -225,7 +226,10 @@ async function doParse(iterator, options = {}) {
225226
}
226227
if (options.minify) {
227228
if (ast.chi.length > 0) {
228-
minify(ast, options, true, errors, false);
229+
let passes = options.pass ?? 1;
230+
while (passes--) {
231+
minify(ast, options, true, errors, false);
232+
}
229233
}
230234
}
231235
const endTime = performance.now();

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tbela99/css-parser",
3-
"version": "0.9.0",
3+
"version": "0.9.1-alpha1",
44
"publish": {
55
"include": [
66
"src",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "@tbela99/css-parser",
33
"description": "CSS parser for node and the browser",
4-
"version": "v0.9.0",
4+
"version": "v0.9.1-alpah1",
55
"exports": {
66
".": "./dist/node/index.js",
7+
"./node": "./dist/node/index.js",
78
"./umd": "./dist/index-umd-web.js",
89
"./web": "./dist/web/index.js",
910
"./cjs": "./dist/index.cjs"

src/@types/index.d.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export declare interface MinifyFeature {
2929

3030
ordering: number;
3131

32-
register(options: MinifyOptions | ParserOptions): void;
32+
register(options: MinifyFeatureOptions | ParserOptions): void;
3333

3434
// run(ast: AstRule | AstAtRule, options: ParserOptions = {}, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: { [key: string]: any }): void;
3535

@@ -42,24 +42,29 @@ export interface ValidationOptions {
4242
lenient?: boolean;
4343
}
4444

45-
export declare interface ParserOptions extends ValidationOptions, PropertyListOptions {
45+
export interface MinifyOptions {
4646

4747
minify?: boolean;
48-
src?: string;
49-
sourcemap?: boolean;
5048
nestingRules?: boolean;
5149
expandNestingRules?: boolean;
52-
removeCharset?: boolean;
50+
removeDuplicateDeclarations?: boolean;
51+
computeShorthand?: boolean;
52+
computeCalcExpression?: boolean;
53+
inlineCssVariables?: boolean;
5354
removeEmpty?: boolean;
55+
pass?: number;
56+
}
57+
58+
export declare interface ParserOptions extends MinifyOptions, ValidationOptions, PropertyListOptions {
59+
60+
src?: string;
61+
sourcemap?: boolean;
62+
removeCharset?: boolean;
5463
resolveUrls?: boolean;
5564
resolveImport?: boolean;
5665
cwd?: string;
5766
parseColor?: boolean;
58-
removeDuplicateDeclarations?: boolean;
59-
computeShorthand?: boolean;
6067
removePrefix?: boolean;
61-
inlineCssVariables?: boolean;
62-
computeCalcExpression?: boolean;
6368
load?: (url: string, currentUrl: string) => Promise<string>;
6469
dirname?: (path: string) => string;
6570
resolve?: (url: string, currentUrl: string, currentWorkingDirectory?: string) => {
@@ -71,15 +76,15 @@ export declare interface ParserOptions extends ValidationOptions, PropertyListOp
7176
setParent?: boolean;
7277
}
7378

74-
export declare interface MinifyOptions extends ParserOptions {
79+
export declare interface MinifyFeatureOptions extends ParserOptions {
7580

7681
features: MinifyFeature[];
7782
}
7883

7984
export declare interface MinifyFeature {
8085

8186
ordering: number;
82-
register: (options: MinifyOptions | ParserOptions) => void;
87+
register: (options: MinifyFeatureOptions | ParserOptions) => void;
8388
run: (ast: AstRule | AstAtRule, options: ParserOptions, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
8489
[key: string]: any
8590
}) => void;

src/lib/ast/features/calc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
AstRule,
66
BinaryExpressionToken,
77
FunctionToken,
8-
MinifyOptions,
8+
MinifyFeatureOptions,
99
NumberToken,
1010
ParensToken,
1111
Token,
@@ -23,7 +23,7 @@ export class ComputeCalcExpressionFeature {
2323
return 1;
2424
}
2525

26-
static register(options: MinifyOptions): void {
26+
static register(options: MinifyFeatureOptions): void {
2727

2828
if (options.computeCalcExpression) {
2929

src/lib/ast/features/inlinecssvariables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
AstRuleStyleSheet,
88
CommentToken,
99
FunctionToken,
10-
MinifyOptions,
10+
MinifyFeatureOptions,
1111
ParserOptions,
1212
VariableScopeInfo
1313
} from "../../../@types";
@@ -55,7 +55,7 @@ export class InlineCssVariablesFeature {
5555
return 0;
5656
}
5757

58-
static register(options: MinifyOptions): void {
58+
static register(options: MinifyFeatureOptions): void {
5959

6060
if (options.inlineCssVariables) {
6161

0 commit comments

Comments
 (0)