CSS Usage

Builder has built-in multiple style resource processing capabilities, including Less / Sass preprocessor, PostCSS, CSS Modules, CSS inline and CSS compression.

In addition, Builder also provides multiple configs to customize the compile rules of style resources.

Using Less, Sass and Stylus

The Builder has built-in community popular CSS preprocessors such as Less, Sass.

By default, you don't need to configure anything for Less and Sass. If you need to customize loader config, you can configure tools.less, tools.sass to set it up.

You can also use Stylus in Builder, just install the Stylus plugin provided by Builder, please refer to Stylus Plugin for usage.

Using PostCSS

Builder has built-in PostCSS to transform the CSS code. You can configure the postcss-loader via tools.postcss.

export default {
  tools: {
    postcss: opts => {
      const viewportPlugin = require('postcss-px-to-viewport')({
        viewportWidth: 375,
      });
      opts.postcssOptions.plugins.push(viewportPlugin);
    },
  },
};

Builtin PostCSS plugins

Builder has some builtin PostCSS plugins, which will perform the following transformations on CSS:

.my-table {
  & td {
    text-align: center;

    &.c {
      text-transform: uppercase;
    }
  }
}
:root {
  --main-bg-color: pink;
}

body {
  background-color: var(--main-bg-color);
}

CSS Minify

Usually, in production environments we compress static assets such as CSS, JS, etc. to achieve better transfer efficiency.

Builder automatically compresses CSS code at production build time with css-minimizer-webpack-plugin (The compression tool used at the bottom is cssnano).

You can configure tools.minifyCss to make it more customizable.

About cssnano

cssnano is a tool for optimizing and minifying CSS files. It reduces the size of CSS files by removing unused rules, merging similar rules, removing comments and whitespace, and converting length units, among other techniques, to improve the loading speed of websites.

Inline CSS Files

By default, Builder will extract CSS into a separate .css file and output it to the dist directory.

If you want to inline styles into your JS file, you can set output.disableCssExtract to true to disable CSS extraction logic.When the JS file is requested by the browser, JS dynamically inserts the <style> tag into the Html to load the CSS styles.

export default {
  output: {
    disableCssExtract: true,
  },
};

This will increase the size of your JS Bundle, so it is usually not recommended to disable the CSS extraction.

Import CSS in node_modules

You can directly import CSS files in node_modules.

  • Import in a component:
src/App.tsx
// Import the Arco Design style:
import '@arco-design/web-react/dist/css/arco.css';
  • Import in a style file:
src/App.css
/* reference normalize.css */
/* https://github.com/necolas/normalize.css */
@import 'normalize.css';

body {
  /* */
}