Skip to content

Commit

Permalink
Merge pull request #686 from joestrouth1/ts-tailwind
Browse files Browse the repository at this point in the history
Convert tailwind package to TypeScript
  • Loading branch information
mxstbr committed Feb 19, 2020
2 parents 3e975e5 + 314f825 commit 1b72aef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
9 changes: 6 additions & 3 deletions packages/tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
"version": "0.3.0",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"author": "John Otander <johnotander@gmail.com>",
"license": "MIT",
"scripts": {
"prepare": "microbundle --no-compress",
"watch": "microbundle watch --no-compress"
"prepare": "microbundle --no-compress --tsconfig tsconfig.json",
"watch": "microbundle watch --no-compress --tsconfig tsconfig.json"
},
"devDependencies": {
"babel-polyfill": "^6.26.0",
"execa": "^4.0.0",
"tailwindcss": "^1.0.4"
"tailwindcss": "^1.0.4",
"@theme-ui/css": "^0.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
21 changes: 14 additions & 7 deletions packages/tailwind/src/index.js → packages/tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const KEY_MAPPING = {
import { Theme } from '@theme-ui/css'

const KEY_MAPPING: {
readonly [Key: string]: string | string[]
} = {
space: 'spacing',
radii: 'borderRadius',
fonts: 'fontFamily',
Expand All @@ -12,23 +16,26 @@ const KEY_MAPPING = {
zIndices: 'zIndex',
}

export default (theme, config = {}) => {
const transformedTheme = Object.entries(theme).reduce((acc, [key, value]) => {
if (!KEY_MAPPING[key]) {
export default (theme: Theme, config: { [Key: string]: unknown } = {}) => {
const transformedTheme = Object.entries(theme).reduce<{
[Key: string]: unknown
}>((acc, [key, value]) => {
const matchingKey = KEY_MAPPING[key]
if (!matchingKey) {
return {
...acc,
[key]: value,
}
} else if (Array.isArray(KEY_MAPPING[key])) {
KEY_MAPPING[key].forEach(twKey => {
} else if (Array.isArray(matchingKey)) {
matchingKey.forEach(twKey => {
acc[twKey] = value
})

return acc
} else {
return {
...acc,
[KEY_MAPPING[key]]: value,
[matchingKey]: value,
}
}
}, {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const theme = {
size: ['10em', '20em', '30em', '40em'],
}

const toJS = theme => `
const toJS = (theme: { [Key: string]: unknown }) => `
module.exports = ${JSON.stringify(theme, null, 2)}
`

Expand Down
9 changes: 9 additions & 0 deletions packages/tailwind/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node",
"strict": true
},
"exclude": ["test"]
}

0 comments on commit 1b72aef

Please sign in to comment.