-
Notifications
You must be signed in to change notification settings - Fork 376
feat(react-styles): standardize css-in-js generation #4066
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
Merged
redallen
merged 3 commits into
patternfly:v4
from
redallen:feat/unified-styles-generation
Apr 13, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| /css | ||
| css |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| const path = require('path'); | ||
| const fs = require('fs-extra'); | ||
| const glob = require('glob'); | ||
| const camelcase = require('camel-case'); | ||
|
|
||
| /** | ||
| * @param {string} cssString - CSS string | ||
| */ | ||
| function getCSSClasses(cssString) { | ||
| return cssString.match(/(\.)(?!\d)([^\s.,{[>+~#:)]*)(?![^{]*})/g); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} className - Class name | ||
| */ | ||
| function formatClassName(className) { | ||
| return camelcase(className.replace(/pf-((c|l|m|u|is|has)-)?/g, '')); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} className - Class name | ||
| */ | ||
| function isModifier(className) { | ||
| return Boolean(className && className.startsWith) && className.startsWith('.pf-m-'); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} cssString - CSS string | ||
| */ | ||
| function getClassMaps(cssString) { | ||
| const res = {}; | ||
| const distinctClasses = new Set(getCSSClasses(cssString)); | ||
|
|
||
| distinctClasses.forEach(className => { | ||
| const key = formatClassName(className); | ||
| const value = className.replace('.', '').trim(); | ||
| if (isModifier(className)) { | ||
| res.modifiers = res.modifiers || {}; | ||
| res.modifiers[key] = value; | ||
| } else { | ||
| res[key] = value; | ||
| } | ||
| }); | ||
|
|
||
| const ordered = {}; | ||
| Object.keys(res) | ||
| .sort() | ||
| .forEach(key => (ordered[key] = res[key])); | ||
|
|
||
| return ordered; | ||
| } | ||
|
|
||
| /** | ||
| * @returns {any} Map of file names to classMaps | ||
| */ | ||
| function generateClassMaps() { | ||
| const pfStylesDir = path.dirname(require.resolve('@patternfly/patternfly/patternfly.css')); | ||
|
|
||
| const patternflyCSSFiles = glob.sync('**/*.css', { | ||
| cwd: pfStylesDir, | ||
| ignore: ['assets/**', '*.css'], | ||
| absolute: true | ||
| }); | ||
| const srcCSSFiles = glob.sync('src/css/**/*.css'); | ||
|
|
||
| const res = {}; | ||
| [...patternflyCSSFiles, ...srcCSSFiles].forEach(file => (res[file] = getClassMaps(fs.readFileSync(file, 'utf8')))); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| module.exports = { | ||
| generateClassMaps | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const { join, basename, resolve, relative, dirname } = require('path'); | ||
| const { outputFileSync, copyFileSync } = require('fs-extra'); | ||
| const { generateClassMaps } = require('./generateClassMaps'); | ||
|
|
||
| const outDir = resolve(__dirname, '../css'); | ||
|
|
||
| const writeCJSExport = (file, classMap) => | ||
| outputFileSync( | ||
| join(outDir, file.replace(/.css$/, '.js')), | ||
| ` | ||
| "use strict"; | ||
| exports.__esModule = true; | ||
| require('./${basename(file, '.css.js')}'); | ||
| exports.default = ${JSON.stringify(classMap, null, 2)}; | ||
| `.trim() | ||
| ); | ||
|
|
||
| const writeDTSExport = (file, classMap) => | ||
| outputFileSync( | ||
| join(outDir, file.replace(/.css$/, '.d.ts')), | ||
| ` | ||
| import './${basename(file, '.css.js')}'; | ||
| declare const _default: ${JSON.stringify(classMap, null, 2)}; | ||
| export default _default; | ||
| `.trim() | ||
| ); | ||
|
|
||
| /** | ||
| * @param {any} classMaps Map of file names to classMaps | ||
| */ | ||
| function writeClassMaps(classMaps) { | ||
| const pfStylesDir = dirname(require.resolve('@patternfly/patternfly/patternfly.css')); | ||
|
|
||
| Object.entries(classMaps).forEach(([file, classMap]) => { | ||
| const outPath = file.includes(pfStylesDir) ? relative(pfStylesDir, file) : relative('src/css', file); | ||
|
|
||
| writeCJSExport(outPath, classMap); | ||
| writeDTSExport(outPath, classMap); | ||
| copyFileSync(file, join(outDir, outPath)); | ||
| }); | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log('Wrote', Object.keys(classMaps).length * 3, 'CSS-in-JS files'); | ||
| } | ||
|
|
||
| writeClassMaps(generateClassMaps()); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is there any way we can use some CSS parser library to pull out these class names, instead of our own regex? Maybe that would be less performant. I'm hesitant to take on maintaining stuff like this in case it's missing some corner case or could be broken by future changes to the CSS spec (however unlikely).
Maybe we could use something like one of these, and then walk the resulting AST:
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 just copied this over from our existing build script. I think it would be fantastic to leverage https://www.npmjs.com/package/css which we already use.
We could, but this would also require us using
ts-nodeinstead ofnode. We have maybe 20 or so such script files in the repo that it'd be good to convert all of them.