Skip to content

Commit 44c5b15

Browse files
committed
add a new test case for removing external deps
1 parent c6a894e commit 44c5b15

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/cache.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,89 @@ test("should cache result when there are external dependencies", async () => {
457457
assert.strictEqual(counter, 2);
458458
});
459459

460+
test("should burst cache when the external dependency is removed from filesystem", async () => {
461+
const dep = path.join(cacheDir, "externalDependency.txt");
462+
463+
fs.writeFileSync(dep, "first update");
464+
465+
let counter = 0;
466+
467+
const config = Object.assign({}, globalConfig, {
468+
cache: {
469+
type: "filesystem",
470+
cacheDirectory: context.cacheDirectory,
471+
},
472+
entry: path.join(__dirname, "fixtures/constant.js"),
473+
output: {
474+
path: context.directory,
475+
},
476+
module: {
477+
rules: [
478+
{
479+
test: /\.jsx?/,
480+
exclude: /node_modules/,
481+
use: [
482+
{
483+
loader: babelLoader,
484+
options: {
485+
babelrc: false,
486+
configFile: false,
487+
cacheDirectory: true,
488+
plugins: [
489+
api => {
490+
api.cache.never();
491+
api.addExternalDependency(dep);
492+
return {
493+
visitor: {
494+
BooleanLiteral(path) {
495+
counter++;
496+
let depContent = "dep is removed";
497+
try {
498+
depContent = fs.readFileSync(dep, "utf8");
499+
} catch {
500+
// ignore if dep is removed
501+
}
502+
path.replaceWith(api.types.stringLiteral(depContent));
503+
path.stop();
504+
},
505+
},
506+
};
507+
},
508+
],
509+
},
510+
},
511+
{
512+
loader: "./test/fixtures/uncacheable-passthrough-loader.cjs",
513+
},
514+
],
515+
},
516+
],
517+
},
518+
});
519+
520+
let stats = await webpackAsync(config);
521+
assert.deepEqual(stats.compilation.warnings, []);
522+
assert.deepEqual(stats.compilation.errors, []);
523+
524+
assert.ok(stats.compilation.fileDependencies.has(dep));
525+
assert.strictEqual(counter, 1);
526+
527+
stats = await webpackAsync(config);
528+
assert.deepEqual(stats.compilation.warnings, []);
529+
assert.deepEqual(stats.compilation.errors, []);
530+
531+
assert.ok(stats.compilation.fileDependencies.has(dep));
532+
assert.strictEqual(counter, 1);
533+
534+
fs.rmSync(dep);
535+
stats = await webpackAsync(config);
536+
assert.deepEqual(stats.compilation.warnings, []);
537+
assert.deepEqual(stats.compilation.errors, []);
538+
539+
assert.ok(stats.compilation.fileDependencies.has(dep));
540+
assert.strictEqual(counter, 2);
541+
});
542+
460543
test("should work with memory type webpack cache", async () => {
461544
const config = Object.assign({}, globalConfig, {
462545
cache: {

0 commit comments

Comments
 (0)