1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = setupWriteToDisk;
|
---|
7 |
|
---|
8 | var _fs = _interopRequireDefault(require("fs"));
|
---|
9 |
|
---|
10 | var _path = _interopRequireDefault(require("path"));
|
---|
11 |
|
---|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
13 |
|
---|
14 | function setupWriteToDisk(context) {
|
---|
15 | const compilers = context.compiler.compilers || [context.compiler];
|
---|
16 |
|
---|
17 | for (const compiler of compilers) {
|
---|
18 | compiler.hooks.emit.tap("DevMiddleware", compilation => {
|
---|
19 | if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
|
---|
20 | return;
|
---|
21 | }
|
---|
22 |
|
---|
23 | compiler.hooks.assetEmitted.tapAsync("DevMiddleware", (file, info, callback) => {
|
---|
24 | let targetPath = null;
|
---|
25 | let content = null; // webpack@5
|
---|
26 |
|
---|
27 | if (info.compilation) {
|
---|
28 | ({
|
---|
29 | targetPath,
|
---|
30 | content
|
---|
31 | } = info);
|
---|
32 | } else {
|
---|
33 | let targetFile = file;
|
---|
34 | const queryStringIdx = targetFile.indexOf("?");
|
---|
35 |
|
---|
36 | if (queryStringIdx >= 0) {
|
---|
37 | targetFile = targetFile.substr(0, queryStringIdx);
|
---|
38 | }
|
---|
39 |
|
---|
40 | let {
|
---|
41 | outputPath
|
---|
42 | } = compiler;
|
---|
43 | outputPath = compilation.getPath(outputPath, {});
|
---|
44 | content = info;
|
---|
45 | targetPath = _path.default.join(outputPath, targetFile);
|
---|
46 | }
|
---|
47 |
|
---|
48 | const {
|
---|
49 | writeToDisk: filter
|
---|
50 | } = context.options;
|
---|
51 | const allowWrite = filter && typeof filter === "function" ? filter(targetPath) : true;
|
---|
52 |
|
---|
53 | if (!allowWrite) {
|
---|
54 | return callback();
|
---|
55 | }
|
---|
56 |
|
---|
57 | const dir = _path.default.dirname(targetPath);
|
---|
58 |
|
---|
59 | const name = compiler.options.name ? `Child "${compiler.options.name}": ` : "";
|
---|
60 | return _fs.default.mkdir(dir, {
|
---|
61 | recursive: true
|
---|
62 | }, mkdirError => {
|
---|
63 | if (mkdirError) {
|
---|
64 | context.logger.error(`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`);
|
---|
65 | return callback(mkdirError);
|
---|
66 | }
|
---|
67 |
|
---|
68 | return _fs.default.writeFile(targetPath, content, writeFileError => {
|
---|
69 | if (writeFileError) {
|
---|
70 | context.logger.error(`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`);
|
---|
71 | return callback(writeFileError);
|
---|
72 | }
|
---|
73 |
|
---|
74 | context.logger.log(`${name}Asset written to disk: "${targetPath}"`);
|
---|
75 | return callback();
|
---|
76 | });
|
---|
77 | });
|
---|
78 | });
|
---|
79 | compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
|
---|
80 | });
|
---|
81 | }
|
---|
82 | } |
---|