1.25.0 • Published 30 days ago

@swissquote/crafty-preset-postcss v1.25.0

Weekly downloads
7
License
Apache-2.0
Repository
github
Last release
30 days ago

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

ValueNote
falseDefault Behaviour, CSS stays in the JavaScript bundle, added to the page via a <style> tag.
trueWill enable extraction and name files with the [bundle]-[name].min.css pattern.
String / ObjectSupports 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

OptionTypeOptional ?RunnerDescription
extractCSSBoolean / String / ObjectYesWebpackThis will extract the CSS out of the bundle. See details above.
1.25.0

30 days ago

1.25.0-alpha.4

1 month ago

1.25.0-alpha.2

1 month ago

1.25.0-alpha.3

1 month ago

1.24.0-alpha.1

3 months ago

1.24.0

3 months ago

1.23.0-alpha.1

6 months ago

1.23.0-alpha.2

6 months ago

1.23.0

6 months ago

1.22.0-alpha.3

12 months ago

1.22.0-alpha.5

12 months ago

1.22.0-alpha.4

12 months ago

1.22.0

12 months ago

1.22.0-alpha.6

12 months ago

1.22.3

11 months ago

1.22.1

11 months ago

1.22.2

11 months ago

1.22.0-alpha.2

1 year ago

1.22.0-alpha.1

1 year ago

1.21.0

1 year ago

1.21.1

1 year ago

1.21.0-alpha.2

1 year ago

1.21.0-alpha.1

1 year ago

1.20.0-alpha.4

1 year ago

1.20.0

1 year ago

1.20.0-alpha.2

2 years ago

1.20.0-alpha.3

2 years ago

1.20.0-alpha.1

2 years ago

1.19.0

2 years ago

1.19.1

2 years ago

1.18.5-alpha.3

2 years ago

1.18.5-alpha.4

2 years ago

1.18.5-alpha.2

2 years ago

1.18.5-alpha.1

2 years ago

1.18.1

2 years ago

1.18.0

2 years ago

1.18.4

2 years ago

1.18.3

2 years ago

1.18.2

2 years ago

1.17.3-alpha.25

2 years ago

1.17.3-alpha.26

2 years ago

1.17.3-alpha.23

2 years ago

1.17.3-alpha.24

2 years ago

1.17.3-alpha.21

2 years ago

1.17.3-alpha.22

2 years ago

1.17.3-alpha.20

2 years ago

1.17.3-alpha.18

2 years ago

1.17.3-alpha.19

2 years ago

1.17.3-alpha.17

2 years ago

1.18.1-alpha.2

2 years ago

1.18.1-alpha.3

2 years ago

1.18.1-alpha.1

2 years ago

1.17.3-alpha.3

2 years ago

1.17.3-alpha.4

2 years ago

1.17.3-alpha.2

2 years ago

1.17.3-alpha.7

2 years ago

1.17.3-alpha.8

2 years ago

1.17.3-alpha.5

2 years ago

1.17.3-alpha.6

2 years ago

1.17.3-alpha.16

2 years ago

1.17.3-alpha.14

2 years ago

1.17.3-alpha.9

2 years ago

1.17.3-alpha.15

2 years ago

1.17.3-alpha.12

2 years ago

1.17.3-alpha.10

2 years ago

1.17.3-alpha.11

2 years ago

1.17.3-alpha.1

2 years ago

1.17.2

2 years ago

1.17.0-beta.1

2 years ago

1.17.1

2 years ago

1.16.3

2 years ago

1.16.1

2 years ago

1.16.1-beta.1

2 years ago

1.16.0

2 years ago

1.16.0-beta.5

2 years ago

1.16.0-beta.6

2 years ago

1.16.0-beta.3

2 years ago

1.16.0-beta.4

2 years ago

1.16.0-beta.2

2 years ago

1.16.0-beta.1

3 years ago

1.16.0-beta.0

3 years ago

1.15.0-beta.0

3 years ago

1.15.0

3 years ago

1.14.0

3 years ago

1.14.0-beta.8

3 years ago

1.14.0-beta.7

3 years ago

1.14.0-beta.3

3 years ago

1.13.7

3 years ago

1.13.6-beta.0

3 years ago

1.14.0-beta.2

3 years ago

1.14.0-beta.1

3 years ago

1.13.5

3 years ago

1.13.5-beta.1

3 years ago

1.13.5-beta.0

3 years ago

1.13.4

3 years ago

1.13.3

3 years ago

1.13.2

3 years ago

1.13.0

3 years ago

1.13.0-beta.2

3 years ago

1.13.0-beta.1

3 years ago

1.13.0-beta.0

3 years ago

1.12.1-beta.2

4 years ago

1.12.0

4 years ago

1.12.0-beta.0

4 years ago

1.11.1

4 years ago

1.11.0

4 years ago

1.10.0

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.0

5 years ago

1.7.3

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.1-beta.0

5 years ago

1.4.0

5 years ago

1.3.1-alpha.27

5 years ago

1.3.0

6 years ago

1.2.2-alpha.22

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago