mirror of https://github.com/webpack/webpack.git
fixed #179 typo in OccurenceOrderPlugin
This commit is contained in:
parent
ee30c84672
commit
48aad3eeee
|
@ -200,9 +200,9 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
|
|||
compiler.apply(new RecordIdsPlugin());
|
||||
|
||||
if(options.optimize && options.optimize.occurenceOrder) {
|
||||
compiler.apply(new MovedToPluginWarningPlugin("optimize.occurenceOrder", "optimize.OccurenceOrderPlugin"));
|
||||
var OccurenceOrderPlugin = require("./optimize/OccurenceOrderPlugin");
|
||||
compiler.apply(new OccurenceOrderPlugin(options.optimize.occurenceOrderPreferEntry));
|
||||
compiler.apply(new MovedToPluginWarningPlugin("optimize.occurenceOrder", "optimize.OccurrenceOrderPlugin"));
|
||||
var OccurrenceOrderPlugin = require("./optimize/OccurrenceOrderPlugin");
|
||||
compiler.apply(new OccurrenceOrderPlugin(options.optimize.occurenceOrderPreferEntry));
|
||||
}
|
||||
|
||||
if(options.optimize && options.optimize.minChunkSize) {
|
||||
|
|
|
@ -2,80 +2,4 @@
|
|||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
function OccurenceOrderPlugin(preferEntry) {
|
||||
this.preferEntry = preferEntry;
|
||||
}
|
||||
module.exports = OccurenceOrderPlugin;
|
||||
OccurenceOrderPlugin.prototype.apply = function(compiler) {
|
||||
var preferEntry = this.preferEntry;
|
||||
compiler.plugin("compilation", function(compilation) {
|
||||
compilation.plugin("optimize-module-order", function(modules) {
|
||||
function entryChunks(m) {
|
||||
return m.chunks.filter(function(c) {
|
||||
return c.initial;
|
||||
}).length;
|
||||
}
|
||||
function occursInEntry(m) {
|
||||
return m.reasons.map(function(r) {
|
||||
if(!r.module) return 0;
|
||||
return entryChunks(r.module);
|
||||
}).reduce(function(a, b) { return a+b; }, 0) + entryChunks(m);
|
||||
}
|
||||
function occurs(m) {
|
||||
return m.reasons.map(function(r) {
|
||||
if(!r.module) return 0;
|
||||
return r.module.chunks.length;
|
||||
}).reduce(function(a, b) { return a+b; }, 0) + m.chunks.length;
|
||||
}
|
||||
modules.sort(function(a, b) {
|
||||
if(preferEntry) {
|
||||
var aEntryOccurs = occursInEntry(a);
|
||||
var bEntryOccurs = occursInEntry(b);
|
||||
if(aEntryOccurs > bEntryOccurs) return -1;
|
||||
if(aEntryOccurs < bEntryOccurs) return 1;
|
||||
}
|
||||
var aOccurs = occurs(a);
|
||||
var bOccurs = occurs(b);
|
||||
if(aOccurs > bOccurs) return -1;
|
||||
if(aOccurs < bOccurs) return 1;
|
||||
if(a.identifier() > b.identifier()) return 1;
|
||||
if(a.identifier() < b.identifier()) return -1;
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
compilation.plugin("optimize-chunk-order", function(chunks) {
|
||||
function occursInEntry(c) {
|
||||
return c.parents.filter(function(p) {
|
||||
return p.initial;
|
||||
}).length + (c.entry ? 1 : 0);
|
||||
}
|
||||
function occurs(c) {
|
||||
return c.blocks.length + (c.entry ? 1 : 0);
|
||||
}
|
||||
chunks.forEach(function(c) {
|
||||
c.modules.sort(function(a, b) {
|
||||
if(a.identifier() > b.identifier()) return 1;
|
||||
if(a.identifier() < b.identifier()) return -1;
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
chunks.sort(function(a, b) {
|
||||
var aEntryOccurs = occursInEntry(a);
|
||||
var bEntryOccurs = occursInEntry(b);
|
||||
if(aEntryOccurs > bEntryOccurs) return -1;
|
||||
if(aEntryOccurs < bEntryOccurs) return 1;
|
||||
var aOccurs = occurs(a);
|
||||
var bOccurs = occurs(b);
|
||||
if(aOccurs > bOccurs) return -1;
|
||||
if(aOccurs < bOccurs) return 1;
|
||||
if(a.modules.length > b.modules.length) return -1;
|
||||
if(a.modules.length < b.modules.length) return 1;
|
||||
for(var i = 0; i < a.modules.length; i++) {
|
||||
if(a.modules[i].identifier() > b.modules[i].identifier()) return -1;
|
||||
if(a.modules[i].identifier() < b.modules[i].identifier()) return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
module.exports = require("./OccurrenceOrderPlugin");
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
function OccurrenceOrderPlugin(preferEntry) {
|
||||
this.preferEntry = preferEntry;
|
||||
}
|
||||
module.exports = OccurrenceOrderPlugin;
|
||||
OccurrenceOrderPlugin.prototype.apply = function(compiler) {
|
||||
var preferEntry = this.preferEntry;
|
||||
compiler.plugin("compilation", function(compilation) {
|
||||
compilation.plugin("optimize-module-order", function(modules) {
|
||||
function entryChunks(m) {
|
||||
return m.chunks.filter(function(c) {
|
||||
return c.initial;
|
||||
}).length;
|
||||
}
|
||||
function occursInEntry(m) {
|
||||
return m.reasons.map(function(r) {
|
||||
if(!r.module) return 0;
|
||||
return entryChunks(r.module);
|
||||
}).reduce(function(a, b) { return a+b; }, 0) + entryChunks(m);
|
||||
}
|
||||
function occurs(m) {
|
||||
return m.reasons.map(function(r) {
|
||||
if(!r.module) return 0;
|
||||
return r.module.chunks.length;
|
||||
}).reduce(function(a, b) { return a+b; }, 0) + m.chunks.length;
|
||||
}
|
||||
modules.sort(function(a, b) {
|
||||
if(preferEntry) {
|
||||
var aEntryOccurs = occursInEntry(a);
|
||||
var bEntryOccurs = occursInEntry(b);
|
||||
if(aEntryOccurs > bEntryOccurs) return -1;
|
||||
if(aEntryOccurs < bEntryOccurs) return 1;
|
||||
}
|
||||
var aOccurs = occurs(a);
|
||||
var bOccurs = occurs(b);
|
||||
if(aOccurs > bOccurs) return -1;
|
||||
if(aOccurs < bOccurs) return 1;
|
||||
if(a.identifier() > b.identifier()) return 1;
|
||||
if(a.identifier() < b.identifier()) return -1;
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
compilation.plugin("optimize-chunk-order", function(chunks) {
|
||||
function occursInEntry(c) {
|
||||
return c.parents.filter(function(p) {
|
||||
return p.initial;
|
||||
}).length + (c.entry ? 1 : 0);
|
||||
}
|
||||
function occurs(c) {
|
||||
return c.blocks.length + (c.entry ? 1 : 0);
|
||||
}
|
||||
chunks.forEach(function(c) {
|
||||
c.modules.sort(function(a, b) {
|
||||
if(a.identifier() > b.identifier()) return 1;
|
||||
if(a.identifier() < b.identifier()) return -1;
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
chunks.sort(function(a, b) {
|
||||
var aEntryOccurs = occursInEntry(a);
|
||||
var bEntryOccurs = occursInEntry(b);
|
||||
if(aEntryOccurs > bEntryOccurs) return -1;
|
||||
if(aEntryOccurs < bEntryOccurs) return 1;
|
||||
var aOccurs = occurs(a);
|
||||
var bOccurs = occurs(b);
|
||||
if(aOccurs > bOccurs) return -1;
|
||||
if(aOccurs < bOccurs) return 1;
|
||||
if(a.modules.length > b.modules.length) return -1;
|
||||
if(a.modules.length < b.modules.length) return 1;
|
||||
for(var i = 0; i < a.modules.length; i++) {
|
||||
if(a.modules[i].identifier() > b.modules[i].identifier()) return -1;
|
||||
if(a.modules[i].identifier() < b.modules[i].identifier()) return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
|
@ -60,6 +60,7 @@ exportPlugins(exports.optimize = {}, "./optimize", [
|
|||
"LimitChunkCountPlugin",
|
||||
"MinChunkSizePlugin",
|
||||
"OccurenceOrderPlugin",
|
||||
"OccurrenceOrderPlugin",
|
||||
"UglifyJsPlugin"
|
||||
]);
|
||||
exportPlugins(exports.dependencies = {}, "./dependencies", [
|
||||
|
|
Loading…
Reference in New Issue