1 | var jsVars = require('interpret').jsVariants,
|
2 | endsWith = require('lodash.endswith'),
|
3 | availableExts = Object.keys(jsVars),
|
4 | chalk = require('chalk');
|
5 |
|
6 |
|
7 |
|
8 | availableExts.sort(function(a, b) {
|
9 | var res = -(a.split(/\./).length - b.split(/\./).length);
|
10 |
|
11 |
|
12 | if(res === 0) {
|
13 | if(a === '.js') {
|
14 | return -1;
|
15 | }
|
16 | if(b === '.js') {
|
17 | return 1;
|
18 | }
|
19 | return 0;
|
20 | }
|
21 | return res;
|
22 | });
|
23 |
|
24 | function getMatchingLoaderFn(configPath, extensions, variants) {
|
25 | let availableExtensions = extensions || availableExts;
|
26 | let jsVariants = variants || jsVars;
|
27 | for(var i = 0, len = availableExtensions.length; i < len; i++) {
|
28 | var ext = availableExtensions[i];
|
29 | if(endsWith(configPath, ext)) {
|
30 | return jsVariants[ext];
|
31 | }
|
32 | }
|
33 | return null;
|
34 | }
|
35 |
|
36 | function callConfigFunction(fn) {
|
37 | return fn(require('minimist')(process.argv, { '--': true }).env || {});
|
38 | }
|
39 |
|
40 | function getConfig(configPath) {
|
41 | var configModule = require(configPath);
|
42 | var configDefault = configModule && configModule.__esModule ? configModule.default : configModule;
|
43 | return typeof configDefault === 'function' ? callConfigFunction(configDefault) : configDefault;
|
44 | }
|
45 |
|
46 | module.exports = {
|
47 | default: function(configPath, matchingLoader) {
|
48 | let getMatchingLoader = matchingLoader || getMatchingLoaderFn;
|
49 |
|
50 | var mod = getMatchingLoader(configPath);
|
51 | if(mod) {
|
52 | var mods = Array.isArray(mod) ? mod : [mod],
|
53 | installed = false;
|
54 |
|
55 | for(var i = 0, len = mods.length; i < len; i++) {
|
56 | mod = mods[i];
|
57 | if(typeof mod === 'string') {
|
58 | try {
|
59 | require(mod);
|
60 | installed = true;
|
61 | } catch(ignored) {}
|
62 | } else if(typeof mod === 'object') {
|
63 | try {
|
64 | var s = require(mod.module);
|
65 | mod.register(s);
|
66 | installed = true;
|
67 | } catch(ignored) {}
|
68 | }
|
69 |
|
70 | if(installed) {
|
71 | break;
|
72 | }
|
73 | }
|
74 |
|
75 | if(!installed) {
|
76 | throw new Error('Could not load required module loading for ' + chalk.underline(configPath));
|
77 | }
|
78 | }
|
79 | return getConfig(configPath);
|
80 | },
|
81 | getMatchingLoader: getMatchingLoaderFn,
|
82 | availableExtensions: availableExts,
|
83 | };
|