Skip to content

Commit f84f000

Browse files
committed
records, typo
1 parent df98aca commit f84f000

File tree

9 files changed

+221
-59
lines changed

9 files changed

+221
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Introduction
66

7-
webpack is a bundler for modules. The main purpose is to bundle javascript files for useage in browser.
7+
webpack is a bundler for modules. The main purpose is to bundle javascript files for usage in browser.
88

99
**TL;DR**
1010

bin/config-optimist.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ module.exports = function(optimist) {
3131

3232
.string("output-library-target").describe("output-library-target")
3333

34+
.string("records-input-path").describe("records-input-path")
35+
36+
.string("records-output-path").describe("records-output-path")
37+
38+
.string("records-path").describe("records-path")
39+
3440
.string("target").describe("target")
3541

3642
.boolean("cache").describe("cache")

bin/convert-argv.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ module.exports = function(optimist, argv, convertOptions) {
184184
options.output.libraryTarget = value;
185185
});
186186

187+
ifArg("records-input-path", function(value) {
188+
options.recordsInputPath = path.resolve(value);
189+
});
190+
191+
ifArg("records-output-path", function(value) {
192+
options.recordsOutputPath = path.resolve(value);
193+
});
194+
195+
ifArg("records-path", function(value) {
196+
options.recordsPath = path.resolve(value);
197+
});
198+
187199
ifArg("target", function(value) {
188200
options.target = value;
189201
});

lib/Compilation.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ function Compilation(compiler) {
3535
this.modules = [];
3636
this._modules = {};
3737
this.cache = null;
38+
this.records = null;
39+
this.nextFreeModuleId = 1;
40+
this.nextFreeChunkId = 1;
3841
this.assets = {};
3942
this.errors = [];
4043
this.warnings = [];
@@ -347,21 +350,32 @@ Compilation.prototype.seal = function seal(callback) {
347350
this.processDependenciesBlockForChunk(module, chunk);
348351
}, this);
349352
this.applyPlugins("optimize");
353+
350354
this.applyPlugins("optimize-modules", this.modules);
351355
this.applyPlugins("after-optimize-modules", this.modules);
356+
352357
this.applyPlugins("optimize-chunks", this.chunks);
353358
this.applyPlugins("after-optimize-chunks", this.chunks);
359+
360+
this.applyPlugins("revive-modules", this.modules, this.records);
354361
this.applyPlugins("optimize-module-order", this.modules);
355362
this.applyModuleIds();
356-
this.applyPlugins("optimize-chunk-order", this.chunks);
357-
this.applyChunkIds();
358363
this.applyPlugins("optimize-module-ids", this.modules);
359364
this.applyPlugins("after-optimize-module-ids", this.modules);
365+
this.applyPlugins("record-modules", this.modules, this.records);
366+
367+
this.applyPlugins("revive-chunks", this.chunks, this.records);
368+
this.applyPlugins("optimize-chunk-order", this.chunks);
369+
this.applyChunkIds();
360370
this.applyPlugins("optimize-chunk-ids", this.chunks);
361371
this.applyPlugins("after-optimize-chunk-ids", this.chunks);
372+
this.applyPlugins("record-chunks", this.chunks, this.records);
373+
362374
this.sortItems();
363375
this.createChunkAssets();
364376
this.summarizeDependencies();
377+
this.applyPlugins("record", this, this.records);
378+
365379
this.applyPluginsAsync("optimize-chunk-assets", this.chunks, function(err) {
366380
if(err) return callback(err);
367381
this.applyPlugins("after-optimize-chunk-assets", this.chunks);
@@ -418,62 +432,22 @@ Compilation.prototype.processDependenciesBlockForChunk = function processDepende
418432
};
419433

420434
Compilation.prototype.applyModuleIds = function applyModuleIds() {
421-
var i = this.cache && this.cache["nextModuleId"] || 1;
422-
var usedIds = {0:true};
423435
this.modules.forEach(function(module) {
424436
if(module.id === null) {
425-
if(module.lastId > 0) {
426-
if(!usedIds[module.lastId]) {
427-
usedIds[module.lastId] = true;
428-
module.id = module.lastId;
429-
return;
430-
}
431-
}
432-
module.id = i++;
437+
module.id = this.nextFreeModuleId++;
433438
}
434-
});
435-
if(this.cache) this.cache["nextModuleId"] = i;
439+
}, this);
436440
};
437441

438442
Compilation.prototype.applyChunkIds = function applyChunkIds() {
439-
var i = this.cache && this.cache["nextChunkId"] || 1;
440-
var usedIds = {0:true};
441-
if(this.cache) {
442-
if(!this.cache.chunks)
443-
this.cache.chunks = {};
444-
var keys = Object.keys(this.cache.chunks).slice();
445-
var cacheChunks = this.cache.chunks;
446-
this.cache.chunks = {};
447-
}
448443
this.chunks.forEach(function(chunk) {
449444
if(chunk.id === null) {
450-
if(this.cache) {
451-
for(var j = 0; j < keys.length; j++) {
452-
var chunkId = keys[j];
453-
var cacheChunk = cacheChunks[chunkId];
454-
if(usedIds[cacheChunk.id]) continue;
455-
if(chunk.blocks.some(function(block) {
456-
return cacheChunk.blocks.indexOf(block) >= 0;
457-
})) {
458-
usedIds[cacheChunk.id] = true;
459-
chunk.id = cacheChunk.id;
460-
break;
461-
}
462-
}
463-
}
464445
if(chunk.id === null)
465-
chunk.id = i++;
466-
if(this.cache) {
467-
this.cache.chunks["c"+chunk.id] = {
468-
id: chunk.id,
469-
blocks: chunk.blocks
470-
};
471-
}
446+
chunk.id = this.nextFreeChunkId++;
472447
}
473448
if(!chunk.ids)
474449
chunk.ids = [chunk.id];
475450
}, this);
476-
if(this.cache) this.cache["nextChunkId"] = i;
477451
};
478452

479453
Compilation.prototype.sortItems = function sortItems() {

lib/Compiler.js

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ var RequireEnsureItemDependency = require("./dependencies/RequireEnsureItemDepen
2323

2424
function Watching(compiler, handler, watchDelay) {
2525
this.startTime = null;
26-
this.running = false;
2726
this.invalid = false;
2827
this.error = null;
2928
this.stats = null;
3029
this.handler = handler;
3130
this.watchDelay = watchDelay;
3231
this.compiler = compiler;
33-
this._go();
32+
this.running = true;
33+
this.compiler.readRecords(function(err) {
34+
if(err) return this._done(err);
35+
36+
this._go();
37+
}.bind(this));
3438
}
3539

3640
Watching.prototype._go = function() {
@@ -44,8 +48,13 @@ Watching.prototype._go = function() {
4448

4549
this.compiler.emitAssets(compilation, function(err) {
4650
if(err) return this._done(err);
51+
if(this.invalid) return this._done();
4752

48-
return this._done(null, compilation);
53+
this.compiler.emitRecords(function(err) {
54+
if(err) return this._done(err);
55+
56+
return this._done(null, compilation);
57+
}.bind(this));
4958
}.bind(this));
5059
}.bind(this));
5160
}.bind(this));
@@ -101,6 +110,10 @@ function Compiler() {
101110
this.inputFileSystem = null;
102111
this.separateExecutor = null;
103112

113+
this.recordsInputPath = null;
114+
this.recordsOutputPath = null;
115+
this.records = {};
116+
104117
this.fileTimestamps = {};
105118
this.contextTimestamps = {};
106119

@@ -130,17 +143,25 @@ Compiler.prototype.run = function(callback) {
130143
this.applyPluginsAsync("run", this, function(err) {
131144
if(err) return callback(err);
132145

133-
this.compile(function(err, compilation) {
146+
this.readRecords(function(err) {
134147
if(err) return callback(err);
135148

136-
this.emitAssets(compilation, function(err) {
149+
this.compile(function(err, compilation) {
137150
if(err) return callback(err);
138151

139-
var stats = compilation.getStats();
140-
stats.startTime = startTime;
141-
stats.endTime = new Date().getTime();
142-
this.applyPlugins("done", stats);
143-
return callback(null, stats);
152+
this.emitAssets(compilation, function(err) {
153+
if(err) return callback(err);
154+
155+
this.emitRecords(function(err) {
156+
if(err) return callback(err);
157+
158+
var stats = compilation.getStats();
159+
stats.startTime = startTime;
160+
stats.endTime = new Date().getTime();
161+
this.applyPlugins("done", stats);
162+
return callback(null, stats);
163+
}.bind(this));
164+
}.bind(this));
144165
}.bind(this));
145166
}.bind(this));
146167
}.bind(this));
@@ -210,6 +231,36 @@ Compiler.prototype.emitAssets = function(compilation, callback) {
210231

211232
};
212233

234+
Compiler.prototype.emitRecords = function emitRecords(callback) {
235+
if(!this.recordsOutputPath) return callback();
236+
this.outputFileSystem.writeFile(this.recordsOutputPath, JSON.stringify(this.records, undefined, 2), callback);
237+
};
238+
239+
Compiler.prototype.readRecords = function readRecords(callback) {
240+
if(!this.recordsInputPath) {
241+
this.records = {};
242+
return callback();
243+
}
244+
this.inputFileSystem.stat(this.recordsInputPath, function(err) {
245+
// It doesn't exist
246+
// We can ignore this.
247+
if(err) return callback();
248+
249+
this.inputFileSystem.readFile(this.recordsInputPath, function(err, content) {
250+
if(err) return callback(err);
251+
252+
try {
253+
this.records = JSON.parse(content);
254+
} catch(e) {
255+
e.message = "Cannot parse records: " + e.message;
256+
return callback(e);
257+
}
258+
259+
return callback();
260+
}.bind(this));
261+
}.bind(this));
262+
};
263+
213264
Compiler.prototype.createChildCompiler = function(compilation, compilerName, outputOptions) {
214265
var childCompiler = new Compiler();
215266
for(var name in this._plugins) {
@@ -248,6 +299,7 @@ Compiler.prototype.newCompilation = function(params) {
248299
compilation.fileTimestamps = this.fileTimestamps;
249300
compilation.contextTimestamps = this.contextTimestamps;
250301
compilation.name = this.name;
302+
compilation.records = this.records;
251303
this.applyPlugins("compilation", compilation, params);
252304
return compilation;
253305
};

lib/DependenciesBlock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = DependenciesBlock;
1313

1414
DependenciesBlock.prototype.addBlock = function(block) {
1515
this.blocks.push(block);
16+
block.parent = this;
1617
}
1718

1819
DependenciesBlock.prototype.addVariable = function(name, expression, dependencies) {

lib/RecordIdsPlugin.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function RecordIdsPlugin() {
6+
}
7+
module.exports = RecordIdsPlugin;
8+
RecordIdsPlugin.prototype.apply = function(compiler) {
9+
compiler.plugin("compilation", function(compilation) {
10+
compilation.plugin("record-modules", function(modules, records) {
11+
records.nextFreeModuleId = compilation.nextFreeModuleId;
12+
if(!records.modules) records.modules = {};
13+
if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
14+
modules.forEach(function(module) {
15+
var identifier = module.identifier();
16+
records.modules.byIdentifier[identifier] = module.id;
17+
});
18+
});
19+
compilation.plugin("revive-modules", function(modules, records) {
20+
if(records.nextFreeModuleId)
21+
compilation.nextFreeModuleId = records.nextFreeModuleId;
22+
if(!records.modules || !records.modules.byIdentifier) return;
23+
var usedIds = {0: true};
24+
modules.forEach(function(module) {
25+
if(module.id !== null) return;
26+
var identifier = module.identifier();
27+
var id = records.modules.byIdentifier[identifier];
28+
if(usedIds[id]) return;
29+
usedIds[id] = true;
30+
module.id = id;
31+
});
32+
});
33+
34+
function getDepBlockIdent(block) {
35+
var ident = [];
36+
while(block.parent) {
37+
var p = block.parent;
38+
var idx = p.blocks.indexOf(block);
39+
var l = p.blocks.length - 1;
40+
ident.unshift(idx + "/" + l);
41+
block = block.parent;
42+
}
43+
if(!block.identifier) return null;
44+
ident.unshift(block.identifier());
45+
return ident.join(":");
46+
}
47+
compilation.plugin("record-chunks", function(chunks, records) {
48+
records.nextFreeChunkId = compilation.nextFreeChunkId;
49+
if(!records.chunks) records.chunks = {};
50+
if(!records.chunks.byName) records.chunks.byName = {};
51+
if(!records.chunks.byBlocks) records.chunks.byBlocks = {};
52+
chunks.forEach(function(chunk) {
53+
var name = chunk.name;
54+
var blockIdents = chunk.blocks.map(getDepBlockIdent).filter(Boolean);
55+
if(name) records.chunks.byName[name] = chunk.id;
56+
blockIdents.forEach(function(blockIdent) {
57+
records.chunks.byBlocks[blockIdent] = chunk.id;
58+
});
59+
});
60+
});
61+
compilation.plugin("revive-chunks", function(chunks, records) {
62+
if(records.nextFreeChunkId)
63+
compilation.nextFreeChunkId = records.nextFreeChunkId;
64+
if(!records.chunks) return;
65+
var usedIds = {0: true};
66+
if(records.chunks.byName) {
67+
chunks.forEach(function(chunk) {
68+
if(chunk.id !== null) return;
69+
if(!chunk.name) return;
70+
var id = records.chunks.byName[chunk.name];
71+
if(usedIds[id]) return;
72+
usedIds[id] = true;
73+
chunk.id = id;
74+
});
75+
}
76+
if(records.chunks.byBlocks) {
77+
var argumentedChunks = chunks.filter(function(chunk) {
78+
return chunk.id === null
79+
}).map(function(chunk) {
80+
return {
81+
chunk: chunk,
82+
blockIdents: chunk.blocks.map(getDepBlockIdent).filter(Boolean)
83+
}
84+
}).filter(function(arg) {
85+
return arg.blockIdents.length > 0;
86+
});
87+
var blockIdentsCount = {};
88+
argumentedChunks.forEach(function(arg, idx) {
89+
arg.blockIdents.forEach(function(blockIdent) {
90+
var id = records.chunks.byBlocks[blockIdent]
91+
if(!id) return;
92+
var accessor = id + ":" + idx;
93+
blockIdentsCount[accessor] = (blockIdentsCount[accessor] || 0) + 1;
94+
});
95+
});
96+
blockIdentsCount = Object.keys(blockIdentsCount).map(function(accessor) {
97+
return [blockIdentsCount[accessor]].concat(accessor.split(":").map(Number));
98+
}).sort(function(a, b) {
99+
return b[0] - a[0];
100+
})
101+
blockIdentsCount.forEach(function(arg) {
102+
var id = arg[1];
103+
if(usedIds[id]) return;
104+
var idx = arg[2];
105+
var chunk = argumentedChunks[idx].chunk;
106+
if(chunk.id !== null) return;
107+
usedIds[id] = true;
108+
chunk.id = id;
109+
});
110+
}
111+
});
112+
});
113+
};

0 commit comments

Comments
 (0)