Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TS support, cleans up modules #335

Merged
merged 8 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions build/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import MaskCornerCuts from '../src/props.masks.corner-cuts.js'

import {buildPropsStylesheet} from './to-stylesheet.js'
import {toTokens} from './to-tokens.js'
import {toObject} from './to-object.js'
import {toTypes, preparedTypes} from './to-types.js'
import {toFigmaTokens} from './to-figmatokens.js'

const [,,prefix='',useWhere,customSubject='',filePrefix=''] = process.argv
Expand Down Expand Up @@ -82,6 +84,32 @@ const figmatokensSYNC = { 'open-props': { ...figmatokens } }
const FigmaTokensSync = fs.createWriteStream('../open-props.figma-tokens.sync.json')
FigmaTokensSync.end(JSON.stringify(figmatokensSYNC, null, 2))

if (!fs.existsSync('../dist'))
fs.mkdirSync('../dist')
argyleink marked this conversation as resolved.
Show resolved Hide resolved

const JS = fs.createWriteStream('../dist/open-props.js')
JS.end(`var OpenProps = ${JSON.stringify(toObject(), null, 2)}`)

const ES = fs.createWriteStream('../dist/open-props.module.js')
ES.end(`export default ${JSON.stringify(toObject(), null, 2)}`)

const CJS = fs.createWriteStream('../dist/open-props.cjs')
CJS.end(`module.exports = ${JSON.stringify(toObject(), null, 2)}`)

// const UMD = fs.createWriteStream('../dist/open-props.umd.js')
// UMD.end(`module.exports = ${JSON.stringify(toObject(), null, 2)}`)

const TS = fs.createWriteStream('../dist/open-props.ts')
argyleink marked this conversation as resolved.
Show resolved Hide resolved
TS.end(`export default ${JSON.stringify(toObject(), null, 2)}`)
argyleink marked this conversation as resolved.
Show resolved Hide resolved

const TSD = fs.createWriteStream('../dist/open-props.d.ts')
TSD.end(`declare const OpenProps: ${JSON.stringify(toTypes(), null, 2).replaceAll(',',';')};\nexport default OpenProps;`)

preparedTypes().forEach(({filename, json}) => {
let bundle = fs.createWriteStream('../src/props.'+filename+'.d.ts');
argyleink marked this conversation as resolved.
Show resolved Hide resolved
bundle.end(`declare const _default: ${JSON.stringify(json, null, 2).replaceAll(',',';')};\nexport default _default;`)
})

// gen prop variants
Object.entries({
...mainbundle,
Expand Down
35 changes: 35 additions & 0 deletions build/to-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Animations from '../src/props.animations.js'
import Sizes from '../src/props.sizes.js'
import Colors from '../src/props.colors.js'
import ColorsHSL from '../src/props.colors-hsl.js'
import Fonts from '../src/props.fonts.js'
import Borders from '../src/props.borders.js'
import Aspects from '../src/props.aspects.js'
import Easings from '../src/props.easing.js'
import Gradients from '../src/props.gradients.js'
import Shadows from '../src/props.shadows.js'
import SVG from '../src/props.svg.js'
import Zindex from '../src/props.zindex.js'
import MaskEdges from '../src/props.masks.edges.js'
import MaskCornerCuts from '../src/props.masks.corner-cuts.js'

import {mapToObjectNotation} from './utils.js'

export const toObject = () => {
return mapToObjectNotation({
...Animations,
...Sizes,
...Colors,
...ColorsHSL,
...Fonts,
...Borders,
...Aspects,
...Easings,
...SVG,
...Gradients,
...Shadows,
...Zindex,
...MaskEdges,
...MaskCornerCuts,
})
}
66 changes: 66 additions & 0 deletions build/to-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Animations from '../src/props.animations.js'
import Sizes from '../src/props.sizes.js'
import Colors from '../src/props.colors.js'
import ColorsHSL from '../src/props.colors-hsl.js'
import Fonts from '../src/props.fonts.js'
import Borders from '../src/props.borders.js'
import Aspects from '../src/props.aspects.js'
import Easings from '../src/props.easing.js'
import Gradients from '../src/props.gradients.js'
import Shadows from '../src/props.shadows.js'
import SVG from '../src/props.svg.js'
import Zindex from '../src/props.zindex.js'
import MaskEdges from '../src/props.masks.edges.js'
import MaskCornerCuts from '../src/props.masks.corner-cuts.js'

import {mapToObjectNotation} from './utils.js'

const mapTypes = collection =>
Object.fromEntries(Object.entries(collection)
.map(([key, val]) =>
[key, typeof val]))

export const toTypes = () => {
return mapToObjectNotation({
...mapTypes(Animations),
...mapTypes(Sizes),
...mapTypes(Colors),
...mapTypes(ColorsHSL),
...mapTypes(Fonts),
...mapTypes(Borders),
...mapTypes(Aspects),
...mapTypes(Easings),
...mapTypes(SVG),
...mapTypes(Gradients),
...mapTypes(Shadows),
...mapTypes(Zindex),
...mapTypes(MaskEdges),
...mapTypes(MaskCornerCuts),
})
}

export const preparedTypes = () => {
return [
{filename: 'animations', props: Animations},
{filename: 'sizes', props: Sizes},
{filename: 'colors', props: Colors},
{filename: 'colors-hsl', props: ColorsHSL},
{filename: 'fonts', props: Fonts},
{filename: 'borders', props: Borders},
{filename: 'aspects', props: Aspects},
{filename: 'easings', props: Easings},
{filename: 'svg', props: SVG},
{filename: 'gradients', props: Gradients},
{filename: 'shadows', props: Shadows},
{filename: 'zindex', props: Zindex},
{filename: 'masks-edges', props: MaskEdges},
{filename: 'masks-corner-cuts', props: MaskCornerCuts},
].map(({filename, props}) => {
const json = mapToObjectNotation(mapTypes(props))

return {
filename,
json,
}
})
}
12 changes: 12 additions & 0 deletions build/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const camelize = text => {
text = text.replace(/[-]+(.)?/g, (_, c) => c
? c.toUpperCase()
: '')
return text.substr(0, 1).toLowerCase() + text.substr(1)
}

export const mapToObjectNotation = props => {
for (var prop in props)
props[camelize(prop)] = props[prop]
return props
}
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"main": "dist/open-props.cjs",
"unpkg": "open-props.min.css",
"module": "dist/open-props.module.js",
"types": "dist/open-props.d.ts",
"exports": {
".": {
"types": "./dist/open-props.d.ts",
"import": "./dist/open-props.module.js",
"require": "./dist/open-props.cjs",
"default": "./dist/open-props.cjs"
Expand Down Expand Up @@ -178,14 +180,11 @@
"scripts": {
"build": "concurrently npm:gen:op npm:gen:shadowdom",
"test": "ava test/basic.test.cjs",
"bundle": "concurrently npm:lib:* -m 25 && concurrently npm:shadow:* -m 25 && npm run module:js",
"bundle:pre-edit": "json -I -f package.json -e \"this.unpkg=''\"",
"bundle:post-edit": "json -I -f package.json -e \"this.unpkg='open-props.min.css'\"",
"bundle": "concurrently npm:lib:* -m 25 && concurrently npm:shadow:* -m 25",
"gen:op": "cd build && node props.js \"\" true",
"gen:nowhere": "cd build && node props \"\" false",
"gen:shadowdom": "cd build && node props \"\" false \":host\" \"shadow\"",
"gen:prefixed": "cd build && node props.js \"op\" true",
"module:js": "npm run bundle:pre-edit && microbundle --no-sourcemap && npm run bundle:post-edit",
"lib:all": "postcss src/index.css -o open-props.min.css",
"lib:normalize": "postcss src/extra/normalize.css -o normalize.min.css && node ./build/extras.js",
"lib:normalize:light": "postcss src/extra/normalize.light.css -o normalize.light.min.css",
Expand Down Expand Up @@ -305,7 +304,6 @@
"concurrently": "^7.2.2",
"cssnano": "^5.1.10",
"json": "^11.0.0",
"microbundle": "^0.13.3",
"open-color": "^1.9.1",
"postcss": "^8.3.9",
"postcss-cli": "^8.3.1",
Expand Down
4 changes: 4 additions & 0 deletions test/basic.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ test('Should produce normalize files', async t => {
test('Should produce optional mask props', async t => {
t.assert(fs.existsSync('./masks.edges.min.css'))
t.assert(fs.existsSync('./masks.corner-cuts.min.css'))

test('Should produce typings files', async t => {
t.assert(fs.existsSync('./dist/open-props.d.ts'))
t.assert(fs.existsSync('./dist/props.sizes.d.ts'))
argyleink marked this conversation as resolved.
Show resolved Hide resolved
})