14

我有一个使用 Angular CLI 创建的 Angular 项目,所以我有用于配置构建的 angular.json 文件。

我想集成 PurgeCSS,因为它似乎是减小 CSS 包大小的好工具。

PurgeCSS 网站上,他们解释了如何与 Webpack 集成。我找到了一个教程,它解释了如何添加自定义 webpack 与 Angular CLI 的集成

有人有 PurgeCSS 与 Angular CLI 项目集成的成功示例吗?

4

4 回答 4

26

还有另一种更符合 Angular CLI 和 Webpack 配置的方式:

你需要安装

npm install -D @angular-builders/custom-webpack postcss-scss @fullhuman/postcss-purgecss

然后创建一个webpack.config.js配置文件:

const purgecss = require('@fullhuman/postcss-purgecss')

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: 'postcss-loader',
        options: {
          ident: 'postcss',
          syntax: 'postcss-scss',
          plugins: () => [
            require('postcss-import'),
            require('autoprefixer'),
            purgecss({
              content: ['./**/*.html'],
              // Example to let PurgeCss know how to exclude cdk and mat prefixes if your using Angular CDK and Angular Material
              whitelistPatterns: [/^cdk-|mat-/]
            })
          ]
        }
      }
    ]
  }
};

然后更改您的angular.json文件以使用此新配置:

...
"build": {
  "builder": "@angular-builders/custom-webpack:browser",
  "options": {
    "customWebpackConfig": {
      "path": "webpack.config.js"
    },
    ...
  }
},
...

确保在捆绑最终应用程序时仅在生产模式下运行此配置。

希望这可以帮助。

我创建了一个帖子来详细解释如何 - https://medium.com/@joao.a.edmundo/angular-cli-tailwindcss-purgecss-2853ef422c02

于 2019-12-27T11:30:40.203 回答
16

我创建了这个 bash 脚本以在我的 Angular 应用程序中使用 PurgeCSS。它将我的“styles.css”文件大小从 63kb 减少到只有 3kb!希望它也适合你。

脚步:

  1. purgecss.sh在项目文件夹中创建一个名为的新文件
  2. 将下面的代码插入purgecss.sh文件
  3. ./purgecss.sh从 CLI运行
#!/bin/bash

# run production build
ng build --prod --output-hashing none

# go to the dist/yourProjectName folder
cd ./dist/yourProjectName

# make a new directory named 'css' (you can name it anything)
mkdir css

# run PurgeCSS & make a new '.css' file inside the 'css' directory
purgecss --css ./styles.css --content ./index.html ./*.js --out ./css

# replace the 'dist/yourProjectName/styles.css' file with the 'dist/yourProjectName/css/styles.css' file
mv ./css/styles.css ./styles.css

# delete the previously created 'css' directory
rm -r css
于 2019-10-02T18:15:24.993 回答
11

此配置对我有用,并为我126kb34kb生产styles.[hash].css构建输出提供了结果。

首先,安装这个:

npm install -D @angular-builders/custom-webpack purgecss-webpack-plugin

然后,webpack.config.js在项目的根目录下创建:

const glob = require('glob');
const PurgeCSSPlugin = require('purgecss-webpack-plugin');

module.exports = {
  plugins: [
    new PurgeCSSPlugin({ paths: glob.sync('./src/**/*.html', { nodir: true }) })
  ]
};

angular.json并按如下方式编辑您的配置:

"architect": {
   "build": {
     "builder": "@angular-builders/custom-webpack:browser",
       "options": {
         "customWebpackConfig": {
           "path": "webpack.config.js"
         },
         ...

我没有这个插件的结果:

styles.ca3c7399cc89e60cfa2d.css   | styles        | 128.99 kB

现在有了purgecss-webpack-plugin

styles.ca3c7399cc89e60cfa2d.css   | styles        |  34.46 kB

太棒了,首先当我看到这个输出时,我在浏览器中检查了我的应用程序,所有样式都已正确包含:-)。

我认为,唯一的缺点可能是这不适用于内联的 html(如果有角度组件)。

于 2020-12-30T10:20:46.537 回答
2

您可以在项目根目录中简单地运行以下命令

npm i -D postcss-import postcss-loader postcss-scss
ng add ngx-build-plus

在 angular.json 中,在 section 中进行以下替换"architect"

"builder": "@angular-devkit/build-angular:browser""builder": "ngx-build-plus:browser""build", _

"builder": "@angular-devkit/build-angular:dev-server""builder": "ngx-build-plus:dev-server""serve", _

"builder": "@angular-devkit/build-angular:karma""builder": "ngx-build-plus:karma""test", _

您还应该"extraWebpackConfig": "webpack.config.js"在相关部分的选项下添加。

于 2020-07-15T14:54:15.243 回答