1 | "use strict";
|
2 |
|
3 | const Plugin = require('broccoli-plugin');
|
4 | const fs = require('fs');
|
5 | const path = require('path');
|
6 | const walkSync = require('walk-sync');
|
7 | const getModuleConfig = require('./lib/get-module-config');
|
8 |
|
9 | function ResolutionMapBuilder(src, config, options) {
|
10 | options = options || {};
|
11 | Plugin.call(this, [src, config], {
|
12 | annotation: options.annotation
|
13 | });
|
14 | this.options = options;
|
15 | }
|
16 |
|
17 | ResolutionMapBuilder.prototype = Object.create(Plugin.prototype);
|
18 |
|
19 | ResolutionMapBuilder.prototype.constructor = ResolutionMapBuilder;
|
20 |
|
21 | ResolutionMapBuilder.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 |
|
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 |
|
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 |
|
106 | module.exports = ResolutionMapBuilder;
|