Skip to content
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

Merged
merged 6 commits into from
Oct 23, 2018

Conversation

montezume
Copy link
Contributor

@montezume montezume commented Oct 22, 2018

*** 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.

  • Create an index.css file in materials that imports all of the variables we want to expose.
  • Use postcss-custom-properties to extract those into a js file (materials/materials.js) and css file (materials/materials.css)
  • Format that file with prettier.
  • Refactor the materials folder and move all internal files to the internal directory.
  • Only expose media-queries.mod.css, materials.css and materials.js through npm
  • Amend the rollup and webpack configs to use the extracted variables file instead of the array of files.
  • Update readme

Copy link
Member

@emmenko emmenko left a 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! 👌

fs.writeFileSync(
`${exportPath}.css`,
prettier.format(exportedCss, { ...prettierConfig, parser: 'scss' })
);
Copy link
Member

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?

@montezume
Copy link
Contributor Author

@emmenko Good call! Also would make sense to update the README

Copy link
Contributor

@tdeekens tdeekens left a 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.

@@ -0,0 +1,113 @@
module.exports = {
customProperties: {
Copy link
Contributor

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"?

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

fs.writeFileSync(
`${exportPath}.css`,
prettier.format(exportedCss, { ...prettierConfig, parser: 'scss' })
);
Copy link
Contributor

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. :)

Copy link
Contributor Author

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 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

@@ -123,7 +108,7 @@ module.exports = (storybookBaseConfig, configType) => {
}),
postcssCustomProperties({
preserve: false,
importFrom: materialSources,
importFrom: 'materials/exports/vars.css',
Copy link
Contributor Author

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 😞

Copy link
Contributor

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.

package.json Outdated
@@ -19,8 +19,9 @@
"dist",
"i18n",
"images",
"materials/*.mod.css",
"materials/**/*.mod.css",
"materials/materials.css",
Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Member

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

Copy link
Contributor

@dferber90 dferber90 left a 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 to custom-properties.css
  • renames imports.js to custom-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",
Copy link
Contributor

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


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
Copy link
Contributor

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.

Copy link
Member

@emmenko emmenko left a 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!

@montezume
Copy link
Contributor Author

Thanks for the help 👍

@dferber90 dferber90 force-pushed the ml-extract-css-vars branch from 726bd43 to e8778e5 Compare October 23, 2018 15:31
@dferber90 dferber90 merged commit b7c4e85 into master Oct 23, 2018
@dferber90 dferber90 deleted the ml-extract-css-vars branch October 23, 2018 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants