UNPKG

3.37 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');
8const getModuleSpecifier = require('./lib/get-module-specifier');
9
10const IGNORED_EXTENSIONS = ['', '.md', '.html'];
11
12function ResolutionMapBuilder(src, config, options) {
13 options = options || {};
14 Plugin.call(this, [src, config], {
15 annotation: options.annotation
16 });
17 this.options = options;
18}
19
20ResolutionMapBuilder.prototype = Object.create(Plugin.prototype);
21
22ResolutionMapBuilder.prototype.constructor = ResolutionMapBuilder;
23
24ResolutionMapBuilder.prototype.build = function() {
25 // Attempt to read config file
26 let configPath = path.join(this.inputPaths[1], this.options.configPath);
27 let config;
28 if (fs.existsSync(configPath)) {
29 let configContents = fs.readFileSync(configPath, { encoding: 'utf8' });
30 config = JSON.parse(configContents);
31 } else {
32 config = {};
33 }
34
35 let moduleConfig = getModuleConfig(config.moduleConfiguration || this.options.defaultModuleConfiguration);
36 if (!moduleConfig) {
37 throw new Error(`The module configuration could not be found. Please add a config file to '${configPath}' and export an object with a 'moduleConfiguration' member.`);
38 }
39
40 let modulePrefix = config.modulePrefix || this.options.defaultModulePrefix;
41 if (!modulePrefix) {
42 throw new Error(`The module prefix could not be found. Add a config file to '${configPath}' and export an object with a 'modulePrefix' member.`);
43 }
44
45 let modulePaths = walkSync(this.inputPaths[0]);
46 let mappedPaths = [];
47 let moduleImports = [];
48 let mapContents = [];
49
50 modulePaths.forEach(function(modulePath) {
51 let pathParts = path.parse(modulePath);
52
53 if (IGNORED_EXTENSIONS.indexOf(pathParts.ext) > -1) {
54 return;
55 }
56
57 let name = pathParts.dir + '/' + pathParts.name;
58
59 // filter out index module
60 if (name !== 'index' && name !== 'main') {
61 mappedPaths.push(modulePath);
62 }
63 });
64
65 if (this.options.logSpecifiers) {
66 this.specifiers = [];
67 }
68
69 mappedPaths.forEach(modulePath => {
70 let module = modulePath.substring(0, modulePath.lastIndexOf('.'));
71 let extension = modulePath.substring(modulePath.lastIndexOf('.') + 1);
72 let specifier = getModuleSpecifier(modulePrefix, moduleConfig, module, extension);
73
74 // Only process non-null specifiers returned.
75 // Specifiers may be null in the case of an unresolvable collection (e.g. utils)
76 if (specifier) {
77 let moduleImportPath = '../' + module;
78 let moduleVar = '__' + module.replace(/\//g, '__').replace(/-/g, '_') + '__';
79 let moduleImport = "import { default as " + moduleVar + " } from '" + moduleImportPath + "';";
80 moduleImports.push(moduleImport);
81 mapContents.push("'" + specifier + "': " + moduleVar);
82
83 if (this.options.logSpecifiers) {
84 this.specifiers.push(specifier);
85 }
86 }
87 });
88
89 let destPath = path.join(this.outputPath, 'config');
90 if (!fs.existsSync(destPath)) {
91 fs.mkdirSync(destPath);
92 }
93
94 let contents = moduleImports.join('\n') + '\n' +
95 "export default {" + mapContents.join(',') + "};" + '\n';
96
97 fs.writeFileSync(path.join(this.outputPath, 'config', 'module-map.js'), contents, { encoding: 'utf8' });
98};
99
100module.exports = ResolutionMapBuilder;