Skip to content

Commit

Permalink
Merge pull request #9658 from webpack/bugfix/cache-pack
Browse files Browse the repository at this point in the history
Persistent Caching fixes
  • Loading branch information
sokra committed Sep 5, 2019
2 parents ffa95bd + d3cf392 commit acc7c3e
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 159 deletions.
86 changes: 71 additions & 15 deletions lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const {
} = require("./util/comparators");
const { compareModulesById } = require("./util/comparators");
const { contextify } = require("./util/identifier");
const { registerNotSerializable } = require("./util/serialization");
const makeSerializable = require("./util/makeSerializable");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
Expand Down Expand Up @@ -79,13 +79,18 @@ class ContextModule extends Module {
constructor(resolveDependencies, options) {
let resource;
let resourceQuery;
const queryIdx = options.resource.indexOf("?");
if (queryIdx >= 0) {
resource = options.resource.substr(0, queryIdx);
resourceQuery = options.resource.substr(queryIdx);
} else {
if (options.resourceQuery) {
resource = options.resource;
resourceQuery = "";
resourceQuery = options.resourceQuery;
} else {
const queryIdx = options.resource.indexOf("?");
if (queryIdx >= 0) {
resource = options.resource.substr(0, queryIdx);
resourceQuery = options.resource.substr(queryIdx);
} else {
resource = options.resource;
resourceQuery = "";
}
}

super("javascript/dynamic", resource);
Expand All @@ -94,9 +99,17 @@ class ContextModule extends Module {
this.resolveDependencies = resolveDependencies;
/** @type {ContextModuleOptions} */
this.options = ({
...options,
resource: resource,
resourceQuery: resourceQuery
resourceQuery: resourceQuery,
mode: options.mode,
recursive: options.recursive,
addon: options.addon,
regExp: options.regExp,
include: options.include,
exclude: options.exclude,
chunkName: options.chunkName,
groupOptions: options.groupOptions,
namespaceObject: options.namespaceObject
});
if (options.resolveOptions !== undefined) {
this.resolveOptions = options.resolveOptions;
Expand Down Expand Up @@ -156,6 +169,9 @@ class ContextModule extends Module {
if (this.options.exclude) {
identifier += `|exclude: ${this.options.exclude}`;
}
if (this.options.chunkName) {
identifier += `|chunkName: ${this.options.chunkName}`;
}
if (this.options.groupOptions) {
identifier += `|groupOptions: ${JSON.stringify(
this.options.groupOptions
Expand Down Expand Up @@ -204,6 +220,9 @@ class ContextModule extends Module {
if (this.options.exclude) {
identifier += ` exclude: ${this.prettyRegExp(this.options.exclude + "")}`;
}
if (this.options.chunkName) {
identifier += ` chunkName: ${this.options.chunkName}`;
}
if (this.options.groupOptions) {
const groupOptions = this.options.groupOptions;
for (const key of Object.keys(groupOptions)) {
Expand Down Expand Up @@ -268,11 +287,14 @@ class ContextModule extends Module {
* @returns {void}
*/
needBuild({ fileSystemInfo }, callback) {
// build if enforced
if (this._forceBuild) return callback(null, true);
fileSystemInfo.getContextTimestamp(this.context, (err, info) => {
if (err || !info) return callback(null, true);

return callback(null, info.safeTime >= this.buildInfo.builtTime);
// always build when we have no snapshot
if (!this.buildInfo.snapshot) return callback(null, true);

fileSystemInfo.checkSnapshotValid(this.buildInfo.snapshot, (err, valid) => {
callback(err, !valid);
});
}

Expand All @@ -288,11 +310,12 @@ class ContextModule extends Module {
this._forceBuild = false;
this.buildMeta = {};
this.buildInfo = {
builtTime: Date.now(),
snapshot: undefined,
contextDependencies: this._contextDependencies
};
this.dependencies.length = 0;
this.blocks.length = 0;
const startTime = Date.now();
this.resolveDependencies(fs, this.options, (err, dependencies) => {
if (err) return callback(err);

Expand Down Expand Up @@ -376,7 +399,18 @@ class ContextModule extends Module {
);
return;
}
callback();
compilation.fileSystemInfo.createSnapshot(
startTime,
null,
[this.context],
null,
{},
(err, snapshot) => {
if (err) return callback(err);
this.buildInfo.snapshot = snapshot;
callback();
}
);
});
}

Expand Down Expand Up @@ -952,8 +986,30 @@ module.exports = webpackEmptyAsyncContext;`;
return size + 5 + element.userRequest.length;
}, initialSize);
}

serialize(context) {
const { write } = context;
// constructor
write(this.options);
// deserialize
write(this._forceBuild);
super.serialize(context);
}

static deserialize(context) {
const { read } = context;
const obj = new ContextModule(null, read());
obj.deserialize(context);
return obj;
}

deserialize(context) {
const { read } = context;
this._forceBuild = read();
super.deserialize(context);
}
}

registerNotSerializable(ContextModule);
makeSerializable(ContextModule, "webpack/lib/ContextModule");

module.exports = ContextModule;

0 comments on commit acc7c3e

Please sign in to comment.