forked from rmarscher/virtual-module-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (79 loc) · 2.14 KB
/
index.js
File metadata and controls
89 lines (79 loc) · 2.14 KB
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
'use strict';
const VirtualStats = require('./virtual-stats');
class VirtualModulePlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
const moduleName = this.options.moduleName;
const contents = this.options.contents;
const ctime = VirtualModulePlugin.statsDate();
let modulePath = this.options.path;
compiler.resolvers.normal.plugin('resolve', function resolverPlugin(request, cb) {
// populate the file system cache with the virtual module
const fs = this.fileSystem;
// webpack 1.x compatibility
if (typeof request === 'string') {
request = cb;
cb = null;
}
if (!modulePath) {
modulePath = this.join(compiler.context, moduleName);
}
VirtualModulePlugin.populateFilesystem({ fs, modulePath, contents, ctime });
if (cb) {
cb();
}
});
}
static populateFilesystem(options) {
const fs = options.fs;
const modulePath = options.modulePath;
const contents = options.contents;
if (fs._readFileStorage.data[modulePath]) {
return;
}
const stats = VirtualModulePlugin.createStats(options);
fs._statStorage.data[modulePath] = [null, stats];
fs._readFileStorage.data[modulePath] = [null, contents];
}
static statsDate(inputDate) {
if (!inputDate) {
inputDate = new Date();
}
return inputDate.toString();
}
static createStats(options) {
if (!options) {
options = {};
}
if (!options.ctime) {
options.ctime = VirtualModulePlugin.statsDate();
}
if (!options.mtime) {
options.mtime = VirtualModulePlugin.statsDate();
}
if (!options.size) {
options.size = 0;
}
if (!options.size && options.contents) {
options.size = options.contents.length;
}
return new VirtualStats({
dev: 8675309,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 44700000,
mode: 33188,
size: options.size,
atime: options.mtime,
mtime: options.mtime,
ctime: options.ctime,
birthtime: options.ctime,
});
}
}
module.exports = VirtualModulePlugin;