Skip to content

Commit fcbf438

Browse files
committed
Add plugin to trim CSS files
1 parent 2604eb0 commit fcbf438

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

build/TrimCSSPlugin.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const pluginName = "TrimCSSPlugin";
2+
const TrimmedSource = require("./TrimmedSource");
3+
4+
module.exports = class TrimCSSPlugin {
5+
isCSSChunk(chunk) {
6+
return chunk.ids.includes("mini-css-extract-plugin");
7+
}
8+
9+
apply(compiler) {
10+
compiler.hooks.compilation.tap(pluginName, compilation => {
11+
compilation.hooks.optimizeChunkAssets.tap(pluginName, chunks => {
12+
for (const chunk of chunks) {
13+
// if (!this.isCSSChunk(chunk)) {
14+
// continue;
15+
// }
16+
17+
for (const file of chunk.files) {
18+
compilation.assets[file] = new TrimmedSource(
19+
compilation.assets[file]
20+
);
21+
}
22+
}
23+
});
24+
});
25+
}
26+
};

build/TrimmedSource.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const Source = require("webpack-sources/lib/Source");
2+
3+
class TrimmedSource extends Source {
4+
constructor(source) {
5+
super();
6+
7+
this._source = source;
8+
}
9+
10+
source() {
11+
const src =
12+
typeof this._source === "string" ? this._source : this._source.source();
13+
14+
if (src.indexOf(".red[data-v-") !== -1) {
15+
// debugger;
16+
}
17+
18+
return trimStr(src);
19+
}
20+
21+
node(options) {
22+
return new SourceNode(null, null, null, [
23+
cloneAndTrim(this._source.node(options))
24+
]);
25+
}
26+
27+
listMap(options) {
28+
return this._source.listMap(options).mapGeneratedCode(trimStr);
29+
}
30+
31+
updateHash(hash) {
32+
if (typeof this._source === "string") {
33+
hash.update(this._source);
34+
} else {
35+
this._source.updateHash(hash);
36+
}
37+
}
38+
}
39+
40+
const whitespacePattern = /(^\s+|\s+$)/g;
41+
42+
function trimStr(str) {
43+
return str.replace(whitespacePattern, "");
44+
}
45+
46+
function cloneAndTrim(node) {
47+
if (typeof node === "string") {
48+
return trimStr(node);
49+
}
50+
51+
const newNode = new SourceNode(
52+
node.line,
53+
node.column,
54+
node.source,
55+
node.children.map(cloneAndTrim),
56+
node.name
57+
);
58+
59+
newNode.sourceContents = node.sourceContents;
60+
61+
return newNode;
62+
}
63+
64+
require("webpack-sources/lib/SourceAndMapMixin")(TrimmedSource.prototype);
65+
66+
module.exports = TrimmedSource;

build/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
module.exports.TrimCSSPlugin = require("./TrimCSSPlugin");
12
module.exports.RemoveStyleScriptChunksPlugin = require("./RemoveStyleScriptChunksPlugin");

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require("path");
22
const webpack = require("webpack");
33
const { VueLoaderPlugin } = require("vue-loader");
4-
const { RemoveStyleScriptChunksPlugin } = require("./build");
4+
const { TrimCSSPlugin, RemoveStyleScriptChunksPlugin } = require("./build");
55

66
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
77
module.exports = {
@@ -57,6 +57,7 @@ module.exports = {
5757
}),
5858

5959
new RemoveStyleScriptChunksPlugin()
60+
// new TrimCSSPlugin(),
6061
],
6162

6263
optimization: {

0 commit comments

Comments
 (0)