Skip to content

Commit 282b61d

Browse files
authored
Convert @emotion/css-prettifier's source code to TypeScript (#3278)
* Convert `@emotion/css-prettifier`'s source code to TypeScript * use .ts * fixed changeset content
1 parent e1bf17e commit 282b61d

File tree

8 files changed

+81
-57
lines changed

8 files changed

+81
-57
lines changed

.changeset/grumpy-suits-cough.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@emotion/use-insertion-effect-with-fallbacks': minor
33
---
44

5-
Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.
5+
Source code has been migrated to TypeScript.

.changeset/young-ears-give.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/css-prettifier': minor
3+
---
4+
5+
Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.

packages/css-prettifier/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"author": "Mateusz Burzyński <[email protected]> (https://github.com/Andarist)",
99
"main": "dist/emotion-css-prettifier.cjs.js",
1010
"module": "dist/emotion-css-prettifier.esm.js",
11+
"types": "dist/emotion-css-prettifier.cjs.d.ts",
1112
"exports": {
1213
".": {
1314
"types": {
@@ -20,19 +21,20 @@
2021
},
2122
"./package.json": "./package.json"
2223
},
23-
"types": "types/index.d.ts",
2424
"license": "MIT",
2525
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/css-prettifier",
2626
"dependencies": {
2727
"@emotion/memoize": "^0.9.0",
2828
"stylis": "4.2.0"
2929
},
30+
"devDependencies": {
31+
"@types/stylis": "^4.2.6"
32+
},
3033
"publishConfig": {
3134
"access": "public"
3235
},
3336
"files": [
3437
"src",
35-
"dist",
36-
"types/*.d.ts"
38+
"dist"
3739
]
3840
}

packages/css-prettifier/src/index.d.ts

-2
This file was deleted.

packages/css-prettifier/src/index.js

-50
This file was deleted.

packages/css-prettifier/src/index.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import memoize from '@emotion/memoize'
2+
import {
3+
compile,
4+
serialize,
5+
combine,
6+
tokenize,
7+
type Middleware,
8+
type Element
9+
} from 'stylis'
10+
11+
// adjusted https://github.com/thysultan/stylis.js/blob/68b9043427c177b95a0fd6a2a13f5b636bf80236/src/Serializer.js#L26-L34
12+
const prettyStringify = memoize(
13+
(indentation): Middleware =>
14+
(element, index, children, callback) => {
15+
switch (element.type) {
16+
case '@import':
17+
return (element.return = element.return || element.value) + '\n\n'
18+
case 'decl':
19+
return (element.return =
20+
element.return || `${element.props}: ${element.children};\n`)
21+
case 'comm':
22+
return ''
23+
case '@media':
24+
case '@supports':
25+
element.value = combine(
26+
tokenize(element.value),
27+
(value, index, children) => {
28+
// (
29+
if (value.charCodeAt(0) === 40 && children[index - 1] !== ' ') {
30+
return ' ' + value
31+
}
32+
return value
33+
}
34+
)
35+
break
36+
case 'rule':
37+
element.value = (element.props as string[]).join(
38+
element.root &&
39+
(element.root.type === '@keyframes' ||
40+
element.root.type === '@-webkit-keyframes')
41+
? ', '
42+
: ',\n'
43+
)
44+
}
45+
46+
const serialized = serialize(element.children as Element[], callback)
47+
return serialized.length
48+
? (element.return =
49+
element.value +
50+
' {\n' +
51+
serialized
52+
.trim()
53+
.replace(/^/gm, indentation)
54+
.replace(/^\s+$/gm, '') +
55+
'\n}\n\n')
56+
: ''
57+
}
58+
)
59+
60+
export default function prettify(styles: string, indentation = ' ') {
61+
return serialize(compile(styles), prettyStringify(indentation)).trim()
62+
}

packages/css-prettifier/types/index.d.ts

-1
This file was deleted.

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,7 @@ __metadata:
27242724
resolution: "@emotion/css-prettifier@workspace:packages/css-prettifier"
27252725
dependencies:
27262726
"@emotion/memoize": ^0.9.0
2727+
"@types/stylis": ^4.2.6
27272728
stylis: 4.2.0
27282729
languageName: unknown
27292730
linkType: soft
@@ -6467,6 +6468,13 @@ __metadata:
64676468
languageName: node
64686469
linkType: hard
64696470

6471+
"@types/stylis@npm:^4.2.6":
6472+
version: 4.2.6
6473+
resolution: "@types/stylis@npm:4.2.6"
6474+
checksum: 3a1685f2b465eb943805252b5b2934fc8054fe8706f76e3e28544f69532296f42c042d0b402fadc486ef8e86cc3d8a2c66d2345241e0af8e07974dd36d85a79c
6475+
languageName: node
6476+
linkType: hard
6477+
64706478
"@types/tapable@npm:^1, @types/tapable@npm:^1.0.5":
64716479
version: 1.0.8
64726480
resolution: "@types/tapable@npm:1.0.8"

0 commit comments

Comments
 (0)