UNPKG

3.19 kBJavaScriptView Raw
1"use strict";
2
3const Plugin = require('broccoli-plugin');
4const fs = require('fs');
5const path = require('path');
6const walkSync = require('walk-sync');
7const getModuleConfig = require('./lib/get-module-config');
8
9function ResolutionMapBuilder(src, config, options) {
10 options = options || {};
11 Plugin.call(this, [src, config], {
12 annotation: options.annotation
13 });
14 this.options = options;
15}
16
17ResolutionMapBuilder.prototype = Object.create(Plugin.prototype);
18
19ResolutionMapBuilder.prototype.constructor = ResolutionMapBuilder;
20
21ResolutionMapBuilder.prototype.build = function() {
22 function specifierFromModule(modulePrefix, moduleConfig, modulePath) {
23 let path;
24 let collectionPath;
25
26 for (let i = 0, l = moduleConfig.collectionPaths.length; i < l; i++) {
27 path = moduleConfig.collectionPaths[i];
28 if (modulePath.indexOf(path) === 0) {
29 collectionPath = path;
30 break;
31 }
32 }
33
34 if (collectionPath) {
35 // trim group/collection from module path
36 modulePath = modulePath.substr(collectionPath.length);
37 } else {
38 collectionPath = 'main';
39 }
40 let parts = modulePath.split('/');
41
42 let collectionName = moduleConfig.collectionMap[collectionPath];
43
44 let name, type, namespace;
45 if (parts.length > 1) {
46 type = parts.pop();
47 }
48 name = parts.pop();
49 if (parts.length > 0) {
50 namespace = parts.join('/');
51 }
52
53 let specifierPath = [modulePrefix, collectionName];
54 if (namespace) {
55 specifierPath.push(namespace);
56 }
57 specifierPath.push(name);
58
59 let specifier = type + ':/' + specifierPath.join('/');
60
61 console.log('specifier:', specifier);
62
63 return specifier;
64 }
65
66 let configPath = path.join(this.inputPaths[1], this.options.configPath);
67 let configContents = fs.readFileSync(configPath, { encoding: 'utf8' });
68 let config = JSON.parse(configContents);
69
70 let modulePrefix = config.modulePrefix;
71 let moduleConfig = getModuleConfig(config);
72 let paths = walkSync(this.inputPaths[0]);
73 let modules = [];
74 let moduleImports = [];
75 let mapContents = [];
76
77 paths.forEach(function(entry) {
78 if (entry.indexOf('.') > -1) {
79 let module = entry.substring(0, entry.lastIndexOf('.'));
80
81 // filter out index module
82 if (module !== 'index' && module !== 'main') {
83 modules.push(module);
84 }
85 }
86 });
87 modules.forEach(function(module) {
88 let specifier = specifierFromModule(modulePrefix, moduleConfig, module);
89 let moduleImportPath = '../' + module;
90 let moduleVar = '__' + module.replace(/\//g, '__').replace(/-/g, '_') + '__';
91 let moduleImport = "import { default as " + moduleVar + " } from '" + moduleImportPath + "';";
92 moduleImports.push(moduleImport);
93 mapContents.push("'" + specifier + "': " + moduleVar);
94 });
95 let destPath = path.join(this.outputPath, 'config');
96 if (!fs.existsSync(destPath)) {
97 fs.mkdirSync(destPath);
98 }
99
100 let contents = moduleImports.join('\n') + '\n' +
101 "export default moduleMap = {" + mapContents.join(',') + "};" + '\n';
102
103 fs.writeFileSync(path.join(this.outputPath, 'config', 'module-map.js'), contents, { encoding: 'utf8' });
104};
105
106module.exports = ResolutionMapBuilder;