-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
75 lines (63 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var BroccoliFilter = require('broccoli-filter')
var Compiler = require('es6-module-transpiler').Compiler;
var extend = require('extend');
module.exports = Filter;
Filter.prototype = Object.create(BroccoliFilter.prototype);
Filter.prototype.constructor = Filter;
function Filter(inputTree, options) {
if (!(this instanceof Filter)) {
return new Filter(inputTree, options);
}
this.inputTree = inputTree;
this.setOptions(options);
}
Filter.prototype.defaults = {
anonymous: true,
moduleType: 'amd',
packageName: null,
main: null
};
Filter.prototype.extensions = ['js']
Filter.prototype.targetExtension = 'js'
Filter.prototype.setOptions = function(options) {
var merged = extend({}, this.defaults, options);
this.options = rip(merged, ['moduleType', 'packageName', 'main']);
this.compilerOptions = merged;
this.validateOptions();
}
Filter.prototype.validateOptions = function() {
if (
this.options.moduleType == 'amd' &&
this.compilerOptions.anonymous === false &&
!this.options.packageName
) {
throw new Error('You must specify a `packageName` option when using the `anonymous: false` option');
}
}
var methods = {
'cjs': 'toCJS',
'amd': 'toAMD'
};
Filter.prototype.getName = function (filePath) {
if (this.compilerOptions.anonymous) {
return null;
}
var name = filePath.replace(/.js$/, '');
var main = this.options.main;
var packageName = this.options.packageName;
return name === main ? packageName : packageName+'/'+name;
};
Filter.prototype.processString = function (fileContents, filePath) {
var name = this.getName(filePath);
var compiler = new Compiler(fileContents, name, this.compilerOptions);
return compiler[methods[this.options.moduleType]]();
};
function rip(obj, props) {
return props.reduce(function(ripped, prop) {
if (obj[prop]) {
ripped[prop] = obj[prop];
delete obj[prop];
}
return ripped;
}, {});
}