Skip to content

Commit 894a70f

Browse files
committed
fetch using cors in web #30
1 parent 656376d commit 894a70f

File tree

10 files changed

+43
-53
lines changed

10 files changed

+43
-53
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## V0.5.0
4+
- [x] render node with parents
5+
- [x] fix relative color from xyz
6+
- [x] fix bug when inlineCss is true bug no css variable exists
7+
- [x] compute more shorthands
8+
- [x] (web) fetch imported css files from external domains using cors
9+
10+
## V0.4.1
11+
12+
no code change
13+
314
## V0.4.0
415

516
Parsing

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $ npm install @tbela99/css-parser
1414

1515
- no dependency
1616
- fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
17-
- efficient minification without unsafe transforms, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
17+
- fast and efficient minification without unsafe transforms, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
1818
- minify colors.
1919
- support css color level 4 & 5: color(), lab(), lch(), oklab(), oklch(), color-mix() and relative color
2020
- generate nested css rules

dist/index-umd-web.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6041,7 +6041,7 @@
60416041
exports.EnumToken.BadUrlTokenType,
60426042
exports.EnumToken.BadStringTokenType
60436043
];
6044-
new Set([
6044+
const enumTokenHints = new Set([
60456045
exports.EnumToken.WhitespaceTokenType, exports.EnumToken.SemiColonTokenType, exports.EnumToken.ColonTokenType, exports.EnumToken.BlockStartTokenType,
60466046
exports.EnumToken.BlockStartTokenType, exports.EnumToken.AttrStartTokenType, exports.EnumToken.AttrEndTokenType, exports.EnumToken.StartParensTokenType, exports.EnumToken.EndParensTokenType,
60476047
exports.EnumToken.CommaTokenType, exports.EnumToken.GtTokenType, exports.EnumToken.LtTokenType, exports.EnumToken.GteTokenType, exports.EnumToken.LteTokenType, exports.EnumToken.CommaTokenType,
@@ -6555,13 +6555,7 @@
65556555
throw new Error('empty string?');
65566556
}
65576557
if (hint != null) {
6558-
return ([
6559-
exports.EnumToken.WhitespaceTokenType, exports.EnumToken.SemiColonTokenType, exports.EnumToken.ColonTokenType, exports.EnumToken.BlockStartTokenType,
6560-
exports.EnumToken.BlockStartTokenType, exports.EnumToken.AttrStartTokenType, exports.EnumToken.AttrEndTokenType, exports.EnumToken.StartParensTokenType, exports.EnumToken.EndParensTokenType,
6561-
exports.EnumToken.CommaTokenType, exports.EnumToken.GtTokenType, exports.EnumToken.LtTokenType, exports.EnumToken.GteTokenType, exports.EnumToken.LteTokenType, exports.EnumToken.CommaTokenType,
6562-
exports.EnumToken.StartMatchTokenType, exports.EnumToken.EndMatchTokenType, exports.EnumToken.IncludeMatchTokenType, exports.EnumToken.DashMatchTokenType, exports.EnumToken.ContainMatchTokenType,
6563-
exports.EnumToken.EOFTokenType
6564-
].includes(hint) ? { typ: hint } : { typ: hint, val });
6558+
return enumTokenHints.has(hint) ? { typ: hint } : { typ: hint, val };
65656559
}
65666560
if (val == ' ') {
65676561
return { typ: exports.EnumToken.WhitespaceTokenType };

dist/index.cjs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6039,7 +6039,7 @@ const BadTokensTypes = [
60396039
exports.EnumToken.BadUrlTokenType,
60406040
exports.EnumToken.BadStringTokenType
60416041
];
6042-
new Set([
6042+
const enumTokenHints = new Set([
60436043
exports.EnumToken.WhitespaceTokenType, exports.EnumToken.SemiColonTokenType, exports.EnumToken.ColonTokenType, exports.EnumToken.BlockStartTokenType,
60446044
exports.EnumToken.BlockStartTokenType, exports.EnumToken.AttrStartTokenType, exports.EnumToken.AttrEndTokenType, exports.EnumToken.StartParensTokenType, exports.EnumToken.EndParensTokenType,
60456045
exports.EnumToken.CommaTokenType, exports.EnumToken.GtTokenType, exports.EnumToken.LtTokenType, exports.EnumToken.GteTokenType, exports.EnumToken.LteTokenType, exports.EnumToken.CommaTokenType,
@@ -6553,13 +6553,7 @@ function getTokenType(val, hint) {
65536553
throw new Error('empty string?');
65546554
}
65556555
if (hint != null) {
6556-
return ([
6557-
exports.EnumToken.WhitespaceTokenType, exports.EnumToken.SemiColonTokenType, exports.EnumToken.ColonTokenType, exports.EnumToken.BlockStartTokenType,
6558-
exports.EnumToken.BlockStartTokenType, exports.EnumToken.AttrStartTokenType, exports.EnumToken.AttrEndTokenType, exports.EnumToken.StartParensTokenType, exports.EnumToken.EndParensTokenType,
6559-
exports.EnumToken.CommaTokenType, exports.EnumToken.GtTokenType, exports.EnumToken.LtTokenType, exports.EnumToken.GteTokenType, exports.EnumToken.LteTokenType, exports.EnumToken.CommaTokenType,
6560-
exports.EnumToken.StartMatchTokenType, exports.EnumToken.EndMatchTokenType, exports.EnumToken.IncludeMatchTokenType, exports.EnumToken.DashMatchTokenType, exports.EnumToken.ContainMatchTokenType,
6561-
exports.EnumToken.EOFTokenType
6562-
].includes(hint) ? { typ: hint } : { typ: hint, val });
6556+
return enumTokenHints.has(hint) ? { typ: hint } : { typ: hint, val };
65636557
}
65646558
if (val == ' ') {
65656559
return { typ: exports.EnumToken.WhitespaceTokenType };

dist/lib/parser/parse.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const BadTokensTypes = [
1616
EnumToken.BadUrlTokenType,
1717
EnumToken.BadStringTokenType
1818
];
19-
new Set([
19+
const enumTokenHints = new Set([
2020
EnumToken.WhitespaceTokenType, EnumToken.SemiColonTokenType, EnumToken.ColonTokenType, EnumToken.BlockStartTokenType,
2121
EnumToken.BlockStartTokenType, EnumToken.AttrStartTokenType, EnumToken.AttrEndTokenType, EnumToken.StartParensTokenType, EnumToken.EndParensTokenType,
2222
EnumToken.CommaTokenType, EnumToken.GtTokenType, EnumToken.LtTokenType, EnumToken.GteTokenType, EnumToken.LteTokenType, EnumToken.CommaTokenType,
@@ -530,13 +530,7 @@ function getTokenType(val, hint) {
530530
throw new Error('empty string?');
531531
}
532532
if (hint != null) {
533-
return ([
534-
EnumToken.WhitespaceTokenType, EnumToken.SemiColonTokenType, EnumToken.ColonTokenType, EnumToken.BlockStartTokenType,
535-
EnumToken.BlockStartTokenType, EnumToken.AttrStartTokenType, EnumToken.AttrEndTokenType, EnumToken.StartParensTokenType, EnumToken.EndParensTokenType,
536-
EnumToken.CommaTokenType, EnumToken.GtTokenType, EnumToken.LtTokenType, EnumToken.GteTokenType, EnumToken.LteTokenType, EnumToken.CommaTokenType,
537-
EnumToken.StartMatchTokenType, EnumToken.EndMatchTokenType, EnumToken.IncludeMatchTokenType, EnumToken.DashMatchTokenType, EnumToken.ContainMatchTokenType,
538-
EnumToken.EOFTokenType
539-
].includes(hint) ? { typ: hint } : { typ: hint, val });
533+
return enumTokenHints.has(hint) ? { typ: hint } : { typ: hint, val };
540534
}
541535
if (val == ' ') {
542536
return { typ: EnumToken.WhitespaceTokenType };

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tbela99/css-parser",
33
"description": "CSS parser for node and the browser",
4-
"version": "0.5.0-alpha.1",
4+
"version": "0.5.0",
55
"exports": {
66
".": "./dist/node/index.js",
77
"./umd": "./dist/index-umd-web.js",
@@ -14,7 +14,7 @@
1414
"build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
1515
"test": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
1616
"test:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
17-
"test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
17+
"test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\" test/allFiles.js",
1818
"test:web-cov": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
1919
"profile": "node --enable-source-maps --inspect-brk test/inspect.js",
2020
"debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."

src/lib/parser/declaration/list.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ export class PropertyList {
2323
constructor(options: PropertyListOptions = {}) {
2424

2525
this.options = options;
26-
// for (const key of Object.keys(this.options)) {
27-
//
28-
// if (key in options) {
29-
//
30-
// // @ts-ignore
31-
// this.options[key] = options[key];
32-
// }
33-
// }
34-
3526
this.declarations = new Map<string, AstNode | PropertySet | PropertyMap>;
3627
}
3728

src/lib/parser/parse.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -751,14 +751,6 @@ async function parseNode(results: TokenizeResult[], context: AstRuleList, stats:
751751
}
752752
}
753753

754-
// function mapToken(token: TokenizeResult, map: Map<Token, Position>): Token {
755-
//
756-
// const node: Token = getTokenType(token.token, token.hint);
757-
//
758-
// map.set(node, token.position);
759-
// return node;
760-
// }
761-
762754
export async function parseDeclarations(src: string, options: ParserOptions = {}): Promise<AstDeclaration[]> {
763755

764756
return doParse(`.x{${src}`, options).then((result: ParseResult) => <AstDeclaration[]>(<AstRule>result.ast.chi[0]).chi);
@@ -787,13 +779,7 @@ function getTokenType(val: string, hint?: EnumToken): Token {
787779

788780
if (hint != null) {
789781

790-
return <Token>([
791-
EnumToken.WhitespaceTokenType, EnumToken.SemiColonTokenType, EnumToken.ColonTokenType, EnumToken.BlockStartTokenType,
792-
EnumToken.BlockStartTokenType, EnumToken.AttrStartTokenType, EnumToken.AttrEndTokenType, EnumToken.StartParensTokenType, EnumToken.EndParensTokenType,
793-
EnumToken.CommaTokenType, EnumToken.GtTokenType, EnumToken.LtTokenType, EnumToken.GteTokenType, EnumToken.LteTokenType, EnumToken.CommaTokenType,
794-
EnumToken.StartMatchTokenType, EnumToken.EndMatchTokenType, EnumToken.IncludeMatchTokenType, EnumToken.DashMatchTokenType, EnumToken.ContainMatchTokenType,
795-
EnumToken.EOFTokenType
796-
].includes(hint) ? {typ: hint} : {typ: hint, val});
782+
return enumTokenHints.has(hint) ? <Token>{typ: hint} : <Token>{typ: hint, val};
797783
}
798784

799785
if (val == ' ') {

test/allFiles.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {readdir} from "node:fs/promises";
2+
import {load, transform} from "../dist/node/index.js";
3+
4+
const baseDir = import.meta.dirname + '/files/css/';
5+
6+
for (const file of await readdir(baseDir)) {
7+
8+
let message = `--> file ${file}: `;
9+
const result = await load(baseDir + file, import.meta.dirname).then(css => transform(css, {src: baseDir + file, minify: true, sourcemap: true, nestingRules: true, resolveImport: true}));
10+
11+
message += `ratio ${(100 * (1 - result.stats.bytesOut / result.stats.bytesIn)).toFixed(2)}%`;
12+
13+
for (const key in result.stats) {
14+
15+
// @ts-ignore
16+
message += ` / ${key}: ${result.stats[key]}`;
17+
}
18+
19+
console.error(message);
20+
}

test/inspect.mjs renamed to test/inspect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {readFileSync} from "fs";
2-
import {dirname} from "path";
1+
import {readFileSync} from "node:fs";
2+
import {dirname} from "node:path";
33
import {transform} from '../dist/node/index.js';
44

55
const css = readFileSync(dirname(new URL(import.meta.url).pathname) + '/files/css/tailwind.css', {encoding: 'utf-8'});

0 commit comments

Comments
 (0)