Skip to content

Commit 4ac4fea

Browse files
authored
New strategy for colors.json and colors-<suffix>.json and colors-<atomic-level>-<suffix>.json to autoloaded sass partial. (#217)
1 parent c83b23b commit 4ac4fea

File tree

5 files changed

+59
-93
lines changed

5 files changed

+59
-93
lines changed

.core/gulp.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const defaultConfig = {
2626
watch: {
2727
js: ['src/app/**/*', 'reactium_modules/**/*'],
2828
markup: ['src/**/*.html', 'src/**/*.css', 'reactium_modules/**/*.css'],
29-
colors: ['src/**/*/colors.json'],
29+
colors: ['src/**/*/colors*.json', 'reactium_modules/**/*/colors*.json'],
3030
pluginAssets: [
3131
'src/app/**/plugin-assets.json',
3232
'src/**/style-assets.json',
@@ -58,7 +58,7 @@ const defaultConfig = {
5858
},
5959
src: {
6060
app: 'src',
61-
colors: ['src/**/*/colors.json'],
61+
colors: ['src/**/*/colors*.json', 'reactium_modules/**/*/colors*.json'],
6262
pluginAssets: [
6363
'src/app/**/plugin-assets.json',
6464
'src/**/style-assets.json',

.core/gulp.tasks.js

+56-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const del = require('del');
44
const fs = require('fs-extra');
55
const op = require('object-path');
6-
const path = require('path');
6+
const path = require('node:path');
77
const globby = require('./globby-patch');
88
const webpack = require('webpack');
99
const browserSync = require('browser-sync');
@@ -529,15 +529,6 @@ $assets: map.set($assets, "{{key}}", "{{{dataURL}}}");
529529
const mappings = [];
530530
for (const entry of entries) {
531531
const [key, fileName] = entry;
532-
console.log(
533-
`Adding ${key}: ${path.resolve(
534-
base,
535-
fileName,
536-
)} to partial at ${path.resolve(
537-
base,
538-
'_reactium-style-variables.scss',
539-
)}`,
540-
);
541532
const dataURL = await fileReader(
542533
new File(path.resolve(base, fileName)),
543534
);
@@ -751,39 +742,68 @@ $assets: map.set($assets, "{{key}}", "{{{dataURL}}}");
751742
done();
752743
};
753744

745+
const colorTemplate = data => {
746+
const template = handlebars.compile(`
747+
//
748+
// DO NOT EDIT!
749+
// This file is generated by gulp styles:colors task.
750+
//
751+
@use "sass:map";
752+
753+
{{#each this}}
754+
\${{{ key }}}: "{{{value}}}" !default;
755+
{{/each}}
756+
757+
$color: () !default;
758+
759+
{{#each this}}
760+
$color: map.set($color, "{{key}}", \${{{ key }}});
761+
{{/each}}
762+
`);
763+
return template(data);
764+
};
765+
754766
const stylesColors = done => {
755767
const colorProfiles = globby.sync(config.src.colors);
756-
if (colorProfiles.length > 0) {
757-
let colorFileContents =
758-
'// WARNING: Do not directly edit this file !!!!\n// File generated by gulp styles:colors task\n';
759-
let colorVars = [];
760-
let colorArr = [];
761768

769+
if (colorProfiles.length > 0) {
762770
colorProfiles.forEach(filePath => {
763-
let profile = fs.readFileSync(path.resolve(filePath));
764-
profile = JSON.parse(profile);
765-
766-
colorVars.push(`\n\n// ~/${filePath}`);
771+
const profile = fs.readJsonSync(path.resolve(filePath));
772+
const location = path.dirname(filePath);
773+
const { name } = path.parse(path.basename(filePath));
774+
let [, prefix = 'variables', ...suffixParts] = name.split('-');
775+
776+
const prefixes = [
777+
'mixins',
778+
'variables',
779+
'base',
780+
'atoms',
781+
'molecules',
782+
'organisms',
783+
'overrides',
784+
];
785+
ReactiumGulp.Hook.runSync('color-profile-prefixes', prefixes);
786+
787+
if (!prefixes.includes(prefix)) {
788+
suffixParts = [prefix].concat(suffixParts);
789+
prefix = 'variables';
790+
}
767791

768-
Object.keys(profile).forEach(k => {
769-
let code = profile[k];
770-
let cvar = `$${k}`;
771-
let vline = `${cvar}: ${code} !default;`;
772-
let cname = k.split('color-').join('');
773-
let aline = `\t"${cname}": ${cvar}`;
792+
const suffix = suffixParts.join('-');
793+
const outputFileName = `_reactium-style-${prefix}-colors${
794+
suffix.length > 0 ? `-${suffix}` : ''
795+
}.scss`;
796+
const outputPath = path.resolve(location, outputFileName);
797+
798+
const colorFileContents = colorTemplate(
799+
Object.entries(profile).map(([key, value]) => ({
800+
key,
801+
value,
802+
})),
803+
);
774804

775-
colorVars.push(vline);
776-
colorArr.push(aline);
777-
});
805+
fs.writeFileSync(outputPath, colorFileContents, 'utf8');
778806
});
779-
780-
colorFileContents += colorVars.join('\n') + '\n\n\n';
781-
colorFileContents += `$color: (\n${colorArr.join(
782-
',\n',
783-
)}\n) !default;\n\n\n`;
784-
785-
fs.ensureFileSync(config.dest.colors);
786-
fs.writeFileSync(config.dest.colors, colorFileContents, 'utf8');
787807
}
788808

789809
done();

src/assets/style/_scss/_colors.scss

-39
This file was deleted.

src/assets/style/_scss/_reactium-modules.scss

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
// WARNING: Do not directly edit this file !!!!
33
// File generated by gulp styles:partials task
44

5+
@import '_reactium-style-variables-colors-default';
56
@import '../../../app/components/Welcome/_reactium-style-organisms';

src/assets/style/_scss/colors.json

-16
This file was deleted.

0 commit comments

Comments
 (0)