0.2.0 • Published 6 years ago

webpack-config-css v0.2.0

Weekly downloads
2
License
CC0-1.0
Repository
github
Last release
6 years ago

#webpack-config-css

A curated CSS loader config for Webpack with CSS Modules, PostCSS, and JS styles.

build status coverage license version downloads

Usage

Install:

yarn add webpack-config-css

Add to your webpack.config.babel.js:

import css from `webpack-config-css`;

css({/* options */})({
  /* existing webpack configuration */
})

Options

NameDefaultDescription
extractNODE_ENV === productionEnable/Disable Extract Text Plugin
modulestrueEnable/Disable CSS Modules
minimizeNODE_ENV === productionEnable/Disable minification
jsStylestrueEnable/Disable support for JS styles
postcsstrueEnable/Disable PostCSS

Any additional properties of the options object are forwarded to CSS Loader.

Features

webpack-config-css comes with optimal default settings and requires zero additional config out of the box. However all of the included features are fully full configurable through the options argument.

CSS Modules

Locally scoped CSS is enable by default using CSS Modules. Set modules: false in the options to disable this and restore traditional global CSS selectors.

PostCSS

PostCSS (with autoprefixer and postcss-nesting) is enabled by default. Set postcss: false in the options to disable it.

To use a custom PostCSS config, pass a config object as postcss in the options or include a postcss.config.js (or equivalent) in your project root or in config/postcss/.

Static CSS Output

Extract Text Plugin is enabled by default when NODE_ENV is production. It can be disabled by setting extract: false in the options.

To change the extracted file name or to further configure the plugin, pass a plugin constructor object as extract in the options.

JS Styles

Support for styles written in JS is enabled by default using css-js-loader. CSS styles can be loaded from css.js files alongside regular .css files.

A .js.css file:

export const className = {
  color: 'red',
  fontSize: 24,
};

Yields:

.className {
  color: red;
  font-size: 24px;
};

To support JS styles written in ES6, babel-loader must be configured to load the .css.js files before webpack-config-css in your webpack config:

import flow from 'lodash/fp/flow'
import {loader} from 'webpack-partial';
import css from `webpack-config-css`;

export default flow(
  loader({
    test: /\.js$/,
    exclude: /node_modules/,
    loader: require.resolve('babel-loader'),
  }),
  css(),
)({
  /* existing webpack configuration */
})

To disable JS styles, pass jsStyles: false in the options.