Skip to content

Commit 3367aac

Browse files
committed
2.0.0
1 parent 912dd70 commit 3367aac

File tree

15 files changed

+112
-67
lines changed

15 files changed

+112
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ coverage
22
dist
33
node_modules
44
package-lock.json
5+
yarn.lock

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes to CSS Tokenizer
22

3+
### 2.0.0 (May 11, 2021)
4+
5+
- Supports function token (previously an identifier token followed by a parenthesis delimiter token).
6+
- Changed from default export to named export (`tokenizer`).
7+
38
### 1.0.0 (September 26, 2020)
49

510
Initial version

README.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CSS Tokenizer
22

33
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/tokenizer.svg" height="20">](https://www.npmjs.com/package/@csstools/tokenizer)
4-
[<img alt="build status" src="https://img.shields.io/travis/csstools/tokenizer/master.svg" height="20">](https://travis-ci.org/github/csstools/tokenizer)
4+
[<img alt="build status" src="https://img.shields.io/travis/csstools/tokenizer/main.svg" height="20">](https://travis-ci.org/github/csstools/tokenizer)
55
[<img alt="code coverage" src="https://img.shields.io/codecov/c/github/csstools/tokenizer" height="20">](https://codecov.io/gh/csstools/tokenizer)
66
[<img alt="issue tracker" src="https://img.shields.io/github/issues/csstools/tokenizer.svg" height="20">](https://github.com/csstools/tokenizer/issues)
77
[<img alt="pull requests" src="https://img.shields.io/github/issues-pr/csstools/tokenizer.svg" height="20">](https://github.com/csstools/tokenizer/pulls)
@@ -29,17 +29,17 @@ npm install @csstools/tokenizer
2929
Tokenize CSS in JavaScript:
3030

3131
```js
32-
import cssTokenizer from '@csstools/tokenizer'
32+
import { tokenizer } from '@csstools/tokenizer'
3333

34-
const tokens = Array.from(cssTokenizer(cssText)) // an array of css tokens
34+
const tokens = Array.from(tokenizer(cssText)) // an array of css tokens
3535
```
3636

3737
Tokenize CSS in _classical_ NodeJS:
3838

3939
```js
40-
const cssTokenizer = require('@csstools/tokenizer')
40+
const { tokenizer } = require('@csstools/tokenizer')
4141

42-
let iterator = cssTokenizer(cssText), iteration
42+
let iterator = tokenizer(cssText), iteration
4343

4444
while (!(iteration = iterator()).done) {
4545
console.log(iteration.value) // logs an individual css token
@@ -51,9 +51,9 @@ Tokenize CSS in client-side scripts:
5151
```html
5252
<script type="module">
5353
54-
import cssTokenizer from 'https://unpkg.com/@csstools/tokenizer?module'
54+
import { tokenizer } from 'https://unpkg.com/@csstools/tokenizer?module'
5555
56-
const tokens = Array.from(cssTokenizer(cssText)) // an array of css tokens
56+
const tokens = Array.from(tokenizer(cssText)) // an array of css tokens
5757
5858
</script>
5959
```
@@ -78,16 +78,25 @@ The CSS tokenizer separates a string of CSS into tokens represented as an array.
7878
/** Position in the string at which the token was retrieved. */
7979
number,
8080

81-
/** Negative number that identifies the kind of token. */
82-
number,
83-
84-
/** Opening token content, like the opening of a comment or the quotation mark of a string. */
81+
/** Number identifying the kind of token. */
82+
| -2 // Comment
83+
| -3 // Space
84+
| -4 // Identifier
85+
| -5 // Function
86+
| -6 // At-Identifier
87+
| -7 // Hash
88+
| -8 // String
89+
| -9 // Numeric
90+
| number // Delimiter (character code)
91+
,
92+
93+
/** Lead content, like the opening of a comment or the quotation mark of a string. */
8594
string,
8695

87-
/** Main token content, like the numbers before a unit, or the letters after an at-sign. */
96+
/** Main content, like the numbers before a unit, or the letters after an at-sign. */
8897
string,
8998

90-
/** Closing token content, like the unit of a number, or the closing of a comment. */
99+
/** Tail content, like the unit of a number, or the closing of a comment. */
91100
string,
92101
]
93102
```
@@ -104,22 +113,23 @@ The **first** number represents the position at which the token was read. The **
104113

105114
## Benchmarks
106115

107-
As of September 26, 2020, these benchmarks were averaged from my local machine:
116+
As of May 11, 2021, these benchmarks were averaged from my local machine:
108117

109118
```
110119
Benchmark: Tailwind CSS
111120
┌──────────────────────────────────────────────────┬───────┬────────┬────────┐
112121
│ (index) │ ms │ ms/50k │ tokens │
113122
├──────────────────────────────────────────────────┼───────┼────────┼────────┤
114-
│ PostCSS x 12.38 ops/sec ±3.33% (35 runs sampled) │ 80.796.97579896
115-
│ Develop x 12.07 ops/sec ±1.54% (34 runs sampled) │ 82.866.17670972
123+
│ PostCSS x 11.86 ops/sec ±2.64% (35 runs sampled) │ 84.314.52933299
124+
│ Develop x 14.98 ops/sec ±1.04% (42 runs sampled) │ 66.753.53946324
116125
└──────────────────────────────────────────────────┴───────┴────────┴────────┘
126+
117127
Benchmark: Bootstrap
118128
┌────────────────────────────────────────────────┬──────┬────────┬────────┐
119129
│ (index) │ ms │ ms/50k │ tokens │
120130
├────────────────────────────────────────────────┼──────┼────────┼────────┤
121-
│ PostCSS x 230 ops/sec ±0.50% (89 runs sampled) │ 4.354.3749807
122-
│ Develop x 142 ops/sec ±0.76% (80 runs sampled) │ 7.045.9259502
131+
│ PostCSS x 369 ops/sec ±0.12% (95 runs sampled) │ 2.712.7749006
132+
│ Develop x 272 ops/sec ±0.10% (93 runs sampled) │ 3.683.2257141
123133
└────────────────────────────────────────────────┴──────┴────────┴────────┘
124134
```
125135

cmd/_.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const spawnNode = (cmd, args, opts) => spawn('node', [path.resolve(cm
1616
export const spawnTsc = (args, opts) => spawnNode('node_modules/typescript/lib/tsc.js', args, opts)
1717
export const spawnTscWatch = (args, opts) => spawnNode('node_modules/tsc-watch/lib/tsc-watch.js', args, opts)
1818

19-
export const rmdir = (...paths) => fs.rmdirSync(resolve(...paths), { recursive: true })
19+
export const rmdir = (...paths) => (fs.rmSync || fs.rmdirSync)(resolve(...paths), { force: true, recursive: true })
2020

2121
export const question = query => new Promise(resolve => {
2222
const rl = readline.createInterface(question.options)

cmd/test.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ console.log(`Starting testing...`)
88
console.log()
99

1010
_.rmdir('coverage')
11+
1112
_.spawnNode(jestBin, [
1213
'--colors',
1314
'--passWithNoTests',

jest.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export default /** @type {import('@jest/types').Config.InitialOptions} */ ({
1111
'js',
1212
'ts'
1313
],
14+
moduleNameMapper: {
15+
"^\\./(.*)\\.js$": [
16+
"./$1.js",
17+
"./$1.ts"
18+
]
19+
},
1420
roots: [
1521
'<rootDir>/src'
1622
],

package.json

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
"$schema": "https://json.schemastore.org/package",
33
"name": "@csstools/tokenizer",
44
"description": "Tokenize CSS according to the CSS Syntax",
5-
"version": "1.0.0",
5+
"version": "2.0.0",
66
"type": "module",
77
"main": "dist/index.cjs",
88
"module": "dist/index.mjs",
99
"types": "dist/index.d.ts",
1010
"unpkg": "dist/index.js",
11+
"exports": {
12+
".": {
13+
"require": "./dist/index.cjs",
14+
"import": "./dist/index.mjs",
15+
"types": "./dist/index.d.ts"
16+
}
17+
},
1118
"files": [
1219
"dist"
1320
],
1421
"engines": {
15-
"node": "^10 || ^12 || ^14"
22+
"node": ">= 10"
1623
},
1724
"scripts": {
1825
"build": "node cmd/build.mjs",
@@ -29,24 +36,25 @@
2936
"access": "public"
3037
},
3138
"devDependencies": {
32-
"@babel/preset-env": "^7.11.5",
33-
"@babel/preset-typescript": "^7.10.4",
34-
"@rollup/plugin-babel": "^5.2.1",
35-
"@types/benchmark": "^1.0.33",
36-
"@types/jest": "^26.0.14",
37-
"@types/node": "^14.11.2",
39+
"@babel/core": "^7.14.0",
40+
"@babel/preset-env": "^7.14.1",
41+
"@babel/preset-typescript": "^7.13.0",
42+
"@rollup/plugin-babel": "^5.3.0",
43+
"@types/benchmark": "^2.1.0",
44+
"@types/jest": "^26.0.23",
45+
"@types/node": "^15.0.2",
3846
"benchmark": "^2.1.4",
39-
"bootstrap": "^4.5.2",
40-
"codecov": "^3.7.2",
41-
"jest": "^26.4.2",
47+
"bootstrap": "^5.0.0",
48+
"codecov": "^3.8.2",
49+
"jest": "^26.6.3",
4250
"magic-string": "^0.25.7",
43-
"postcss": "^8.0.9",
44-
"rollup": "^2.28.2",
51+
"postcss": "^8.2.15",
52+
"rollup": "^2.47.0",
4553
"rollup-plugin-bundle-size": "^1.0.3",
4654
"rollup-plugin-terser": "^7.0.2",
47-
"tailwindcss": "^1.8.10",
55+
"tailwindcss": "^2.1.2",
4856
"tsc-watch": "^4.2.9",
49-
"tslib": "^2.0.1",
50-
"typescript": "^4.0.3"
57+
"tslib": "^2.2.0",
58+
"typescript": "^4.2.4"
5159
}
5260
}

rollup.config.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const terserOptions = {
2424
const resolveJsTsExtension = {
2525
name: 'resolve-js-ts-extension',
2626
resolveId(/** @type {string} */ importee, /** @type {string} */ importer) {
27-
if (importer?.endsWith('.ts') && importee) return path.join(path.dirname(importer), importee + '.ts')
27+
if (importer?.endsWith('.ts') && importee) return path.join(path.dirname(importer), importee).replace(/(\.js)?$/, '.ts')
2828
return null
2929
}
3030
}
@@ -45,7 +45,8 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
4545
{
4646
input: './src/index.ts',
4747
output: {
48-
exports: 'default',
48+
esModule: false,
49+
exports: 'named',
4950
file: './dist/index.mjs',
5051
format: 'esm',
5152
strict: true,
@@ -60,7 +61,8 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
6061
{
6162
input: './src/index.ts',
6263
output: {
63-
exports: 'default',
64+
esModule: false,
65+
exports: 'named',
6466
file: './dist/index.cjs',
6567
format: 'cjs',
6668
strict: true,
@@ -73,8 +75,9 @@ const config = /** @type {import('rollup').NormalizedInputOptions[]} */ ([
7375
],
7476
},
7577
{
76-
input: './src/index.ts',
78+
input: './src/index.iife.ts',
7779
output: {
80+
esModule: false,
7881
exports: 'default',
7982
file: './dist/index.js',
8083
format: 'iife',

src/index.benchmark.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs'
22
import Benchmark from 'benchmark'
3-
import tokenizerPostCSS from 'postcss/lib/tokenize'
4-
import tokenizerDevelop from './index'
3+
import tokenizerPostCSS from 'postcss/lib/tokenize.js'
4+
import tokenizerDevelop from './index.js'
55

66
const counter = { value: 0 }
77

src/index.iife.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { tokenizer as default } from './index.js'

0 commit comments

Comments
 (0)