- Notifications
You must be signed in to change notification settings - Fork2
✨ Tool for transforming CSS into JS
License
americanexpress/css-to-js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Tool for transforming CSS into JS
Want to get paid for your contributions toCSS-to-JS?
Send your resume tooneamex.careers@aexp.com
- Converts CSS to JS
- Supports multiple input and output formats (inline code, files, directories)
- Generates style objects as e6 exports with options to split into separate files
- Reverse media query distribution
Install:
npm install @americanexpress/css-to-js --save-dev
CLI:
css-to-js<inputPath><outputPath> (--mediaReverse) (--splitFile| --shakeFile) Arguments: inputPath input path to css file or dir of css files outputPath output path to js file or dir Options: --help --version --mediaReverse reverse mediaQuery style properties --splitFile generate multiple files with exportsforeach selectorin css file --shakeFile generate single file with exportsforeach selectorin css file If shakeFile or splitFile is not specified, generate single file with single exported object with selectorsin css file as keys See APIfor more details.
Node:
import{convert}from'css-to-js';//Input CSS string, Outputs JS Objectconvert(`.myClass { color: 'red' }`);//Input CSS string, Output file exporting JS Objectconvert(`.myClass { color: 'red' }`,{outputType:'file',outputPath:'outPath'});//Input CSS file path, Output file exporting JS Objectconvert('absolute-path-to-css-file',{outputType:'file',outputPath:'outPath'});//Input CSS file path, Output file exporting multiple JS Objectsconvert('absolute-path-to-css-file',{outputType:'shakeFile',outputPath:'outPath'});//Input CSS file path, Outputs dir of files exporting single JS Objects (If treeshaking not supported)convert('absolute-path-to-css-file',{outputType:'splitFile',outputPath:'outPath'});//Input Dir path of CSS files, Output array of JS Objectsconvert('absolute-path-to-dir');//Input Dir path of CSS files, Output dir of converted files exporting JS Objectconvert('absolute-path-to-dir'',{outputType:'file',outputPath:'outPath'});
| Convert Function | Input Type | Output Type(s) |
|---|---|---|
| convert(css); | Path to single CSS file or string of CSS code | JS style object |
| convert(css, { outputType: 'file', outputPath: 'example.js' }); | Path to single CSS file or string of CSS code | Generated JS file 'example.js' with a default export of a JS style object |
| convert(css, { outputType: 'shakeFile', outputPath: 'shakeExample.js' }); | Path to single CSS file or string of CSS code | Generated JS file 'shakeExample.js' with multiple named exports of JS style objects |
| convert(css, { outputType: 'splitFile', outputPath: 'splitExample' }); | Path to single CSS file or string of CSS code | Generated directory 'splitExample' with multiple files each with a default export of a JS style object |
| convert(dirPath); | Path to directory of CSS files | Array of JS style objects |
| convert(dirPath, { outputType: 'file', outputPath: 'exampleDir' }); | Path to directory of CSS files | Generated directory of JS files; Each file has a default export of a JS style object and is named after the corresponding css file |
| convert(dirPath, { outputType: 'shakeFile', outputPath: 'shakeExample.js' }); | Path to directory of CSS files | Generated directory of JS files; Each file has multiple named exports of JS style objects and is named after the corresponding css file |
| convert(dirPath, { outputType: 'splitFile', outputPath: 'splitExample' }); | Path to directory of CSS files | Generated directory of directories; Each file has a default export of a JS style object and is named after the corresponding css file |
Sample Input:nav.css
.class1 {color:'red'; } .class2 {color:'blue'; }
Example 1: File - Creates a file with a single object export representing tree of full converted css
convert( path.resolve(__dirname, './nav.css'), { outputType: 'file', outputPath: path.resolve(__dirname, '../example1.js') });Output:Example1.js
exportdefault{class1:{color:'red'},class2:{color:'blue'},};
Example 2: ShakeFile - Create file with multiple exports representing each selector of converted css
(Useful for tree-shaking style objects that do not get used)
convert( path.resolve(__dirname, './nav.css'), { outputType: 'shakeFile', outputPath: path.resolve(__dirname, '../example2.js') });Output:Example2.js
exportconstclass1={color:'red'};exportconstclass2={color:'blue'};
Example 3: SplitFile - Create multiple files with exports representing each selector of converted css
(Useful for excluding style objects that do not get used if tree-shaking is not implemented)
convert( path.resolve(__dirname, './nav.css'), { outputType: 'splitFile', outputPath: path.resolve(__dirname, '../example3') });Output:Example3/
class1.js
exportdefault{color:'red'};
class2.js
exportdefault{color:'blue'};
Note:mediaReverse is always true if output type isshakeFile orsplitFile
Sometimes it's useful to have the styles located in media queries distributed into the selector properties rather than be their own property.
If themediaReverse flag is set to true, reverse the positioning of media queries to be inside the property.
nav.css
.class1 {color:'red'; }@media (min-width:500px) { .class1 {color:'blue'; } }@media (min-width:1000px) { .class1 {color:'green'; } }
If mediaReverse is not set:
convert( path.resolve(__dirname, './nav.css'), { outputType: 'file', outputPath: path.resolve(__dirname, '../example4.js') });Example4.js
exportdefault{class1:{color:'red'},'@media (min-width:500px)':{class2:{color:'blue'},},'@media (min-width:1000px)':{class2:{color:'green'},},};
If mediaReverse is set totrue:
convert( path.resolve(__dirname, './nav.css'), { outputType: 'file', outputPath: path.resolve(__dirname, '../example4.js'), mediaReverse: true });Example4.js
exportdefault{class1:{color:'red'},class2:{'@media (min-width:500px)':{color:'blue',},'@media (min-width:1000px)':{color:'green',},},};
These commands will be automatically run during normal git operations like committing code.
pre-commit
This hook runsnpm test before allowing a commit to be checked in.
commit-msg
This hook verifies that your commit message matches the One Amex conventions. See thecommit
message section in theCONTRIBUTING.md.
We welcome Your interest in the American Express Open Source Community on Github.Any Contributor to any Open Source Project managed by the American Express OpenSource Community must accept and sign an Agreement indicating agreement to theterms below. Except for the rights granted in this Agreement to American Expressand to recipients of software distributed by American Express, You reserve allright, title, and interest, if any, in and to Your Contributions. Pleasefillout the Agreement.
Please feel free to open pull requests and seeCONTRIBUTING.md for commit formatting details.
Any contributions made under this project will be governed by theApache License 2.0.
This project adheres to theAmerican Express Community Guidelines.
By participating, you are expected to honor these guidelines.
About
✨ Tool for transforming CSS into JS
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.
