-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathindex.js
319 lines (289 loc) · 9.52 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { rollup as rollup0 } from 'rollup';
import path from 'path';
import resolve0 from '@rollup/plugin-node-resolve';
import commonjs0 from '@rollup/plugin-commonjs';
import * as babelParser from '@agoric/babel-parser';
import babelGenerate from '@babel/generator';
import babelTraverse from '@babel/traverse';
import { makeTransform } from '@agoric/transform-eventual-send';
import { SourceMapConsumer } from 'source-map';
const DEFAULT_MODULE_FORMAT = 'nestedEvaluate';
const DEFAULT_FILE_PREFIX = '/bundled-source';
const SUPPORTED_FORMATS = ['getExport', 'nestedEvaluate'];
const IMPORT_RE = new RegExp('\\b(import)(\\s*(?:\\(|/[/*]))', 'sg');
const HTML_COMMENT_START_RE = new RegExp(`${'<'}!--`, 'g');
const HTML_COMMENT_END_RE = new RegExp(`--${'>'}`, 'g');
export function tildotPlugin() {
const transformer = makeTransform(babelParser, babelGenerate);
return {
transform(code, _id) {
return {
code: transformer(code),
map: null,
};
},
};
}
export default async function bundleSource(
startFilename,
moduleFormat = DEFAULT_MODULE_FORMAT,
powers = undefined,
) {
if (!SUPPORTED_FORMATS.includes(moduleFormat)) {
throw Error(`moduleFormat ${moduleFormat} is not implemented`);
}
const {
commonjsPlugin = commonjs0,
rollup = rollup0,
resolvePlugin = resolve0,
pathResolve = path.resolve,
externals = [],
} = powers || {};
const resolvedPath = pathResolve(startFilename);
const bundle = await rollup({
input: resolvedPath,
treeshake: false,
preserveModules: moduleFormat === 'nestedEvaluate',
external: [...externals],
plugins: [
resolvePlugin({ preferBuiltins: true }),
tildotPlugin(),
commonjsPlugin(),
],
});
const { output } = await bundle.generate({
exports: 'named',
format: 'cjs',
sourcemap: true,
});
// console.log(output);
// Create a source bundle.
const sourceBundle = {};
let entrypoint;
for (const chunk of output) {
if (chunk.isAsset) {
throw Error(`unprepared for assets: ${chunk.fileName}`);
}
const { code, fileName, isEntry } = chunk;
if (isEntry) {
entrypoint = fileName;
}
// Parse the rolled-up chunk with Babel.
// We are prepared for different module systems.
const ast = (babelParser.parse || babelParser)(code);
let unmapLoc;
if (
moduleFormat === 'nestedEvaluate' &&
!fileName.startsWith('_virtual/')
) {
// We rearrange the rolled-up chunk according to its sourcemap to move
// its source lines back to the right place.
// eslint-disable-next-line no-await-in-loop
const consumer = await new SourceMapConsumer(chunk.map);
const unmapped = new WeakSet();
let lastPos = { ...ast.loc.start };
unmapLoc = loc => {
if (!loc || unmapped.has(loc)) {
return;
}
// Make sure things start at least at the right place.
loc.end = { ...loc.start };
for (const pos of ['start', 'end']) {
if (loc[pos]) {
const newPos = consumer.originalPositionFor(loc[pos]);
if (newPos.source !== null) {
lastPos = {
line: newPos.line,
column: newPos.column,
};
}
loc[pos] = lastPos;
}
}
unmapped.add(loc);
};
}
const rewriteComment = node => {
node.type = 'CommentBlock';
// Within comments...
node.value = node.value
// ...strip extraneous comment whitespace
.replace(/^\s+/gm, ' ')
// ...replace HTML comments with a defanged version to pass SES restrictions.
.replace(HTML_COMMENT_START_RE, '<!X-')
.replace(HTML_COMMENT_END_RE, '-X>')
// ...replace import expressions with a defanged version to pass SES restrictions.
.replace(IMPORT_RE, 'X$1$2');
if (unmapLoc) {
unmapLoc(node.loc);
}
// console.log(JSON.stringify(node, undefined, 2));
};
babelTraverse(ast, {
enter(p) {
const { loc, leadingComments, trailingComments } = p.node;
if (p.node.comments) {
p.node.comments = [];
}
// Rewrite all comments.
(leadingComments || []).forEach(rewriteComment);
if (p.node.type.startsWith('Comment')) {
rewriteComment(p.node);
}
// If not a comment, and we are unmapping the source maps,
// then do it for this location.
if (unmapLoc) {
unmapLoc(loc);
}
(trailingComments || []).forEach(rewriteComment);
},
});
// Now generate the sources with the new positions.
sourceBundle[fileName] = babelGenerate(ast, { retainLines: true }).code;
// console.log(`==== sourceBundle[${fileName}]\n${sourceBundle[fileName]}\n====`);
}
if (!entrypoint) {
throw Error('No entrypoint found in output bundle');
}
// 'sourceBundle' is now an object that contains multiple programs, which references
// require() and sets module.exports . This is close, but we need a single
// stringifiable function, so we must wrap it in an outer function that
// returns the entrypoint exports.
//
// build-kernel.js will prefix this with 'export default' so it becomes an
// ES6 module. The Vat controller will wrap it with parenthesis so it can
// be evaluated and invoked to get at the exports.
// const sourceMap = `//# sourceMappingURL=${output[0].map.toUrl()}\n`;
// console.log(sourceMap);
let sourceMap;
let source;
if (moduleFormat === 'getExport') {
sourceMap = `//# sourceURL=${resolvedPath}\n`;
if (Object.keys(sourceBundle).length !== 1) {
throw Error('unprepared for more than one chunk');
}
source = `\
function getExport() { 'use strict'; \
let exports = {}; \
const module = { exports }; \
\
${sourceBundle[entrypoint]}
return module.exports;
}
${sourceMap}`;
} else if (moduleFormat === 'nestedEvaluate') {
sourceMap = `//# sourceURL=${DEFAULT_FILE_PREFIX}-preamble.js\n`;
// This function's source code is inlined in the output bundle.
// It creates an evaluable string for a given module filename.
const filePrefix = DEFAULT_FILE_PREFIX;
function createEvalString(filename) {
const code = sourceBundle[filename];
if (!code) {
return undefined;
}
return `\
(function getExport(require, exports) { \
'use strict'; \
const module = { exports }; \
\
${code}
return module.exports;
})
//# sourceURL=${filePrefix}/${filename}
`;
}
// This function's source code is inlined in the output bundle.
// It figures out the exports from a given module filename.
const nsBundle = {};
const nestedEvaluate = _src => {
throw Error('need to override nestedEvaluate');
};
function computeExports(filename, exportPowers, exports) {
const { require: systemRequire, _log } = exportPowers;
// This captures the endowed require.
const match = filename.match(/^(.*)\/[^/]+$/);
const thisdir = match ? match[1] : '.';
const contextRequire = mod => {
// Do path algebra to find the actual source.
const els = mod.split('/');
let prefix;
if (els[0][0] === '@') {
// Scoped name.
prefix = els.splice(0, 2).join('/');
} else if (els[0][0] === '.') {
// Relative.
els.unshift(...thisdir.split('/'));
} else {
// Bare or absolute.
prefix = els.splice(0, 1);
}
const suffix = [];
for (const el of els) {
if (el === '.' || el === '') {
// Do nothing.
} else if (el === '..') {
// Traverse upwards.
suffix.pop();
} else {
suffix.push(el);
}
}
// log(mod, prefix, suffix);
if (prefix !== undefined) {
suffix.unshift(prefix);
}
let modPath = suffix.join('/');
if (modPath.startsWith('./')) {
modPath = modPath.slice(2);
}
// log('requiring', modPath);
if (!(modPath in nsBundle)) {
// log('evaluating', modPath);
// Break cycles, but be tolerant of modules
// that completely override their exports object.
nsBundle[modPath] = {};
nsBundle[modPath] = computeExports(
modPath,
exportPowers,
nsBundle[modPath],
);
}
// log('returning', nsBundle[modPath]);
return nsBundle[modPath];
};
const code = createEvalString(filename);
if (!code) {
// log('missing code for', filename, sourceBundle);
if (systemRequire) {
return systemRequire(filename);
}
throw Error(
`require(${JSON.stringify(
filename,
)}) failed; no toplevel require endowment`,
);
}
// log('evaluating', code);
return nestedEvaluate(code)(contextRequire, exports);
}
source = `\
function getExportWithNestedEvaluate(filePrefix) {
'use strict';
// Serialised sources.
if (filePrefix === undefined) {
filePrefix = ${JSON.stringify(DEFAULT_FILE_PREFIX)};
}
const moduleFormat = ${JSON.stringify(moduleFormat)};
const entrypoint = ${JSON.stringify(entrypoint)};
const sourceBundle = ${JSON.stringify(sourceBundle, undefined, 2)};
const nsBundle = {};
${createEvalString}
${computeExports}
// Evaluate the entrypoint recursively, seeding the exports.
return computeExports(entrypoint, { require }, {});
}
${sourceMap}`;
}
// console.log(sourceMap);
return { source, sourceMap, moduleFormat };
}