-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (92 loc) · 3.17 KB
/
Copy pathindex.js
File metadata and controls
102 lines (92 loc) · 3.17 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
90
91
92
93
94
95
96
97
98
99
100
101
102
let AdaptiveFS = require('arc-fs');
let proxyLoaderPath = require.resolve('./proxy-loader');
class AdaptivePlugin {
constructor({ flags, proxy } = {}) {
if(!flags && !proxy) {
throw new Error('The AdaptivePlugin should be passed flags or proxy should be true.');
}
this.flags = flags;
this.proxy = proxy || false;
}
apply(compiler) {
let fs = compiler.inputFileSystem;
let afs = new AdaptiveFS({ fs, flags: this.flags });
compiler.inputFileSystem = new Proxy(afs, {
get(afs, property) {
if (afs[property]) {
return afs[property];
}
const value = fs[property];
if (typeof value === 'function') {
return value.bind(fs);
} else {
return value;
}
},
set(afs, property, value) {
fs[property] = value;
}
});
if (this.proxy) {
compiler.hooks.normalModuleFactory.tap('arc', normalModuleFactory => {
normalModuleFactory.hooks.afterResolve.tap('arc', data => {
let resource =
(data.createData && data.createData.resource) || data.resource;
if (!resource) {
return;
}
const contextInfo =
data.contextInfo || data.resourceResolveData.context;
const query = (/\?.*$/.exec(resource) || '')[0] || '';
resource = resource.slice(0, query ? -query.length : undefined);
if (contextInfo.issuer !== resource) {
let isAdaptive;
try {
isAdaptive = afs.isAdaptiveSync(resource);
} catch (e) {
// An error may be thrown if the resource cannot be found.
// However this hook would not have been called if the resource
// could not have been resolved. We'll assume some other plugin
// is making this resource available to webpack and that it's not
// adaptive.
isAdaptive = false;
}
if (isAdaptive) {
const matches = afs.getMatchesSync(resource);
(data.createData || data).loaders = [
{
options: {
matches,
query
},
loader: proxyLoaderPath
}
];
data.request = resource + '?arc-proxy';
if (data.createData) {
data.createData.request = resource + '?arc-proxy';
}
}
}
});
});
} else {
compiler.resolverFactory.hooks.resolver
.for('normal')
.tap('arc', resolver => {
resolver.hooks.result.tap('arc', req => {
try {
req.path = afs.resolveSync(req.path);
} catch (e) {
// An error may be thrown if the resource cannot be found.
// However this hook would not have been called if the resource
// could not have been resolved. We'll assume some other plugin
// is making this resource available to webpack and that it's not
// adaptive.
}
});
});
}
}
}
module.exports = AdaptivePlugin;