Socket
Socket
Sign inDemoInstall

optimize-css-assets-webpack-plugin

Package Overview
Dependencies
360
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    optimize-css-assets-webpack-plugin

A Webpack plugin to optimize \ minimize CSS assets.


Version published
Weekly downloads
2.1M
increased by0.49%
Maintainers
1
Install size
10.4 MB
Created
Weekly downloads
 

Package description

What is optimize-css-assets-webpack-plugin?

The optimize-css-assets-webpack-plugin is a plugin for webpack that applies a chosen CSS optimizer/minifier on the CSS assets created by the CSS extraction plugin (like MiniCssExtractPlugin), optimizing and minimizing CSS files to ensure better load times and performance on the web.

What are optimize-css-assets-webpack-plugin's main functionalities?

CSS Optimization

This feature allows you to optimize and minimize CSS assets. The code sample shows how to include the OptimizeCSSAssetsPlugin in the webpack configuration to optimize CSS files.

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');

module.exports = {
  optimization: {
    minimizer: [
      new TerserJSPlugin({}),
      new OptimizeCSSAssetsPlugin({})
    ]
  }
};

Other packages similar to optimize-css-assets-webpack-plugin

Readme

Source

Optimize CSS Assets Webpack Plugin

A Webpack plugin to optimize \ minimize CSS assets.

:warning: For webpack v5 or above please use css-minimizer-webpack-plugin instead.

What does the plugin do?

It will search for CSS assets during the Webpack build and will optimize \ minimize the CSS (by default it uses cssnano but a custom CSS processor can be specified).

Solves extract-text-webpack-plugin CSS duplication problem:

Since extract-text-webpack-plugin only bundles (merges) text chunks, if it's used to bundle CSS, the bundle might have duplicate entries (chunks can be duplicate free but when merged, duplicate CSS can be created).

Installation:

Using npm:

$ npm install --save-dev optimize-css-assets-webpack-plugin

:warning: For webpack v3 or below please use optimize-css-assets-webpack-plugin@3.2.0. The optimize-css-assets-webpack-plugin@4.0.0 version and above supports webpack v4.

Configuration:

The plugin can receive the following options (all of them are optional):

  • assetNameRegExp: A regular expression that indicates the names of the assets that should be optimized \ minimized. The regular expression provided is run against the filenames of the files exported by the ExtractTextPlugin instances in your configuration, not the filenames of your source CSS files. Defaults to /\.css$/g
  • cssProcessor: The CSS processor used to optimize \ minimize the CSS, defaults to cssnano. This should be a function that follows cssnano.process interface (receives a CSS and options parameters and returns a Promise).
  • cssProcessorOptions: The options passed to the cssProcessor, defaults to {}
  • cssProcessorPluginOptions: The plugin options passed to the cssProcessor, defaults to {}
  • canPrint: A boolean indicating if the plugin can print messages to the console, defaults to true

Example:

var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('styles.css'),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.optimize\.css$/g,
      cssProcessor: require('cssnano'),
      cssProcessorPluginOptions: {
        preset: ['default', { discardComments: { removeAll: true } }],
      },
      canPrint: true
    })
  ]
};

License

MIT (http://www.opensource.org/licenses/mit-license.php)

Keywords

FAQs

Last updated on 23 Jun 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc