# @csstools/postcss-custom-functions 中文文档

规范地址：https://npmcn.com/package/%40csstools/postcss-custom-functions/

版本：1\.0\.1
npm 元数据更新时间：2026-10-02T15:54:11.451Z
中文译文更新时间：2026-11-11T03:31:09.121Z
软件包许可：MIT-0

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

原始 npm 页面：https://www.npmjs.com/package/%40csstools/postcss-custom-functions
原文对照：https://npmcn.com/package/%40csstools/postcss-custom-functions/?lang=original
项目主页：https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-custom-functions#readme
源码仓库：https://github.com/csstools/postcss-plugins

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

- 包体积（解压后）：28.8 kB，6 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=20\.19\.0
- 运行依赖：4 个
- peer 依赖：postcss ^8\.4
- 首次发布：2026-10-01T06:15:46.223Z
- 最新版本发布：2026-10-02T15:54:11.225Z
- 近 30 天下载量：1102939（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 2 个）

## 中文简介

自定义函数允许作者拥有与自定义属性相同的权限,但参数化

## 安装

```sh
npm install @csstools/postcss-custom-functions
```

也可以使用：`pnpm add @csstools/postcss-custom-functions`、`yarn add @csstools/postcss-custom-functions`、`bun add @csstools/postcss-custom-functions`

## README

# PostCSS 自定义函数 [![PostCSS Logo](https://postcss.github.io/postcss/logo.svg)](https://github.com/postcss/postcss)

`npm install @csstools/postcss-custom-functions --save-dev`

[PostCSS 自定义函数](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-custom-functions) 让您使用 `@function` 以下自定义函数 [CSS 自定义函数和混合函数 1](https://drafts.csswg.org/css-mixins-1/#function-rule)。 。 。 .

自定义函数仍然是早期的草稿 .  
此插件只是部分执行,以避免与最终规格相冲突.

不支持 :

-   无默认值的输入参数
-   返回默认以外的类型 `type(*)`
-   `@function` 内部定义 `@container` 或者说 `@scope`
-   `@function` 定义a `@layer` 这本身属于有条件的集团规则
-   `@function` 样式规则中的定义
-   除声明和有条件的集团规则外,其主体包含其他任何内容的定义
-   `result` 通过有条件的规则或规则解决全CSS关键字的描述符 `var()` 倒计时
-   确认输入的默认值与其参数类型相符

内部定义 `@layer`, (中文(简体) ), `@media` 财务报告和审定财务报表 `@supports` 被支持。 当一个名字 有几种活性定义,浏览器级联选择最强的定义.

不支持的定义留为正数 。 定义仅在每次 呼叫站点是由此插件所转换的, 因此调用站点被保留为 内部示例 `@keyframes` 或嵌入式有条件声明) 定义.

支持带有默认值的非类型参数,并可能使用任何值, 包括相对单位和 `var()` 参考文献.

默认值的输入参数由浏览器通过一个 生成 `@property` 登记。 默认值不是 计算独立(例如 `1em` 或者说 `var(--x)`无法使用) 作为 `initial-value` ,使函数不支持.

```css
@function --negative(--value) {
    result: calc(-1 * var(--value));
}

@function --double(--value <length>: 1px) {
    result: calc(2 * var(--value));
}

html {
    --gap: 1em;
    padding: --negative(var(--gap));
    margin: --double(4px);
}

/* becomes */

html {
    --gap: 1em;
    --_csstools-cf-1gice1e-1-arg-0: var(--gap);
    --_csstools-cf-1gice1e-0-result: calc(-1 * var(--_csstools-cf-1gice1e-1-arg-0));
    padding: var(--_csstools-cf-1gice1e-0-result);
    --_csstools-cf-1gice1e-3-arg-0: 4px;
    --_csstools-cf-1gice1e-2-result: calc(2 * var(--_csstools-cf-1gice1e-3-arg-0));
    margin: var(--_csstools-cf-1gice1e-2-result);
}
@property --_csstools-cf-1gice1e-3-arg-0 {
    syntax: "<length>";
    inherits: false;
    initial-value: 1px;
}
```

## 使用量

添加 [PostCSS 自定义函数](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-custom-functions) 对您的项目:

```bash
npm install postcss @csstools/postcss-custom-functions --save-dev
```

当作一个 [邮政](https://github.com/postcss/postcss) 插件 :

```js
const postcss = require('postcss');
const postcssCustomFunctions = require('@csstools/postcss-custom-functions');

postcss([
    postcssCustomFunctions(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
```

## 选项

### 保护

这个 `preserve` 选项决定原始标记是否 被保存下来。 默认情况下,它没有被保存.

```js
postcssCustomFunctions({ preserve: true })
```

```css
@function --negative(--value) {
    result: calc(-1 * var(--value));
}

@function --double(--value <length>: 1px) {
    result: calc(2 * var(--value));
}

html {
    --gap: 1em;
    padding: --negative(var(--gap));
    margin: --double(4px);
}

/* becomes */

@function --negative(--value) {
    result: calc(-1 * var(--value));
}

@function --double(--value <length>: 1px) {
    result: calc(2 * var(--value));
}

html {
    --gap: 1em;
    --_csstools-cf-1gice1e-1-arg-0: var(--gap);
    --_csstools-cf-1gice1e-0-result: calc(-1 * var(--_csstools-cf-1gice1e-1-arg-0));
    padding: var(--_csstools-cf-1gice1e-0-result);
    padding: --negative(var(--gap));
    --_csstools-cf-1gice1e-3-arg-0: 4px;
    --_csstools-cf-1gice1e-2-result: calc(2 * var(--_csstools-cf-1gice1e-3-arg-0));
    margin: var(--_csstools-cf-1gice1e-2-result);
    margin: --double(4px);
}

@property --_csstools-cf-1gice1e-3-arg-0 {
    syntax: "<length>";
    inherits: false;
    initial-value: 1px;
}
```

## 常见问题

### @csstools/postcss-custom-functions 是什么？

自定义函数允许作者拥有与自定义属性相同的权限,但参数化

### 如何安装 @csstools/postcss-custom-functions？

在项目目录运行 npm install @csstools/postcss-custom-functions，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：postcss（^8\.4）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @csstools/postcss-custom-functions 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @csstools/postcss-custom-functions 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @csstools/postcss-custom-functions 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=20\.19\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @csstools/postcss-custom-functions 可以商用吗？

@csstools/postcss-custom-functions 使用 MIT-0 许可。声明的许可为 MIT-0，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @csstools/postcss-custom-functions 的下载量有多少？

根据 npm 官方下载统计，@csstools/postcss-custom-functions 在 2026/10/11 – 2026/11/09 这 30 天内被下载 1,102,939 次，日均约 36,765 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @csstools/postcss-custom-functions 还在维护吗？

最新版本 1\.0\.1 发布于 2026/10/02（1 个月前）；至今共发布 2 个版本，其中过去 12 个月发布了 2 个；首次发布于 2026/10/01。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

