UNPKG

2.59 kBJavaScriptView Raw
1'use strict';
2
3module.exports = function(modulePrefix, moduleConfig, modulePath, moduleExtension) {
4 let path;
5 let collectionPath;
6
7 // TODO allow this setting in the resolver config
8 const defaultTypesByExtension = {
9 '.hbs': 'template',
10 '.handlebars': 'template'
11 };
12
13 // console.log('path', modulePath, 'extension', moduleExtension);
14
15 for (let i = 0, l = moduleConfig.collectionPaths.length; i < l; i++) {
16 path = moduleConfig.collectionPaths[i];
17 if (modulePath.indexOf(path) === 0) {
18 collectionPath = path;
19 break;
20 }
21 }
22
23 if (collectionPath) {
24 // trim group/collection from module path
25 modulePath = modulePath.substr(collectionPath.length + 1);
26 } else {
27 collectionPath = 'main';
28 }
29
30 let name, type;
31 let rootCollectionName = moduleConfig.collectionMap[collectionPath];
32 let rootCollection = moduleConfig.collections[rootCollectionName];
33 let parts = modulePath.split('/');
34
35 let collection = rootCollection;
36 let collectionName = rootCollectionName;
37
38 // scan for private collections
39 parts.forEach(part => {
40 if (part.indexOf('-') === 0) {
41 let privateCollectionName = part.substr(1);
42
43 if (collection.privateCollections.indexOf(privateCollectionName) === -1) {
44 throw new Error(`The collection '${collectionName}' is not configured to contain a collection '${privateCollectionName}'`);
45 }
46
47 collectionName = privateCollectionName;
48 collection = moduleConfig.collections[collectionName];
49 }
50 });
51
52 if (collection.unresolvable) {
53 return null;
54 }
55
56 let part = parts[parts.length - 1];
57
58 if (collection.types.indexOf(part) > -1) {
59 type = parts.pop();
60 if (parts.length > 0) {
61 name = parts.pop();
62 } else {
63 throw new Error(`The name of module '${modulePath}' could not be identified`);
64 }
65 } else {
66 name = parts.pop();
67 if (defaultTypesByExtension[moduleExtension]) {
68 type = defaultTypesByExtension[moduleExtension];
69 } else if (collection.defaultType) {
70 type = collection.defaultType;
71 } else {
72 throw new Error(`The type of module '${modulePath}' could not be identified`);
73 }
74 }
75
76 let specifierPath = [modulePrefix, rootCollectionName];
77
78 // Append any remaining parts as a namespace
79 if (parts.length > 0) {
80 Array.prototype.push.apply(specifierPath, parts);
81 }
82
83 specifierPath.push(name);
84
85 let specifier = type + ':/' + specifierPath.join('/');
86
87 return specifier;
88};