13
13
var through = require ( 'through' ) ;
14
14
var esprima = require ( 'esprima' ) ;
15
15
var escodegen = require ( 'escodegen' ) ;
16
+ var convert = require ( 'convert-source-map' ) ;
17
+ var transfer = require ( 'multi-stage-sourcemap' ) . transfer ;
16
18
var unassert = require ( 'unassert' ) ;
17
19
18
- function isDebugMode ( options ) {
19
- return ( options && options . _flags && options . _flags . debug ) ;
20
+ function mergeSourceMap ( incomingSourceMap , outgoingSourceMap ) {
21
+ if ( typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String ) {
22
+ outgoingSourceMap = JSON . parse ( outgoingSourceMap ) ;
23
+ }
24
+ if ( ! incomingSourceMap ) {
25
+ return outgoingSourceMap ;
26
+ }
27
+ return JSON . parse ( transfer ( { fromSourceMap : outgoingSourceMap , toSourceMap : incomingSourceMap } ) ) ;
20
28
}
21
29
22
- function applyUnassert ( code , options ) {
30
+ function handleIncomingSourceMap ( originalCode ) {
31
+ var commented = convert . fromSource ( originalCode ) ;
32
+ if ( commented ) {
33
+ return commented . toObject ( ) ;
34
+ }
35
+ return null ;
36
+ }
37
+
38
+ function applyUnassertWithSourceMap ( code , filepath , options ) {
39
+ var ast = esprima . parse ( code , { sourceType : 'module' } ) ;
40
+ var inMap = handleIncomingSourceMap ( code ) ;
41
+ var instrumented = escodegen . generate ( unassert ( ast ) , {
42
+ sourceMap : filepath ,
43
+ sourceContent : code ,
44
+ sourceMapWithCode : true
45
+ } ) ;
46
+ var outMap = convert . fromJSON ( instrumented . map . toString ( ) ) ;
47
+ if ( inMap ) {
48
+ var mergedRawMap = mergeSourceMap ( inMap , outMap . toObject ( ) ) ;
49
+ var reMap = convert . fromObject ( mergedRawMap ) ;
50
+ if ( inMap . sources ) {
51
+ reMap . setProperty ( 'sources' , inMap . sources ) ;
52
+ }
53
+ if ( inMap . sourceRoot ) {
54
+ reMap . setProperty ( 'sourceRoot' , inMap . sourceRoot ) ;
55
+ }
56
+ if ( inMap . sourcesContent ) {
57
+ reMap . setProperty ( 'sourcesContent' , inMap . sourcesContent ) ;
58
+ }
59
+ return instrumented . code + '\n' + reMap . toComment ( ) + '\n' ;
60
+ } else {
61
+ return instrumented . code + '\n' + outMap . toComment ( ) + '\n' ;
62
+ }
63
+ }
64
+
65
+ function applyUnassertWithoutSourceMap ( code , filepath , options ) {
23
66
var ast = esprima . parse ( code , { sourceType : 'module' } ) ;
24
67
return escodegen . generate ( unassert ( ast ) ) ;
25
68
}
26
69
27
- module . exports = function unassertify ( filepath , options ) {
28
- if ( isDebugMode ( options ) ) {
29
- return through ( ) ;
30
- }
70
+ function isDebugMode ( options ) {
71
+ return ( options && options . _flags && options . _flags . debug ) ;
72
+ }
31
73
74
+ module . exports = function unassertify ( filepath , options ) {
32
75
var data = '' ,
33
76
stream = through ( write , end ) ;
34
77
@@ -37,7 +80,11 @@ module.exports = function unassertify (filepath, options) {
37
80
}
38
81
39
82
function end ( ) {
40
- stream . queue ( applyUnassert ( data , options ) ) ;
83
+ if ( isDebugMode ( options ) ) {
84
+ stream . queue ( applyUnassertWithSourceMap ( data , filepath , options ) ) ;
85
+ } else {
86
+ stream . queue ( applyUnassertWithoutSourceMap ( data , filepath , options ) ) ;
87
+ }
41
88
stream . queue ( null ) ;
42
89
}
43
90
0 commit comments