From 5f992f0c9b1d11f2e508a4b61d442dbcd517ad43 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 24 Mar 2023 02:40:57 +0800 Subject: [PATCH] fix and test --- .yarnrc.yml | 3 ++ src/index.js | 4 +-- test/cache.test.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/.yarnrc.yml b/.yarnrc.yml index ef12e5ef..b041d68e 100644 --- a/.yarnrc.yml +++ b/.yarnrc.yml @@ -1 +1,4 @@ yarnPath: .yarn/releases/yarn-3.2.3.cjs +enableGlobalCache: true +nodeLinker: node-modules + diff --git a/src/index.js b/src/index.js index 0c518c93..18962f89 100644 --- a/src/index.js +++ b/src/index.js @@ -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); } @@ -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; } diff --git a/test/cache.test.js b/test/cache.test.js index 8fa0c3e2..7e940966 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -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); + }); + }); + }); +});