@civictheme/drupal-attribute

中文文档
2.1.0发布于 2026/10/31Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 3348

Emulate Drupal Attribute in JavaScript.

原始使用文档
文档目录11 节
  1. Using with twig.js
  2. Prerequisites
  3. Installation
  4. Usage
  5. Build the test suite
  6. Execute the test suite
  7. Build the main artifact
  8. Releasing
  9. Writing and executing tests
  10. Contributing
  11. License

drupal-attribute

NPM version

This is a fork of drupal-attribute by Eric MORAND, maintained by CivicTheme. All credit for the original implementation belongs to the upstream authors.

This is not drupal-attribute. This is the project behind drupal-attribute.

Using with twig.js

createAttribute() is the Twig-facing entry point, equivalent to Drupal's create_attribute(). Register it with twig.js:

import Twig from 'twig';
import { createAttribute } from '@civictheme/drupal-attribute';

Twig.extendFunction('create_attribute', createAttribute);

twig.js stamps every object literal compiled from a template with an own, enumerable _keys array recording the authored key order. A collection from this package never renders it, whether you build it with createAttribute(), the constructor, or setAttribute() — so no consumer has to strip it.

What createAttribute() adds is ordering. The create_attribute shipped by drupal-twig-extensions walks the mapping with Object.keys(), which returns the remaining keys in the wrong order — integer-like keys hoist to the front, and twig.js's expression stack assigns the rest out of order:

<button{{ create_attribute({'data-slider-previous': '', 'class': 'btn'}) }}></button>
<!-- drupal-twig-extensions -->
<button class="btn" data-slider-previous=""></button>

<!-- createAttribute -->
<button data-slider-previous="" class="btn"></button>

createAttribute() reads _keys for its ordering, so output matches what Drupal's PHP Attribute produces for the same template. Objects that carry no _keys — anything reaching the function from the render context rather than from a template literal — keep their own enumeration order.

Upstream tracks the same defect as drupal-twig-extensions#1. Note that addDrupalExtensions(Twig) runs on every compiled Twig module import and re-registers its own implementation, so depending on module evaluation order you may need to register through a wrapper rather than once at startup:

const extendFunction = Twig.extendFunction.bind(Twig);

Twig.extendFunction = (name, definition) =>
  extendFunction(name, name === 'create_attribute' ? createAttribute : definition);

Twig.extendFunction('create_attribute', createAttribute);

Prerequisites

  • Node.js 20 and above
  • Rollup installed globally

It is also recommended to have the following tools installed globally to ease the writing of tests and the tracking of the code coverage:

Installation

The project can be installed by running the following command:

npm install

Usage

Build the test suite

npm run build:test

This command outputs the test suite to the directory src/test/target.

Execute the test suite

The test suite can be executed by running the following command:

node src/test/target/index.mjs

Or, for convenience:

npm t

Build the main artifact

The main artifact can be built by running the following command:

npm run build:main

This command outputs the main artifact to the directory src/main/target, under version SNAPSHOT.

The version can be provided by populating the VERSION environment variable accordingly:

VERSION=1.2.3 npm run build:main

Releasing

npm publishing requires an interactive OTP now that tokens no longer bypass 2FA, so releases run from a maintainer machine rather than CI:

git checkout main
git pull
npm ci
npm run lint
npm run typecheck
npm test
npm version minor -m "Released version v%s."   # or patch / major
npm publish --access public --tag latest
git push origin main --follow-tags
gh release create vX.Y.Z --title vX.Y.Z --notes-file RELEASE-NOTES-X.Y.Z.md

Writing and executing tests

Assuming one want to execute the test located in src/test/foo/bar.ts, one would run:

tsx src/test/foo/bar.ts

It is even possible - and recommended - to track the coverage while writing tests:

odz tsx src/test/foo/bar.ts

Of course, it is also perfectly possible to pipe the result of the test to your favorite tap formatter:

src/test/foo$ tsx bar.ts | tap-nyan
 9   -_-_-_-_-_,------,
 0   -_-_-_-_-_|   /\_/\ 
 0   -_-_-_-_-^|__( ^ .^) 
     -_-_-_-_-  ""  "" 
  Pass!

Contributing

  • Fork this repository
  • Code
  • Implement tests using tape
  • Issue a pull request keeping in mind that all pull requests must reference an issue in the issue queue

License

Apache-2.0 © Eric MORAND

Files from the original project have been modified by CivicTheme contributors since 2025. See the commit history for the full list of changes.

版本与发布记录

@civictheme/drupal-attribute 共发布 3 个版本,首次发布于 2026/08/04;过去 12 个月发布了 3 个版本。

版本发布日期距今
2.1.0latest2026/10/319 天前
2.0.12026/08/043 个月前
2.0.02026/08/043 个月前

关于 @civictheme/drupal-attribute 的常见问题

@civictheme/drupal-attribute 是什么?

Emulate Drupal Attribute in JavaScript.

如何安装 @civictheme/drupal-attribute?

在项目目录运行 npm install @civictheme/drupal-attribute,或使用 pnpm add / yarn add / bun add。

@civictheme/drupal-attribute 支持 TypeScript 吗?

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

@civictheme/drupal-attribute 支持 ESM 还是 CommonJS?

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

@civictheme/drupal-attribute 需要什么版本的 Node.js?

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

@civictheme/drupal-attribute 可以商用吗?

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

@civictheme/drupal-attribute 的下载量有多少?

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

@civictheme/drupal-attribute 还在维护吗?

最新版本 2.1.0 发布于 2026/10/31(9 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/08/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。