Skip to content

Commit 1794d51

Browse files
committed
extract function for creating VirtualStats
1 parent 327bb22 commit 1794d51

File tree

1 file changed

+30
-47
lines changed

1 file changed

+30
-47
lines changed

lib/virtual.js

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ function getModulePath(filePath, compiler) {
2626
return path.isAbsolute(filePath) ? filePath : path.join(compiler.context, filePath);
2727
}
2828

29+
function createVirtualStats(len) {
30+
const time = new Date();
31+
const timeMs = time.getTime();
32+
33+
return new VirtualStats({
34+
dev: 8675309,
35+
ino: inode++,
36+
mode: 16877,
37+
nlink: 0,
38+
uid: 1000,
39+
gid: 1000,
40+
rdev: 0,
41+
size: len,
42+
blksize: 4096,
43+
blocks: Math.floor(len / 4096),
44+
atimeMs: timeMs,
45+
mtimeMs: timeMs,
46+
ctimeMs: timeMs,
47+
birthtimeMs: timeMs,
48+
atime: time,
49+
mtime: time,
50+
ctime: time,
51+
birthtime: time
52+
});
53+
}
54+
2955
/**
3056
* @param {Compiler} compiler - the webpack compiler
3157
*/
@@ -64,28 +90,7 @@ function VirtualModulesPlugin(compiler) {
6490
try {
6591
compiler.inputFileSystem.readdirSync(dir);
6692
} catch (e) {
67-
const time = new Date();
68-
const timeMs = time.getTime();
69-
const dirStats = new VirtualStats({
70-
dev: 8675309,
71-
nlink: 0,
72-
uid: 1000,
73-
gid: 1000,
74-
rdev: 0,
75-
blksize: 4096,
76-
ino: inode++,
77-
mode: 16877,
78-
size: stats.size,
79-
blocks: Math.floor(stats.size / 4096),
80-
atimeMs: timeMs,
81-
mtimeMs: timeMs,
82-
ctimeMs: timeMs,
83-
birthtimeMs: timeMs,
84-
atime: time,
85-
mtime: time,
86-
ctime: time,
87-
birthtime: time
88-
});
93+
const dirStats = createVirtualStats(stats.size);
8994
setData(readDirStorage, dir, createWebpackData([]));
9095
setData(statStorage, dir, createWebpackData(dirStats));
9196
}
@@ -118,29 +123,7 @@ function VirtualModulesPlugin(compiler) {
118123

119124
VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
120125
const len = contents ? contents.length : 0;
121-
const time = new Date();
122-
const timeMs = time.getTime();
123-
124-
const stats = new VirtualStats({
125-
dev: 8675309,
126-
ino: inode++,
127-
mode: 16877,
128-
nlink: 0,
129-
uid: 1000,
130-
gid: 1000,
131-
rdev: 0,
132-
size: len,
133-
blksize: 4096,
134-
blocks: Math.floor(len / 4096),
135-
atimeMs: timeMs,
136-
mtimeMs: timeMs,
137-
ctimeMs: timeMs,
138-
birthtimeMs: timeMs,
139-
atime: time,
140-
mtime: time,
141-
ctime: time,
142-
birthtime: time
143-
});
126+
const stats = createVirtualStats(len);
144127
const modulePath = getModulePath(filePath, this.compiler);
145128

146129
// When using the WatchIgnorePlugin (https://github.com/webpack/webpack/blob/52184b897f40c75560b3630e43ca642fcac7e2cf/lib/WatchIgnorePlugin.js),
@@ -164,12 +147,12 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
164147
delete fileWatcher.directoryWatcher._cachedTimeInfoEntries;
165148
fileWatcher.directoryWatcher.setFileTime(
166149
filePath,
167-
time,
150+
stats.birthtime,
168151
false,
169152
false,
170153
null
171154
);
172-
fileWatcher.emit("change", time, null);
155+
fileWatcher.emit("change", stats.birthtime, null);
173156
}
174157
});
175158
}

0 commit comments

Comments
 (0)