UNPKG

3.58 kBJavaScriptView Raw
1'use strict';
2
3var EventEmitter = require('events').EventEmitter;
4var inherits = require('util').inherits;
5var fs = require('fs');
6var path = require('path');
7
8var handlebars = require('handlebars');
9var objectMerge = require('object-merge');
10var is = require('annois');
11var zip = require('annozip');
12
13
14var UMD = function UMD(code, options) {
15 if(!code) {
16 throw new Error('Missing code to convert!');
17 }
18
19 EventEmitter.call(this);
20 this.code = code;
21 this.options = options || {};
22
23 this.template = this.loadTemplate(this.options.template);
24};
25
26inherits(UMD, EventEmitter);
27
28UMD.prototype.loadTemplate = function loadTemplate(filepath) {
29 var tplPath,
30 exists = fs.existsSync;
31
32 if (filepath) {
33 if (exists(filepath)) {
34 tplPath = filepath;
35 }
36 else {
37 tplPath = path.join(__dirname, 'templates', filepath + '.hbs');
38
39 if (!exists(tplPath)) {
40 tplPath = path.join(__dirname, 'templates', filepath);
41
42 if (!exists(tplPath)) {
43 this.emit('error', 'Cannot find template file "' + filepath + '".');
44 return;
45 }
46 }
47 }
48 }
49 else {
50 tplPath = path.join(__dirname, 'templates', 'umd.hbs');
51 }
52
53 try {
54 return handlebars.compile(fs.readFileSync(tplPath, 'utf-8'));
55 }
56 catch (e) {
57 this.emit('error', e.message);
58 }
59};
60
61UMD.prototype.generate = function generate() {
62 var options = this.options,
63 code = this.code,
64 ctx = objectMerge({}, options);
65
66 var depsOptions = objectMerge(
67 getDependencyDefaults(this.options.globalAlias),
68 convertDependencyArrays(options.deps) || {}
69 );
70
71 var defaultDeps = depsOptions['default'].items;
72 var deps = defaultDeps ? defaultDeps || defaultDeps.items || [] : [];
73 var dependency, dependencyType, items, prefix, separator, suffix;
74
75 for (dependencyType in depsOptions) {
76 dependency = depsOptions[dependencyType];
77 items = dependency.items || defaultDeps || [];
78 prefix = dependency.prefix || '';
79 separator = dependency.separator || ', ';
80 suffix = dependency.suffix || '';
81 ctx[dependencyType + 'Dependencies'] = {
82 normal: items,
83 wrapped: items.map(wrap(prefix, suffix)).join(separator),
84 };
85 }
86
87 ctx.dependencies = deps.join(', ');
88
89 ctx.code = code;
90
91 return this.template(ctx);
92};
93
94function wrap(pre, post) {
95 pre = pre || '';
96 post = post || '';
97
98 return function (v) {
99 return pre + v + post;
100 };
101}
102
103function convertDependencyArrays(deps) {
104 if(!deps) {
105 return;
106 }
107
108 return zip.toObject(zip(deps).map(function(pair) {
109 if(is.array(pair[1])) {
110 return [pair[0], {
111 items: pair[1]
112 }];
113 }
114
115 return pair;
116 }));
117}
118
119function getDependencyDefaults(globalAlias) {
120 return {
121 'default': {
122 items: null,
123 },
124 amd: {
125 items: null,
126 prefix: '\"',
127 separator: ',',
128 suffix: '\"',
129 },
130 cjs: {
131 items: null,
132 prefix: 'require(\"',
133 separator: ',',
134 suffix: '\")',
135 },
136 global: {
137 items: null,
138 prefix: globalAlias? globalAlias + '.': '\"',
139 separator: ',',
140 suffix: '\"',
141 }
142 };
143}
144
145module.exports = function(code, options) {
146 var u = new UMD(code, options);
147
148 return u.generate();
149};