@@ -5,6 +5,8 @@ import commonjs0 from '@rollup/plugin-commonjs';
5
5
import eventualSend from '@agoric/acorn-eventual-send' ;
6
6
import * as acorn from 'acorn' ;
7
7
8
+ import { SourceMapConsumer } from 'source-map' ;
9
+
8
10
const DEFAULT_MODULE_FORMAT = 'nestedEvaluate' ;
9
11
const DEFAULT_FILE_PREFIX = '/bundled-source' ;
10
12
const SUPPORTED_FORMATS = [ 'getExport' , 'nestedEvaluate' ] ;
@@ -55,11 +57,46 @@ export default async function bundleSource(
55
57
if ( isEntry ) {
56
58
entrypoint = fileName ;
57
59
}
60
+ let unmappedCode = code ;
61
+ if (
62
+ moduleFormat === 'nestedEvaluate' &&
63
+ ! fileName . startsWith ( '_virtual/' )
64
+ ) {
65
+ unmappedCode = '' ;
66
+ // eslint-disable-next-line no-await-in-loop
67
+ const consumer = await new SourceMapConsumer ( chunk . map ) ;
68
+ const genLines = code . split ( '\n' ) ;
69
+ let lastLine = 1 ;
70
+ for ( let genLine = 0 ; genLine < genLines . length ; genLine += 1 ) {
71
+ const pos = consumer . originalPositionFor ( {
72
+ line : genLine + 1 ,
73
+ column : 0 ,
74
+ } ) ;
75
+ const { line : origLine } = pos ;
76
+
77
+ const srcLine = origLine === null ? lastLine : origLine ;
78
+ const priorChar = unmappedCode [ unmappedCode . length - 1 ] ;
79
+ if (
80
+ srcLine === lastLine &&
81
+ ! [ '\n' , ';' , '{' , undefined ] . includes ( priorChar ) &&
82
+ ! [ '}' ] . includes ( genLines [ genLine ] [ 0 ] )
83
+ ) {
84
+ unmappedCode += `;` ;
85
+ }
86
+ while ( lastLine < srcLine ) {
87
+ unmappedCode += `\n` ;
88
+ lastLine += 1 ;
89
+ }
90
+ unmappedCode += genLines [ genLine ] ;
91
+ }
92
+ }
93
+
58
94
// Rewrite apparent import expressions so that they don't fail under SES.
59
95
// We also do apparent HTML comments.
60
- const defangedCode = code
96
+ const defangedCode = unmappedCode
61
97
. replace ( IMPORT_RE , '$1notreally' )
62
98
. replace ( HTML_COMMENT_RE , '<->' ) ;
99
+ // console.log(`<<<<<< ${fileName}\n${defangedCode}\n>>>>>>>>`);
63
100
sourceBundle [ fileName ] = defangedCode ;
64
101
}
65
102
@@ -191,7 +228,7 @@ ${sourceMap}`;
191
228
) ;
192
229
}
193
230
194
- // log('evaluating', typeof nestedEvaluate, code);
231
+ // log('evaluating', code);
195
232
return nestedEvaluate ( code ) ( contextRequire ) ;
196
233
}
197
234
@@ -212,7 +249,7 @@ function getExportWithNestedEvaluate(filePrefix) {
212
249
${ computeExports }
213
250
214
251
// Evaluate the entrypoint recursively.
215
- return computeExports(entrypoint, { require, log(...args) { return console.log(...args); } });
252
+ return computeExports(entrypoint, { require });
216
253
}
217
254
${ sourceMap } `;
218
255
}
0 commit comments