| 1 |
import { outputFileSync } from 'fs-extra/esm'; |
| 2 |
import { resolve, join } from 'node:path'; |
| 3 |
import { generateTokens } from './generateTokens.mjs'; |
| 4 |
|
| 5 |
const outDir = resolve(import.meta.dirname, '../dist'); |
| 6 |
|
| 7 |
const writeESMExport = (tokenName, tokenString) => |
| 8 |
outputFileSync( |
| 9 |
join(outDir, 'esm/', `${tokenName}.js`), |
| 10 |
` |
| 11 |
export const ${tokenName} = ${tokenString}; |
| 12 |
export default ${tokenName}; |
| 13 |
`.trim() |
| 14 |
); |
| 15 |
|
| 16 |
const writeCJSExport = (tokenName, tokenString) => |
| 17 |
outputFileSync( |
| 18 |
join(outDir, 'js', `${tokenName}.js`), |
| 19 |
` |
| 20 |
"use strict"; |
| 21 |
exports.__esModule = true; |
| 22 |
exports.${tokenName} = ${tokenString}; |
| 23 |
exports["default"] = exports.${tokenName}; |
| 24 |
`.trim() |
| 25 |
); |
| 26 |
|
| 27 |
const writeDTSExport = (tokenName, tokenString) => { |
| 28 |
const text = ` |
| 29 |
export const ${tokenName}: ${tokenString}; |
| 30 |
export default ${tokenName}; |
| 31 |
`.trim(); |
| 32 |
const filename = `${tokenName}.d.ts`; |
| 33 |
outputFileSync(join(outDir, 'esm', filename), text); |
| 34 |
outputFileSync(join(outDir, 'js', filename), text); |
| 35 |
}; |
| 36 |
|
| 37 |
const allIndex = {}; |
| 38 |
const componentIndex = []; |
| 39 |
|
| 40 |
const outputIndex = (index, indexFile) => { |
| 41 |
const esmIndexString = index.map((file) => `export { ${file} } from './${file}';`).join('\n'); |
| 42 |
outputFileSync(join(outDir, 'esm', indexFile), esmIndexString); |
| 43 |
outputFileSync( |
| 44 |
join(outDir, 'js', indexFile), |
| 45 |
` |
| 46 |
"use strict"; |
| 47 |
function __export(m) { |
| 48 |
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; |
| 49 |
} |
| 50 |
exports.__esModule = true; |
| 51 |
${index.map((file) => `__export(require('./${file}'));`).join('\n')} |
| 52 |
`.trim() |
| 53 |
); |
| 54 |
outputFileSync(join(outDir, 'esm', indexFile.replace('.js', '.d.ts')), esmIndexString); |
| 55 |
outputFileSync(join(outDir, 'js', indexFile.replace('.js', '.d.ts')), esmIndexString); |
| 56 |
}; |
| 57 |
|
| 58 |
/** |
| 59 |
* Writes CJS and ESM tokens to `dist` directory |
| 60 |
* |
| 61 |
* @param {any} tokens tokens from generateTokens |
| 62 |
*/ |
| 63 |
function writeTokens(tokens) { |
| 64 |
Object.entries(tokens).forEach(([tokenName, tokenValue]) => { |
| 65 |
const tokenString = JSON.stringify(tokenValue, null, 2); |
| 66 |
|
| 67 |
writeESMExport(tokenName, tokenString); |
| 68 |
writeCJSExport(tokenName, tokenString); |
| 69 |
writeDTSExport(tokenName, tokenString); |
| 70 |
allIndex[tokenName] = true; |
| 71 |
componentIndex.push(tokenName); |
| 72 |
|
| 73 |
// Legacy token support -- values may be incorrect. |
| 74 |
Object.values(tokenValue) |
| 75 |
.map((values) => Object.entries(values)) |
| 76 |
.reduce((acc, val) => acc.concat(val), []) // flatten |
| 77 |
.forEach(([oldTokenName, { name, value }]) => { |
| 78 |
const isChart = oldTokenName.includes('chart'); |
| 79 |
const oldToken = { |
| 80 |
name, |
| 81 |
value: isChart && !isNaN(+value) ? +value : value, |
| 82 |
var: isChart ? `var(${name}, ${value})` : `var(${name})` // Include fallback value for chart vars |
| 83 |
}; |
| 84 |
const oldTokenString = JSON.stringify(oldToken, null, 2); |
| 85 |
writeESMExport(oldTokenName, oldTokenString); |
| 86 |
writeCJSExport(oldTokenName, oldTokenString); |
| 87 |
writeDTSExport(oldTokenName, oldTokenString); |
| 88 |
allIndex[oldTokenName] = true; |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
// Index files including legacy tokens |
| 93 |
outputIndex(Object.keys(allIndex), 'index.js'); |
| 94 |
outputIndex(componentIndex, 'componentIndex.js'); |
| 95 |
|
| 96 |
// eslint-disable-next-line no-console |
| 97 |
console.log('Wrote', Object.keys(allIndex).length * 4 + 4, 'token files'); |
| 98 |
} |
| 99 |
|
| 100 |
writeTokens(generateTokens()); |