New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor weird things #5926

Merged
merged 6 commits into from Nov 8, 2017
Jump to file or symbol
Failed to load files and symbols.
+332 −443
Diff settings

Always

Just for now

View
@@ -49,6 +49,7 @@ class Chunk {
this.ids = null;
this.debugId = debugId++;
this.name = name;
this.entryModule = undefined;
this._modules = new SortableSet(undefined, sortByIdentifier);
this.entrypoints = [];
this._chunks = new SortableSet(undefined, sortById);
@@ -57,6 +58,10 @@ class Chunk {
this.origins = [];
this.files = [];
this.rendered = false;
this.hash = undefined;
this.renderedHash = undefined;
this.chunkReason = undefined;
this.extraAsync = false;
if(module) {
this.origins.push({
module,
View
@@ -73,7 +73,7 @@ class Compilation extends Tapable {
this.chunks = [];
this.namedChunks = {};
this.modules = [];
this._modules = {};
this._modules = new Map();
this.cache = null;
this.records = null;
this.nextFreeModuleIndex = undefined;
@@ -87,6 +87,9 @@ class Compilation extends Tapable {
this.dependencyTemplates = new Map();
this.dependencyTemplates.set("hash", "");
this.childrenCounters = {};
this._buildingModules = new Map();
this._rebuildingModules = new Map();
}
getStats() {
@@ -100,29 +103,29 @@ class Compilation extends Tapable {
addModule(module, cacheGroup) {
const identifier = module.identifier();
if(this._modules[identifier]) {
if(this._modules.get(identifier)) {
return false;
}
const cacheName = (cacheGroup || "m") + identifier;
if(this.cache && this.cache[cacheName]) {
const cacheModule = this.cache[cacheName];
let rebuild = true;
if(!cacheModule.error && cacheModule.cacheable && this.fileTimestamps && this.contextTimestamps) {
if(this.fileTimestamps && this.contextTimestamps) {
rebuild = cacheModule.needRebuild(this.fileTimestamps, this.contextTimestamps);
}
if(!rebuild) {
cacheModule.disconnect();
this._modules[identifier] = cacheModule;
this._modules.set(identifier, cacheModule);
this.modules.push(cacheModule);
cacheModule.errors.forEach(err => this.errors.push(err), this);
cacheModule.warnings.forEach(err => this.warnings.push(err), this);
return cacheModule;
}
module.unbuild();
}
module.unbuild();
this._modules[identifier] = module;
this._modules.set(identifier, module);
if(this.cache) {
this.cache[cacheName] = module;
}
@@ -132,22 +135,36 @@ class Compilation extends Tapable {
getModule(module) {
const identifier = module.identifier();
return this._modules[identifier];
return this._modules.get(identifier);
}
findModule(identifier) {
return this._modules[identifier];
return this._modules.get(identifier);
}
waitForBuildingFinished(module, callback) {
let callbackList = this._buildingModules.get(module);
if(callbackList) {
callbackList.push(() => callback());
} else {
process.nextTick(callback);
}
}
buildModule(module, optional, origin, dependencies, thisCallback) {
this.applyPlugins1("build-module", module);
if(module.building) return module.building.push(thisCallback);
const building = module.building = [thisCallback];
function callback(err) {
module.building = undefined;
building.forEach(cb => cb(err));
let callbackList = this._buildingModules.get(module);
if(callbackList) {
callbackList.push(thisCallback);
return;
}
this._buildingModules.set(module, callbackList = [thisCallback]);
const callback = err => {
this._buildingModules.delete(module);
callbackList.forEach(cb => cb(err));
};
module.build(this.options, this, this.resolvers.normal, this.inputFileSystem, (error) => {
const errors = module.errors;
for(let indexError = 0; indexError < errors.length; indexError++) {
@@ -252,7 +269,7 @@ class Compilation extends Tapable {
let afterFactory;
function isOptional() {
return dependencies.filter(d => !d.optional).length === 0;
return dependencies.every(d => d.optional);
}
function errorOrWarningAndCallback(err) {
@@ -287,16 +304,11 @@ class Compilation extends Tapable {
dependentModule.profile.factory = afterFactory - start;
}
dependentModule.issuer = module;
const newModule = _this.addModule(dependentModule, cacheGroup);
if(!newModule) { // from cache
dependentModule = _this.getModule(dependentModule);
if(dependentModule.optional) {
dependentModule.optional = isOptional();
}
iterationDependencies(dependencies);
if(_this.profile) {
@@ -310,16 +322,16 @@ class Compilation extends Tapable {
}
semaphore.release();
return process.nextTick(callback);
_this.waitForBuildingFinished(dependentModule, callback);
return;
}
if(newModule instanceof Module) {
if(_this.profile) {
newModule.profile = dependentModule.profile;
}
newModule.optional = isOptional();
newModule.issuer = dependentModule.issuer;
newModule.issuer = module;
dependentModule = newModule;
iterationDependencies(dependencies);
@@ -337,7 +349,7 @@ class Compilation extends Tapable {
}
}
dependentModule.optional = isOptional();
dependentModule.issuer = module;
iterationDependencies(dependencies);
@@ -429,13 +441,20 @@ class Compilation extends Tapable {
onModule(module);
dependency.module = module;
module.addReason(null, dependency);
if(this.profile) {
const afterBuilding = Date.now();
module.profile.building = afterBuilding - afterFactory;
}
this.semaphore.release();
return callback(null, module);
this.waitForBuildingFinished(module, err => {
if(err) return callback(err);
callback(null, module);
});
return;
}
if(result instanceof Module) {
@@ -447,12 +466,18 @@ class Compilation extends Tapable {
onModule(module);
dependency.module = module;
module.addReason(null, dependency);
moduleReady.call(this);
return;
}
onModule(module);
dependency.module = module;
module.addReason(null, dependency);
this.buildModule(module, false, null, null, (err) => {
if(err) {
this.semaphore.release();
@@ -489,9 +514,7 @@ class Compilation extends Tapable {
this.preparedChunks.push(slot);
this._addModuleChain(context, entry, (module) => {
entry.module = module;
this.entries.push(module);
module.issuer = null;
}, (err, module) => {
if(err) {
@@ -512,39 +535,35 @@ class Compilation extends Tapable {
this._addModuleChain(context, dependency, module => {
module.prefetched = true;
module.issuer = null;
}, callback);
}
rebuildModule(module, thisCallback) {
if(module.variables.length || module.blocks.length)
throw new Error("Cannot rebuild a complex module with variables or blocks");
if(module.rebuilding) {
return module.rebuilding.push(thisCallback);
let callbackList = this._rebuildingModules.get(module);
if(callbackList) {
callbackList.push(thisCallback);
return;
}
const rebuilding = module.rebuilding = [thisCallback];
this._rebuildingModules.set(module, callbackList = [thisCallback]);
function callback(err) {
module.rebuilding = undefined;
rebuilding.forEach(cb => cb(err));
}
const deps = module.dependencies.slice();
const callback = err => {
this._rebuildingModules.delete(module);
callbackList.forEach(cb => cb(err));
};
const oldDependencies = module.dependencies.slice();
const oldVariables = module.variables.slice();
const oldBlocks = module.blocks.slice();
this.buildModule(module, false, module, null, (err) => {
if(err) return callback(err);
this.processModuleDependencies(module, (err) => {
if(err) return callback(err);
deps.forEach(d => {
if(d.module && d.module.removeReason(module, d)) {
module.forEachChunk(chunk => {
if(!d.module.hasReasonForChunk(chunk)) {
if(d.module.removeChunk(chunk)) {
this.removeChunkFromDependencies(d.module, chunk);
}
}
});
}
this.removeReasonsOfDependencyBlock(module, {
dependencies: oldDependencies,
variables: oldVariables,
blocks: oldBlocks
});
callback();
});
@@ -1077,16 +1096,46 @@ class Compilation extends Tapable {
}
}
removeChunkFromDependencies(block, chunk) {
removeReasonsOfDependencyBlock(module, block) {
const iteratorDependency = d => {
if(!d.module) {
return;
}
if(!d.module.hasReasonForChunk(chunk)) {
if(d.module.removeChunk(chunk)) {
this.removeChunkFromDependencies(d.module, chunk);
}
if(d.module.removeReason(module, d)) {
d.module.forEachChunk(chunk => this.patchChunksAfterReasonRemoval(d.module, chunk));
}
};
if(block.blocks) {
iterationOfArrayCallback(block.blocks, block => this.removeReasonsOfDependencyBlock(module, block));
}
if(block.dependencies) {
iterationOfArrayCallback(block.dependencies, iteratorDependency);
}
if(block.variables) {
iterationBlockVariable(block.variables, iteratorDependency);
}
}
patchChunksAfterReasonRemoval(module, chunk) {
if(!module.hasReasons()) {
this.removeReasonsOfDependencyBlock(module, module);
}
if(!module.hasReasonForChunk(chunk)) {
if(module.removeChunk(chunk)) {
this.removeChunkFromDependencies(module, chunk);
}
}
}
removeChunkFromDependencies(block, chunk) {
const iteratorDependency = d => {
if(!d.module) {
return;
}
this.patchChunksAfterReasonRemoval(d.module, chunk);
};
const blocks = block.blocks;
View
@@ -19,12 +19,16 @@ class ContextModule extends Module {
// options: ContextOptions
constructor(resolveDependencies, options) {
super();
// Info from Factory
this.resolveDependencies = resolveDependencies;
this.options = options;
this.cacheable = true;
this.context = options.resource;
// Info from Build
this.builtTime = undefined;
this.contextDependencies = [this.context];
this.built = false;
if(typeof options.mode !== "string")
throw new Error("options.mode is a required option");
}
@@ -109,20 +113,15 @@ class ContextModule extends Module {
return ts >= this.builtTime;
}
unbuild() {
this.built = false;
super.unbuild();
}
build(options, compilation, resolver, fs, callback) {
this.built = true;
this.builtTime = Date.now();
this.resolveDependencies(fs, this.options, (err, dependencies) => {
if(err) return callback(err);
// Reset children
this.dependencies = [];
this.blocks = [];
this.dependencies.length = 0;
this.blocks.length = 0;
// abort if something failed
// this will create an empty context
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.