-
Notifications
You must be signed in to change notification settings - Fork 36
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
token json を結合して color や数値をとれるトークンオブジェクトを作成する #639
Changes from all commits
8ac92a3
b28a6a3
417573f
6ba7864
356f180
dd30623
51a5c46
02bb4dc
3305284
a3f0d19
5af6f0b
0216ce6
9d8abfe
66e33c6
16eadc3
001e909
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* eslint-disable no-console */ | ||
import { readFileSync, mkdirSync, writeFileSync } from 'node:fs' | ||
import path from 'node:path' | ||
import { createCSSTokenObject, createTokenObject } from '../src/token-object' | ||
import type { TokenDictionary } from '../src/token-object/types' | ||
import { camelCaseKeys } from '../src/token-object' | ||
|
||
type ValueStyle = 'cssVariable' | ||
type KeyStyle = 'camelCase' | ||
|
||
type Config = { | ||
tokenFile: string | ||
baseFile: string | ||
outputFile: string | ||
keyStyle?: KeyStyle | ||
valueStyle?: ValueStyle | ||
} | ||
|
||
const configurations: Config[] = [ | ||
{ | ||
tokenFile: './src/json/pixiv-light.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/pixiv-light.json', | ||
keyStyle: undefined, | ||
valueStyle: undefined, | ||
}, | ||
{ | ||
tokenFile: './src/json/pixiv-dark.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/pixiv-dark.json', | ||
keyStyle: undefined, | ||
valueStyle: undefined, | ||
}, | ||
|
||
{ | ||
tokenFile: './src/json/pixiv-light.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/camel/pixiv-light.json', | ||
keyStyle: 'camelCase', | ||
valueStyle: undefined, | ||
}, | ||
{ | ||
tokenFile: './src/json/pixiv-dark.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/camel/pixiv-dark.json', | ||
keyStyle: 'camelCase', | ||
valueStyle: undefined, | ||
}, | ||
|
||
{ | ||
tokenFile: './src/json/pixiv-light.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/css-variables/pixiv-light.json', | ||
keyStyle: undefined, | ||
valueStyle: 'cssVariable', | ||
}, | ||
{ | ||
tokenFile: './src/json/pixiv-dark.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/css-variables/pixiv-dark.json', | ||
keyStyle: undefined, | ||
valueStyle: 'cssVariable', | ||
}, | ||
|
||
{ | ||
tokenFile: './src/json/pixiv-light.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/css-variables/camel/pixiv-light.json', | ||
keyStyle: 'camelCase', | ||
valueStyle: 'cssVariable', | ||
}, | ||
{ | ||
tokenFile: './src/json/pixiv-dark.json', | ||
baseFile: './src/json/base.json', | ||
outputFile: './dist/tokens/css-variables/camel/pixiv-dark.json', | ||
keyStyle: 'camelCase', | ||
valueStyle: 'cssVariable', | ||
}, | ||
] | ||
|
||
for (const { | ||
tokenFile, | ||
baseFile, | ||
outputFile, | ||
keyStyle, | ||
valueStyle, | ||
} of configurations) { | ||
const baseJson = JSON.parse( | ||
readFileSync(path.resolve(baseFile), 'utf8') | ||
) as TokenDictionary | ||
|
||
const createToken = <T extends TokenDictionary>( | ||
value: T | ||
): Record<string, unknown> => { | ||
switch (valueStyle) { | ||
case 'cssVariable': { | ||
return createCSSTokenObject(value, (x) => `charcoal-${x}`) | ||
} | ||
default: { | ||
return createTokenObject(value, baseJson) | ||
} | ||
} | ||
} | ||
const transformKey = <T extends Record<string, unknown>>( | ||
value: T | ||
): Record<string, unknown> => { | ||
switch (keyStyle) { | ||
case 'camelCase': { | ||
return camelCaseKeys(value) | ||
} | ||
default: { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
const tokenJson = JSON.parse( | ||
readFileSync(path.resolve(tokenFile), 'utf8') | ||
) as TokenDictionary | ||
const tokenObject = transformKey(createToken(tokenJson)) | ||
|
||
mkdirSync(path.dirname(outputFile), { recursive: true }) | ||
writeFileSync(path.resolve(outputFile), JSON.stringify(tokenObject, null, 2)) | ||
|
||
console.log( | ||
`Generated ${outputFile} with keyStyle ${keyStyle} and valueStyle ${valueStyle}.` | ||
) | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jsonをexportsに足す必要がありそうです There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/pixiv/charcoal/blob/feta/create-styled-tokens/packages/theme/package.json#L21 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. いいえ、distにあっても"exports"に足さないとbundlerによってimportすることを許されない気がします。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
勘違いしていました。そうでしたね。修正します |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,39 +4,69 @@ | |
"license": "Apache-2.0", | ||
"main": "./dist/index.cjs.js", | ||
"module": "./dist/index.esm.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs.js", | ||
"import": "./dist/index.esm.js", | ||
"default": "./dist/index.esm.js" | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs.js", | ||
"import": "./dist/index.esm.js", | ||
"default": "./dist/index.esm.js" | ||
}, | ||
"./token-object": { | ||
"types": "./dist/token-object/index.d.ts", | ||
"require": "./dist/token-object/index.cjs.js", | ||
"import": "./dist/token-object/index.esm.js", | ||
"default": "./dist/token-object/index.esm.js" | ||
}, | ||
"./css/*": "./dist/css/*", | ||
"./tokens/*": "./dist/tokens/*" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"token-object": [ | ||
"./dist/token-object/index.d.ts" | ||
], | ||
"tokens/*": [ | ||
"./dist/tokens/*" | ||
], | ||
"css/*": [ | ||
"./dist/css/*" | ||
] | ||
} | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"sideEffects": [ | ||
"*.css" | ||
], | ||
"scripts": { | ||
"build": "npm-run-all --print-label --parallel 'build:*' --sequential serialize", | ||
"build": "npm-run-all --print-label -s 'build:*' --sequential serialize", | ||
"build:bundle": "FORCE_COLOR=1 tsup", | ||
"build:dts": "tsc --project tsconfig.build.json --pretty --emitDeclarationOnly", | ||
"build:copy-css": "cpx 'src/css/**/*.css' dist/css && cpx 'src/json/**/*.json' dist/json", | ||
"build:copy-css": "cpx --clean 'src/css/**/*.{css,d.ts}' dist/css", | ||
"build:token-object": "tsx cli/token-object.ts", | ||
"serialize": "node cli/index.js", | ||
"typecheck": "run-p --print-label 'typecheck:*'", | ||
"typecheck:main": "tsc --project tsconfig.build.json --pretty --noEmit", | ||
"typecheck:cli": "tsc --project cli/tsconfig.build.json --noEmit", | ||
"clean": "rimraf dist .tsbuildinfo", | ||
"test": "vitest run --passWithNoTests" | ||
"test": "vitest run --passWithNoTests", | ||
"bench": "vitest bench --passWithNoTests" | ||
}, | ||
"devDependencies": { | ||
"@types/css": "^0.0.38", | ||
"cpx": "^1.5.0", | ||
"css": "^3.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 気づいてなかったでした。 |
||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^3.0.2", | ||
"tsup": "^6.5.0", | ||
"tsx": "^4.19.1", | ||
"typescript": "^4.9.5", | ||
"vitest": "^2.0.2" | ||
}, | ||
"dependencies": { | ||
"@charcoal-ui/foundation": "^4.0.0-beta.15", | ||
"@charcoal-ui/utils": "^4.0.0-beta.15", | ||
"change-case-all": "^2.1.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change-case が esm only で commonjs build できないので対応しているほうを導入 https://www.npmjs.com/package/change-case-all?activeTab=dependents There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 割とesmがblockingになるケース最近あんまりない気がするけど、一旦change-case-allにしても大丈夫です。change-caseの若干(500Bくらい)小さいです There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使う側はそうなんですが、@charcoal/theme 自体が dual pacakge build しているので create-token-object を package として export するなら esm, commonjs 対応の依存が必要になってくるというやつでした |
||
"deepmerge": "^4.3.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 二つのライブラリーもruntimeに入れるくらいなら事前に作ったものと実際比較してみたいです また、dependenciesにあるとビルドされても使う側でもう一度installされっるのでdevDependenciesに移動したいかもしれないです There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. css variables のものと camelcase のもの, json の key そのままのものを複数用意しました。すべて dist/json に吐き出されるようにしています There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのdepsもうdevdependenciesに移動していい気がしました There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. subpath で export している以上 dependencies には必要かなと思っていたんですが、devDependencies にして subpath による export 切っちゃったほうが良いですかね? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ビルド時bundleされていれば不要と言おうとしていたがdepsはbundleされていないようですね。一旦現状でも大丈夫です |
||
"polished": "^4.1.4" | ||
}, | ||
"files": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "*.css" { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "*.css" { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configurationごとreadFile + JSON.parseする必要がないのと、このくらいな量ならasync apiのほうがいいかもしれないですが、ここでそこまでパフォーマンス求めないので一旦これでも大丈夫です