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

Style dictionary build #33

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "npm run test --workspace=@muon/library",
"test": "muon-build-styles && npm run test --workspace=@muon/library",
"test:watch": "npm run test --workspace=@muon/library -- --watch",
"test:snapshots": "npm run test --workspace=@muon/library -- --update-snapshots",
"dev": "muon-serve",
Expand Down
3 changes: 2 additions & 1 deletion packages/library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"bin": {
"muon": "./scripts/index.mjs",
"muon-serve": "./scripts/serve.mjs"
"muon-serve": "./scripts/serve.mjs",
"muon-build-styles": "./scripts/build-styles.mjs"
},
"keywords": [],
"author": "",
Expand Down
5 changes: 5 additions & 0 deletions packages/library/scripts/build-styles.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { createTokens } from './style-dictionary-create.mjs';

createTokens();
30 changes: 4 additions & 26 deletions packages/library/scripts/serve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@ import { startDevServer } from '@web/dev-server';
import deepmerge from 'deepmerge';
import commandLineArgs from 'command-line-args';
import StorybookConfig from '../storybook/server.config.mjs';
import StyleDictionary from 'style-dictionary';
import styleConfig from './style-dictionary.mjs';
import colorTransform from '../tokens/utils/transforms/color.js';
import stringTransform from '../tokens/utils/transforms/string.js';
import _ from 'lodash';

import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
import postcssPreset from 'postcss-preset-env';
import postcssImport from 'postcss-import';

import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

import { createTokens } from './style-dictionary-create.mjs';

const globalCSSUrl = path.join(__filename, '..', '..', 'css', 'global.css');

Expand All @@ -43,23 +38,6 @@ catch (e) {
process.exit(1);
}

// Set the overriding tokens if there are any
if (config.tokens && config.tokens.dir) {
styleConfig.source = config.tokens.dir;
}
const tokenUtils = path.join(__dirname, '..', 'tokens', 'utils');
const cssFontTemplate = _.template(fs.readFileSync(path.join(tokenUtils, 'templates', 'font-face.css.template')));

const styleDictionary = StyleDictionary.extend(styleConfig);

styleDictionary.registerFormat({
name: 'css/fonts',
formatter: cssFontTemplate
});

styleDictionary.registerTransform(stringTransform);
styleDictionary.registerTransform(colorTransform);

/*
- Set variables from config
*/
Expand Down Expand Up @@ -125,8 +103,8 @@ const analyse = async () => {
};

const createStyleTokens = async () => {
await styleDictionary.buildAllPlatforms();
await createGlobalCSS();
await createTokens();
await createGlobalCSS();

copyDir(path.join(__filename, '..', '..', 'build'), destination);
};
Expand Down
49 changes: 49 additions & 0 deletions packages/library/scripts/style-dictionary-create.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import StyleDictionary from 'style-dictionary';
import _ from 'lodash';
import path from 'path';
import fs from 'fs';

import styleConfig from './style-dictionary.mjs';
import colorTransform from '../tokens/utils/transforms/color.js';
import stringTransform from '../tokens/utils/transforms/string.js';

import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

let config = {};

try {
config = JSON.parse(fs.readFileSync('muon.config.json').toString());
}
catch (e) {
console.error('Missing config, is this the right folder?', e);
process.exit(1);
}

// Set the overriding tokens if there are any
if (config.tokens && config.tokens.dir) {
styleConfig.source = config.tokens.dir;
}

const tokenUtils = path.join(__dirname, '..', 'tokens', 'utils');
const cssFontTemplate = _.template(fs.readFileSync(path.join(tokenUtils, 'templates', 'font-face.css.template')));

const styleDictionary = StyleDictionary.extend(styleConfig);

styleDictionary.registerFormat({
name: 'css/fonts',
formatter: cssFontTemplate
});

styleDictionary.registerTransform(stringTransform);
styleDictionary.registerTransform(colorTransform);

const createTokens = async () => {
await styleDictionary.buildAllPlatforms();
};

export {
styleDictionary,
createTokens
};