Détail du package

@metalsmith/sass

metalsmith541LGPL-3.0-or-later1.12.0

A Metalsmith plugin to compile SASS/SCSS files

sass, scss, styling, metalsmith-plugin

readme

@metalsmith/sass

A Metalsmith plugin to compile SASS/SCSS files

metalsmith: core plugin npm: version ci: build code coverage license: MIT

Compile SASS/SCSS source & lib files to CSS using dart-sass.

Features

  • Automatically compiles all .scss/.sass files in Metalsmith.source() and removes all _partial.scss/sass files from the output.
  • Uses sensible defaults according to metalsmith.env('NODE_ENV').
  • Add files from outside the source dir with the entries option. Specify 'relative/to/dir/style.scss': 'relative/to/destination/style.css' key-value pairs in the entries object for all root stylesheets.
  • Provides sourcemaps and access to all advanced sass options except async.
  • Compatible with @metalsmith/postcss (including sourcemaps)

Installation

NPM:

npm install @metalsmith/sass

Yarn:

yarn add @metalsmith/sass

Usage

Pass @metalsmith/sass to metalsmith.use :

import sass from '@metalsmith/sass'
const isDev = metalsmith.env('NODE_ENV') === 'development'

// compile all scss/sass files in metalsmith.source()
metalsmith.use(sass()) // defaults

metalsmith.use(sass({  // explicit defaults
  style:  isDev ? 'expanded' : 'compressed',
  sourceMap: isDev,
  sourceMapIncludeSources: isDev,
  loadPaths: ['node_modules']
  entries: {
    // add scss entry points from
   'lib/outside-source.scss': 'relative/to/dest.css'
  }
}))

If metalsmith.env('NODE_ENV') is explicitly set to development,@metalsmith/sass will automatically generate sourcemaps and will not minify the output.

Entries

If you had a blog project with 2 SCSS stylesheets, index.scss to be loaded everywhere, and blogposts.scss only on blog post pages:

my-blog
├── lib
|   ├── index.scss
│   └── _lib-partial.scss
└── src
    ├── blog.html
    ├── index.html
    └── css
        ├── _in-source-partial.scss
        └── blogposts.scss

...you could specify the following config:

metalsmith.use(
  sass({
    entries: {
      'lib/index.scss': 'css/index.css'
    }
  })
)

Note: the keys in the entries option are relative to Metalsmith.directory, while the values are relative to Metalsmith.destination.

With this setup metalsmith will generate the following build:

build
  ├── css
  │   ├── blogposts.css
  │   └── index.css
  ├── blog.html
  └── index.html

Partial _in-source-partial.scss is automatically removed from the build after compilation. When not explicitly specified in the config, in-source .scss/.sass files are added as entries '<source>/file.scss': <dest>/file.css. If you want to move or rename the in-source SCSS entries in the build, specify them explicitly in the entries config. For example let's write the blogpost.scss to css/blog/index.css instead, without touching our source dir structure:

metalsmith.use(
  sass({
    entries: {
      'lib/index.scss': 'css/index.css',
      'src/blogposts.scss': 'css/blog/index.css'
    }
  })
)

The result:

build
  ├── css
  │   ├── index.css
  │   └── blog
  │       └── index.css
  ├── blog.html
  └── index.html

@import/ @use partials

Sass partials are processed by dart-sass. @metalsmith/sass will gracefully handle in-source partials, but they will be read into memory by Metalsmith. If you don't need to preprocess sass partials with any other metalsmith plugin you can save some disk reads by storing partials outside the source directory, eg:

my-blog
├── lib
│   ├── _partial1.scss
│   └── _partial2.scss
└── src
    └── css
        └── index.scss

Passing metadata to SASS files

You can pass metadata to SASS files inside Metalsmith.source() through front-matter in the file or global metadata. For example, let's pass metalsmith theme metadata to SASS and pre-compile with @metalsmith/in-place and jstransformer-handlebars (notice the final .hbs extension):

index.scss.hbs

---
fontfamily: 'Arial, sans-serif'
---
$color-primary: {{ theme.color.primary }};
$color-background: {{ theme.color.background }};

body {
  font-family: {{ fontfamily }};
  color: $color-primary;
  background-color: $color-background;
}

Just take care to run the in-place plugin before sass:

import Metalsmith from 'metalsmith'
import inPlace from '@metalsmith/in-place'
import sass from '@metalsmith/sass'
import { fileURLToPath } from 'url'
import { dirname } from 'path'

const __dirname = dirname(fileURLToPath(import.meta.url))

Metalsmith(__dirname)
  .metadata({
    theme: {
      color: {
        primary: '#333444',
        background: '#EEEFFF'
      }
    }
  })
  .use(inPlace('jstransformer-handlebars'))
  .use(sass())
  .build((err) => {
    if (err) throw err
    console.log('Success!')
  })

Debug

To enable debug logs, set the DEBUG environment variable to @metalsmith/sass*:

metalsmith.env('DEBUG', '@metalsmith/sass*')

Alternatively you can set DEBUG to @metalsmith/* to debug all Metalsmith core plugins.

CLI usage

To use this plugin with the Metalsmith CLI, add @metalsmith/sass to the plugins key in your metalsmith.json file:

{
  "plugins": [
    {
      "@metalsmith/sass": {
        "style": "compressed",
        "sourceMap": false,
        "sourceMapIncludeSources": false,
        "loadPaths": ["node_modules"],
        "entries": {
          "lib/scss/index.scss": "assets/styles.css"
        }
      }
    }
  ]
}

Node compatibility

This plugin runs on Node >= 14.18.0. If you need to compile sass/scss on earier Node versions, use metalsmith-sass which uses the (no longer canonical) lib-sass.

License

LGPL-3.0

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

1.12.0

  • Updates sass 1.80.5 -> 1.85.0 2c74f0f

1.11.0

1 November 2024

  • Updates sass 1.75.0 -> 1.80.5 49ba7ac

1.10.0

22 April 2024

  • Updates sass 1.71.1 -> 1.75.0 ba0429d

1.9.0

22 February 2024

  • Updates sass 1.69.5 -> 1.71.1 a6e1bc0
  • Drop support for 4 outdated Node.js 14 minor versions (node: protocol imports) adb2240

1.8.1

23 December 2023

  • Updates sass 1.69.3 -> 1.69.5 4268260

1.8.0

17 October 2023

  • Updates sass 1.66.1 -> 1.69.3 b69b8c8
  • Minor corrections in README 62455fd

1.7.0

22 August 2023

  • Updates sass 1.63.3 -> 1.66.1 f1a6e94

1.6.0

12 June 2023

  • Updates sass 1.62.1 -> 1.63.3 fabbfca

1.5.0

6 June 2023

  • Update sass 1.59.3 -> 1.62.1 867f19e
  • Includes source maps for better debugging d40b590
  • Drop support for Node < 14.14.0 (Node 12 EOL = 2023-04-30) 5b7f9fb

1.4.0

18 March 2023

  • Updates sass 1.58.0 -> 1.59.3 b607e1c

1.3.0

4 February 2023

  • Updates sass from 1.56.1 -> 1.58.0 30730cf

1.2.0

16 November 2022

  • Fixes #5: normalize destination entries forward slash to backslash on Windows #5
  • Updates sass 1.54.3 -> 1.56.1, drops support for metalsmith < 2.5.0 64a47c3
  • Depend on metalsmith.env('NODE_ENV') instead of process.env.NODE_ENV 5193f71
  • Updates README, docs, adds async perf benchmark comment 16174d7
  • Replace custom debug with metalsmith.debug 07ec185
  • Enhancement: renames default export to sass (more straightforward for import type hints) 10db991
  • tests: fix tests on Windows 132e194
  • Ensures metalsmith.match operates on live files object in repeat runs b1cebd5

1.1.0

7 August 2022

  • Provides dual ESM/CJS exports 057423c
  • Add Typescript definitions f5485d3
  • Updates sass 1.53.0 -> 1.54.3 04cd68c

1.0.1

30 June 2022

1.0.0

9 February 2022

  • Update sass 1.49.0 -> 1.49.7 upgrade package-lock d98dac6
  • deps: upgrade devDependencies, drop Node < 12, format f6dc222
  • feat: update docs, allow preprocessing sass metadata, add tests f7f5deb
  • deps: require metalsmith^2.4.1 42c13c3
  • tests: add implicit entries tests 75dcc24
  • feat: allow parameter-less defaults, optimize for in-sourcedir sass files a5fadb5
  • feat: allow front-matter & plugin interop for sass files in Metalsmith.source() fbe4400
  • feat: add source file metadata to output when applicable 395dc34
  • fix: Move options log to plugin execution cbcbfc2

0.2.0

22 January 2022

  • Load node_modules imports, disable async, fix Jsdoc typo, add test 1599dcf
  • Update README.md, sourceMapIncludeSources when NODE_ENV=dev bb713ce
  • deps: Update sass 1.48.0 to 1.49.0 73be67c

0.1.0

18 January 2022