Skip to content

Commit

Permalink
feat(theme): filter css variables
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamind authored and Westbrook committed Jun 28, 2022
1 parent 32b3adb commit 1761f3a
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 24,357 deletions.
7 changes: 6 additions & 1 deletion packages/action-group/test/benchmark/basic-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ governing permissions and limitations under the License.
*/

import '@spectrum-web-components/action-group/sp-action-group.js';
import '@spectrum-web-components/action-button/sp-action-button.js';
import { html } from 'lit';
import { measureFixtureCreation } from '../../../../test/benchmark/helpers.js';

measureFixtureCreation(html`
<sp-action-group open></sp-action-group>
<sp-action-group>
<sp-action-button>Button 1</sp-action-button>
<sp-action-button>Button 2</sp-action-button>
<sp-action-button>Button 3</sp-action-button>
</sp-action-group>
`);
6,094 changes: 37 additions & 6,057 deletions packages/styles/express/spectrum-scale-large.css

Large diffs are not rendered by default.

6,158 changes: 45 additions & 6,113 deletions packages/styles/express/spectrum-scale-medium.css

Large diffs are not rendered by default.

6,094 changes: 37 additions & 6,057 deletions packages/styles/spectrum-scale-large.css

Large diffs are not rendered by default.

6,158 changes: 45 additions & 6,113 deletions packages/styles/spectrum-scale-medium.css

Large diffs are not rendered by default.

126 changes: 112 additions & 14 deletions scripts/spectrum-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,79 @@ import fs from 'fs-extra';
import postcss from 'postcss';
import { postCSSPlugins } from './css-processing.cjs';
import { fileURLToPath } from 'url';
// import postcssCustomProperties from 'postcss-custom-properties';
import fg from 'fast-glob';

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

const processCSSData = async (data, identifier, from) => {
let removedVariableDeclarations = 0;

/**
* Use postcss to remove CSS Custom Properties that are not leveraged in the project
*
* @param { variables: string[] } options - list of CSS Custom Properties found to be used
*/
function postcssFilterVariableDeclarations(options) {
options = options || {};

var variables = options.variables;

function filterDeclarations(root) {
root.walk(function (rule) {
if (rule.type === 'rule') {
rule.each(function (decl, index) {
if (decl.variable) {
// always include global and alias vars
if (
decl.prop.startsWith('--spectrum-global-') ||
decl.prop.startsWith('--spectrum-alias-')
) {
return;
}
// otherwise if the variable is not in the allowed list, remove it
if (!variables.has(decl.prop)) {
decl.remove();
removedVariableDeclarations += 1;
}
}
});
}
});
}

return filterDeclarations;
}

const varRegex = /--spectrum-[^:,)\s]+/g;

/**
* Construct an array of the all the CSS custom properties leveraged in packages
* that ARE NOT the styles or theme packages.
* @returns string[]
*/
const findUsedVars = async () => {
const usedVariables = new Set();
for (const cssPath of await fg(`./packages/*/src/*.css`)) {
if (
cssPath.includes('packages/styles') ||
cssPath.includes('packages/theme')
) {
continue;
}
const originCSS = fs.readFileSync(cssPath, 'utf8');
const foundVars = originCSS.matchAll(varRegex);
for (const variable of foundVars) {
usedVariables.add(variable[0]);
}
}
return usedVariables;
};

const processCSSData = async (
data,
identifier,
from,
usedVariables = undefined
) => {
/* lit-html is a JS litteral, so `\` escapes by default.
* for there to be unicode characters, the escape must
* escape itself...
Expand Down Expand Up @@ -55,7 +123,15 @@ const processCSSData = async (data, identifier, from) => {
);
}

result = await postcss(postCSSPlugins())
const plugins = postCSSPlugins();
if (usedVariables) {
plugins.push(
postcssFilterVariableDeclarations({
variables: usedVariables,
})
);
}
result = await postcss(plugins)
.process(result, {
from,
})
Expand All @@ -65,14 +141,28 @@ const processCSSData = async (data, identifier, from) => {
return result;
};

const processCSS = async (srcPath, dstPath, identifier, from) => {
fs.readFile(srcPath, 'utf8', async function (error, data) {
if (error) {
return console.log(error);
}
const processCSS = async (
srcPath,
dstPath,
identifier,
from,
usedVariables = undefined
) => {
return new Promise((res) => {
fs.readFile(srcPath, 'utf8', async function (error, data) {
if (error) {
return console.log(error);
}

let result = await processCSSData(data, identifier, from);
fs.writeFile(dstPath, result, 'utf8');
let result = await processCSSData(
data,
identifier,
from,
usedVariables
);
fs.writeFileSync(dstPath, result, 'utf8');
res();
});
});
};

Expand Down Expand Up @@ -125,6 +215,8 @@ const scales = ['medium', 'large'];
const cores = ['global'];
const processes = [];

const foundVars = await findUsedVars();

spectrumPaths.forEach(async (spectrumPath, i) => {
const packageDir = ['styles'];
const isExpress = i === 1;
Expand Down Expand Up @@ -158,7 +250,9 @@ spectrumPaths.forEach(async (spectrumPath, i) => {
)
);
console.log(`processing scale ${srcPath}`);
processes.push(await processCSS(srcPath, dstPath, scale));
processes.push(
await processCSS(srcPath, dstPath, scale, undefined, foundVars)
);
});

cores.forEach(async (core) => {
Expand All @@ -177,7 +271,7 @@ spectrumPaths.forEach(async (spectrumPath, i) => {
});
});

(async () => {
async function processSpectrumVars() {
{
// Typography
const typographyPath = path.join(
Expand Down Expand Up @@ -246,6 +340,10 @@ spectrumPaths.forEach(async (spectrumPath, i) => {
}

Promise.all(processes).then(() => {
console.log('complete.');
console.log(
`Spectrum Vars processed. ${removedVariableDeclarations} Custom Property declarations were removed as unused.`
);
});
})();
}

processSpectrumVars();
4 changes: 2 additions & 2 deletions web-test-runner.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ export default {
'**/node_modules/**',
],
threshold: {
statements: 99,
statements: 98.5,
branches: 96,
functions: 98,
lines: 99,
lines: 98.5,
},
},
testFramework: {
Expand Down

0 comments on commit 1761f3a

Please sign in to comment.