Skip to content
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: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
"resolve": "^1.5.0",
"rimraf": "^2.6.2",
"sass-extract": "^2.1.0",
"sass-extract-js": "^0.3.0",
"sass-lint": "^1.12.1",
"sass-lint-auto-fix": "^0.15.0",
"sass-loader": "^6.0.6",
Expand Down
3 changes: 2 additions & 1 deletion scripts/compile-scss.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const globModule = require('glob');
const chalk = require('chalk');
const postcss = require('postcss');
const sassExtract = require('sass-extract');
const sassExtractJsPlugin = require('./sass-extract-js-plugin');

const postcssConfiguration = require('../src-docs/postcss.config.js');

Expand Down Expand Up @@ -65,7 +66,7 @@ async function compileScssFile(inputFilename, outputCssFilename, outputVarsFilen
outFile: outputCssFilename,
},
{
plugins: [{ plugin: 'sass-extract-js' }],
plugins: [sassExtractJsPlugin],
}
);

Expand Down
99 changes: 99 additions & 0 deletions scripts/sass-extract-js-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// The contents of this file is derived from https://github.com/adamgruber/sass-extract-js by Adam Gruber
// MIT License
// Copyright (c) 2017 Adam Gruber
// 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.

/*
* Add escaped quotes around font names other than the generic CSS font families
* While quotes are not required, they are recommended by the spec
* https://www.w3.org/TR/css-fonts-3/#generic-font-families
*
* @param {string} str Font family name
*
* @return {string}
*/
function quoteFontName(str) {
const genericFonts = [
'serif',
'sans-serif',
'cursive',
'fantasy',
'monospace',
];
return genericFonts.includes(str.toLowerCase()) ? str : `'${str}'`;
}

/*
* Get the CSS value from a sass-extract data structure
* https://github.com/jgranstrom/sass-extract#general-variable-value-structure
*
* @param {object} sassVar Abstract data structure for SASS variable
*
* @return {string|int} CSS value
*/
function getSassValue(sassVar) {
const { type, value } = sassVar;
switch (type) {
case 'SassNumber':
return sassVar.unit ? `${value}${sassVar.unit}` : value;

case 'SassColor': {
const { r, g, b, a, hex } = value;
const hasAlpha = a !== 1;
return hasAlpha
? `rgba(${r.toFixed()}, ${g.toFixed()}, ${b.toFixed()}, ${a})`
: hex;
}

case 'SassList': {
const isStringList = value.every(item => item.type === 'SassString');
const newList = value.map(getSassValue);
return isStringList
? newList.map(quoteFontName).join(', ')
: newList.join(' ');
}

case 'SassMap':
return transformVars(value);

default:
return value;
}
}

/*
* Transform style object key
* - Strip leading '$'
*
* @param {string} key Style object key
*
* @return {string} Converted key
*/
function transformKey(key) {
return key.replace('$', '');
}

/*
* Reduce SASS-compiled variables object into theme object
*
* @param {object} varsObj Output from `sass-extract` render
*
* @return {object} Transformed variables object
*/
function transformVars(varsObj) {
return Object.keys(varsObj).reduce((acc, key) => {
const newKey = transformKey(key);
const newVal = getSassValue(varsObj[key]);
acc[newKey] = newVal;
return acc;
}, {});
}

module.exports = {
run: () => ({
postExtract: extractedVariables =>
transformVars(extractedVariables.global),
}),
};
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ callsites@^2.0.0:
resolved "https://registry.yarnpkg.com/callsites/-/callsites-2.0.0.tgz#06eb84f00eea413da86affefacbffb36093b3c50"
integrity sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=

camel-case@3.0.x, camel-case@^3.0.0:
camel-case@3.0.x:
version "3.0.0"
resolved "https://registry.yarnpkg.com/camel-case/-/camel-case-3.0.0.tgz#ca3c3688a4e9cf3a4cda777dc4dcbc713249cf73"
integrity sha1-yjw2iKTpzzpM2nd9xNy8cTJJz3M=
Expand Down Expand Up @@ -12185,13 +12185,6 @@ sane@^2.0.0:
optionalDependencies:
fsevents "^1.1.1"

sass-extract-js@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/sass-extract-js/-/sass-extract-js-0.3.0.tgz#3fc5be20d84ce55c29a8b089a49254fbfb69a2a3"
integrity sha512-JYyx28EgoPjduSRu/Xyv7+ec/D3CrWiLujNOawYea1YQCBPcgNk12sKxMYkgHeoRGhsIOfu1MXsY6zGcm02mrA==
dependencies:
camel-case "^3.0.0"

sass-extract@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/sass-extract/-/sass-extract-2.1.0.tgz#c65e6ca3103cbcf2fca0dcd81b07e4e49a6cc583"
Expand Down