Skip to content

Commit 12ecab4

Browse files
committed
use cache in BannerPlugin
1 parent bb0fdc9 commit 12ecab4

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

lib/BannerPlugin.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class BannerPlugin {
7777
undefined,
7878
options
7979
);
80+
const cache = new WeakMap();
8081

8182
compiler.hooks.compilation.tap("BannerPlugin", compilation => {
8283
compilation.hooks.processAssets.tap(
@@ -102,10 +103,15 @@ class BannerPlugin {
102103

103104
const comment = compilation.getPath(banner, data);
104105

105-
compilation.updateAsset(
106-
file,
107-
old => new ConcatSource(comment, "\n", old)
108-
);
106+
compilation.updateAsset(file, old => {
107+
let cached = cache.get(old);
108+
if (!cached || cached.comment !== comment) {
109+
const source = new ConcatSource(comment, "\n", old);
110+
cache.set(old, { source, comment });
111+
return source;
112+
}
113+
return cached.source;
114+
});
109115
}
110116
}
111117
}

test/BannerPlugin.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const fs = require("graceful-fs");
5+
6+
const webpack = require("..");
7+
8+
it("should cache assets", done => {
9+
const entry1File = path.join(__dirname, "js", "BannerPlugin", "entry1.js");
10+
const entry2File = path.join(__dirname, "js", "BannerPlugin", "entry2.js");
11+
try {
12+
fs.mkdirSync(path.join(__dirname, "js", "BannerPlugin"), {
13+
recursive: true
14+
});
15+
} catch (e) {
16+
// empty
17+
}
18+
const compiler = webpack({
19+
mode: "development",
20+
entry: {
21+
entry1: entry1File,
22+
entry2: entry2File
23+
},
24+
output: {
25+
path: path.join(__dirname, "js", "BannerPlugin", "output")
26+
},
27+
plugins: [new webpack.BannerPlugin("banner is a string")]
28+
});
29+
fs.writeFileSync(entry1File, "1", "utf-8");
30+
fs.writeFileSync(entry2File, "1", "utf-8");
31+
compiler.run(err => {
32+
if (err) return done(err);
33+
fs.writeFileSync(entry2File, "2", "utf-8");
34+
compiler.run((err, stats) => {
35+
const { assets } = stats.toJson();
36+
expect(assets.find(as => as.name === "entry1.js").emitted).toBe(false);
37+
expect(assets.find(as => as.name === "entry2.js").emitted).toBe(true);
38+
done(err);
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)