Skip to content

Commit bb8347a

Browse files
refactor: remove outdated code (#1535)
1 parent d2896c5 commit bb8347a

File tree

2 files changed

+39
-104
lines changed

2 files changed

+39
-104
lines changed

src/utils/setupWriteToDisk.js

Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,96 +21,60 @@ function setupWriteToDisk(context) {
2121
(context.compiler).compilers || [context.compiler];
2222

2323
for (const compiler of compilers) {
24-
compiler.hooks.emit.tap(
25-
"DevMiddleware",
26-
/**
27-
* @param {Compilation} compilation
28-
*/
29-
(compilation) => {
30-
// @ts-ignore
31-
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
32-
return;
33-
}
34-
35-
compiler.hooks.assetEmitted.tapAsync(
36-
"DevMiddleware",
37-
(file, info, callback) => {
38-
/**
39-
* @type {string}
40-
*/
41-
let targetPath;
42-
/**
43-
* @type {Buffer}
44-
*/
45-
let content;
46-
47-
// webpack@5
48-
if (info.compilation) {
49-
({ targetPath, content } = info);
50-
} else {
51-
let targetFile = file;
24+
compiler.hooks.emit.tap("DevMiddleware", () => {
25+
// @ts-ignore
26+
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
27+
return;
28+
}
5229

53-
const queryStringIdx = targetFile.indexOf("?");
30+
compiler.hooks.assetEmitted.tapAsync(
31+
"DevMiddleware",
32+
(file, info, callback) => {
33+
const { targetPath, content } = info;
34+
const { writeToDisk: filter } = context.options;
35+
const allowWrite =
36+
filter && typeof filter === "function" ? filter(targetPath) : true;
5437

55-
if (queryStringIdx >= 0) {
56-
targetFile = targetFile.slice(0, queryStringIdx);
57-
}
58-
59-
let { outputPath } = compiler;
38+
if (!allowWrite) {
39+
return callback();
40+
}
6041

61-
outputPath = compilation.getPath(outputPath, {});
62-
// @ts-ignore
63-
content = info;
64-
targetPath = path.join(outputPath, targetFile);
65-
}
42+
const dir = path.dirname(targetPath);
43+
const name = compiler.options.name
44+
? `Child "${compiler.options.name}": `
45+
: "";
6646

67-
const { writeToDisk: filter } = context.options;
68-
const allowWrite =
69-
filter && typeof filter === "function"
70-
? filter(targetPath)
71-
: true;
47+
return fs.mkdir(dir, { recursive: true }, (mkdirError) => {
48+
if (mkdirError) {
49+
context.logger.error(
50+
`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`
51+
);
7252

73-
if (!allowWrite) {
74-
return callback();
53+
return callback(mkdirError);
7554
}
7655

77-
const dir = path.dirname(targetPath);
78-
const name = compiler.options.name
79-
? `Child "${compiler.options.name}": `
80-
: "";
81-
82-
return fs.mkdir(dir, { recursive: true }, (mkdirError) => {
83-
if (mkdirError) {
56+
return fs.writeFile(targetPath, content, (writeFileError) => {
57+
if (writeFileError) {
8458
context.logger.error(
85-
`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`
59+
`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`
8660
);
8761

88-
return callback(mkdirError);
62+
return callback(writeFileError);
8963
}
9064

91-
return fs.writeFile(targetPath, content, (writeFileError) => {
92-
if (writeFileError) {
93-
context.logger.error(
94-
`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`
95-
);
96-
97-
return callback(writeFileError);
98-
}
99-
100-
context.logger.log(
101-
`${name}Asset written to disk: "${targetPath}"`
102-
);
65+
context.logger.log(
66+
`${name}Asset written to disk: "${targetPath}"`
67+
);
10368

104-
return callback();
105-
});
69+
return callback();
10670
});
107-
}
108-
);
71+
});
72+
}
73+
);
10974

110-
// @ts-ignore
111-
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
112-
}
113-
);
75+
// @ts-ignore
76+
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
77+
});
11478
}
11579
}
11680

test/utils/setupWriteToDisk.test.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from "fs";
2-
import path from "path";
32

43
import setupWriteToDisk from "../../src/utils/setupWriteToDisk";
54

@@ -88,34 +87,6 @@ describe("setupWriteToDisk", () => {
8887
expect(mkdirSpy.mock.calls.length).toEqual(0);
8988
});
9089

91-
it("handles query string with webpack@4", () => {
92-
const filter = jest.fn(() => false);
93-
context.options = {
94-
writeToDisk: filter,
95-
};
96-
setupWriteToDisk(context);
97-
const cb = jest.fn();
98-
// webpack@4 info style
99-
runAssetEmitted(
100-
"file?query=example",
101-
{
102-
targetPath: "targetPath",
103-
},
104-
cb
105-
);
106-
107-
// the getPath helper is needed for webpack@4
108-
expect(getPath.mock.calls.length).toEqual(1);
109-
110-
expect(filter.mock.calls.length).toEqual(1);
111-
// need to fix path for windows test
112-
expect(filter.mock.calls[0][0]).toEqual(path.join("/output/path/file"));
113-
// the callback should always be called
114-
expect(cb.mock.calls.length).toEqual(1);
115-
// the filter prevents a directory from being made
116-
expect(mkdirSpy.mock.calls.length).toEqual(0);
117-
});
118-
11990
const writeErrors = [
12091
{
12192
title: "with no write errors",

0 commit comments

Comments
 (0)