-
-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathfs.js
132 lines (105 loc) · 3.59 KB
/
fs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
const fs = require('fs');
const path = require('path');
const MemoryFileSystem = require('memory-fs');
const mkdirp = require('mkdirp');
const { colors } = require('webpack-log');
const DevMiddlewareError = require('./DevMiddlewareError');
module.exports = {
toDisk(context) {
const compilers = context.compiler.compilers || [context.compiler];
for (const compiler of compilers) {
compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => {
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
return;
}
compiler.hooks.assetEmitted.tapAsync(
'WebpackDevMiddleware',
(file, content, callback) => {
let targetFile = file;
const queryStringIdx = targetFile.indexOf('?');
if (queryStringIdx >= 0) {
targetFile = targetFile.substr(0, queryStringIdx);
}
let { outputPath } = compiler;
// TODO Why? Need remove in future major release
if (outputPath === '/') {
outputPath = compiler.context;
}
outputPath = compilation.getPath(outputPath, {});
const targetPath = path.join(outputPath, targetFile);
const { writeToDisk: filter } = context.options;
const allowWrite =
filter && typeof filter === 'function'
? filter(targetPath)
: true;
if (!allowWrite) {
return callback();
}
const { log } = context;
const dir = path.dirname(targetPath);
return mkdirp(dir, (mkdirpError) => {
if (mkdirpError) {
return callback(mkdirpError);
}
return fs.writeFile(targetPath, content, (writeFileError) => {
if (writeFileError) {
return callback(writeFileError);
}
log.debug(
colors.cyan(
`Asset written to disk: ${path.relative(
process.cwd(),
targetPath
)}`
)
);
return callback();
});
});
}
);
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
});
}
},
setFs(context, compiler) {
if (
typeof compiler.outputPath === 'string' &&
!path.posix.isAbsolute(compiler.outputPath) &&
!path.win32.isAbsolute(compiler.outputPath)
) {
throw new DevMiddlewareError(
'`output.path` needs to be an absolute path or `/`.'
);
}
let fileSystem;
// store our files in memory
const isConfiguredFs = context.options.fs;
const isMemoryFs =
!isConfiguredFs &&
!compiler.compilers &&
compiler.outputFileSystem instanceof MemoryFileSystem;
if (isConfiguredFs) {
// eslint-disable-next-line no-shadow
const { fs } = context.options;
if (typeof fs.join !== 'function') {
// very shallow check
throw new Error(
'Invalid options: options.fs.join() method is expected'
);
}
// eslint-disable-next-line no-param-reassign
compiler.outputFileSystem = fs;
fileSystem = fs;
} else if (isMemoryFs) {
fileSystem = compiler.outputFileSystem;
} else {
fileSystem = new MemoryFileSystem();
// eslint-disable-next-line no-param-reassign
compiler.outputFileSystem = fileSystem;
}
// eslint-disable-next-line no-param-reassign
context.fs = fileSystem;
},
};