Skip to content

Commit

Permalink
Add tests from #984
Browse files Browse the repository at this point in the history
# Conflicts:
#	.yarnrc.yml
#	src/index.js
  • Loading branch information
liuxingbaoyu authored and JLHwung committed Aug 26, 2024
1 parent 94ff4eb commit a6727fc
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
enableGlobalCache: true
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-3.6.4.cjs
87 changes: 87 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,90 @@ test("should allow to specify the .babelrc file", async () => {
// is { "presets": ["@babel/preset-env"] }
assert.ok(files.length === 1);
});

test.cb("should cache external dependencies", t => {
const dep = path.join(cacheDir, "externalDependency.txt");

fs.writeFileSync(dep, "123");

let counter = 0;

const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.js$/,
loader: babelLoader,
options: {
babelrc: false,
configFile: false,
cacheDirectory: t.context.cacheDirectory,
plugins: [
api => {
api.cache.never();
api.addExternalDependency(dep);
return {
visitor: {
BooleanLiteral(path) {
counter++;
path.replaceWith(
api.types.stringLiteral(fs.readFileSync(dep, "utf8")),
);
path.stop();
},
},
};
},
],
},
},
],
},
});

webpack(config, (err, stats) => {
t.deepEqual(stats.compilation.warnings, []);
t.deepEqual(stats.compilation.errors, []);

t.true(stats.compilation.fileDependencies.has(dep));

t.is(counter, 1);

webpack(config, (err, stats) => {
t.deepEqual(stats.compilation.warnings, []);
t.deepEqual(stats.compilation.errors, []);

t.true(stats.compilation.fileDependencies.has(dep));

t.is(counter, 2);

webpack(config, (err, stats) => {
t.deepEqual(stats.compilation.warnings, []);
t.deepEqual(stats.compilation.errors, []);

t.true(stats.compilation.fileDependencies.has(dep));

t.is(counter, 2);

fs.writeFileSync(dep, "456");

setTimeout(() => {
webpack(config, (err, stats) => {
t.deepEqual(stats.compilation.warnings, []);
t.deepEqual(stats.compilation.errors, []);

t.true(stats.compilation.fileDependencies.has(dep));

t.is(counter, 3);

t.end();
});
}, 1000);
});
});
});
});

0 comments on commit a6727fc

Please sign in to comment.