-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: extract css variables to js and css files #181
Conversation
19103ee
to
014032a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I like it! 👌
scripts/extract-materials.js
Outdated
fs.writeFileSync( | ||
`${exportPath}.css`, | ||
prettier.format(exportedCss, { ...prettierConfig, parser: 'scss' }) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Should we maybe inject a comment about those files being autogenerated?
@emmenko Good call! Also would make sense to update the README |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I think @dferber90 should comment if this aligns with this plan to pass styles as an object to react-select.
materials/exports/vars.js
Outdated
@@ -0,0 +1,113 @@ | |||
module.exports = { | |||
customProperties: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: is this something we can set? Why is it "wrapped"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tdeekens this is the shape that postcss-custom-properties generates and expects for it's importFrom.
There's an example here as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
scripts/extract-materials.js
Outdated
fs.writeFileSync( | ||
`${exportPath}.css`, | ||
prettier.format(exportedCss, { ...prettierConfig, parser: 'scss' }) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to format them but to be fair: I don't mind too much how an autogenerated file is formatted. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tdeekens it's either this or to add it to the prettier ignore 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True.
.storybook/webpack.config.js
Outdated
@@ -123,7 +108,7 @@ module.exports = (storybookBaseConfig, configType) => { | |||
}), | |||
postcssCustomProperties({ | |||
preserve: false, | |||
importFrom: materialSources, | |||
importFrom: 'materials/exports/vars.css', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like these names but I'm not feeling very creative 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rename imports.js/css
to custom-properties.js/css
. That name seems more appropriate. "custom properties" is more or less a fancy name for "css variables". And that's what we're exposing here.
9607d18
to
c5a759e
Compare
package.json
Outdated
@@ -19,8 +19,9 @@ | |||
"dist", | |||
"i18n", | |||
"images", | |||
"materials/*.mod.css", | |||
"materials/**/*.mod.css", | |||
"materials/materials.css", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe should just call them index.css and index.js?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about materials/imports.css
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also fine with the index
, I don't mind much as long as it's proper documented
cc65ef7
to
32a5957
Compare
32a5957
to
3095e72
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the changes I was about to request (and some more). These are merely suggestions and I'm okay with undoing them in case you don't agree.
The latest commit:
- renames
imports.css
tocustom-properties.css
- renames
imports.js
tocustom-properties.json
- exports
customProperties
from@commercetools-frontend/ui-kit
for easier use on the consumer side (only one import instead of two; no "reach-into-the-internals") - fixes the
prebuild
script - changes the
custom-properties.css
generation by only writing to disk once (I found this a bit easier to follow after changing the write to JSON anyways) - fixes the machine-depended generated path in the css-comment to be machine-independent
package.json
Outdated
@@ -35,7 +36,7 @@ | |||
], | |||
"scripts": { | |||
"changelog": "conventional-changelog -p angular -u", | |||
"prebuild": "node ./scripts/generate-icon-exports.js && node ./scripts/generate-base-colors.js && node ./scripts/generate-base-shadows.js && node ./scripts/generate-colors-for-story.js", | |||
"prebuild": "node ./scripts/generate-icon-exports.js && node ./scripts/generate-base-colors.js && node ./scripts/generate-base-shadows.js && node ./scripts/generate-colors-for-story.js node ./scripts/extract-materials.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is missing a &&
before node ./scripts/extract-materials.js
scripts/extract-materials.js
Outdated
|
||
const commentDoNotModify = `/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
This file is created by the 'scripts/extract-materials.js' script. The real | ||
values of the variables should be updated in '${relativePath}' */ \n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This resolves to the actual path on the system, so for example /Users/dom/repos/ui-kit/materials/internals
. This is bad because it would change every time this file gets generated on a different machine. We need to print the path relative to the root-folder, or just hardcode it.
99a4fe5
to
726bd43
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I like it! Thanks for updating the documentation!
Thanks for the help 👍 |
726bd43
to
e8778e5
Compare
*** THIS CONSTITUTES A BREAKING CHANGE 😱 ***
In MC, the @imports will have to either be removed, or to be changed to the new singular css file.
Summary
This is (as far as I can tell), the simplest way to extract the materials vars to js and css without refactoring the materials folder and making a whole bunch of decisions with regards to the tokens.
Approach.
index.css
file in materials that imports all of the variables we want to expose.postcss-custom-properties
to extract those into a js file (materials/materials.js
) and css file (materials/materials.css
)materials
folder and move all internal files to theinternal
directory.media-queries.mod.css
,materials.css
andmaterials.js
through npmrollup
andwebpack
configs to use the extracted variables file instead of the array of files.