Skip to content

Commit 75383c9

Browse files
authored
Merge pull request #15454 from webpack/fix/issue-15447
use cache in BannerPlugin
2 parents b53fe2f + 12ecab4 commit 75383c9

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy