source: frontend/node_modules/webpack-dev-middleware/dist/utils/setupWriteToDisk.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[9af201e]1"use strict";
2
3const fs = require("fs");
4
5const path = require("path");
6/** @typedef {import("webpack").Compiler} Compiler */
7
8/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
9
10/** @typedef {import("webpack").Compilation} Compilation */
11
12/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
13
14/** @typedef {import("../index.js").ServerResponse} ServerResponse */
15
16/**
17 * @template {IncomingMessage} Request
18 * @template {ServerResponse} Response
19 * @param {import("../index.js").Context<Request, Response>} context
20 */
21
22
23function setupWriteToDisk(context) {
24 /**
25 * @type {Compiler[]}
26 */
27 const compilers =
28 /** @type {MultiCompiler} */
29 context.compiler.compilers || [context.compiler];
30
31 for (const compiler of compilers) {
32 compiler.hooks.emit.tap("DevMiddleware",
33 /**
34 * @param {Compilation} compilation
35 */
36 compilation => {
37 // @ts-ignore
38 if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
39 return;
40 }
41
42 compiler.hooks.assetEmitted.tapAsync("DevMiddleware", (file, info, callback) => {
43 /**
44 * @type {string}
45 */
46 let targetPath;
47 /**
48 * @type {Buffer}
49 */
50
51 let content; // webpack@5
52
53 if (info.compilation) {
54 ({
55 targetPath,
56 content
57 } = info);
58 } else {
59 let targetFile = file;
60 const queryStringIdx = targetFile.indexOf("?");
61
62 if (queryStringIdx >= 0) {
63 targetFile = targetFile.slice(0, queryStringIdx);
64 }
65
66 let {
67 outputPath
68 } = compiler;
69 outputPath = compilation.getPath(outputPath, {}); // @ts-ignore
70
71 content = info;
72 targetPath = path.join(outputPath, targetFile);
73 }
74
75 const {
76 writeToDisk: filter
77 } = context.options;
78 const allowWrite = filter && typeof filter === "function" ? filter(targetPath) : true;
79
80 if (!allowWrite) {
81 return callback();
82 }
83
84 const dir = path.dirname(targetPath);
85 const name = compiler.options.name ? `Child "${compiler.options.name}": ` : "";
86 return fs.mkdir(dir, {
87 recursive: true
88 }, mkdirError => {
89 if (mkdirError) {
90 context.logger.error(`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`);
91 return callback(mkdirError);
92 }
93
94 return fs.writeFile(targetPath, content, writeFileError => {
95 if (writeFileError) {
96 context.logger.error(`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`);
97 return callback(writeFileError);
98 }
99
100 context.logger.log(`${name}Asset written to disk: "${targetPath}"`);
101 return callback();
102 });
103 });
104 }); // @ts-ignore
105
106 compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
107 });
108 }
109}
110
111module.exports = setupWriteToDisk;
Note: See TracBrowser for help on using the repository browser.