Skip to content

Commit 0fd7abb

Browse files
alan-agius4Keen Yee Liau
authored andcommitted
refactor: cleancss-webpack-plugin to use async/await
1 parent 16f490b commit 0fd7abb

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"@bazel/typescript": "~0.26.0",
9090
"@types/browserslist": "^4.4.0",
9191
"@types/caniuse-lite": "^1.0.0",
92+
"@types/clean-css": "^4.2.1",
9293
"@types/copy-webpack-plugin": "^4.4.1",
9394
"@types/express": "^4.16.0",
9495
"@types/glob": "^7.0.0",

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/cleancss-webpack-plugin.ts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
* found in the LICENSE file at https://angular.io/license
1010
*/
1111

12+
import * as CleanCSS from 'clean-css';
1213
import { Compiler, compilation } from 'webpack';
1314
import { RawSource, Source, SourceMapSource } from 'webpack-sources';
1415

15-
const CleanCSS = require('clean-css');
16-
1716
interface Chunk {
1817
files: string[];
1918
}
@@ -73,10 +72,10 @@ export class CleanCssWebpackPlugin {
7372

7473
const actions = files
7574
.filter(file => this._options.test(file))
76-
.map(file => {
75+
.map(async file => {
7776
const asset = compilation.assets[file] as Source;
7877
if (!asset) {
79-
return Promise.resolve();
78+
return;
8079
}
8180

8281
let content: string;
@@ -90,44 +89,42 @@ export class CleanCssWebpackPlugin {
9089
}
9190

9291
if (content.length === 0) {
93-
return Promise.resolve();
92+
return;
93+
}
94+
95+
const output = await cleancss.minify(content, map);
96+
97+
let hasWarnings = false;
98+
if (output.warnings && output.warnings.length > 0) {
99+
compilation.warnings.push(...output.warnings);
100+
hasWarnings = true;
101+
}
102+
103+
if (output.errors && output.errors.length > 0) {
104+
output.errors
105+
.forEach((error: string) => compilation.errors.push(new Error(error)));
106+
return;
107+
}
108+
109+
// generally means invalid syntax so bail
110+
if (hasWarnings && output.stats.minifiedSize === 0) {
111+
return;
112+
}
113+
114+
let newSource;
115+
if (output.sourceMap) {
116+
newSource = new SourceMapSource(
117+
output.styles,
118+
file,
119+
output.sourceMap.toString() as any,
120+
content,
121+
map,
122+
);
123+
} else {
124+
newSource = new RawSource(output.styles);
94125
}
95126

96-
return Promise.resolve()
97-
.then(() => map ? cleancss.minify(content, map) : cleancss.minify(content))
98-
.then((output: any) => {
99-
let hasWarnings = false;
100-
if (output.warnings && output.warnings.length > 0) {
101-
compilation.warnings.push(...output.warnings);
102-
hasWarnings = true;
103-
}
104-
105-
if (output.errors && output.errors.length > 0) {
106-
output.errors
107-
.forEach((error: string) => compilation.errors.push(new Error(error)));
108-
return;
109-
}
110-
111-
// generally means invalid syntax so bail
112-
if (hasWarnings && output.stats.minifiedSize === 0) {
113-
return;
114-
}
115-
116-
let newSource;
117-
if (output.sourceMap) {
118-
newSource = new SourceMapSource(
119-
output.styles,
120-
file,
121-
output.sourceMap.toString(),
122-
content,
123-
map,
124-
);
125-
} else {
126-
newSource = new RawSource(output.styles);
127-
}
128-
129-
compilation.assets[file] = newSource;
130-
});
127+
compilation.assets[file] = newSource;
131128
});
132129

133130
return Promise.all(actions);

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@
353353
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.1.tgz#9794c69c8385d0192acc471a540d1f8e0d16218a"
354354
integrity sha512-FhlMa34NHp9K5MY1Uz8yb+ZvuX0pnvn3jScRSNAb75KHGB8d3rEU6hqMs3Z2vjuytcMfRg6c5CHMc3wtYyD2/A==
355355

356+
"@types/clean-css@^4.2.1":
357+
version "4.2.1"
358+
resolved "https://registry.yarnpkg.com/@types/clean-css/-/clean-css-4.2.1.tgz#cb0134241ec5e6ede1b5344bc829668fd9871a8d"
359+
integrity sha512-A1HQhQ0hkvqqByJMgg+Wiv9p9XdoYEzuwm11SVo1mX2/4PSdhjcrUlilJQoqLscIheC51t1D5g+EFWCXZ2VTQQ==
360+
dependencies:
361+
"@types/node" "*"
362+
356363
"@types/connect@*":
357364
version "3.4.32"
358365
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.32.tgz#aa0e9616b9435ccad02bc52b5b454ffc2c70ba28"

0 commit comments

Comments
 (0)