refactor(es6): multiline string, arr.includes, arrow functions in ./lib and ./benchmark

This commit is contained in:
EugeneHlushko 2018-02-17 13:09:35 +02:00
parent d057477969
commit 735c8ce9e0
6 changed files with 82 additions and 73 deletions

View File

@ -15,7 +15,7 @@ const benchmarkOptions = {
function runTimes(compiler, times, deferred) {
fs.writeFileSync(path.join(fixtures, "0.js"), "module.exports = " + Math.random(), "utf-8");
compiler.run((err, stats) => {
compiler.run(err => {
if(err) throw err;
if(times === 1)
deferred.resolve();
@ -30,12 +30,12 @@ const tests = {
(size, deferred) => {
webpack({
context: fixtures,
entry: "./" + size + ".js",
entry: `./${size}.js`,
output: {
path: outputPath,
filename: "bundle.js"
}
}, (err, stats) => {
}, err => {
if(err) throw err;
deferred.resolve();
});
@ -46,16 +46,16 @@ const tests = {
(size, deferred) => {
webpack({
context: fixtures,
entry: "./" + size + ".big.js",
entry: `./${size}.big.js`,
output: {
path: outputPath,
filename: "bundle.js"
},
devtool: "eval"
}, (err, stats) => {
}, err => {
if(err) throw err;
deferred.resolve();
})
});
}
],
"sourcemap build": [
@ -63,16 +63,16 @@ const tests = {
(size, deferred) => {
webpack({
context: fixtures,
entry: "./" + size + ".big.js",
entry: `./${size}.big.js`,
output: {
path: outputPath,
filename: "bundle.js"
},
devtool: "source-map"
}, (err, stats) => {
}, err => {
if(err) throw err;
deferred.resolve();
})
});
}
],
"cheap sourcemap build": [
@ -80,16 +80,32 @@ const tests = {
(size, deferred) => {
webpack({
context: fixtures,
entry: "./" + size + ".big.js",
entry: `./${size}.big.js`,
output: {
path: outputPath,
filename: "bundle.js"
},
devtool: "cheap-source-map"
}, function(err, stats) {
}, err => {
if(err) throw err;
deferred.resolve();
})
});
}
],
"build w/ chunks": [
[0, 1, 5, 10, 50, 100, 200],
(size, deferred) => {
webpack({
context: fixtures,
entry: `./${size}.async.js`,
output: {
path: outputPath,
filename: "bundle.js"
}
}, err => {
if(err) throw err;
deferred.resolve();
});
}
],
"build w/ chunks": [
@ -102,26 +118,10 @@ const tests = {
path: outputPath,
filename: "bundle.js"
}
}, function(err, stats) {
}, err => {
if(err) throw err;
deferred.resolve();
})
}
],
"build w/ chunks": [
[0, 1, 5, 10, 50, 100, 200],
(size, deferred) => {
webpack({
context: fixtures,
entry: "./" + size + ".async.js",
output: {
path: outputPath,
filename: "bundle.js"
}
}, function(err, stats) {
if(err) throw err;
deferred.resolve();
})
});
}
],
"incremental": [
@ -161,7 +161,7 @@ const tests = {
var compiler = webpack({
cache: true,
context: fixtures,
entry: "./" + size + ".js",
entry: `./${size}.js`,
output: {
path: outputPath,
filename: "bundle.js"
@ -176,7 +176,7 @@ const tests = {
var compiler = webpack({
cache: true,
context: fixtures,
entry: "./" + size + ".js",
entry: `./${size}.js`,
output: {
path: outputPath,
filename: "bundle.js"
@ -191,7 +191,7 @@ const tests = {
var compiler = webpack({
cache: true,
context: fixtures,
entry: "./" + size + ".js",
entry: `./${size}.js`,
output: {
path: outputPath,
filename: "bundle.js"
@ -204,17 +204,17 @@ const tests = {
const suite = new Benchmark.Suite;
Object.keys(tests).filter((name) => (process.argv.length > 2) ? name.indexOf(process.argv[2]) >= 0 : true)
.forEach((name) => {
Object.keys(tests).filter(name => process.argv.length > 2 ? name.includes(process.argv[2]) : true)
.forEach(name => {
const test = tests[name];
test[0].forEach((size) => {
suite.add(name + " " + size, (deferred) => {
test[0].forEach(size => {
suite.add(`${name} ${size}`, deferred => {
test[1](size, deferred);
}, benchmarkOptions);
});
});
suite.on("cycle", (event) => {
suite.on("cycle", event => {
process.stderr.write("\n");
const b = event.target;
console.log(b.name + "\t" + Math.floor(1000 * (b.stats.mean - b.stats.moe)) + "\t" + Math.floor(1000 * (b.stats.mean + b.stats.moe)));

View File

@ -7,16 +7,21 @@ try {
fs.mkdirSync(fixtures);
} catch(e) {}
function generateRequireString(conditional, suffix) {
const prefixedSuffix = suffix ? `.${suffix}` : "";
return `require(${JSON.stringify(`./${conditional}${prefixedSuffix}.js`)});`;
}
for(let i = 0; i < 10000; i++) {
const source = [];
if(i > 8)
source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".js") + ");");
source.push(generateRequireString((i / 8 | 0)));
if(i > 4)
source.push("require(" + JSON.stringify("./" + (i / 4 | 0) + ".js") + ");");
source.push(generateRequireString((i / 4 | 0)));
if(i > 2)
source.push("require(" + JSON.stringify("./" + (i / 2 | 0) + ".js") + ");");
source.push(generateRequireString((i / 2 | 0)));
if(i > 0)
source.push("require(" + JSON.stringify("./" + (i - 1) + ".js") + ");");
source.push(generateRequireString((i - 1)));
source.push("module.exports = " + i + ";");
fs.writeFileSync(path.join(fixtures, i + ".js"), source.join("\n"), "utf-8");
}
@ -25,13 +30,13 @@ for(let i = 0; i < 10000; i++) {
const source = [];
source.push("require.ensure([], function(require) {");
if(i > 8)
source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".async.js") + ");");
source.push(generateRequireString((i / 8 | 0), "async"));
if(i > 4)
source.push("require(" + JSON.stringify("./" + (i / 4 | 0) + ".async.js") + ");");
source.push(generateRequireString((i / 4 | 0), "async"));
if(i > 2)
source.push("require(" + JSON.stringify("./" + (i / 2 | 0) + ".async.js") + ");");
source.push(generateRequireString((i / 2 | 0), "async"));
if(i > 0)
source.push("require(" + JSON.stringify("./" + (i - 1) + ".async.js") + ");");
source.push(generateRequireString((i - 1), "async"));
source.push("});");
source.push("module.exports = " + i + ";");
fs.writeFileSync(path.join(fixtures, i + ".async.js"), source.join("\n"), "utf-8");
@ -40,13 +45,13 @@ for(let i = 0; i < 10000; i++) {
for(let i = 0; i < 100; i++) {
const source = [];
if(i > 8)
source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".big.js") + ");");
source.push(generateRequireString((i / 8 | 0), "big"));
if(i > 4)
source.push("require(" + JSON.stringify("./" + (i / 4 | 0) + ".big.js") + ");");
source.push(generateRequireString((i / 4 | 0), "big"));
if(i > 2)
source.push("require(" + JSON.stringify("./" + (i / 2 | 0) + ".big.js") + ");");
source.push(generateRequireString((i / 2 | 0), "big"));
if(i > 0)
source.push("require(" + JSON.stringify("./" + (i - 1) + ".big.js") + ");");
source.push(generateRequireString((i - 1), "big"));
for(let j = 0; j < 300; j++)
source.push("if(Math.random())hello.world();test.a.b.c.d();x(1,2,3,4);var a,b,c,d,e,f;");
source.push("module.exports = " + i + ";");

View File

@ -9,7 +9,7 @@ const asyncLib = require("async");
class CachePlugin {
constructor(cache) {
this.cache = cache || {};
this.FS_ACCURENCY = 2000;
this.FS_ACCURACY = 2000;
}
apply(compiler) {
@ -19,7 +19,7 @@ class CachePlugin {
});
} else {
const registerCacheToCompiler = (compiler, cache) => {
compiler.hooks.thisCompilation.tap("CachePlugin", (compilation) => {
compiler.hooks.thisCompilation.tap("CachePlugin", compilation => {
compilation.cache = cache;
compilation.hooks.childCompiler.tap("CachePlugin", (childCompiler, compilerName, compilerIndex) => {
if(cache) {
@ -61,13 +61,13 @@ class CachePlugin {
if(err) return callback(err);
for(const [file, ts] of fileTs) {
fileTs.set(file, ts + this.FS_ACCURENCY);
fileTs.set(file, ts + this.FS_ACCURACY);
}
callback();
});
});
compiler.hooks.afterCompile.tap("CachePlugin", (compilation) => {
compiler.hooks.afterCompile.tap("CachePlugin", compilation => {
compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies;
compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies;
});
@ -76,14 +76,14 @@ class CachePlugin {
/* istanbul ignore next */
applyMtime(mtime) {
if(this.FS_ACCURENCY > 1 && mtime % 2 !== 0)
this.FS_ACCURENCY = 1;
else if(this.FS_ACCURENCY > 10 && mtime % 20 !== 0)
this.FS_ACCURENCY = 10;
else if(this.FS_ACCURENCY > 100 && mtime % 200 !== 0)
this.FS_ACCURENCY = 100;
else if(this.FS_ACCURENCY > 1000 && mtime % 2000 !== 0)
this.FS_ACCURENCY = 1000;
if(this.FS_ACCURACY > 1 && mtime % 2 !== 0)
this.FS_ACCURACY = 1;
else if(this.FS_ACCURACY > 10 && mtime % 20 !== 0)
this.FS_ACCURACY = 10;
else if(this.FS_ACCURACY > 100 && mtime % 200 !== 0)
this.FS_ACCURACY = 100;
else if(this.FS_ACCURACY > 1000 && mtime % 2000 !== 0)
this.FS_ACCURACY = 1000;
}
}
module.exports = CachePlugin;

View File

@ -13,9 +13,11 @@ module.exports = class CaseSensitiveModulesWarning extends WebpackError {
this.name = "CaseSensitiveModulesWarning";
const sortedModules = this._sort(modules);
const modulesList = this._moduleMessages(sortedModules);
this.message = "There are multiple modules with names that only differ in casing.\n" +
"This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.\n" +
`Use equal casing. Compare these module identifiers:\n${modulesList}`;
this.message = `There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Use equal casing. Compare these module identifiers:
${modulesList}`;
this.origin = this.module = sortedModules[0];
Error.captureStackTrace(this, this.constructor);

View File

@ -8,6 +8,8 @@ const util = require("util");
const SortableSet = require("./util/SortableSet");
const GraphHelpers = require("./GraphHelpers");
let debugId = 1000;
const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()";
const ERR_CHUNK_INITIAL = "Chunk.initial was removed. Use canBeInitial/isOnlyInitial()";
const sortById = (a, b) => {
if(a.id < b.id) return -1;
@ -59,19 +61,19 @@ class Chunk {
}
get entry() {
throw new Error("Chunk.entry was removed. Use hasRuntime()");
throw new Error(ERR_CHUNK_ENTRY);
}
set entry(data) {
throw new Error("Chunk.entry was removed. Use hasRuntime()");
throw new Error(ERR_CHUNK_ENTRY);
}
get initial() {
throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
throw new Error(ERR_CHUNK_INITIAL);
}
set initial(data) {
throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
throw new Error(ERR_CHUNK_INITIAL);
}
hasRuntime() {

View File

@ -20,22 +20,22 @@ describe("CachePlugin", () => {
it("sets file system accuracy to 1 for granular modification timestamp", () => {
env.plugin.applyMtime(1483819067001);
env.plugin.FS_ACCURENCY.should.be.exactly(1);
env.plugin.FS_ACCURACY.should.be.exactly(1);
});
it("sets file system accuracy to 10 for moderately granular modification timestamp", () => {
env.plugin.applyMtime(1483819067004);
env.plugin.FS_ACCURENCY.should.be.exactly(10);
env.plugin.FS_ACCURACY.should.be.exactly(10);
});
it("sets file system accuracy to 100 for moderately coarse modification timestamp", () => {
env.plugin.applyMtime(1483819067040);
env.plugin.FS_ACCURENCY.should.be.exactly(100);
env.plugin.FS_ACCURACY.should.be.exactly(100);
});
it("sets file system accuracy to 1000 for coarse modification timestamp", () => {
env.plugin.applyMtime(1483819067400);
env.plugin.FS_ACCURENCY.should.be.exactly(1000);
env.plugin.FS_ACCURACY.should.be.exactly(1000);
});
});
});