Skip to content

Commit b7e5450

Browse files
committed
next
1 parent 045d0bf commit b7e5450

19 files changed

Lines changed: 809 additions & 920 deletions

README.md

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ Tokenize CSS in JavaScript:
3131
```js
3232
import { tokenize } from '@csstools/tokenizer'
3333

34-
for (const token of tokenize(cssText)) {
34+
tokenize(cssText, (token) => {
3535
console.log(token) // logs an individual CSSToken
36-
}
36+
})
3737
```
3838

3939
Tokenize CSS in _classical_ NodeJS:
4040

4141
```js
4242
const { tokenizer } = require('@csstools/tokenizer')
4343

44-
let iterator = tokenizer(cssText), iteration
45-
46-
while (!(iteration = iterator()).done) {
47-
console.log(iteration.value) // logs an individual CSSToken
48-
}
44+
tokenize(cssText, (token) => {
45+
console.log(token) // logs an individual CSSToken
46+
})
4947
```
5048

5149
Tokenize CSS in client-side scripts:
@@ -62,17 +60,6 @@ for (const token of tokenize(cssText)) {
6260
</script>
6361
```
6462

65-
Tokenize CSS in _classical_ client-side scripts:
66-
67-
```html
68-
<script src="http://unpkg.com/@csstools/tokenizer"></script>
69-
<script>
70-
71-
const tokens = Array.from(tokenizeCSS(cssText)) // an array of CSSTokens
72-
73-
</script>
74-
```
75-
7663
## How it works
7764

7865
The CSS tokenizer separates a string of CSS into tokens.
@@ -82,58 +69,43 @@ interface CSSToken {
8269
/** Position in the string at which the token was retrieved. */
8370
tick: number
8471

85-
/** Number identifying the kind of token. */
86-
type:
87-
| 1 // Symbol
88-
| 2 // Comment
89-
| 3 // Space
90-
| 4 // Word
91-
| 5 // Function
92-
| 6 // Atword
93-
| 7 // Hash
94-
| 8 // String
95-
| 9 // Number
96-
97-
/** Code, like the character code of a symbol, or the character code of the opening parenthesis of a function. */
98-
code: number
99-
100-
/** Lead, like the opening of a comment, the quotation mark of a string, or the name of a function. */
101-
lead: string,
102-
103-
/** Data, like the numbers before a unit, the word after an at-sign, or the opening parenthesis of a Function. */
104-
data: string,
105-
106-
/** Tail, like the unit after a number, or the closing of a comment. */
107-
tail: string,
72+
/** Number identifying the kind of token; or the character code of a symbol. */
73+
type: number
74+
75+
/** Data, the number of characters in the token. */
76+
data: number,
77+
78+
/** Tail, the number of characters in a unit after a number, or in the closing of a comment. */
79+
edge: string,
10880
}
10981
```
11082

11183
As an example, the CSS string `@media` would become a **Atword** token where `@` and `media` are recognized as distinct parts of that token. As another example, the CSS string `5px` would become a **Number** token where `5` and `px` are recognized as distinct parts of that token. As a final example, the string `5px 10px` would become 3 tokens; the **Number** as mentioned before (`5px`), a **Space** token that represents a single space (` `), and then another **Number** token (`10px`).
11284

11385
## Benchmarks
11486

115-
As of August 23, 2021, these benchmarks were averaged from my local machine:
87+
As of April 11, 2022, these benchmarks were reported from my local machine:
11688

11789
```
11890
Benchmark: Tailwind CSS
11991
┌────────────────────────────────────────────────────┬───────┬────────┬────────┐
12092
│ (index) │ ms │ ms/50k │ tokens │
12193
├────────────────────────────────────────────────────┼───────┼────────┼────────┤
122-
│ CSSTree 1 x 35.04 ops/sec ±6.55% (64 runs sampled) │ 28.54 │ 1.51 │ 946205 │
123-
│ CSSTree 2 x 41.76 ops/sec ±7.57% (58 runs sampled) │ 23.95 │ 1.27 │ 946205 │
124-
│ PostCSS 8 x 14.18 ops/sec ±3.31% (40 runs sampled) │ 70.54 │ 3.77 │ 935282 │
125-
Tokenizer x 17.40 ops/sec ±0.98% (48 runs sampled) │ 57.48 3.04946206
94+
│ CSSTree 1 x 35.86 ops/sec ±6.82% (65 runs sampled) │ 27.88 │ 1.47 │ 946205 │
95+
│ CSSTree 2 x 43.15 ops/sec ±7.57% (60 runs sampled) │ 23.17 │ 1.22 │ 946205 │
96+
│ PostCSS 8 x 14.82 ops/sec ±2.33% (42 runs sampled) │ 67.46 │ 3.61 │ 935282 │
97+
CSS Tools x 36.76 ops/sec ±0.14% (65 runs sampled) │ 27.2 1.44946205
12698
└────────────────────────────────────────────────────┴───────┴────────┴────────┘
12799
128-
Benchmark: Bootstrap
129-
┌──────────────────────────────────────────────────┬──────┬────────┬────────┐
130-
(index) │ ms │ ms/50k │ tokens │
131-
├──────────────────────────────────────────────────┼──────┼────────┼────────┤
132-
│ CSSTree 1 x 600 ops/sec ±0.87% (96 runs sampled) │ 1.67 │ 1.4159236
133-
│ CSSTree 2 x 695 ops/sec ±0.08% (100 runs sampled) │ 1.44 │ 1.21 │ 59236
134-
│ PostCSS 8 x 432 ops/sec ±0.94% (94 runs sampled) │ 2.31 │ 2.2651170
135-
Tokenizer x 288 ops/sec ±0.40% (93 runs sampled) │ 3.482.9359237
136-
└──────────────────────────────────────────────────┴──────┴────────┴────────┘
100+
Benchmark: Bootstrap CSS
101+
┌──────────────────────────────────────────────────┬──────┬────────┬────────┐
102+
│ (index) │ ms │ ms/50k │ tokens │
103+
├──────────────────────────────────────────────────┼──────┼────────┼────────┤
104+
│ CSSTree 1 x 608 ops/sec ±0.26% (97 runs sampled) │ 1.65 │ 1.3859543
105+
│ CSSTree 2 x 702 ops/sec ±0.16% (97 runs sampled) │ 1.42 │ 1.2 │ 59543
106+
│ PostCSS 8 x 440 ops/sec ±0.10% (94 runs sampled) │ 2.27 │ 2.2151453
107+
CSS Tools x 623 ops/sec ±0.15% (97 runs sampled) │ 1.611.3559543
108+
└──────────────────────────────────────────────────┴──────┴────────┴────────┘
137109
```
138110

139111
## Development
@@ -161,7 +133,7 @@ npm run benchmark
161133

162134
The **test** command tests the coverage and accuracy of the tokenizer.
163135

164-
As of September 26, 2020, this tokenizer has 100% test coverage:
136+
As of April 18, 2022, this tokenizer maintains 100% test coverage:
165137

166138
```sh
167139
npm run test

cmd/benchmark.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as _ from './_.mjs'
22

33
const isWatch = process.argv.includes('--watch')
44

5-
console.log(`Starting compilation...`)
5+
console.log(`Building...`)
66

77
_.rmdir('dist')
88

cmd/build.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as _ from './_.mjs'
22

33
const isWatch = process.argv.includes('--watch')
44

5-
console.log(`Starting compilation...`)
5+
console.log(`Building...`)
66

77
_.rmdir('dist')
88
_.mkdir('dist')

cmd/publish.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import './build.mjs'
44

55
const isDryRun = Boolean(process?.env?.npm_config_dryRun)
66

7-
console.log(), console.log('Starting publishing...')
7+
console.log(), console.log('Publishing...')
88

99
_.question.options = { input: process.stdin, output: process.stdout }
1010

cmd/test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const isWatch = process.argv.includes('--watch')
44

55
const jestBin = 'node_modules/jest/bin/jest.js'
66

7-
console.log(`Starting testing...`)
7+
console.log(`Testing...`)
88
console.log()
99

1010
_.rmdir('coverage')

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,27 @@
4646
"access": "public"
4747
},
4848
"devDependencies": {
49-
"@babel/core": "7.15.0",
50-
"@babel/preset-env": "7.15.0",
51-
"@babel/preset-typescript": "7.15.0",
52-
"@rollup/plugin-babel": "5.3.0",
49+
"@babel/core": "7.17.9",
50+
"@babel/preset-env": "7.16.11",
51+
"@babel/preset-typescript": "7.16.7",
52+
"@rollup/plugin-babel": "5.3.1",
5353
"@types/benchmark": "2.1.1",
54-
"@types/jest": "27.0.1",
55-
"@types/node": "16.7.4",
54+
"@types/jest": "27.4.1",
55+
"@types/node": "17.0.23",
5656
"benchmark": "2.1.4",
57-
"bootstrap": "5.1.0",
57+
"bootstrap": "5.1.3",
5858
"codecov": "3.8.3",
5959
"css-tree1": "npm:css-tree@^1.1.3",
60-
"css-tree2": "npm:css-tree@^2.0.1",
61-
"jest": "27.1.0",
62-
"magic-string": "0.25.7",
63-
"postcss": "8.3.6",
64-
"rollup": "2.56.3",
60+
"css-tree2": "npm:css-tree@^2.1.0",
61+
"jest": "27.5.1",
62+
"magic-string": "0.26.1",
63+
"postcss": "8.4.12",
64+
"rollup": "2.70.1",
6565
"rollup-plugin-bundle-size": "1.0.3",
6666
"rollup-plugin-terser": "7.0.2",
6767
"tailwindcss": "2.2.8",
68-
"tsc-watch": "4.5.0",
68+
"tsc-watch": "5.0.3",
6969
"tslib": "2.3.1",
70-
"typescript": "4.4.2"
70+
"typescript": "4.6.3"
7171
}
7272
}

rollup.config.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ const resolveJsTsExtension = {
2929
}
3030
}
3131

32-
const reduceImpact = {
32+
const reduceImpact = (trim = 0) => ({
3333
name: 'reduce-impact',
3434
renderChunk(code) {
3535
const str = new MagicString(code)
36-
str.trim(';').remove(0, 4)
36+
37+
str.trim(';').remove(0, trim)
38+
3739
return {
3840
code: str.toString(),
3941
map: str.generateMap({ hires: true }),
4042
}
4143
}
42-
}
44+
})
4345

4446
const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
4547
{
@@ -55,6 +57,8 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
5557
plugins: [
5658
resolveJsTsExtension,
5759
babel({ ...babelOptions }),
60+
terser({ ...terserOptions }),
61+
reduceImpact(0),
5862
bundleSize()
5963
],
6064
},
@@ -71,6 +75,8 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
7175
plugins: [
7276
resolveJsTsExtension,
7377
babel({ ...babelOptions }),
78+
terser({ ...terserOptions }),
79+
reduceImpact(13),
7480
bundleSize()
7581
],
7682
},
@@ -89,7 +95,7 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
8995
resolveJsTsExtension,
9096
babel({ ...babelOptions }),
9197
terser({ ...terserOptions }),
92-
reduceImpact,
98+
reduceImpact(4),
9399
bundleSize(),
94100
]
95101
},
@@ -106,6 +112,8 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
106112
plugins: [
107113
resolveJsTsExtension,
108114
babel({ ...babelOptions }),
115+
terser({ ...terserOptions }),
116+
reduceImpact(0),
109117
bundleSize()
110118
],
111119
},
@@ -122,6 +130,8 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
122130
plugins: [
123131
resolveJsTsExtension,
124132
babel({ ...babelOptions }),
133+
terser({ ...terserOptions }),
134+
reduceImpact(13),
125135
bundleSize()
126136
],
127137
},
@@ -140,7 +150,7 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
140150
resolveJsTsExtension,
141151
babel({ ...babelOptions }),
142152
terser({ ...terserOptions }),
143-
reduceImpact,
153+
reduceImpact(4),
144154
bundleSize(),
145155
]
146156
},

0 commit comments

Comments
 (0)