编译/缩小SCSS&JS的CLI,以及相关的预调试挂钩。

web-compile的Python项目详细描述


web编译

PyPI

用于编译/缩小SCSS&JS和相关联的预提交钩子的CLI。在

这个CLI是libsass-python的一个小包装, rJSminjinja2, 它也旨在与pre-commit兼容, 并提供预提交钩子。在

NOTE:该包在alpha版本中,但看起来工作正常, 将在sphinx-panels进行试验, 然后sphinx-book-theme。在

安装

要直接用作CLI,请执行以下操作:

pip install web-compileweb-compile --help

要通过预提交使用,请执行以下操作:

添加到您的.pre-commit-config.yaml

^{pr2}$

默认情况下,将为所有文本文件更改启动钩子。 但建议将其限制在已知的配置文件和输入/输出文件夹中。在

配置

您可以通过CLI或使用配置文件直接配置编译; 只需将-替换为_,CLI优先于该文件:

$ web-compile --help
Usage: web-compile [OPTIONS]  Compile web assets.Options:  --version                       Show the version and exit.  -c, --config FILE               Allowed extensions: json, toml, yml, yaml                                  [default: web-compile-config.yml]  --sass-files DICT               File mapping (config only)  --sass-format [nested|expanded|compact|compressed]                                  [default: compressed]  --sass-precision INTEGER        precision for numbers.  [default: 5]  --sass-sourcemap                Output source map.  --sass-encoding TEXT            [default: utf8]  --js-files DICT                 File mapping (config only)  --js-comments                   Keep comments starting with '/*!'.  --js-encoding TEXT              [default: utf8]  --jinja-files DICT              File mapping (config only)  --jinja-variables DICT          Global variable mapping (config only)  --jinja-encoding TEXT           [default: utf8]  --git-add / --no-git-add        Add new files to git index.  [default: True]  --continue-on-error             Do not stop on the first error.  --exit-code INTEGER             Exit code when files changed.  [default: 3]  --test-run                      Do not delete/create any files.  -q, --quiet                     Remove stdout logging.  -v, --verbose                   Increase stdout logging.  --help                          Show this message and exit.

--config可以指向三种文件格式中的任何一种:

config.yml/config.yaml

web-compile:sass:encoding:utf8files:tests/example_src/example1.scss:tests/example_dist/example1.[hash].csstests/example_src/example2.scss:tests/example_dist/example2.[hash].cssformat:compressedprecision:5sourcemap:truejs:comments:falseencoding:utf8files:tests/example_src/example1.js:tests/example_dist/example1.[hash].jsjinja:files:tests/example_src/example1.j2:tests/example_dist/example1.txttests/example_src/example3.j2:tests/example_dist/example3.txtvariables:a:bcontinue_on_error:trueexit_code:2test_run:falseverbose:falsequiet:false

config.toml

[web-compile]exit_code=2verbose=falsetest_run=falsecontinue_on_error=truequiet=false[web-compile.sass]precision=5sourcemap=trueformat="compressed"encoding="utf8"[web-compile.js]comments=falseencoding="utf8"[web-compile.sass.files]"tests/example_src/example1.scss"="tests/example_dist/example1.[hash].css""tests/example_src/example2.scss"="tests/example_dist/example2.[hash].css"[web-compile.js.files]"tests/example_src/example1.js"="tests/example_dist/example1.[hash].js"[web-compile.jinja.files]"tests/example_src/example1.j2"="tests/example_dist/example1.txt""tests/example_src/example3.j2"="tests/example_dist/example3.txt"[web-compile.jinja.variables]a="b"

config.json

{"web-compile":{"sass":{"files":{"tests/example_src/example1.scss":"tests/example_dist/example1.[hash].css","tests/example_src/example2.scss":"tests/example_dist/example2.[hash].css"},"precision":5,"sourcemap":true,"format":"compressed","encoding":"utf8"},"js":{"files":{"tests/example_src/example1.js":"tests/example_dist/example1.[hash].js"},"comments":false,"encoding":"utf8"},"jinja":{"files":{"tests/example_src/example1.j2":"tests/example_dist/example1.txt","tests/example_src/example3.j2":"tests/example_dist/example3.txt"},"variables":{"a":"b"}},"exit_code":2,"verbose":false,"test_run":false,"continue_on_error":true,"quiet":false}}

使用

SCSS编译

只需将源SCSS文件映射到输出CSS文件名.路径应该是相对于配置文件的,并且@import\@useed部分文件也将被读取:

web-compile:sass:files:src/file.scss:dist/file.css
$ web-compile
src/
    _partial.scss
    file.scss
dist/
    file.css

如果使用sourcemap选项,则还将输出一个sourcemap, 以及添加到CSS的sourceMappingURL注释:

web-compile:sass:files:src/file.scss:dist/file.csssourcemap:true
$ web-compile
dist/
    file.css
    file.scss.map.json

如果将[hash]添加到CSS文件名中,那么它将被替换为内容哈希。 此外,将删除与模式匹配且具有不同哈希的任何现有文件:

web-compile:sass:files:src/file.scss:dist/file.[hash].css
$ web-compile
dist/
    file.beabd761a3703567b4ce06c9a6adde55.css

JavaScript

Javascript文件被缩小,并且配置类似于scs。在

web-compile:js:files:src/file.js:dist/file.[hash].js
$ web-compile
dist/
    file.beabd761a3703567b4ce06c9a6adde55.js

Jinja模板

可以从Jinja模板创建文件。 它们是在编译SCSS和JS文件后创建的,并允许使用特殊的compiled_name过滤器, 它将ab输入文件路径转换为编译的文件名:

src/file.j2

{{"src/file.scss"|compiled_name}}{{var1}}
web-compile:sass:files:src/file.scss:dist/file.[hash].cssjinja:files:src/file.j2:dist/file.txtvariables:var1:other
$ web-compile

dist/file.txt

file.beabd761a3703567b4ce06c9a6adde55.css
other

发展

要运行测试:

pip install toxtox -e py37

要测试CLI,请执行以下操作:

tox -e py37-cli

要测试预提交钩子:

tox -e try-repo

对于代码样式:

pip install pre-commitpre-commit run --all

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
二进制字符串的java NumberFormatExceprion   java如何在Neo4j中查找/匹配/选择标识符名称   java哪一年的日期与原始年份相同?   java什么时候JAXB可以用于Json而不是XML?   java使用PayPal权限API进行PayPal支付   java Getting error在使用安卓 vision api扫描二维码时加载图像失败   java直接突出显示RichTextFX中的一个文本范围   java Resolve@RegisteredAuth2AuthorizedClient,其令牌在spring Security 5.2的spring server之外获得。十、   yyyymmddhhmmss的Java正则表达式   java我试图将google recaptcha与spring mvc集成,但GreCaptCharResponse总是返回false。这里有一些代码   java使用JsonPath将文本转换为json   java无法解析符号。Maven依赖项已就位,但代码为红色   java使用循环查找范围内具有不同数字的数字   java这个SwingWorker是否不重用ThreadPoolExecutor中的线程?