-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
85 lines (65 loc) · 2.07 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
var path = require('path'),
sourceMapSupport = require('source-map-support'),
babelRegisterCache = require('@babel/register/lib/cache');
module.exports = function(sails) {
return {
/**
* Default configuration
*
* We do this in a function since the configuration key for
* the hook is itself configurable, so we can't just return
* an object.
*/
defaults: {
__configKey__: {
// Turn babel compile on by default
compile: true,
// Doesn't import polyfill by default
polyfill: false,
// Activates preset tranformations
presets: ['@babel/env']
}
},
/**
* Initialize the hook
* @param {Function} cb Callback for when we're done initializing
*/
configure: function() {
var config = sails.config[this.configKey];
// If the hook has been deactivated, just return
if (!config.compile) {
sails.log.verbose('Babel hook deactivated.');
} else {
if (sails.config.environment != 'production') {
sourceMapSupport.install({
retrieveSourceMap: function(file) {
var cache = babelRegisterCache.get(),
sourceMap = null;
Object.keys(cache).some(function(hash) {
var fileCache = cache[hash];
if (typeof fileCache == 'undefined' || typeof fileCache.options == 'undefined'
|| fileCache.options.filename != file
) {
return false;
}
sourceMap = {
url: file,
map: fileCache.map
};
return true;
});
return sourceMap;
}
});
}
if (config.polyfill) {
require('@babel/polyfill');
}
delete config.polyfill;
delete config.compile;
require('@babel/register')(config);
sails.log.verbose('Babel hook activated. Enjoy ES6/7 power in your Sails app.');
}
},
};
};