Skip to content

Commit

Permalink
perf: purgecss for css minification integrated into webpack build (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored Feb 24, 2021
1 parent f596d0b commit 2199b82
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 27 deletions.
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ ARG serviceWorker
RUN node schematics/customization/service-worker ${serviceWorker} || true
COPY templates/webpack/* /workspace/templates/webpack/
RUN npm run ng -- build -c ${configuration}
COPY purgecss.config.js /workspace/
RUN npm run postbuild
# synchronize-marker:pwa-docker-build:end

# ^ this part above is copied to Dockerfile_noSSR and should be kept in sync
Expand Down
2 changes: 0 additions & 2 deletions Dockerfile_noSSR
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ ARG serviceWorker
RUN node schematics/customization/service-worker ${serviceWorker} || true
COPY templates/webpack/* /workspace/templates/webpack/
RUN npm run ng -- build -c ${configuration}
COPY purgecss.config.js /workspace/
RUN npm run postbuild
# synchronize-marker:pwa-docker-build:end

# ^ this part above is copied from the standard Dockerfile and should be kept in sync
Expand Down
4 changes: 2 additions & 2 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
},
{
"type": "initial",
"maximumWarning": "3mb",
"maximumError": "5mb"
"maximumWarning": "2mb",
"maximumError": "4mb"
},
{
"type": "anyComponentStyle",
Expand Down
7 changes: 2 additions & 5 deletions docs/guides/optimizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ If the PWA is built using `production` configuration. (Either by building with `
- Angular CLI [build-optimizer](https://github.com/angular/angular-cli/tree/master/packages/angular_devkit/build_optimizer#angular-build-optimizer)
- Webpack [SplitChunksPlugin](https://webpack.js.org/plugins/split-chunks-plugin/) is instructed to produce only `main`, `vendor`, `polyfills` and one `common` bundle for the code for optimized compression and download of the application.
- All `data-testing` attributes are removed from the HTML templates to reduce output.

## CSS Optimization

On npm `postbuild`, we integrated [PurgeCSS](https://purgecss.com) to remove unused CSS classes from the CSS output.
[Configuration](https://purgecss.com/configuration.html), especially whitelisting certain classes, can be done in [`purgecss.config.js`](../../purgecss.config.js).
- [PurgeCSS](https://purgecss.com) is used to remove unused CSS classes from the CSS output.
[Configuration](https://purgecss.com/configuration.html), especially [safelisting](https://purgecss.com/safelisting.html) certain classes, can be done on the plugin configuration or directly in your CSS with a special comment.

# Further References

Expand Down
483 changes: 475 additions & 8 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"synchronize-lazy-components": "ng g lazy-components",
"build:watch": "ng build --aot --watch",
"build": "node scripts/build-pwa",
"postbuild": "purgecss --config purgecss.config.js",
"analyze": "ng build --prod --stats-json && npx webpack-bundle-analyzer --host 0.0.0.0 dist/browser/stats.json dist/browser",
"serve": "node dist/server/main.js",
"start": "npm-run-all build serve",
Expand Down Expand Up @@ -132,7 +131,7 @@
"npm-run-all": "^4.1.5",
"postinstall": "^0.7.0",
"prettier": "^2.2.1",
"purgecss": "^4.0.0",
"purgecss-webpack-plugin": "^4.0.0",
"rxjs-tslint-rules": "^4.34.7",
"stylelint": "^13.8.0",
"stylelint-config-prettier": "^8.0.2",
Expand Down
6 changes: 0 additions & 6 deletions purgecss.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/styles/pages/content-pages/page-content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ol {

// HELPDESK

/* purgecss start ignore */
.helpdesk-info-box {
width: 100%;
padding: $space-default/2;
Expand All @@ -33,6 +34,8 @@ ol {
}
}

/* purgecss end ignore */

// SITEMAP

.sitemap-navigation {
Expand Down
14 changes: 14 additions & 0 deletions templates/webpack/webpack.custom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { CustomWebpackBrowserSchema, TargetOptions } from '@angular-builders/custom-webpack';
import { AngularCompilerPlugin } from '@ngtools/webpack';
import { existsSync } from 'fs';
import * as glob from 'glob';
import { join, resolve } from 'path';
import * as webpack from 'webpack';

const purgecssPlugin = require('purgecss-webpack-plugin');

const log = (...txt) => {
// tslint:disable-next-line: no-console
console.log('Custom Webpack:', ...txt);
Expand Down Expand Up @@ -68,6 +71,17 @@ export default (
test: /\.html$/,
use: [{ loader: join(__dirname, 'data-testing-id-loader.js') }],
});

log('setting up purgecss CSS minification');
config.plugins.push(
new purgecssPlugin({
paths: glob.sync('./{src,projects}/**/*', { nodir: true }),
safelist: {
standard: [/(p|m)(l|r|x|y|t|b)?-[0-5]/],
greedy: [/\bfa\b/, /\bmodal\b/, /\bswiper\b/, /\bcarousel\b/, /\bslide\b/],
},
})
);
}

if (key) {
Expand Down

0 comments on commit 2199b82

Please sign in to comment.