Skip to content

Commit

Permalink
make Module.hash official and use it for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 4, 2017
1 parent 7d87f34 commit 2acd0d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
7 changes: 7 additions & 0 deletions lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Module extends DependenciesBlock {
// Unique Id
this.debugId = debugId++;

// Hash
this.hash = undefined;
this.renderedHash = undefined;

// Info from Factory
// TODO refactor: pass as constructor argument
this.context = null;
Expand Down Expand Up @@ -93,6 +97,9 @@ class Module extends DependenciesBlock {
}

disconnect() {
this.hash = undefined;
this.renderedHash = undefined;

this.reasons.length = 0;
this._rewriteChunkInReasons = undefined;
this._chunks.clear();
Expand Down
27 changes: 13 additions & 14 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const ModuleParseError = require("./ModuleParseError");
const ModuleBuildError = require("./ModuleBuildError");
const ModuleError = require("./ModuleError");
const ModuleWarning = require("./ModuleWarning");
const createHash = require("./util/createHash");

const runLoaders = require("loader-runner").runLoaders;
const getContext = require("loader-runner").getContext;
Expand Down Expand Up @@ -88,7 +87,8 @@ class NormalModule extends Module {
this._source = null;
this.buildTimestamp = undefined;
this.cacheable = false;
this._cachedSource = null;
this._cachedSource = undefined;
this._cachedSourceHash = undefined;

// Options for the NormalModule set by plugins
// TODO refactor this -> options object filled from Factory
Expand Down Expand Up @@ -294,7 +294,8 @@ class NormalModule extends Module {
this.dependencies.length = 0;
this.variables.length = 0;
this.blocks.length = 0;
this._cachedSource = null;
this._cachedSource = undefined;
this._cachedSourceHash = undefined;

// if we have an error mark module as failed and exit
if(err) {
Expand Down Expand Up @@ -328,10 +329,7 @@ class NormalModule extends Module {

getHashDigest(dependencyTemplates) {
let dtHash = dependencyTemplatesHashMap.get("hash");
const hash = createHash("md5");
this.updateHash(hash);
hash.update(`${dtHash}`);
return hash.digest("hex");
return `${this.hash}-${dtHash}`;
}

sourceDependency(dependency, dependencyTemplates, source, outputOptions, requestShortener) {
Expand Down Expand Up @@ -489,22 +487,23 @@ class NormalModule extends Module {
source(dependencyTemplates, outputOptions, requestShortener) {
if(this.type.startsWith("javascript")) {
const hashDigest = this.getHashDigest(dependencyTemplates);
if(this._cachedSource && this._cachedSource.hash === hashDigest) {
return this._cachedSource.source;
if(this._cachedSourceHash === hashDigest) {
// We can reuse the cached source
return this._cachedSource;
}

if(!this._source) {
return new RawSource("throw new Error('No source available');");
}

const source = new ReplaceSource(this._source);
this._cachedSource = {
source: source,
hash: hashDigest
};

this.sourceBlock(this, [], dependencyTemplates, source, outputOptions, requestShortener);
return new CachedSource(source);

const cachedSource = new CachedSource(source);
this._cachedSource = cachedSource;
this._cachedSourceHash = hashDigest;
return cachedSource;
}
return this._source;
}
Expand Down

0 comments on commit 2acd0d4

Please sign in to comment.