Skip to content

Commit bac34e5

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): recover from CSS optimization errors
(cherry picked from commit 7d56d48)
1 parent d8bedd1 commit bac34e5

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/optimize-css-webpack-plugin.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as cssNano from 'cssnano';
99
import { ProcessOptions, Result } from 'postcss';
1010
import { Compilation, Compiler, sources } from 'webpack';
11-
import { addWarning } from '../../utils/webpack-diagnostics';
11+
import { addError, addWarning } from '../../utils/webpack-diagnostics';
1212

1313
export interface OptimizeCssWebpackPluginOptions {
1414
sourceMap: boolean;
@@ -86,33 +86,39 @@ export class OptimizeCssWebpackPlugin {
8686
map: map && { annotation: false, prev: map },
8787
};
8888

89-
const output = await new Promise<Result>((resolve, reject) => {
90-
// @types/cssnano are not up to date with version 5.
91-
// tslint:disable-next-line: no-any
92-
(cssNano as any)(cssNanoOptions).process(content, postCssOptions)
93-
.then(resolve)
94-
.catch((err: Error) => reject(new Error(`${file} ${err.message}`)));
95-
});
96-
97-
for (const { text } of output.warnings()) {
98-
addWarning(compilation, text);
99-
}
100-
101-
let newSource;
102-
if (output.map) {
103-
newSource = new sources.SourceMapSource(
104-
output.css,
105-
file,
89+
try {
90+
const output = await new Promise<Result>((resolve, reject) => {
91+
// @types/cssnano are not up to date with version 5.
10692
// tslint:disable-next-line: no-any
107-
output.map.toString() as any,
108-
content,
109-
map,
110-
);
111-
} else {
112-
newSource = new sources.RawSource(output.css);
113-
}
93+
(cssNano as any)(cssNanoOptions).process(content, postCssOptions)
94+
.then(resolve)
95+
.catch((err: Error) => reject(err));
96+
});
97+
98+
99+
for (const { text } of output.warnings()) {
100+
addWarning(compilation, text);
101+
}
102+
103+
let newSource;
104+
if (output.map) {
105+
newSource = new sources.SourceMapSource(
106+
output.css,
107+
file,
108+
output.map.toString(),
109+
content,
110+
map,
111+
);
112+
} else {
113+
newSource = new sources.RawSource(output.css);
114+
}
115+
116+
compilation.assets[file] = newSource;
117+
} catch (error) {
118+
addError(compilation, error.message);
114119

115-
compilation.assets[file] = newSource;
120+
return;
121+
}
116122
});
117123

118124
return Promise.all(actions).then(() => {});

0 commit comments

Comments
 (0)