Skip to content

Commit

Permalink
fix and test
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Mar 23, 2023
1 parent fcc791d commit 5f992f0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .yarnrc.yml
@@ -1 +1,4 @@
yarnPath: .yarn/releases/yarn-3.2.3.cjs
enableGlobalCache: true
nodeLinker: node-modules

4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -193,7 +193,7 @@ async function loader(source, inputSourceMap, overrides, data) {
for (const dep of Object.keys(data.cachedDepMtimes)) {
let mtime = 0;
try {
mtime = fs.statSync(dep).mtime;
mtime = fs.statSync(dep).mtimeMs;
} catch (error) {}
cachedDepMtimes.push(dep + mtime);
}
Expand Down Expand Up @@ -231,7 +231,7 @@ async function loader(source, inputSourceMap, overrides, data) {
if (data.cachedDepMtimes[dep] == null) {
let mtime = 0;
try {
mtime = fs.statSync(dep).mtime;
mtime = fs.statSync(dep).mtimeMs;
} catch (error) {}
data.cachedDepMtimes[dep] = mtime;
}
Expand Down
87 changes: 87 additions & 0 deletions test/cache.test.js
Expand Up @@ -389,3 +389,90 @@ test.cb("should allow to specify the .babelrc file", t => {
});
});
});

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 5f992f0

Please sign in to comment.