UNPKG

2.27 kBJavaScriptView Raw
1'use strict';
2
3function parseDepends(deps) {
4 if (!deps) return undefined;
5 // allow depends: [ '..' ] and depends: '..'
6 deps = Array.isArray(deps) ? deps : [ deps ];
7
8 return deps
9 .reduce(function (acc, d) {
10 var parts = d.split(':');
11 if (!parts
12 || parts.length > 2
13 || parts.length < 1
14 || !parts[0])
15 throw new Error('Invalid depends specification: "' + d + '". Needs to have format: "nameORpath:export"');
16
17 parts = parts.map(function (p) {
18 return typeof p === 'string' ? p.trim() : p
19 });
20
21 // if parts[1] is not defined that means that we depend on module named in parts[0]
22 // but we don't need it to be attached to the window under a certain name
23 acc[parts[0]] = parts[1] || null;
24 return acc;
25 }, {});
26}
27
28/**
29 * Parses inlined shims-config and returns a config in the same format that is used by external shims
30 *
31 * Example:
32 *
33 * Given:
34 * { jquery: '$',
35 * 'non-cjs': 'noncjs',
36 * 'non-cjs-dep': { exports: 'noncjsdep', depends: 'non-cjs:noncjs' },
37 * 'just-dep': { exports: 'justdep', depends: [ 'non-cjs:noncjs', 'jquery:$' ] }
38 * }
39 *
40 * returns:
41 * { jquery: { exports: '$', depends: undefined },
42 * 'non-cjs': { exports: 'noncjs', depends: undefined },
43 * 'non-cjs-dep': { exports: 'noncjsdep', depends: { 'non-cjs': 'noncjs' } },
44 * 'just-dep': { exports: 'justdep', depends: { 'non-cjs': 'noncjs', jquery: '$' } }
45 * }
46 *
47 * @name parseInlineShims
48 * @function
49 * @param {Object} config inlined shims config
50 * @return {Object} parsed config
51 */
52var go = module.exports = function (config) {
53 // all errors thrown are caught inside resolve-shims and passed back to browserify-shim
54 return Object.keys(config)
55 .reduce(function (acc, field) {
56 var conf = config[field];
57
58 // normalize two possible formats:
59 // "key": "export,
60 // "key": { "exports": "export" .. }
61 if (typeof conf === 'string') conf = { exports: conf };
62
63 var exps = conf.exports && conf.exports.length ? conf.exports.trim() : null;
64
65 acc[field.trim()] = {
66 exports: exps
67 , depends: parseDepends(conf.depends)
68 }
69
70 return acc;
71 }, {});
72}