This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.js
281 lines (206 loc) · 7.64 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* global -Promise */
'use strict';
// This is the main for node.js, and runs in node.js only.
var Promise = require('promise');
var pathogen = require('pathogen');
var esprima = require('esprima');
var escodegen = require('escodegen');
var esmangle = require('esmangle');
var QuickStart = require('./lib/quickstart');
var Resolver = require('./util/resolver');
var program = require('./util/program');
var sequence = require('./util/sequence').use(Promise);
var transport = require('./util/transport');
var Messages = require('./util/messages');
var injectGlobals = require('./transforms/inject-globals');
var resolver = new Resolver;
function compileSelf(options, appRuntimePath, appRuntimeData, messages) {
var root = options.root;
if (options.browserSourceMap == null) options.browserSourceMap = true;
// Instantiate QuickStart with the root specified in options
// which gives the QuickStart compiler a different root to work on
// it defaults to the current working directory.
var compiler = new QuickStart({
messages: messages,
loc: !!options.sourceMap,
root: root,
node: false,
parsers: {},
transforms: [injectGlobals]
});
// Configuration object that we will inject in the compiled compiler
// as the compiler never reads from external sources for portability, except for builtins.
// This includes the app's runtime as a string, parser and transforms (populated in a later step)
// and some options.
var config = {
main: options.main ? pathogen(options.main) : './',
runtimeData: appRuntimeData,
runtimePath: pathogen.relative(root, appRuntimePath),
sourceMap: !!options.browserSourceMap
};
if (options.defaultPath) config.defaultPath = pathogen.relative(root, options.defaultPath);
var runtimePath = pathogen(__dirname + '/runtime/browser.js');
return sequence.map(options.parsers, function(path) {
return compiler.require(root, path);
}).then(function(parsers) {
// all the resolved parser paths
config.parsers = parsers || {};
return sequence.map(options.transforms, function(path) {
return compiler.require(root, path);
});
}).then(function(transforms) {
// all the resolved transforms paths
config.transforms = transforms || [];
// Add a fake module called @config.json, in the app root, with the config object we created
var configPath = pathogen.resolve(root, './@config.json');
var configData = JSON.stringify(config);
transport.cache.get[configPath] = Promise.resolve(configData);
return compiler.parse(configPath, configData);
}).then(function() {
return transport(pathogen(__dirname + '/app.js'));
}).then(function(appData) {
return compiler.parse(pathogen.resolve(root, './@app.js'), appData);
}).then(function(module) {
return transport(runtimePath).then(function(runtimeData) {
return {
main: module.uid,
modules: compiler.modules,
runtimePath: runtimePath,
runtimeData: runtimeData
};
});
});
}
function compileApp(options, appRuntimePath, appRuntimeData, messages) {
var root = options.root;
var transforms;
var parsers;
return sequence.map(options.parsers, function(parserPath) {
return resolver.resolve(root, parserPath).then(require).catch(function(error) {
messages.group('Errors').error({
id: 'RequireError',
message: 'unable to require parser ' + parserPath,
source: './'
});
// load error
throw error;
});
}).then(function(parserModules) {
parsers = parserModules;
return sequence.map(options.transforms, function(transformPath) {
return resolver.resolve(root, transformPath).then(require).catch(function(error) {
messages.group('Errors').error({
id: 'RequireError',
message: 'unable to require transform ' + transformPath,
source: './'
});
// load error
throw error;
});
});
}).then(function(transformModules) {
transforms = transformModules;
// instantiate QuickStart with parsers, transforms and options
var compiler = new QuickStart({
messages: messages,
transforms: transforms,
parsers: parsers,
root: root,
loc: !!options.sourceMap,
node: options.node,
defaultPath: options.defaultPath
});
return compiler.require(root, options.main ? pathogen(options.main) : './').then(function(main) {
return {
main: main,
modules: compiler.modules,
runtimePath: appRuntimePath,
runtimeData: appRuntimeData
};
});
});
}
function compile(options, messages) {
if (options == null) options = {};
if (!messages) messages = new Messages;
// options.output should default to true.
if (options.output == null) options.output = true;
var root = options.root = options.root ? pathogen.resolve(options.root) : pathogen.cwd();
if (options.defaultPath) options.defaultPath = pathogen.resolve(root, options.defaultPath + '/');
var appRuntimePath;
// Get the appropriate runtime path based on options.
if (options.runtime) appRuntimePath = options.runtime; // let it resolve
else if (options.node) appRuntimePath = pathogen.resolve(__dirname, './runtime/node.js');
else appRuntimePath = pathogen.resolve(__dirname, './runtime/browser.js');
return resolver.resolve(root, appRuntimePath).then(function(resolved) {
return appRuntimePath = resolved;
}).then(transport).then(function(appRuntimeData) {
// building with --self means compiling a compiler for a browser.
if (options.self) return compileSelf(options, appRuntimePath, appRuntimeData, messages);
// otherwise we compile the app.
return compileApp(options, appRuntimePath, appRuntimeData, messages);
}).then(function(compileResult) {
var main = compileResult.main;
var modules = compileResult.modules;
var runtimePath = compileResult.runtimePath;
var runtimeData = compileResult.runtimeData;
var runtimeSource = pathogen.relative(root, runtimePath);
return Promise.resolve().then(function() {
var runtimeTree = esprima.parse(runtimeData, {
loc: !!options.sourceMap,
source: runtimeSource
});
// program the full ast
return program(main, modules, runtimeTree);
}).catch(function(error) {
messages.group('Errors').error({
id: 'ParseError',
message: 'unable to parse',
source: pathogen.relative(root, runtimePath)
});
throw error;
});
}).then(function(tree) {
// compress
if (!options.compress) return tree;
return Promise.resolve().then(function() {
tree = esmangle.optimize(tree);
tree = esmangle.mangle(tree);
return tree;
}).catch(function(error) {
messages.group('Errors').error({
id: 'CompressionError',
message: error.message
});
throw error;
});
}).then(function(tree) {
return Promise.resolve().then(function() {
var output = escodegen.generate(tree, {
format: options.compress ? {
compact: true,
parentheses: false
} : {
indent: { style: ' ' },
quotes: 'single'
},
sourceMap: !!options.sourceMap,
sourceMapWithCode: true
});
// trigger the callback with everything:
// the sourceTree (AST) JavaScript as a string, and the sourceMap.
return {
ast: tree,
source: output.code,
sourceMap: output.map && output.map.toJSON()
};
}).catch(function(error) {
messages.group('Errors').error({
id: 'GenerationError',
message: error.message
});
throw error;
});
});
}
module.exports = compile;