@americanexpress/css-to-js

中文文档
1.0.1发布于 2020/03/18Apache-2.0ESM + CJS近 30 天下载 2902

Convert pure CSS to JavaScript literal objects or React Style

原始使用文档
文档目录9 节
  1. 👩‍💻 Hiring 👨‍💻
  2. 📖 Table of Contents
  3. ✨ Features
  4. 🤹‍ Usage
  5. 🎛️ API
  6. 🎣 Git Hooks
  7. 🏆 Contributing
  8. 🗝️ License
  9. 🗣️ Code of Conduct

CSS to JS - One Amex

npm Travis (.org) branch

Tool for transforming CSS into JS

👩‍💻 Hiring 👨‍💻

Want to get paid for your contributions to CSS-to-JS?

Send your resume to [email protected]


📖 Table of Contents


✨ Features

  • 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

🤹‍ Usage

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 exports for each selector in css file   
  --shakeFile  generate single file with exports for each selector in css file   
    
  If shakeFile or splitFile is not specified, generate single file with single exported object with selectors in css file as keys  
 See API for more details.

Node:

import { convert } from 'css-to-js';  
Example Usage:   

//Input CSS string, Outputs JS Object  
convert(`.myClass { color: 'red' }`);   

//Input CSS string, Output file exporting JS Object  
convert(`.myClass { color: 'red' }`, { outputType: 'file', outputPath: 'outPath' });   

//Input CSS file path, Output file exporting JS Object  
convert('absolute-path-to-css-file', { outputType: 'file', outputPath: 'outPath' });  
  
//Input CSS file path, Output file exporting multiple JS Objects  
convert('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 Objects  
convert('absolute-path-to-dir');  
  
//Input Dir path of CSS files, Output dir of converted files exporting JS Object  
convert('absolute-path-to-dir'', { outputType: 'file', outputPath: 'outPath' });  
  

🎛️ API

Input and Output type options

Ensure paths are absolute or relative to the root only
Convert FunctionInput TypeOutput Type(s)
convert(css);Path to single CSS file or string of CSS codeJS style object
convert(css, { outputType: 'file', outputPath: 'example.js' });Path to single CSS file or string of CSS codeGenerated 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 codeGenerated 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 codeGenerated directory 'splitExample' with multiple files each with a default export of a JS style object
convert(dirPath);Path to directory of CSS filesArray of JS style objects
convert(dirPath, { outputType: 'file', outputPath: 'exampleDir' });Path to directory of CSS filesGenerated 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 filesGenerated 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 filesGenerated directory of directories; Each file has a default export of a JS style object and is named after the corresponding css file

File conversion examples

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

export default {   
  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

export const class1 = { color: 'red' };  
export const class2 = { 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

export default { color: 'red' };  

class2.js

export default { color: 'blue' }; 

Media Reverse

Note: mediaReverse is always true if output type is shakeFile or splitFile

Sometimes it's useful to have the styles located in media queries distributed into the selector properties rather than be their own property.
If the mediaReverse 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

export default {   
  class1: { color: 'red' },   
  '@media (min-width:500px)': {  
    class2: { color: 'blue' }   
  },  
  '@media (min-width:1000px)': {
    class2: { color: 'green' }   
  }  
}  

If mediaReverse is set to true:

convert(
   path.resolve(__dirname, './nav.css'), 
   { 
     outputType: 'file', 
     outputPath: path.resolve(__dirname, '../example4.js'),
     mediaReverse: true 
   }
);

Example4.js

export default {   
 class1: { color: 'red' },   
 class2: {   
   '@media (min-width:500px)': {  
     color: 'blue'   
   },  
   '@media (min-width:1000px)': {
     color: 'green'   
   }    
 }  
}  

🎣 Git Hooks

These commands will be automatically run during normal git operations like committing code.

pre-commit

This hook runs npm test before allowing a commit to be checked in.

commit-msg

This hook verifies that your commit message matches the One Amex conventions. See the commit
message
section in the CONTRIBUTING.md.


🏆 Contributing

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 Open Source Community must accept and sign an Agreement indicating agreement to the terms below. Except for the rights granted in this Agreement to American Express and to recipients of software distributed by American Express, You reserve all right, title, and interest, if any, in and to Your Contributions. Please fill out the Agreement.

Please feel free to open pull requests and see CONTRIBUTING.md for commit formatting details.


🗝️ License

Any contributions made under this project will be governed by the Apache License 2.0.


🗣️ Code of Conduct

This project adheres to the American Express Community Guidelines.
By participating, you are expected to honor these guidelines.

版本与发布记录

@americanexpress/css-to-js 共发布 2 个版本,首次发布于 2020/02/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2020/03/186 年前
1.0.02020/02/126 年前

关于 @americanexpress/css-to-js 的常见问题

@americanexpress/css-to-js 是什么?

Convert pure CSS to JavaScript literal objects or React Style

如何安装 @americanexpress/css-to-js?

在项目目录运行 npm install @americanexpress/css-to-js,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 css-to-js,可以用 npx css-to-js 直接运行。

@americanexpress/css-to-js 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/americanexpress__css-to-js 是否存在。

@americanexpress/css-to-js 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@americanexpress/css-to-js 可以商用吗?

@americanexpress/css-to-js 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@americanexpress/css-to-js 的下载量有多少?

根据 npm 官方下载统计,@americanexpress/css-to-js 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,902 次,日均约 97 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@americanexpress/css-to-js 还在维护吗?

最新版本 1.0.1 发布于 2020/03/18(6 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/02/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。