Skip to content

Commit 5c3f8cc

Browse files
committed
Don't render and emit entry assets if not needed
webpack/webpack-dev-server#21
1 parent 04e24ed commit 5c3f8cc

7 files changed

Lines changed: 41 additions & 19 deletions

‎lib/AmdMainTemplateDecorator.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ AmdMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTemplate
3434
return new ConcatSource("define(function() { return ", source, "});");
3535
}
3636
};
37+
38+
AmdMainTemplateDecorator.prototype.useChunkHash = function(chunk) {
39+
if(!this.mainTemplate.useChunkHash || !this.mainTemplate.useChunkHash(chunk)) return false;
40+
return !Template.REGEXP_HASH.test(this.name || "");
41+
};
42+
3743
AmdMainTemplateDecorator.prototype.updateHash = function(hash) {
3844
hash.update("exports amd");
3945
hash.update(this.name + "");

‎lib/Compilation.js‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -594,28 +594,19 @@ Compilation.prototype.createChunkAssets = function createChunkAssets() {
594594
var filenameTemplate = chunk.filenameTemplate ? chunk.filenameTemplate :
595595
chunk.initial ? filename :
596596
chunkFilename;
597-
if(chunk.entry) {
598-
if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash == this.fullHash) {
599-
source = this.cache["c" + chunk.id].source;
600-
} else {
601-
source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
602-
if(this.cache) {
603-
this.cache["c" + chunk.id] = {
604-
hash: this.fullHash,
605-
source: source
606-
}
607-
}
608-
}
597+
var usedHash = !chunk.entry || (this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk)) ? chunkHash : this.fullHash;
598+
if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash == usedHash) {
599+
source = this.cache["c" + chunk.id].source;
609600
} else {
610-
if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash == chunkHash) {
611-
source = this.cache["c" + chunk.id].source;
601+
if(chunk.entry) {
602+
source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
612603
} else {
613604
source = this.chunkTemplate.render(chunk, this.moduleTemplate, this.dependencyTemplates);
614-
if(this.cache) {
615-
this.cache["c" + chunk.id] = {
616-
hash: chunkHash,
617-
source: source
618-
}
605+
}
606+
if(this.cache) {
607+
this.cache["c" + chunk.id] = {
608+
hash: usedHash,
609+
source: source
619610
}
620611
}
621612
}

‎lib/HotModuleReplacementPlugin.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
127127
return buf;
128128
};
129129

130+
compilation.mainTemplate.useChunkHash = false;
131+
130132
compilation.mainTemplate.renderCurrentHashCode = function(hash) {
131133
return "hotCurrentHash";
132134
};

‎lib/JsonpExportMainTemplateDecorator.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ JsonpExportMainTemplateDecorator.prototype.render = function(hash, chunk, module
1919
.replace(Template.REGEXP_NAME, chunk.name || "");
2020
return new ConcatSource(name + "(", source, ");");
2121
};
22+
23+
SetVarMainTemplateDecorator.prototype.useChunkHash = function(chunk) {
24+
if(!this.mainTemplate.useChunkHash || !this.mainTemplate.useChunkHash(chunk)) return false;
25+
return !Template.REGEXP_HASH.test(this.name || "");
26+
};
27+
2228
JsonpExportMainTemplateDecorator.prototype.updateHash = function(hash) {
2329
hash.update("jsonp export");
2430
hash.update(this.name + "");

‎lib/MainTemplate.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,9 @@ MainTemplate.prototype.updateHash = function(hash) {
167167
hash.update("maintemplate");
168168
hash.update("2");
169169
hash.update(this.outputOptions.publicPath + "");
170+
};
171+
172+
MainTemplate.prototype.useChunkHash = function(chunk) {
173+
var publicPath = this.outputOptions.publicPath || "";
174+
return !Template.REGEXP_HASH.test(publicPath);
170175
};

‎lib/SetVarMainTemplateDecorator.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ SetVarMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTempl
2626
return new ConcatSource(prefix, source);
2727
}
2828
};
29+
30+
SetVarMainTemplateDecorator.prototype.useChunkHash = function(chunk) {
31+
if(!this.mainTemplate.useChunkHash || !this.mainTemplate.useChunkHash(chunk)) return false;
32+
return !Template.REGEXP_HASH.test(this.varExpression || "");
33+
};
34+
2935
SetVarMainTemplateDecorator.prototype.updateHash = function(hash) {
3036
hash.update("set var");
3137
hash.update(this.varExpression + "");

‎lib/UmdMainTemplateDecorator.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ UmdMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTemplate
9090
) +
9191
"})(this, function(" + externalsArguments + ") {\nreturn ", "webpack/universalModuleDefinition"), source, "\n})\n");
9292
};
93+
94+
UmdMainTemplateDecorator.prototype.useChunkHash = function(chunk) {
95+
if(!this.mainTemplate.useChunkHash || !this.mainTemplate.useChunkHash(chunk)) return false;
96+
return !Template.REGEXP_HASH.test([].concat(this.name || "").join("|"));
97+
};
98+
9399
UmdMainTemplateDecorator.prototype.updateHash = function(hash) {
94100
hash.update("umd");
95101
hash.update(this.name + "");

0 commit comments

Comments
 (0)