/
index.js
175 lines (149 loc) · 4.88 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
console.error(
'\x1b[31m' +
'| DEPRECATED: esnext has been deprecated in favor of 6to5.\n' +
'| \n' +
'| See the esnext README for more information on transitioning,\n' +
'| or check out https://6to5.org/ for information on 6to5.' +
'\x1b[0m'
);
var fs = require('fs');
var through = require('through');
var es6arrowfn = require('es6-arrow-function');
var es6class = require('es6-class');
var es6computedProperties = require('es6-computed-properties');
var es6defaultParams = require('es6-default-params');
var es6destructuring = require('es6-destructuring');
var es6objectConcise = require('es6-object-concise');
var es6objectShort = require('es6-object-short');
var es6restParams = require('es6-rest-params');
var es6spread = require('es6-spread');
var es6templates = require('es6-templates');
var regenerator = require('regenerator');
var regexpu = require('regexpu');
var recast = require('recast');
/**
* Compile the given next-generation JavaScript into JavaScript usable in
* today's runtime environments. Pass options to control which features to
* include. See {#transform} for the available options.
*
* @param {string} source
* @param {object} options
* @return {{code:string, map:string}}
*/
function compile(source, options) {
if (!options) { options = {}; }
source = source.replace(/^#![^\n]+/, '');
var ast = recast.parse(source, {
sourceFileName: options.sourceFileName
});
ast = transform(ast, options);
return recast.print(ast, {
sourceMapName: options.sourceMapName
});
}
/**
* Transform the given next-generation JavaScript AST into a JavaScript AST
* usable in today's runtime environments. Pass options to control which
* features to include. By default, all options are enabled.
*
* Options
*
* arrowFunction - Compile ES6 arrow functions into normal functions.
* class - Compile ES6 classes into ES5 constructors.
* computedPropertyKeys - Compile ES6 computed property keys.
* defaultParams - Compile ES6 default parameters to ES5.
* destructuring - Compile ES6 destructuring assignment.
* generator - Compile generator functions into ES5.
* objectConcise - Compile object literal concise method definitions.
* objectShorthand - Compile ES6 object literal shorthand assignment.
* rest - Compile rest params into ES5.
* spread - Compile spread operator.
* templates - Compile template strings into ES5.
*
* @param {object} ast
* @param {object} options
* @return {object}
*/
function transform(ast, options) {
if (!options) { options = {}; }
if (options.computedPropertyKeys !== false) {
ast = es6computedProperties.transform(ast);
}
if (options.arrowFunction !== false) {
ast = es6arrowfn.transform(ast);
}
if (options['class'] !== false) {
ast = es6class.transform(ast);
}
if (options.rest !== false) {
ast = es6restParams.transform(ast);
}
if (options.defaultParams !== false) {
ast = es6defaultParams.transform(ast);
}
if (options.generator !== false) {
ast = regenerator.transform(ast);
}
if (options.regexpu !== false) {
ast = regexpu.transform(ast);
}
if (options.spread !== false) {
ast = es6spread.transform(ast);
}
if (options.templates !== false) {
ast = es6templates.transform(ast);
}
if (options.generator !== false && options.includeRuntime) {
var runtime = fs.readFileSync(regenerator.runtime.path, 'utf8');
injectRuntime(runtime, regenerator.runtime.path, ast.program);
}
if (options.destructuring !== false) {
ast = es6destructuring.transform(ast);
}
if (options.objectShorthand !== false) {
ast = es6objectShort.transform(ast);
}
if (options.objectConcise !== false) {
ast = es6objectConcise.transform(ast);
}
return ast;
}
/**
* Originally from http://facebook.github.io/regenerator/.
*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* https://raw.github.com/facebook/regenerator/master/LICENSE file. An
* additional grant of patent rights can be found in the PATENTS file in
* the same directory.
*/
function injectRuntime(runtime, filename, ast) {
recast.types.builtInTypes.string.assert(runtime);
recast.types.namedTypes.Program.assert(ast);
// Include the runtime by modifying the AST rather than by concatenating
// strings. This technique will allow for more accurate source mapping.
if (runtime !== "") {
var runtimeBody = recast.parse(runtime, {
sourceFileName: filename
}).program.body;
var body = ast.body;
body.unshift.apply(body, runtimeBody);
}
return ast;
}
/**
* End Facebook Copyright.
*/
module.exports = function() {
var data = '';
return through(write, end);
function write(buf) { data += buf; }
function end() {
this.queue(module.exports.compile(data).code);
this.queue(null);
}
};
module.exports.compile = compile;
module.exports.transform = transform;