Skip to content

Commit 6dc7ea1

Browse files
authored
Inline sass-extract-js plugin (#1590)
* Inline sass-extract-js plugin Remove camelcasing Preserve hex values over rgb where possible * Bump yarn lock file * Add MIT license and derived note
1 parent 898d5e6 commit 6dc7ea1

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@
152152
"resolve": "^1.5.0",
153153
"rimraf": "^2.6.2",
154154
"sass-extract": "^2.1.0",
155-
"sass-extract-js": "^0.3.0",
156155
"sass-lint": "^1.12.1",
157156
"sass-lint-auto-fix": "^0.15.0",
158157
"sass-loader": "^6.0.6",

scripts/compile-scss.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const globModule = require('glob');
66
const chalk = require('chalk');
77
const postcss = require('postcss');
88
const sassExtract = require('sass-extract');
9+
const sassExtractJsPlugin = require('./sass-extract-js-plugin');
910

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

@@ -65,7 +66,7 @@ async function compileScssFile(inputFilename, outputCssFilename, outputVarsFilen
6566
outFile: outputCssFilename,
6667
},
6768
{
68-
plugins: [{ plugin: 'sass-extract-js' }],
69+
plugins: [sassExtractJsPlugin],
6970
}
7071
);
7172

scripts/sass-extract-js-plugin.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// The contents of this file is derived from https://github.com/adamgruber/sass-extract-js by Adam Gruber
2+
// MIT License
3+
// Copyright (c) 2017 Adam Gruber
4+
// 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:
5+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
// 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.
7+
8+
/*
9+
* Add escaped quotes around font names other than the generic CSS font families
10+
* While quotes are not required, they are recommended by the spec
11+
* https://www.w3.org/TR/css-fonts-3/#generic-font-families
12+
*
13+
* @param {string} str Font family name
14+
*
15+
* @return {string}
16+
*/
17+
function quoteFontName(str) {
18+
const genericFonts = [
19+
'serif',
20+
'sans-serif',
21+
'cursive',
22+
'fantasy',
23+
'monospace',
24+
];
25+
return genericFonts.includes(str.toLowerCase()) ? str : `'${str}'`;
26+
}
27+
28+
/*
29+
* Get the CSS value from a sass-extract data structure
30+
* https://github.com/jgranstrom/sass-extract#general-variable-value-structure
31+
*
32+
* @param {object} sassVar Abstract data structure for SASS variable
33+
*
34+
* @return {string|int} CSS value
35+
*/
36+
function getSassValue(sassVar) {
37+
const { type, value } = sassVar;
38+
switch (type) {
39+
case 'SassNumber':
40+
return sassVar.unit ? `${value}${sassVar.unit}` : value;
41+
42+
case 'SassColor': {
43+
const { r, g, b, a, hex } = value;
44+
const hasAlpha = a !== 1;
45+
return hasAlpha
46+
? `rgba(${r.toFixed()}, ${g.toFixed()}, ${b.toFixed()}, ${a})`
47+
: hex;
48+
}
49+
50+
case 'SassList': {
51+
const isStringList = value.every(item => item.type === 'SassString');
52+
const newList = value.map(getSassValue);
53+
return isStringList
54+
? newList.map(quoteFontName).join(', ')
55+
: newList.join(' ');
56+
}
57+
58+
case 'SassMap':
59+
return transformVars(value);
60+
61+
default:
62+
return value;
63+
}
64+
}
65+
66+
/*
67+
* Transform style object key
68+
* - Strip leading '$'
69+
*
70+
* @param {string} key Style object key
71+
*
72+
* @return {string} Converted key
73+
*/
74+
function transformKey(key) {
75+
return key.replace('$', '');
76+
}
77+
78+
/*
79+
* Reduce SASS-compiled variables object into theme object
80+
*
81+
* @param {object} varsObj Output from `sass-extract` render
82+
*
83+
* @return {object} Transformed variables object
84+
*/
85+
function transformVars(varsObj) {
86+
return Object.keys(varsObj).reduce((acc, key) => {
87+
const newKey = transformKey(key);
88+
const newVal = getSassValue(varsObj[key]);
89+
acc[newKey] = newVal;
90+
return acc;
91+
}, {});
92+
}
93+
94+
module.exports = {
95+
run: () => ({
96+
postExtract: extractedVariables =>
97+
transformVars(extractedVariables.global),
98+
}),
99+
};

yarn.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,7 @@ callsites@^3.0.0:
23982398
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.0.0.tgz#fb7eb569b72ad7a45812f93fd9430a3e410b3dd3"
23992399
integrity sha512-tWnkwu9YEq2uzlBDI4RcLn8jrFvF9AOi8PxDNU3hZZjJcjkcRAq3vCI+vZcg1SuxISDYe86k9VZFwAxDiJGoAw==
24002400

2401-
camel-case@3.0.x, camel-case@^3.0.0:
2401+
camel-case@3.0.x:
24022402
version "3.0.0"
24032403
resolved "https://registry.yarnpkg.com/camel-case/-/camel-case-3.0.0.tgz#ca3c3688a4e9cf3a4cda777dc4dcbc713249cf73"
24042404
integrity sha1-yjw2iKTpzzpM2nd9xNy8cTJJz3M=
@@ -12395,13 +12395,6 @@ sane@^3.0.0:
1239512395
optionalDependencies:
1239612396
fsevents "^1.2.3"
1239712397

12398-
sass-extract-js@^0.3.0:
12399-
version "0.3.0"
12400-
resolved "https://registry.yarnpkg.com/sass-extract-js/-/sass-extract-js-0.3.0.tgz#3fc5be20d84ce55c29a8b089a49254fbfb69a2a3"
12401-
integrity sha512-JYyx28EgoPjduSRu/Xyv7+ec/D3CrWiLujNOawYea1YQCBPcgNk12sKxMYkgHeoRGhsIOfu1MXsY6zGcm02mrA==
12402-
dependencies:
12403-
camel-case "^3.0.0"
12404-
1240512398
sass-extract@^2.1.0:
1240612399
version "2.1.0"
1240712400
resolved "https://registry.yarnpkg.com/sass-extract/-/sass-extract-2.1.0.tgz#c65e6ca3103cbcf2fca0dcd81b07e4e49a6cc583"

0 commit comments

Comments
 (0)