forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleancss-webpack-plugin.ts
More file actions
142 lines (122 loc) · 4 KB
/
cleancss-webpack-plugin.ts
File metadata and controls
142 lines (122 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Compiler, compilation } from 'webpack';
import { RawSource, Source, SourceMapSource } from 'webpack-sources';
const CleanCSS = require('clean-css');
interface Chunk {
files: string[];
}
export interface CleanCssWebpackPluginOptions {
sourceMap: boolean;
test: (file: string) => boolean;
}
function hook(
compiler: any,
action: (compilation: any, chunks: Array<Chunk>) => Promise<void | void[]>,
) {
if (compiler.hooks) {
// Webpack 4
compiler.hooks.compilation.tap('cleancss-webpack-plugin', (compilation: any) => {
compilation.hooks.optimizeChunkAssets.tapPromise(
'cleancss-webpack-plugin',
(chunks: Array<Chunk>) => action(compilation, chunks),
);
});
} else {
// Webpack 3
compiler.plugin('compilation', (compilation: any) => {
compilation.plugin(
'optimize-chunk-assets',
(chunks: Array<Chunk>, callback: (err?: Error) => void) => action(compilation, chunks)
.then(() => callback())
.catch((err) => callback(err)),
);
});
}
}
export class CleanCssWebpackPlugin {
private readonly _options: CleanCssWebpackPluginOptions;
constructor(options: Partial<CleanCssWebpackPluginOptions>) {
this._options = {
sourceMap: false,
test: (file) => file.endsWith('.css'),
...options,
};
}
apply(compiler: Compiler): void {
hook(compiler, (compilation: compilation.Compilation, chunks: Array<Chunk>) => {
const cleancss = new CleanCSS({
compatibility: 'ie9',
level: 2,
inline: false,
returnPromise: true,
sourceMap: this._options.sourceMap,
});
const files: string[] = [...compilation.additionalChunkAssets];
chunks.forEach(chunk => {
if (chunk.files && chunk.files.length > 0) {
files.push(...chunk.files);
}
});
const actions = files
.filter(file => this._options.test(file))
.map(file => {
const asset = compilation.assets[file] as Source;
if (!asset) {
return Promise.resolve();
}
let content: string;
let map: any;
if (this._options.sourceMap && asset.sourceAndMap) {
const sourceAndMap = asset.sourceAndMap();
content = sourceAndMap.source;
map = sourceAndMap.map;
} else {
content = asset.source();
}
if (content.length === 0) {
return Promise.resolve();
}
return Promise.resolve()
.then(() => map ? cleancss.minify(content, map) : cleancss.minify(content))
.then((output: any) => {
let hasWarnings = false;
if (output.warnings && output.warnings.length > 0) {
compilation.warnings.push(...output.warnings);
hasWarnings = true;
}
if (output.errors && output.errors.length > 0) {
output.errors
.forEach((error: string) => compilation.errors.push(new Error(error)));
return;
}
// generally means invalid syntax so bail
if (hasWarnings && output.stats.minifiedSize === 0) {
return;
}
let newSource;
if (output.sourceMap) {
newSource = new SourceMapSource(
output.styles,
file,
output.sourceMap.toString(),
content,
map,
);
} else {
newSource = new RawSource(output.styles);
}
compilation.assets[file] = newSource;
});
});
return Promise.all(actions);
});
}
}