# @americanexpress/css-to-js 中文文档

规范地址：https://npmcn.com/package/%40americanexpress/css-to-js/

版本：1\.0\.1
npm 元数据更新时间：2022-04-04T13:48:11.155Z
中文译文更新时间：2026-11-06T15:32:01.594Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40americanexpress/css-to-js
原文对照：https://npmcn.com/package/%40americanexpress/css-to-js/?lang=original
项目主页：https://github.com/americanexpress/css-to-js
源码仓库：https://github.com/americanexpress/css-to-js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：116 kB，18 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：1 个
- 首次发布：2020-02-11T17:04:36.314Z
- 最新版本发布：2020-03-18T15:36:46.480Z
- 近 30 天下载量：2902（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

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

## 安装

```sh
npm install @americanexpress/css-to-js
```

也可以使用：`pnpm add @americanexpress/css-to-js`、`yarn add @americanexpress/css-to-js`、`bun add @americanexpress/css-to-js`

## README

# ![CSS to JS - One Amex](https://github.com/americanexpress/css-to-js/raw/master/css-to-js.png)

[![npm](https://img.shields.io/npm/v/@americanexpress/css-to-js)](https://www.npmjs.com/package/@americanexpress/css-to-js) [![Travis (.org) branch](https://img.shields.io/travis/americanexpress/css-to-js/master)](https://travis-ci.org/americanexpress/css-to-js)

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

## 雇工吗

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

> 把你的简历发给 [aexp.com (中文(简体) )](mailto:oneamex.careers@aexp.com)

  

## 目录

-   [特征](https://npmcn.com/package/%40americanexpress/css-to-js/#readme-Features)
-   [使用量](https://npmcn.com/package/%40americanexpress/css-to-js/#readme-Usage)
-   [API 密码](https://npmcn.com/package/%40americanexpress/css-to-js/#readme-API)
-   [巨钩](https://npmcn.com/package/%40americanexpress/css-to-js/#readme-Git%20Hooks)
-   [捐款](https://npmcn.com/package/%40americanexpress/css-to-js/#readme-Contributing)

  

## \* 特性

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

  

## 使用量

安装 :

```bash
npm install @americanexpress/css-to-js --save-dev  
```

CLI: 爱丽丝

```bash
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.
```

节点 :

```bash
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`**

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

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

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

```js
export default { color: 'red' };  
```

**`class2.js`**

```js
export default { color: 'blue' }; 
```

#### 媒体倒转

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

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

**`nav.css`**

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

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

```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 的常规 。 见 **承诺  
消息** 节次 [捐款额](https://npmcn.com/package/%40americanexpress/css-to-js/CONTRIBUTING.md)。 。 。 .

  

## 捐款

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

请随意打开拉取请求,看看 [捐款额](https://npmcn.com/package/%40americanexpress/css-to-js/CONTRIBUTING.md) 用于输入详细格式.

  

## 许可证

在本项目下提供的任何捐款将受项目 [Apache 许可证 2.0](https://npmcn.com/package/%40americanexpress/css-to-js/LICENSE.txt)。 。 。 .

  

## 行为守则

本项目遵循 [美国快递社区准则](https://npmcn.com/package/%40americanexpress/css-to-js/CODE_OF_CONDUCT.md)。 。 。 .  
通过参与,你们将遵守这些准则.

## 常见问题

### @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 和提交记录判断。

