Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): recover from CSS optimization errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 authored and filipesilva committed Apr 20, 2021
1 parent bc0f329 commit 7d56d48
Showing 1 changed file with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as cssNano from 'cssnano';
import { ProcessOptions, Result } from 'postcss';
import { Compilation, Compiler, sources } from 'webpack';
import { addWarning } from '../../utils/webpack-diagnostics';
import { addError, addWarning } from '../../utils/webpack-diagnostics';

export interface OptimizeCssWebpackPluginOptions {
sourceMap: boolean;
Expand Down Expand Up @@ -86,33 +86,39 @@ export class OptimizeCssWebpackPlugin {
map: map && { annotation: false, prev: map },
};

const output = await new Promise<Result>((resolve, reject) => {
// @types/cssnano are not up to date with version 5.
// tslint:disable-next-line: no-any
(cssNano as any)(cssNanoOptions).process(content, postCssOptions)
.then(resolve)
.catch((err: Error) => reject(new Error(`${file} ${err.message}`)));
});

for (const { text } of output.warnings()) {
addWarning(compilation, text);
}

let newSource;
if (output.map) {
newSource = new sources.SourceMapSource(
output.css,
file,
try {
const output = await new Promise<Result>((resolve, reject) => {
// @types/cssnano are not up to date with version 5.
// tslint:disable-next-line: no-any
output.map.toString() as any,
content,
map,
);
} else {
newSource = new sources.RawSource(output.css);
}
(cssNano as any)(cssNanoOptions).process(content, postCssOptions)
.then(resolve)
.catch((err: Error) => reject(err));
});


for (const { text } of output.warnings()) {
addWarning(compilation, text);
}

let newSource;
if (output.map) {
newSource = new sources.SourceMapSource(
output.css,
file,
output.map.toString(),
content,
map,
);
} else {
newSource = new sources.RawSource(output.css);
}

compilation.assets[file] = newSource;
} catch (error) {
addError(compilation, error.message);

compilation.assets[file] = newSource;
return;
}
});

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

0 comments on commit 7d56d48

Please sign in to comment.