This repository has been archived by the owner on Sep 17, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
148 lines (122 loc) · 3.48 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Module dependencies.
*/
var babel = require('babel-core');
var debug = require('debug')('duo-babel');
var extend = require('extend');
var path = require('path');
/**
* Helper methods.
*/
var canCompile = babel.util.canCompile;
/**
* Expose `plugin`.
*/
module.exports = plugin;
/**
* Babel plugin for Duo.
*
* Available options:
* - onlyLocals {Boolean}
* - only {Array:String} List of modules to allow
* - ignore {Array:String} List of modules to exclude
*
* All other options will be proxied to babel directly.
*
* @param {Object} o
* @return {Function}
*/
function plugin(o) {
if (!o) o = {};
debug('initialized with options', o);
var extensions = extract(o, 'extensions');
if (extensions) debug('extensions to compile', extensions);
var onlyLocals = extract(o, 'onlyLocals');
if (onlyLocals) debug('only compiling locals');
var only = extract(o, 'only');
if (only) debug('only compiling files matching', only);
var ignore = extract(o, 'ignore');
if (ignore) debug('not compiling files matching', ignore);
return function* babel(file, entry) {
// compile only what babel recognizes
if (!canCompile(file.path, extensions)) return debug('ignoring file: %s', file.path);
// ignore remotes if configured to
if (onlyLocals && file.remote()) return debug('ignoring remote: %s', file.id);
var duo = file.duo;
var es5 = yield run(duo, file, o, only, ignore);
file.src = es5.code;
file.type = 'js';
};
}
/**
* Run the compilation, but utilizes the cache if available.
*
* @param {Duo} duo Duo instance
* @param {File} file File to be compiled
* @param {Object} options User-defined config
* @param {Array} only User-defined whilelist
* @param {Array} ignore User-defined blacklist
* @returns {Object} Results of babel compile
*/
function* run(duo, file, options, only, ignore) {
var cache = yield duo.getCache();
if (!cache) {
debug('cache not enabled for %s', file.id);
return compile(duo, file, options, only, ignore);
}
var key = [ duo.hash(file.src), duo.hash(options) ];
var cached = yield cache.plugin('babel', key);
if (cached) {
debug('retrieved %s from cache', file.id);
return cached;
}
var results = compile(duo, file, options, only, ignore);
yield cache.plugin('babel', key, results);
debug('saved %s to cache', file.id);
return results;
}
/**
* Compiles the file given options from user.
*/
function compile(duo, file, options, only, ignore) {
var root = duo.root();
var sourceMap = duo.sourceMap();
var o = extend(true, {
ast: false,
filename: file.path,
filenameRelative: file.id,
sourceMap: sourceMap ? 'inline' : false,
sourceRoot: '/',
only: prepend(only, root),
ignore: prepend(ignore, root)
}, options);
debug('attempting to compile: %s', file.id, o);
var es5 = babel.transform(file.src, o);
if (file.src === es5.code) debug('did not compile: %s', file.id);
return es5;
}
/**
* Prepend a value to each item in the given array.
*
* @param {Array} list
* @param {String} prefix
* @returns {Boolean|Array}
*/
function prepend(list, prefix) {
if (!list) return null;
return list.map(function (item) {
return path.resolve(prefix, item);
});
}
/**
* Extracts a value from the input object, deleting the property afterwards.
*
* @param {Object} object
* @param {String} key
* @returns {Mixed}
*/
function extract(object, key) {
var value = object[key];
delete object[key];
return value;
}