forked from plone/volto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-relative-resolver.js
57 lines (49 loc) · 1.73 KB
/
webpack-relative-resolver.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
const path = require('path');
class RelativeResolverPlugin {
constructor(registry) {
this.registry = registry;
this.voltoPaths = Object.assign(
{ '@plone/volto/': `${registry.voltoPath}/src` },
...Object.keys(registry.packages).map((k) => ({
[k]: registry.packages[k].modulePath,
})),
); // map of [addon_name]:path
}
isAddon(request) {
const voltoPaths = Object.values(this.voltoPaths);
const issuer = request.context.issuer;
// is issuer path a Volto-compiled module?
if (voltoPaths.findIndex((p) => issuer.startsWith(p)) === -1) return false;
const resourcePath = path.join(request.path, request.request);
return voltoPaths.findIndex((p) => resourcePath.startsWith(p)) > -1;
}
getResolvePath(request) {
const resourcePath = path.join(request.path, request.request);
const addon = Object.keys(this.voltoPaths).find((name) => {
return resourcePath.startsWith(this.voltoPaths[name]);
});
const addonPath = this.voltoPaths[addon];
const localPath = request.path.slice(addonPath.length);
return path.join(addon, localPath, request.request);
}
apply(resolver) {
resolver.plugin('resolve', (request, callback) => {
if (
request.request.startsWith('.') &&
request.context &&
request.context.issuer &&
this.isAddon(request)
) {
const normalizedResourceName = this.getResolvePath(request);
const nextRequest = Object.assign({}, request, {
request: normalizedResourceName,
path: this.registry.projectRootPath,
});
resolver.doResolve('resolve', nextRequest, '', callback);
} else {
callback();
}
});
}
}
module.exports = RelativeResolverPlugin;