1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SubFolderPlugin = exports.resolveCache = void 0;
|
4 | // This plugin tries to redirect failing request that look like `module/something`
|
5 | // into `module/path-to-main/something`.
|
6 | // For example, supposing `aurelia-charts` resolves to `aurelia-charts/dist/index.js`,
|
7 | // Then if `aurelia-charts/pie` fails, we'll try `aurelia-charts/dist/pie`.
|
8 | const path = require("path");
|
9 | const subFolderTrial = Symbol();
|
10 | exports.resolveCache = {};
|
11 | class SubFolderPlugin {
|
12 | apply(resolver) {
|
13 | resolver.getHook("after-resolve")
|
14 | .tapAsync("Aurelia:SubFolder", (request, resolveContext, cb) => {
|
15 | // Only look for request not starting with a dot (module names)
|
16 | // and followed by a path (slash). Support @scoped/modules.
|
17 | let match = /^(?!\.)((?:@[^/]+\/)?[^/]+)(\/.*)$/i.exec(request.request);
|
18 | // Fix: it seems that under some error conditions `request.context` might end up being null.
|
19 | // this is bad but to help users find relevant errors on the web, we don't want to crash
|
20 | // so instead we just skip the request.
|
21 | if (!match || !request.context || request.context[subFolderTrial]) {
|
22 | cb();
|
23 | return;
|
24 | }
|
25 | let [, module, rest] = match;
|
26 | // Try resolve just the module name to locate its actual root
|
27 | let rootRequest = Object.assign({}, request, { request: module });
|
28 | // Note: if anything doesn't work while probing or trying alternate paths,
|
29 | // we just ignore the error and pretend nothing happened (i.e. call cb())
|
30 | resolver.doResolve(resolver.hooks.resolve, rootRequest, "module sub-folder: identify root", {}, (err, result) => {
|
31 | if (!result ||
|
32 | !result.relativePath ||
|
33 | !result.relativePath.startsWith('./')) {
|
34 | cb();
|
35 | return;
|
36 | }
|
37 | // It worked, let's try a relative folder from there
|
38 | let root = path.posix.dirname(result.relativePath);
|
39 | let newRequest = Object.assign({}, request, { request: root.replace(/^\./, module) + rest });
|
40 | newRequest.context[subFolderTrial] = true;
|
41 | resolver.doResolve(resolver.hooks.resolve, newRequest, "try module sub-folder: " + root, {}, (err, result) => {
|
42 | if (result)
|
43 | cb(null, result);
|
44 | else
|
45 | cb();
|
46 | });
|
47 | });
|
48 | });
|
49 | }
|
50 | }
|
51 | exports.SubFolderPlugin = SubFolderPlugin;
|