Skip to content

Commit

Permalink
Merge pull request #548 from greenpeace/bump-css-vars-extract
Browse files Browse the repository at this point in the history
PLANET-5983: Properly extract vars from multiple files
  • Loading branch information
Inwerpsel authored Apr 6, 2021
2 parents 0c4ab92 + f044e8f commit d4492cd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 57 deletions.
94 changes: 62 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"webpack-fix-style-only-entries": "^0.3.1"
},
"dependencies": {
"@greenpeace/planet4-postcss-css-variables": "^0.2.0",
"classnames": "^2.2.6",
"font-picker-react": "^3.5.2",
"jest-environment-node": "^26.0.1",
"photoswipe": "^4.1.3",
"postcss-css-variables-extract": "^0.1.2",
"react-color": "^2.19.3",
"react-hotkeys-hook": "^3.0.3",
"tinycolor2": "^1.4.2"
Expand Down
28 changes: 4 additions & 24 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ const TerserJSPlugin = require('terser-webpack-plugin');
const FixStyleOnlyEntriesPlugin = require("webpack-fix-style-only-entries");
const RemovePlugin = require('remove-files-webpack-plugin');
const dashDash = require('@greenpeace/dashdash');
const cssVariables = require( 'postcss-css-variables-extract' );
const fs = require( 'fs' );
const collectVarUsages = require( 'postcss-css-variables-extract/lib/scss-var-usages' );
const mergeVarUsages = require( 'postcss-css-variables-extract/lib/merge-var-usages' );
const cssVariables = require( '@greenpeace/planet4-postcss-css-variables' );
const VariableCombinePlugin = require( '@greenpeace/planet4-postcss-css-variables/variableCombinePlugin' )

let allCssVars = {};
const allCssVars = {};

const entryPoints = blockName => {
return {
Expand Down Expand Up @@ -127,25 +125,7 @@ module.exports = {
]
}
}),
{
apply: ( compiler ) => {
compiler.hooks.done.tap( 'DoneWriteCssVars', ( ) => {
// We use postcss to get the selector and resolved default value. For the original file and line number
// we use a separate scripts which loops through all scss files. Only variables that are in the final css
// are included.
const scssUsages = collectVarUsages( './assets/src' );
const mergedUsages = mergeVarUsages( allCssVars, scssUsages );
fs.writeFile(
'./assets/build/css-variables.json',
JSON.stringify( mergedUsages, null, 2 ),
err => !!err && console.log( err )
);
// todo: handle multiple files. The next line only works as intended if there is one style file.
// allCssVars = {}
} );
}
}

VariableCombinePlugin({filename: './assets/build/css-variables.json', allCssVars})
],
optimization: {
...defaultConfig.optimization,
Expand Down

0 comments on commit d4492cd

Please sign in to comment.