-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
index.js
156 lines (123 loc) · 4.7 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
149
150
151
152
153
154
155
156
'use strict';
const Filter = require('broccoli-persistent-filter');
const clone = require('clone');
const { transformString } = require('./lib/parallel-api');
const { transformIsParallelizable } = require('./lib/parallel-api');
const optionsHash = require('./lib/options-hash');
const heimdall = require('heimdalljs');
function getExtensionsRegex(extensions) {
return extensions.map(extension => {
return new RegExp('\.' + extension + '$');
});
}
function replaceExtensions(extensionsRegex, name) {
for (let i = 0, l = extensionsRegex.length; i < l; i++) {
name = name.replace(extensionsRegex[i], '');
}
return name;
}
if(!heimdall.hasMonitor('babel')) {
heimdall.registerMonitor('babel', function BabelSchema() {
this.stringsProcessed = 0;
this.isParallelizable = false;
});
}
module.exports = class Babel extends Filter {
constructor(inputTree, options = {}) {
options.persist = 'persist' in options ? options.persist : true;
options.async = true;
super(inputTree, options);
this._optionsHash = null;
this.console = options.console || console;
this.throwUnlessParallelizable = options.throwUnlessParallelizable;
this.inputTree = inputTree;
this.options = options;
this.extensions = this.options.filterExtensions || ['js'];
this.targetExtension = 'js';
this.extensionsRegex = getExtensionsRegex(this.extensions);
this.name = 'broccoli-babel-transpiler';
if (this.options.helperWhiteList) {
this.helperWhiteList = this.options.helperWhiteList;
}
let { isParallelizable, errors } = transformIsParallelizable(options.babel);
heimdall.statsFor('babel').isParallelizable = isParallelizable;
if ((this.throwUnlessParallelizable || process.env.THROW_UNLESS_PARALLELIZABLE) && isParallelizable === false) {
throw new Error(this.toString() +
' was configured to `throwUnlessParallelizable` and was unable to parallelize a plugin. \nplugins:\n' + joinCount(errors) + '\nPlease see: https://github.com/babel/broccoli-babel-transpiler#parallel-transpilation for more details');
}
}
baseDir() {
return __dirname;
}
transform(string, options) {
return transformString(string, options);
}
/*
* @private
*
* @method optionsString
* @returns a stringified version of the input options
*/
optionsHash() {
if (this._optionsHash == null) {
this._optionsHash = optionsHash(this.options, this.console);
}
return this._optionsHash;
}
cacheKeyProcessString(string, relativePath) {
return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
}
processString(string, relativePath) {
heimdall.statsFor('babel').stringsProcessed++;
let options = this.copyOptions();
options.babel.filename = options.babel.sourceFileName = relativePath;
if (options.babel.moduleId === true) {
options.babel.moduleId = replaceExtensions(this.extensionsRegex, options.babel.filename);
}
let optionsObj = { babel: options.babel, cacheKey: this._optionsHash};
return this.transform(string, optionsObj)
.then(transpiled => {
if (this.helperWhiteList) {
let invalidHelpers = transpiled.metadata.usedHelpers.filter(helper => {
return this.helperWhiteList.indexOf(helper) === -1;
});
validateHelpers(invalidHelpers, relativePath);
}
return transpiled.code;
});
}
copyOptions() {
let cloned = clone(this.options);
if (cloned.filterExtensions) {
delete cloned.filterExtensions;
}
if (cloned.targetExtension) {
delete cloned.targetExtension;
}
return cloned;
};
}
function joinCount(list) {
let summary = '';
for (let i = 0; i < list.length; i++) {
summary += `${i + 1}: ${list[i]}\n`
}
return summary;
}
function validateHelpers(invalidHelpers, relativePath) {
if (invalidHelpers.length > 0) {
let message = relativePath + ' was transformed and relies on `' + invalidHelpers[0] + '`, which was not included in the helper whitelist. Either add this helper to the whitelist or refactor to not be dependent on this runtime helper.';
if (invalidHelpers.length > 1) {
let helpers = invalidHelpers.map((item, i) => {
if (i === invalidHelpers.length - 1) {
return '& `' + item;
} else if (i === invalidHelpers.length - 2) {
return item + '`, ';
}
return item + '`, `';
}).join('');
message = relativePath + ' was transformed and relies on `' + helpers + '`, which were not included in the helper whitelist. Either add these helpers to the whitelist or refactor to not be dependent on these runtime helpers.';
}
throw new Error(message);
}
}