Can angular-cli remove unused css?

后端 未结 6 1460
时光说笑
时光说笑 2020-12-23 21:22

so far the smallest bundle I can create with angular cli is by running

ng build --aot true -prod

I was wondering if the build p

相关标签:
6条回答
  • 2020-12-23 21:51
    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

    0 讨论(0)
  • 2020-12-23 21:54

    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.

    0 讨论(0)
  • 2020-12-23 22:03

    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
          },
      
    0 讨论(0)
  • 2020-12-23 22:04

    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

    0 讨论(0)
  • 2020-12-23 22:06

    I did some research recently about this, but I could not find any really safe way of how to remove unused CSS. However I came across some tools which would help you detect dead-code in VS Code. There is an extention which is not perfect but looks promising. Also I did some investigation of how to remove unused Angular Material CSS (if you use it) and created a video about it. You can check this out here.

    But at least now (in 2020) there is no any reliable way to achieve what you want and see also an answer from Angular Core Team member about this topic

    0 讨论(0)
  • 2020-12-23 22:08

    In Angular the best option you have is to create a separate CSS file for each component and use ViewEncapsulated.Emulated.

    And in this file you will add just CSS used by this component. You can discover styles used by each page with "coverge" from Google Chrome

    0 讨论(0)
提交回复
热议问题