Merge pull request #14704 from webpack/issue-14700

fix: remove links in clean plugin
This commit is contained in:
Tobias Koppers 2021-11-15 11:19:40 +01:00 committed by GitHub
commit 4876a16dcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 1 deletions

View File

@ -16,6 +16,7 @@ const processAsyncTree = require("./util/processAsyncTree");
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./logging/Logger").Logger} Logger */
/** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
/** @typedef {import("./util/fs").StatsCallback} StatsCallback */
/** @typedef {(function(string):boolean)|RegExp} IgnoreItem */
/** @typedef {function(IgnoreItem): void} AddToIgnoreCallback */
@ -102,6 +103,20 @@ const getDiffToOldAssets = (currentAssets, oldAssets) => {
return diff;
};
/**
* @param {OutputFileSystem} fs filesystem
* @param {string} filename path to file
* @param {StatsCallback} callback callback for provided filename
* @returns {void}
*/
const doStat = (fs, filename, callback) => {
if ("lstat" in fs) {
fs.lstat(filename, callback);
} else {
fs.stat(filename, callback);
}
};
/**
* @param {OutputFileSystem} fs filesystem
* @param {string} outputPath output path
@ -150,7 +165,7 @@ const applyDiff = (fs, outputPath, dry, logger, diff, isKept, callback) => {
log(`${filename} will be kept`);
return process.nextTick(callback);
}
fs.stat(path, (err, stats) => {
doStat(fs, path, (err, stats) => {
if (err) return handleError(err);
if (!stats.isDirectory()) {
push({

View File

@ -93,6 +93,7 @@ const path = require("path");
* @property {function(string, Callback): void=} rmdir
* @property {function(string, Callback): void=} unlink
* @property {function(string, StatsCallback): void} stat
* @property {function(string, StatsCallback): void=} lstat
* @property {function(string, BufferOrStringCallback): void} readFile
* @property {(function(string, string): string)=} join
* @property {(function(string, string): string)=} relative

View File

@ -0,0 +1 @@
it("should compile and run the test", function() {});

View File

@ -0,0 +1,16 @@
const fs = require("fs");
const path = require("path");
module.exports = () => {
try {
fs.symlinkSync(
path.join(__dirname, "index.js"),
path.join(__dirname, ".testlink"),
"file"
);
fs.unlinkSync(path.join(__dirname, ".testlink"));
return true;
} catch (e) {
return false;
}
};

View File

@ -0,0 +1,41 @@
const fs = require("fs");
const path = require("path");
const readDir = require("../enabled/readdir");
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
clean: true
},
plugins: [
compiler => {
let once = true;
compiler.hooks.environment.tap("Test", () => {
if (once) {
const outputPath = compiler.options.output.path;
const originalPath = path.join(outputPath, "file.ext");
fs.writeFileSync(originalPath, "");
const customDir = path.join(outputPath, "this/dir/should/be/removed");
fs.mkdirSync(customDir, { recursive: true });
fs.symlinkSync(
originalPath,
path.join(customDir, "file-link.ext"),
"file"
);
once = false;
}
});
compiler.hooks.afterEmit.tap("Test", compilation => {
const outputPath = compilation.getPath(compiler.outputPath, {});
expect(readDir(outputPath)).toMatchInlineSnapshot(`
Object {
"directories": Array [],
"files": Array [
"bundle0.js",
],
}
`);
});
}
]
};

4
types.d.ts vendored
View File

@ -8348,6 +8348,10 @@ declare interface OutputFileSystem {
arg0: string,
arg1: (arg0?: null | NodeJS.ErrnoException, arg1?: IStats) => void
) => void;
lstat?: (
arg0: string,
arg1: (arg0?: null | NodeJS.ErrnoException, arg1?: IStats) => void
) => void;
readFile: (
arg0: string,
arg1: (arg0?: null | NodeJS.ErrnoException, arg1?: string | Buffer) => void