@vulcancreative/cross-colour
v1.0.0
Published
Dynamically translates a SASS/SCSS colour file to a JS object
Downloads
2
Readme
CrossColour
This is a simple utility, intended for the dynamic (re-)compilation of SASS/SCSS colours to a Javascript file or JSON object. The intent behind this is to keep your code DRY, and ensure cohesive style adherenece throughtout your app. This is currently in production use on our ad agency's website.
The utility can be used in one of two ways. Either as a webpack plugin or as a library.
npm i @vulcancreative/cross-colour --save-dev
Example
CrossColour can take a SCSS colour-reference file:
// vulcan colors
$yellow: #ffe380;
$cyan: #4ec7bc;
$magenta: #fe939d;
// tonals
$white: #ffffff;
$black: #211f22;
$light-grey: #f7f8fa;
$line-grey: #e2e1e5;
$line-darkgrey: #444245;
$mid-grey: #dedede;
$slate-grey: #6e7077;
$dark-grey: #323034;
$space-black: #1e112c;
And it will translate it to something you can use in your JavaScript, without having to worry about code duplication, incorrect colours, or getting slapped around by your design team.
// Auto-generated via CrossColour
const colours = {
"yellow": "#ffe380",
"cyan": "#4ec7bc",
"magenta": "#fe939d",
"white": "#ffffff",
"black": "#211f22",
"lightGrey": "#f7f8fa",
"lineGrey": "#e2e1e5",
"lineDarkgrey": "#444245",
"midGrey": "#dedede",
"slateGrey": "#6e7077",
"darkGrey": "#323034",
"spaceBlack": "#1e112c"
};
export default colours;
CrossColours will ignore comments (including CSS-style block comments) in the final output.
Webpack Use
Webpack use is quite simple. Merely include it in your plugin chain and pass your config. In development mode, rossColour will rebuild your colours for JS whenever it detects your SASS/SCSS file has been updated.
const { CrossColourPlugin } = require("@vulcancreative/cross-colour");
module.exports = {
// ...
plugins: [
new CrossColourPlugin({
sassFilename: path.resolve("src", "styles", "colours.scss"),
jsFilename: path.resolve("src", "shared", "colours.js")
}),
]
};
CrossColourPlugin accepts the following options:
| Option | Required | Default | Description | |--------------|----------|-----------|------------------------------------------------------------------------| | sassFilename | true | undefined | Absolute path to the (pre-existing) SASS/SCSS file. | | jsFilename | true | undefined | Absolute path to the (non-existent or pre-existing) JS output file. | | silent | false | false | If true, will not print when colours have been updated to the console. |
Library Use
Library use involves creating a CrossColours instance, and either passing it a configuration or passing the variables in imperatively.
import path from "path";
import { CrossColour } from "@vulcancreative/cross-colour";
const sassFilename = path.resolve("src", "styles", "colours.scss");
const jsFilename = path.resolve("src", "shared", "colours.js");
const cross = new CrossColour({ sassFilename, jsFilename, silent: true });
// simply read and write, based on config
cross.readWrite(); // or, if you hate yourself: cross.read().write();
// read to a JavaScript object and write to JS file in config
const obj = cross.colours();
cross.write(obj);
// manually write out somewhere that isn't specified in config
const silent = true; // defaults to false, if not passed
const elsewhere = path.resolve("elsewhere", "colours.js");
cross.write(obj, elsewhere, silent);
// aside from importing, you can also use the silly US spelling "colors"
const sillyUSObject = cross.colors();
License
MIT License
Copyright (c) 2020 Vulcan
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.