npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@swissquote/crafty-preset-postcss

v1.27.0

Published

<table> <tr><th>Compatible Runners</th><td>

Downloads

124

Readme

[TOC]

Description

The principle of CSS is easy to grasp, yet CSS is complicated to write at large scales.

We want to offer the best experience for writing CSS that is compatible with most browsers without the long configuration process.

Features

PostCSS is a CSS parser that supports plugins, countless plugins are maintained by a big community, we use a handful of them.

Features and examples

Linting

Stylelint is a wonderful tool to lint CSS in according to your rules, we have a custom configuration preset for Stylelint that comes pre-configured.

Stylelint is provided automatically in this preset by also including crafty-preset-stylelint.

Installation

npm install @swissquote/crafty-preset-postcss --save
module.exports = {
  presets: [
    "@swissquote/crafty-preset-postcss",
    "@swissquote/crafty-runner-webpack", // optional
    "@swissquote/crafty-runner-gulp" // optional
  ]
};

Usage with Webpack

Webpack defines the right loaders to support CSS.

To use it, add import "myfile.scss" in your Webpack imported file.

Hot Module Replacement

When setting hot: true in your crafty.config.js for your main JavaScript bundle, you can enable Hot Module Replacement.

With this, the CSS files imported in your Webpack bundles are automatically reloaded upon changes.

This is used inside crafty watch, the build mode will not take it into account.

Extracting CSS

By default, the CSS will be embedded in your bundle, but you can provide the extractCSS option to extract your styles using the MiniCssExtractPlugin.

extractCSS controls if the CSS should be embedded within the JavaScript bundle or extracted in a separate file. By default, the default is that the CSS content remains within the main file.

Possible options

| Value | Note | | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | false | Default Behaviour, CSS stays in the JavaScript bundle, added to the page via a <style> tag. | | true | Will enable extraction and name files with the [bundle]-[name].min.css pattern. | | String / Object | Supports all Official options. with the addition of [bundle] which is replaced by the bundle name. |

Side effects

Be careful when using extractCSS option and sideEffects: false in package.json of your project. Crafty is using css-loader and when you import a CSS file in your project, it needs to be added to the side effect list so it will not be unintentionally dropped in production mode.

Webpack docs and examples

Usage with Gulp

module.exports = {
  presets: [
    "@swissquote/crafty-preset-postcss",
    "@swissquote/crafty-runner-gulp"
  ],
  css: {
    app: {
      runner: "gulp", // optional if you have a single runner defined
      source: "css/app.scss",
      watch: ["css/**"]
    }
  }
};

Extending from your crafty.config.js

For this you need to add a postcss method to your crafty.config.js

module.exports = {
  /**
   * Represents the extension point for Postcss configuration
   * @param {Crafty} crafty - The instance of Crafty.
   * @param {ProcessorMap} config - The list of plugins currently configured
   * @param {Object} bundle - The bundle that is being prepared for build (name, input, source, destination)
   */
  postcss(crafty, config, bundle) {
    // Add postcss-fixes
    // We recommend that for all plugins you add, you set a "before",
    // because otherwise they run as last plugins and some other plugins might miss some optimizations
    // For example if your plugin adds a `calc()` or a `var()` postcss-calc and postcss-custom-properties will already have run
    config.processor("postcss-fixes").before("autoprefixer");

    // Replace postcss-csso with cssnano,
    // - only enabled in production
    // - runs before postcss-reporter
    // - use cssnano's default preset
    config.delete("postcss-csso");
    config
      .processor("cssnano")
      .enableIf(options => crafty.getEnvironment() === "production")
      .before("postcss-reporter")
      .setOptions({
        preset: "default"
      });

    // Change autoprefixer's options to disable autoprefixing for flexbox
    const autoprefixerOptions = config.processor("autoprefixer").options;
    autoprefixerOptions.flexbox = false;

    // Override CSS custom properties in code
    const customProperties = config.processor("postcss-custom-properties")
      .options;
    customProperties.variables = {
      color: "#fa5b35"
    };
  }
};

Read about the full API

Bundle Options

| Option | Type | Optional ? | Runner | Description | | ------------ | ------------------------- | ---------- | ------- | --------------------------------------------------------------- | | extractCSS | Boolean / String / Object | Yes | Webpack | This will extract the CSS out of the bundle. See details above. |