Skip to content

Commit 0dc429a

Browse files
authored
Merge pull request webpack#3086 from jamestautges/master
Cache portable id to avoid expensive function call
2 parents a148d6e + 80d713e commit 0dc429a

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎lib/Module.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function Module() {
1616
this.debugId = debugId++;
1717
this.lastId = -1;
1818
this.id = null;
19+
this.portableId = null;
1920
this.index = null;
2021
this.index2 = null;
2122
this.used = null;

‎lib/RecordIdsPlugin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ RecordIdsPlugin.prototype.apply = function(compiler) {
2323
if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
2424
if(!records.modules.usedIds) records.modules.usedIds = {};
2525
modules.forEach(function(module) {
26-
var identifier = makeRelative(compiler, module.identifier());
26+
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
27+
var identifier = module.portableId;
2728
records.modules.byIdentifier[identifier] = module.id;
2829
records.modules.usedIds[module.id] = module.id;
2930
});
@@ -34,7 +35,8 @@ RecordIdsPlugin.prototype.apply = function(compiler) {
3435
var usedIds = {};
3536
modules.forEach(function(module) {
3637
if(module.id !== null) return;
37-
var identifier = makeRelative(compiler, module.identifier());
38+
if(!module.portableId) module.portableId = makeRelative(compiler, module.identifier());
39+
var identifier = module.portableId;
3840
var id = records.modules.byIdentifier[identifier];
3941
if(id === undefined) return;
4042
if(usedIds[id]) return;

‎test/RecordIdsPlugin.test.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var should = require("should");
2+
3+
var path = require("path");
4+
var webpack = require("../lib/webpack");
5+
6+
var RecordIdsPlugin = require("../lib/RecordIdsPlugin");
7+
8+
function makeRelative(compiler, identifier) {
9+
var context = compiler.context;
10+
return identifier.split("|").map(function(str) {
11+
return str.split("!").map(function(str) {
12+
return path.relative(context, str);
13+
}).join("!");
14+
}).join("|");
15+
}
16+
17+
describe("RecordIdsPlugin", function() {
18+
19+
var compiler;
20+
21+
before(function() {
22+
compiler = webpack({
23+
entry: "./nodetest/entry",
24+
context: path.join(__dirname, "fixtures"),
25+
output: {
26+
path: path.join(__dirname, "nodetest", "js"),
27+
filename: "result1.js"
28+
}
29+
});
30+
31+
compiler.plugin("compilation", function(compilation, callback) {
32+
compilation.plugin("should-record", function() {
33+
return true;
34+
});
35+
});
36+
});
37+
38+
it("should cache identifiers", function(done) {
39+
compiler.compile(function(err, compilation) {
40+
if(err) done(err);
41+
var pass = true;
42+
for(var i = 0; i < compilation.modules.length; i++) {
43+
try {
44+
should.exist(compilation.modules[i].portableId);
45+
compilation.modules[i].portableId.should.equal(makeRelative(compiler, compilation.modules[i].identifier()));
46+
} catch(e) {
47+
done(e);
48+
pass = false;
49+
break;
50+
}
51+
}
52+
if(pass) done();
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)