Skip to content

Commit

Permalink
Convert @emotion/css-prettifier's source code to TypeScript (#3278)
Browse files Browse the repository at this point in the history
* Convert `@emotion/css-prettifier`'s source code to TypeScript

* use .ts

* fixed changeset content
  • Loading branch information
Andarist authored Dec 3, 2024
1 parent e1bf17e commit 282b61d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .changeset/grumpy-suits-cough.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@emotion/use-insertion-effect-with-fallbacks': minor
---

Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.
Source code has been migrated to TypeScript.
5 changes: 5 additions & 0 deletions .changeset/young-ears-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/css-prettifier': minor
---

Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.
8 changes: 5 additions & 3 deletions packages/css-prettifier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"author": "Mateusz Burzyński <mateuszburzynski@gmail.com> (https://github.com/Andarist)",
"main": "dist/emotion-css-prettifier.cjs.js",
"module": "dist/emotion-css-prettifier.esm.js",
"types": "dist/emotion-css-prettifier.cjs.d.ts",
"exports": {
".": {
"types": {
Expand All @@ -20,19 +21,20 @@
},
"./package.json": "./package.json"
},
"types": "types/index.d.ts",
"license": "MIT",
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/css-prettifier",
"dependencies": {
"@emotion/memoize": "^0.9.0",
"stylis": "4.2.0"
},
"devDependencies": {
"@types/stylis": "^4.2.6"
},
"publishConfig": {
"access": "public"
},
"files": [
"src",
"dist",
"types/*.d.ts"
"dist"
]
}
2 changes: 0 additions & 2 deletions packages/css-prettifier/src/index.d.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/css-prettifier/src/index.js

This file was deleted.

62 changes: 62 additions & 0 deletions packages/css-prettifier/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import memoize from '@emotion/memoize'
import {
compile,
serialize,
combine,
tokenize,
type Middleware,
type Element
} from 'stylis'

// adjusted https://github.com/thysultan/stylis.js/blob/68b9043427c177b95a0fd6a2a13f5b636bf80236/src/Serializer.js#L26-L34
const prettyStringify = memoize(
(indentation): Middleware =>
(element, index, children, callback) => {
switch (element.type) {
case '@import':
return (element.return = element.return || element.value) + '\n\n'
case 'decl':
return (element.return =
element.return || `${element.props}: ${element.children};\n`)
case 'comm':
return ''
case '@media':
case '@supports':
element.value = combine(
tokenize(element.value),
(value, index, children) => {
// (
if (value.charCodeAt(0) === 40 && children[index - 1] !== ' ') {
return ' ' + value
}
return value
}
)
break
case 'rule':
element.value = (element.props as string[]).join(
element.root &&
(element.root.type === '@keyframes' ||
element.root.type === '@-webkit-keyframes')
? ', '
: ',\n'
)
}

const serialized = serialize(element.children as Element[], callback)
return serialized.length
? (element.return =
element.value +
' {\n' +
serialized
.trim()
.replace(/^/gm, indentation)
.replace(/^\s+$/gm, '') +
'\n}\n\n')
: ''
}
)

export default function prettify(styles: string, indentation = ' ') {
return serialize(compile(styles), prettyStringify(indentation)).trim()
}
1 change: 0 additions & 1 deletion packages/css-prettifier/types/index.d.ts

This file was deleted.

8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,7 @@ __metadata:
resolution: "@emotion/css-prettifier@workspace:packages/css-prettifier"
dependencies:
"@emotion/memoize": ^0.9.0
"@types/stylis": ^4.2.6
stylis: 4.2.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -6467,6 +6468,13 @@ __metadata:
languageName: node
linkType: hard

"@types/stylis@npm:^4.2.6":
version: 4.2.6
resolution: "@types/stylis@npm:4.2.6"
checksum: 3a1685f2b465eb943805252b5b2934fc8054fe8706f76e3e28544f69532296f42c042d0b402fadc486ef8e86cc3d8a2c66d2345241e0af8e07974dd36d85a79c
languageName: node
linkType: hard

"@types/tapable@npm:^1, @types/tapable@npm:^1.0.5":
version: 1.0.8
resolution: "@types/tapable@npm:1.0.8"
Expand Down

0 comments on commit 282b61d

Please sign in to comment.