2letterbem 中文文档教程

发布于 5年前 浏览 25 项目主页 更新于 3年前

2 Letter Bem

将你的 css 类压缩到几个字符(可能是 2 个),然后你可以在你的 html 中使用它们。 这是作为 npm 脚本运行的独立包。

Compressing css this way + gzip = negligible results

示例 - bootstrap 4.4.1 css 缩小和压缩:

  • bootstrap.min.css.gz -> 23.6KB
  • 2 letter bem version -> 22.8KB
  • However, this can be used with html, to shave a kilobyte or two. This could be used to obfuscate your work.

输入 css 文件:

.something { ... }
.something__block { ... }
.something__block:hover .that-thing { ... }
.js-action-state something__block.active { ... }

输出 - 创建 2 个文件 css:

.a { ... }
.b { ... }
.b:hover.c { ... }
.js-action-state b.d { ... }

json:

{
"something": "a",
"something__block": "b",
"that-thing": "c",
"active" "d"
}

任何在你的 css 中以 .js 开头的东西都不会被压缩。 您可以指定您不想被触及的类的 whiteList。 它们也不会出现在 json 文件中。 您现在可以将 json 文件用作 html 的字典。

PHP 中的简单示例:

function klass($cssName){
    $output = [];
    $cssNames = explode(' ', $cssInput);
    foreach ($cssNames as $cssName) {
      if (array_key_exists($cssName, $allYourClassNamesReadFromJSON)) {
        $output[] = $allYourClassNamesReadFromJSON[$cssName];
      } else {
        $output[] = $cssName;
      }
    }
    return implode(' ', $output);
}
<div class="klass('something')"></div> renders as <div class="a"></div>

How to use

以 package.json 为例:

{
...
 "config": {
    "2letterbem": {
      "whiteList": [
        ".is-active",
        ".wf-inactive",
      ]
    }
  },
  dependencies: { ... }
  devDependencies: { ... }
  scripts: {
   "2letterbem": "node ./node_modules/2letterbem/2letterbem.js --i ./inputFile.css --o ./outputFile.css --j './jsonMapFile.json"
  }
...

或 package.json 配置中的所有内容

{
  ...
  "config": {
    "2letterbem": {
      "cssPath": "./inputFile.css",
      "jsonSpace": 2,
      "outputCssPath": "./outputFile.css", 
      "outputJsonPath": "./jsonMapFile.json",
      "permutationArgs": {
        "maxSize": 2,
        "recursive": true
      },
      "permutationLetters": "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
      "whiteList": [
        ".is-active",
        ".wf-inactive"
      ]
    }
  },
  scripts: {
    "2letterbem": "node ./node_modules/2letterbem/2letterbem.js"
  },
  ...
}

2 Letter Bem

Compresses your css classes down to a few characters ( probably 2 ), which you can then use in your html. This is standalone package that is run as a npm script.

Compressing css this way + gzip = negligible results

Example - bootstrap 4.4.1 css minified and gzipped:

  • bootstrap.min.css.gz -> 23.6KB
  • 2 letter bem version -> 22.8KB
  • However, this can be used with html, to shave a kilobyte or two. This could be used to obfuscate your work.

input css file:

.something { ... }
.something__block { ... }
.something__block:hover .that-thing { ... }
.js-action-state something__block.active { ... }

output - creates 2 files css:

.a { ... }
.b { ... }
.b:hover.c { ... }
.js-action-state b.d { ... }

json:

{
"something": "a",
"something__block": "b",
"that-thing": "c",
"active" "d"
}

Anything prepended with .js in your css is not compressed. You can specify a whiteList of classes that you don't want to be touched. They won't appear in the json file either. You can use the json file as dictionary now for your html.

Simple example in PHP:

function klass($cssName){
    $output = [];
    $cssNames = explode(' ', $cssInput);
    foreach ($cssNames as $cssName) {
      if (array_key_exists($cssName, $allYourClassNamesReadFromJSON)) {
        $output[] = $allYourClassNamesReadFromJSON[$cssName];
      } else {
        $output[] = $cssName;
      }
    }
    return implode(' ', $output);
}
<div class="klass('something')"></div> renders as <div class="a"></div>

How to use

In package.json as an example:

{
...
 "config": {
    "2letterbem": {
      "whiteList": [
        ".is-active",
        ".wf-inactive",
      ]
    }
  },
  dependencies: { ... }
  devDependencies: { ... }
  scripts: {
   "2letterbem": "node ./node_modules/2letterbem/2letterbem.js --i ./inputFile.css --o ./outputFile.css --j './jsonMapFile.json"
  }
...

or everything in package.json config

{
  ...
  "config": {
    "2letterbem": {
      "cssPath": "./inputFile.css",
      "jsonSpace": 2,
      "outputCssPath": "./outputFile.css", 
      "outputJsonPath": "./jsonMapFile.json",
      "permutationArgs": {
        "maxSize": 2,
        "recursive": true
      },
      "permutationLetters": "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
      "whiteList": [
        ".is-active",
        ".wf-inactive"
      ]
    }
  },
  scripts: {
    "2letterbem": "node ./node_modules/2letterbem/2letterbem.js"
  },
  ...
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文