Can angular-cli remove unused css?

穿精又带淫゛_ 提交于 2019-12-02 17:28:01

If you are using web pack then you can do it as:-

First, install purifycss-webpackusing npm i -D purifycss-webpack

module.export={
  plugins: [
    new ExtractTextPlugin('[name].[contenthash].css'),
    // Make sure this is after ExtractTextPlugin!
    new PurifyCSSPlugin({
      // Give paths to parse for rules. These should be absolute!
      paths: glob.sync(path.join(__dirname, 'app/*.html')),
    })
  ]

};

Visit the link below for the detailed understanding.

https://github.com/webpack-contrib/purifycss-webpack

Maybe you can have a look at https://github.com/Angular-RU/angular-cli-webpack. This gives you the possibility to change the webpack configuration without ejecting. And guess what the first example is: Removes unused selectors from your CSS. I have not tried it myself but it might be an option.

If you are ejected, i.e. ng eject. Then you can customize the webpack build to do most anything. I have a couple options turned on to minimize styles as part of the build with minifyCSS in two of the plugins.

  1. LoaderOptionsPlugin

    new LoaderOptionsPlugin({
      "sourceMap": false,
      "options": {
        "html-minifier-loader": {
            "removeComments": true,
            "collapseWhitespace": true,
            "conservativeCollapse": true,
            "preserveLineBreaks": true,
            "caseSensitive": true,
            "minifyCSS": true
        },
    
  2. HtmlWebpackPlugin

    new HtmlWebpackPlugin({
      "template": "./src\\index.ejs",
      "filename": "./index.html",
      "hash": true,
      "inject": true,
      "compile": true,
      "favicon": 'src/assets/Flag.png',
      "minify": {
          collapseWhitespace: true,
          removeComments: true,
          minifyCSS: true
        },
    

Don't know if this count as an answer because it's not really related to angular-cli, but I open my project in sublime text, and I launch UnusedCssFinder, which highlight all the unused properties in my css file.

module.export={
  plugins: [
    new ExtractTextPlugin('[name].[contenthash].css'),
    // Make sure this is after ExtractTextPlugin!
    new PurifyCSSPlugin({
      // Give paths to parse for rules. These should be absolute!
      paths: glob.sync(path.join(__dirname, 'app/*.html')),
    })
  ]

};

install purifycss webpack first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!