UNPKG

@terrazzo/plugin-js

Version:

Generate JS, TS, and JSON from your design tokens schema (requires @terrazzo/cli)

82 lines 3.93 kB
/** * @module @terrazzo/plugin-js * @license MIT License * * Copyright (c) 2021 Drew Powers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { transformJSValue } from '@terrazzo/token-tools/js'; import { buildDTS, buildJS } from './build.js'; import { FORMAT_DTS_ID, FORMAT_JS_ID, TYPE_MAP } from './lib.js'; export * from './build.js'; export * from './lib.js'; export default function pluginJS(options) { const customTransform = options?.transform; return { name: '@terrazzo/plugin-js', async transform({ tokens, getTransforms, setTransform }) { // skip work if another .js plugin has already run const jsTokens = getTransforms({ format: FORMAT_JS_ID, id: '*', mode: '.' }); if (jsTokens.length) { return; } for (const [id, token] of Object.entries(tokens)) { // .d.ts (only default "." mode needed) setTransform(id, { format: FORMAT_DTS_ID, value: { description: token.$description, value: `Record<"${Object.keys(token.mode).join('" | "')}", ${TYPE_MAP[token.$type]}["$value"]>`, }, mode: '.', }); // .js (all modes) for (const mode of Object.keys(token.mode)) { if (customTransform) { const transformedValue = customTransform(token, mode); if (transformedValue !== undefined && transformedValue !== null) { setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode }); continue; } } const transformedValue = transformJSValue(token, { mode, startingIndent: 4 }); if (transformedValue !== undefined) { setTransform(id, { format: FORMAT_JS_ID, value: transformedValue, mode }); } } } }, async build({ getTransforms, outputFile }) { // if (options?.json) { // const contents = buildJSON({ getTransforms }); // outputFile(typeof options?.json === 'string' ? options.json : 'index.json', contents); // } if (options?.js) { const js = buildJS({ getTransforms }); const jsFilename = typeof options?.js === 'string' ? options.js : 'index.js'; outputFile(jsFilename, js); const dts = buildDTS({ getTransforms }); const dtsFilename = typeof options?.js === 'string' ? options.js.replace(/\.js$/, '.d.ts') : 'index.d.ts'; outputFile(dtsFilename, dts); } }, }; } //# sourceMappingURL=index.js.map