-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
154 lines (137 loc) · 4.36 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* eslint-disable no-underscore-dangle */
'use strict';
const path = require('path');
const VirtualStats = require('./virtual-stats');
class VirtualModulePlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
const moduleName = this.options.moduleName;
const ctime = VirtualModulePlugin.statsDate();
let modulePath = this.options.path;
let contents;
if (typeof this.options.contents === 'string') {
contents = this.options.contents;
}
if (typeof this.options.contents === 'object') {
if (typeof this.options.contents.then !== 'function') {
contents = JSON.stringify(this.options.contents);
}
}
if (typeof this.options.contents === 'function') {
contents = this.options.contents();
}
if (typeof contents === 'string') {
contents = Promise.resolve(contents);
}
function resolverPlugin(request, cb) {
// populate the file system cache with the virtual module
const fs = (this && this.fileSystem) || compiler.inputFileSystem;
const join = (this && this.join) || path.join;
// webpack 1.x compatibility
if (typeof request === 'string') {
request = cb;
cb = null;
}
if (!modulePath) {
modulePath = join(compiler.context, moduleName);
}
const resolve = (data) => {
VirtualModulePlugin.populateFilesystem({ fs, modulePath, contents: data, ctime });
};
const resolved = contents.then(resolve);
if (!cb) {
return;
}
resolved.then(() => cb());
}
const waitForResolvers = !compiler.resolvers.normal;
function addPlugin() {
const useModuleFactory = !compiler.resolvers.normal.plugin;
if (useModuleFactory) {
if (compiler.hooks) {
compiler.hooks.normalModuleFactory.tap('VirtualModulePlugin', (nmf) => {
nmf.hooks.beforeResolve.tap('VirtualModulePlugin', resolverPlugin);
});
} else {
compiler.plugin('normal-module-factory', (nmf) => {
nmf.plugin('before-resolve', resolverPlugin);
});
}
} else {
compiler.resolvers.normal.plugin('before-resolve', resolverPlugin);
}
}
if (waitForResolvers) {
compiler.plugin('after-resolvers', addPlugin);
} else {
addPlugin();
}
}
static populateFilesystem(options) {
const fs = options.fs;
const modulePath = options.modulePath;
const contents = options.contents;
const mapIsAvailable = typeof Map !== 'undefined';
const statStorageIsMap = mapIsAvailable && fs._statStorage.data instanceof Map;
const readFileStorageIsMap = mapIsAvailable && fs._readFileStorage.data instanceof Map;
if (readFileStorageIsMap) { // [email protected] or greater
if (fs._readFileStorage.data.has(modulePath)) {
return;
}
} else if (fs._readFileStorage.data[modulePath]) { // [email protected] or lower
return;
}
const stats = VirtualModulePlugin.createStats(options);
if (statStorageIsMap) { // [email protected] or greater
fs._statStorage.data.set(modulePath, [null, stats]);
} else { // [email protected] or lower
fs._statStorage.data[modulePath] = [null, stats];
}
if (readFileStorageIsMap) { // [email protected] or greater
fs._readFileStorage.data.set(modulePath, [null, contents]);
} else { // [email protected] or lower
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;