Skip to main content

A CLI to compile/minify SCSS & JS, and associated pre-commit hook.

Project description

web-compile

A CLI for compiling SCSS files to CSS, and associated pre-commit hook.

This CLI is a small wrapper around libsass-python, rJSmin and jinja2, which also aims to be compatible with pre-commit, and provide a pre-commit hook.

NOTE: The package in on alpha release, but looks to be working as intended, and will be trialled in sphinx-panels, and then sphinx-book-theme.

Installation

To use directly as a CLI:

pip install web-compile
web-compile --help

To use via pre-commit:

Add to your .pre-commit-config.yaml

-   repo: https://github.com/executablebooks/web-compile
    rev: v0.2.0
    hooks:
      - id: web-compile
        # optional
        args: [--config=config.yml]
        files: >-
            (?x)^(
                web-compile-config.yml|
                src/.*|
                dist/.*
            )$

By default, the hook will be initiated for all text file changes. But it is advisable to constrain this to the known configuration file, and input/output folders.

Configuration

You can can configure the compilation directly via the CLI or using a configuration file; simply replace - with _, CLI takes priority over the file:

$ 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 can point to any of three file-formats:

config.yml/config.yaml:

web-compile:
  sass:
    encoding: utf8
    files:
      tests/example_src/example1.scss: tests/example_dist/example1.[hash].css
      tests/example_src/example2.scss: tests/example_dist/example2.[hash].css
    format: compressed
    precision: 5
    sourcemap: true
  js:
    comments: false
    encoding: utf8
    files:
      tests/example_src/example1.js: tests/example_dist/example1.[hash].js
  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
  continue_on_error: true
  exit_code: 2
  test_run: false
  verbose: false
  quiet: false

config.toml:

[web-compile]
exit_code = 2
verbose = false
test_run = false
continue_on_error = true
quiet = false

[web-compile.sass]
precision = 5
sourcemap = true
format = "compressed"
encoding = "utf8"

[web-compile.js]
comments = false
encoding = "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
  }
}

Usage

SCSS Compilation

Simply map a source SCSS file to an output CSS filename.Paths should be relative to the configuration files and @import \ @useed partial files will also be read:

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

If you use the sourcemap option, then a sourcemap will also be output, and a sourceMappingURL comment added to the CSS:

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

If you add [hash] to the CSS filename, then it will be replaced with the content hash. Also, any existing files, matching the pattern, with a different hash will be removed:

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

JavaScript

Javascript files are minified and are configured similarly to SCSS.

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

Jinja Templates

Files can be created from Jinja templates. These are created after the SCSS and JS files are compiled, and allow for a special compiled_name filter, which converts ab input file path to the compiled file name:

src/file.j2:

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

dist/file.txt:

file.beabd761a3703567b4ce06c9a6adde55.css
other

Development

To run the tests:

pip install tox
tox -e py37

To test out the CLI:

tox -e py37-cli

To test the pre-commit hook:

tox -e try-repo

For code style:

pip install pre-commit
pre-commit run --all

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web-compile-0.2.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

web_compile-0.2.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file web-compile-0.2.0.tar.gz.

File metadata

  • Download URL: web-compile-0.2.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.9

File hashes

Hashes for web-compile-0.2.0.tar.gz
Algorithm Hash digest
SHA256 658c4b9967f7d29e2ee5f3bf72e4af5cf5ad9e325ec96c9083294df7e2863e09
MD5 5550cbc9c0320ef1701fd61e8db3570b
BLAKE2b-256 5d59ae0cb2ed0c0bb576f359edc2de50a3c6b9969927bf298e477b3ea3af5748

See more details on using hashes here.

File details

Details for the file web_compile-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: web_compile-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.9

File hashes

Hashes for web_compile-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48f23b745d6b1d9522d52bf938269840e6bd1f8809a4cc75ac44be6829c60df0
MD5 f37bc9c350ca476816a2ecf8da10a067
BLAKE2b-256 392ad303598b6d3828df9401e010699cdfd2f3de8aa4c3bf142c6712ebcf90c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page