Skip to content

Commit

Permalink
refactor: remove outdated code (#1535)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored May 2, 2023
1 parent d2896c5 commit bb8347a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 104 deletions.
114 changes: 39 additions & 75 deletions src/utils/setupWriteToDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,96 +21,60 @@ function setupWriteToDisk(context) {
(context.compiler).compilers || [context.compiler];

for (const compiler of compilers) {
compiler.hooks.emit.tap(
"DevMiddleware",
/**
* @param {Compilation} compilation
*/
(compilation) => {
// @ts-ignore
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
return;
}

compiler.hooks.assetEmitted.tapAsync(
"DevMiddleware",
(file, info, callback) => {
/**
* @type {string}
*/
let targetPath;
/**
* @type {Buffer}
*/
let content;

// webpack@5
if (info.compilation) {
({ targetPath, content } = info);
} else {
let targetFile = file;
compiler.hooks.emit.tap("DevMiddleware", () => {
// @ts-ignore
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
return;
}

const queryStringIdx = targetFile.indexOf("?");
compiler.hooks.assetEmitted.tapAsync(
"DevMiddleware",
(file, info, callback) => {
const { targetPath, content } = info;
const { writeToDisk: filter } = context.options;
const allowWrite =
filter && typeof filter === "function" ? filter(targetPath) : true;

if (queryStringIdx >= 0) {
targetFile = targetFile.slice(0, queryStringIdx);
}

let { outputPath } = compiler;
if (!allowWrite) {
return callback();
}

outputPath = compilation.getPath(outputPath, {});
// @ts-ignore
content = info;
targetPath = path.join(outputPath, targetFile);
}
const dir = path.dirname(targetPath);
const name = compiler.options.name
? `Child "${compiler.options.name}": `
: "";

const { writeToDisk: filter } = context.options;
const allowWrite =
filter && typeof filter === "function"
? filter(targetPath)
: true;
return fs.mkdir(dir, { recursive: true }, (mkdirError) => {
if (mkdirError) {
context.logger.error(
`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`
);

if (!allowWrite) {
return callback();
return callback(mkdirError);
}

const dir = path.dirname(targetPath);
const name = compiler.options.name
? `Child "${compiler.options.name}": `
: "";

return fs.mkdir(dir, { recursive: true }, (mkdirError) => {
if (mkdirError) {
return fs.writeFile(targetPath, content, (writeFileError) => {
if (writeFileError) {
context.logger.error(
`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`
`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`
);

return callback(mkdirError);
return callback(writeFileError);
}

return fs.writeFile(targetPath, content, (writeFileError) => {
if (writeFileError) {
context.logger.error(
`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`
);

return callback(writeFileError);
}

context.logger.log(
`${name}Asset written to disk: "${targetPath}"`
);
context.logger.log(
`${name}Asset written to disk: "${targetPath}"`
);

return callback();
});
return callback();
});
}
);
});
}
);

// @ts-ignore
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
}
);
// @ts-ignore
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
});
}
}

Expand Down
29 changes: 0 additions & 29 deletions test/utils/setupWriteToDisk.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from "fs";
import path from "path";

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

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

it("handles query string with webpack@4", () => {
const filter = jest.fn(() => false);
context.options = {
writeToDisk: filter,
};
setupWriteToDisk(context);
const cb = jest.fn();
// webpack@4 info style
runAssetEmitted(
"file?query=example",
{
targetPath: "targetPath",
},
cb
);

// the getPath helper is needed for webpack@4
expect(getPath.mock.calls.length).toEqual(1);

expect(filter.mock.calls.length).toEqual(1);
// need to fix path for windows test
expect(filter.mock.calls[0][0]).toEqual(path.join("/output/path/file"));
// the callback should always be called
expect(cb.mock.calls.length).toEqual(1);
// the filter prevents a directory from being made
expect(mkdirSpy.mock.calls.length).toEqual(0);
});

const writeErrors = [
{
title: "with no write errors",
Expand Down

0 comments on commit bb8347a

Please sign in to comment.