Skip to content

Commit

Permalink
ensure all successful compiled modules are persistent cached
Browse files Browse the repository at this point in the history
fix some test cases and code to ensure this is true
  • Loading branch information
sokra committed Feb 23, 2021
1 parent f96194e commit 1a584d3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,11 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si

for (let i = 0; i < dependencies.length; i++) {
const dependency = dependencies[i];
moduleGraph.setResolvedModule(originModule, dependency, module);
moduleGraph.setResolvedModule(
recursive ? originModule : null,
dependency,
module
);
}

moduleGraph.setIssuerIfUnset(
Expand Down
8 changes: 4 additions & 4 deletions lib/util/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ exports.parseRange = str => {

/* eslint-disable eqeqeq */
const rangeToString = range => {
var fixCount = range[0];
var str = "";
if (range.length === 1) {
return "*";
} else if (0 in range) {
var str = "";
var fixCount = range[0];
} else if (fixCount + 0.5) {
str +=
fixCount == 0
? ">="
Expand Down Expand Up @@ -464,7 +464,7 @@ exports.versionLtRuntimeCode = runtimeTemplate =>
exports.rangeToStringRuntimeCode = runtimeTemplate =>
`var rangeToString = ${runtimeTemplate.basicFunction("range", [
"// see webpack/lib/util/semver.js for original code",
'if(1===range.length)return"*";if(0 in range){var r="",n=range[0];r+=0==n?">=":-1==n?"<":1==n?"^":2==n?"~":n>0?"=":"!=";for(var e=1,a=1;a<range.length;a++){e--,r+="u"==(typeof(t=range[a]))[0]?"-":(e>0?".":"")+(e=2,t)}return r}var g=[];for(a=1;a<range.length;a++){var t=range[a];g.push(0===t?"not("+o()+")":1===t?"("+o()+" || "+o()+")":2===t?g.pop()+" "+g.pop():rangeToString(t))}return o();function o(){return g.pop().replace(/^\\((.+)\\)$/,"$1")}'
'var r=range[0],n="";if(1===range.length)return"*";if(r+.5){n+=0==r?">=":-1==r?"<":1==r?"^":2==r?"~":r>0?"=":"!=";for(var e=1,a=1;a<range.length;a++){e--,n+="u"==(typeof(t=range[a]))[0]?"-":(e>0?".":"")+(e=2,t)}return n}var g=[];for(a=1;a<range.length;a++){var t=range[a];g.push(0===t?"not("+o()+")":1===t?"("+o()+" || "+o()+")":2===t?g.pop()+" "+g.pop():rangeToString(t))}return o();function o(){return g.pop().replace(/^\\((.+)\\)$/,"$1")}'
])}`;
//#endregion

Expand Down
30 changes: 29 additions & 1 deletion test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,37 @@ const describeCases = config => {
rimraf.sync(outputDirectory);
fs.mkdirSync(outputDirectory, { recursive: true });
const deprecationTracker = deprecationTracking.start();
webpack(options, err => {
webpack(options, (err, stats) => {
deprecationTracker();
if (err) return handleFatalError(err, done);
const { modules, children, errorsCount } = stats.toJson({
all: false,
modules: true,
errorsCount: true
});
if (errorsCount === 0) {
const allModules = children
? children.reduce(
(all, { modules }) => all.concat(modules),
modules || []
)
: modules;
if (
allModules.some(
m => m.type !== "cached modules" && !m.cached
)
) {
return done(
new Error(
`Some modules were not cached:\n${stats.toString({
all: false,
modules: true,
modulesSpace: 100
})}`
)
);
}
}
done();
});
}, 20000);
Expand Down
4 changes: 3 additions & 1 deletion test/configCases/rebuild/finishModules/loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = function (source) {
if (this.shouldReplace)
if (this.shouldReplace) {
this._module.buildInfo._isReplaced = true;
return "module.exports = { foo: { foo: 'bar' }, doThings: (v) => v}";
}
return source;
};
4 changes: 4 additions & 0 deletions test/configCases/rebuild/finishModules/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var testPlugin = compiler => {
throw new Error("something went wrong");
}

// Check if already build the updated version
// this will happen when using caching
if (module.buildInfo._isReplaced) return callback();

shouldReplace = true;
compilation.rebuildModule(module, err => {
shouldReplace = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module.exports = function (source) {
if (this.shouldReplace)
if (this.shouldReplace) {
this._module.buildInfo._isReplaced = true;
return `import otherFile from './other-file.js';
export default otherFile;
`;
}
return source;
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var testPlugin = compiler => {
throw new Error("something went wrong");
}

// Check if already build the updated version
// this will happen when using caching
if (module.buildInfo._isReplaced) return callback();

shouldReplace = true;
compilation.rebuildModule(module, err => {
shouldReplace = false;
Expand Down

0 comments on commit 1a584d3

Please sign in to comment.