@americanexpress/css-to-js

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

将纯 CSS 转换为 JavaScript 字面对象或反应样式

中文使用文档
文档目录9 节
  1. 雇工吗
  2. 目录
  3. * 特性
  4. 使用量
  5. • API
  6. 基特虎克队
  7. 捐款
  8. 许可证
  9. 行为守则

CSS to JS - One Amex

npm Travis (.org) branch

将中央支助系统转变为联署材料的工具

雇工吗

想要得到报酬 为你的捐款 CSS-to-JS? 吗?

把你的简历发给 aexp.com (中文(简体) )


目录


* 特性

  • 将 CSS 转换为 JS
  • 支持多种输入和输出格式(内置代码、文件、目录)
  • 生成样式对象为 e6 导出, 选项可拆分为单独的文件
  • 反向媒体查询分布

使用量

安装 :

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.

节点 :

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

输入和输出类型选项

确保路径绝对或仅相对于根
转换函数输入类型输出类型
转换;单一 CSS 文件或 CSS 代码字符串的路径JS 风格对象
转换( css, { 输出类型 : “ 文件 ” , 输出路径 : “ 实例 . js ”) ;单一 CSS 文件或 CSS 代码字符串的路径生成 JS 文件 '例. js' , 默认导出 JS 样式对象
转换( css, { 输出类型 : “ shakeFile” , 输出路径 : “ shakeExample.js' } );单一 CSS 文件或 CSS 代码字符串的路径生成 JS 文件 “ shakeExample.js ” , 并有多处命名的 JS 风格对象导出
转换( css, { 输出类型 : “ splitFile ” , 输出路径 : “ splitExample ” ) ;单一 CSS 文件或 CSS 代码字符串的路径生成目录“ splitExample” , 每个文件有多个文件, 默认导出 JS 样式对象
转换( dirPath) ;CSS 文件目录路径JS 样式对象阵列
转换( dirPath, { 输出Type: “文件” , 输出Path: “实例Dir” } );CSS 文件目录路径生成 JS 文件目录; 每个文件有一个默认导出 JS 样式对象, 并以相应的 cs 文件命名
转换( dirPath, { 输出类型 : “ shakeFile ” , 输出类型 : “ shakeExample.js' } );CSS 文件目录路径生成 JS 文件目录; 每个文件有多个命名导出 JS 样式对象,并以相应的 cs 文件命名
转换( dirPath, { 输出类型 : “ splitFile ” , 输出类型 : “ splitExample ” ) ;CSS 文件目录路径生成目录目录; 每个文件有一个默认导出 JS 样式对象, 并以相应的 fs 文件命名

文件转换示例

样本输入 : nav.css

.class1 {    
  color: 'red';  
}  
.class2 {
  color: 'blue'; 
}

例1:文件 - 创建一个文件,以一个单一对象导出,代表完全转换的 Css 树

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

输出 : Example1.js

export default {   
  class1: { color: 'red' },   
  class2: { color: 'blue' } 
}  

例2:摇动花样 - 创建多个导出文件, 代表转换的每个选择器
(用于不被使用的摇树样式对象)

convert(
   path.resolve(__dirname, './nav.css'), 
   { 
     outputType: 'shakeFile', 
     outputPath: path.resolve(__dirname, '../example2.js') 
   }
);

输出 : Example2.js

export const class1 = { color: 'red' };  
export const class2 = { color: 'blue' };  

例3:拆分文件 - 创建多个文件,其导出代表转换的css的每个选择器
(用于排除如果不执行树摇动而不会被使用的样式对象)

convert(
   path.resolve(__dirname, './nav.css'), 
   { 
     outputType: 'splitFile', 
     outputPath: path.resolve(__dirname, '../example3') 
   }
);

输出 : Example3/

class1.js

export default { color: 'red' };  

class2.js

export default { color: 'blue' }; 

媒体倒转

说明: mediaReverse 如果输出类型是 shakeFile 或者说 splitFile

有时将媒体查询中的样式分布到选择器属性中,而不是作为自己的属性,是有用的.
如果 mediaReverse 旗被设定为真实,倒置媒体查询的位置以在属性内.

nav.css

.class1 { color: 'red'; }  
  
@media (min-width:500px) {    
  .class1 { color: 'blue'; } 
}  
@media (min-width:1000px) {    
  .class1 { color: 'green'; } 
}  

如果未设置反向介质 :

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' }   
  }  
}  

如果媒体逆向设定为 真实数字 :

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 操作中自动运行.

pre-commit

这个钩子在跑 npm test 之前,允许 承诺被检查.

commit-msg

这个钩子验证您的承诺信息符合 One Amex 的常规 。 见 承诺
消息
节次 捐款额。 。 。 .


捐款

我们欢迎您对吉苏布的美国快递开放源代码社区感兴趣. 美国快递开放管理的任何开源项目的任何贡献者 来源地社区必须接受并签署一项协定,表明同意 条件如下。 除本协定赋予美国快递公司的权利外 以及美国快递公司发行的软件的接收者, 您保留所有 右,头衔,和利息,如果有的话, 对你的贡献。 请 填满 退出协议。 。 。 .

请随意打开拉取请求,看看 捐款额 用于输入详细格式.


许可证

在本项目下提供的任何捐款将受项目 Apache 许可证 2.0。 。 。 .


行为守则

本项目遵循 美国快递社区准则。 。 。 .
通过参与,你们将遵守这些准则.

版本与发布记录

@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 是什么?

将纯 CSS 转换为 JavaScript 字面对象或反应样式

如何安装 @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 和提交记录判断。