10
10
'use strict' ;
11
11
12
12
var through = require ( 'through' ) ,
13
- espower = require ( 'espower' ) ,
14
- esprima = require ( 'esprima' ) ,
15
- escodegen = require ( 'escodegen' ) ,
16
- extend = require ( 'xtend' ) ,
17
- convert = require ( 'convert-source-map' ) ,
18
- transfer = require ( 'multi-stage-sourcemap' ) . transfer ;
19
-
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 } ) ) ;
28
- }
29
-
30
- function handleUpstreamSourceMap ( jsCode , options ) {
31
- var inMap ;
32
- if ( options . sourceMap ) {
33
- inMap = options . sourceMap ;
34
- } else {
35
- var commented = convert . fromSource ( jsCode ) ;
36
- if ( commented ) {
37
- inMap = commented . toObject ( ) ;
38
- options . sourceMap = inMap ;
39
- }
40
- }
41
- return inMap ;
42
- }
43
-
44
- function instrument ( jsCode , filepath , options ) {
45
- var jsAst = esprima . parse ( jsCode , { tolerant : true , loc : true , source : filepath } ) ;
46
- var modifiedAst = espower ( jsAst , options ) ;
47
- // keep paths absolute by not using `file` and `sourceMapRoot`
48
- // paths will be resolved by mold-source-map
49
- return escodegen . generate ( modifiedAst , {
50
- sourceMap : true ,
51
- sourceMapWithCode : true
52
- } ) ;
53
- }
54
-
55
- function mergeEspowerOptions ( options , filepath ) {
56
- return extend ( espower . defaultOptions ( ) , options , {
57
- destructive : true ,
58
- path : filepath
59
- } ) ;
60
- }
61
-
62
- function transform ( jsCode , filepath , options ) {
63
- var espowerOptions = mergeEspowerOptions ( options , filepath ) ;
64
- var inMap = handleUpstreamSourceMap ( jsCode , espowerOptions ) ;
65
- var instrumented = instrument ( jsCode , filepath , espowerOptions ) ;
66
- var outMap = convert . fromJSON ( instrumented . map . toString ( ) ) ;
67
- if ( inMap ) {
68
- var mergedRawMap = mergeSourceMap ( inMap , outMap . toObject ( ) ) ;
69
- var reMap = convert . fromObject ( mergedRawMap ) ;
70
- reMap . setProperty ( 'sources' , inMap . sources ) ;
71
- reMap . setProperty ( 'sourcesContent' , inMap . sourcesContent ) ;
72
- return instrumented . code + '\n' + reMap . toComment ( ) + '\n' ;
73
- } else {
74
- // Keeping paths absolute. Paths will be resolved by mold-source-map.
75
- outMap . setProperty ( 'sources' , [ filepath ] ) ;
76
- outMap . setProperty ( 'sourcesContent' , [ jsCode ] ) ;
77
- return instrumented . code + '\n' + outMap . toComment ( ) + '\n' ;
78
- }
79
- }
13
+ espowerSource = require ( 'espower-source' ) ;
80
14
81
15
/**
82
16
* Apply espower through the browserify transform chain.
@@ -95,7 +29,7 @@ function espowerify(filepath, options) {
95
29
}
96
30
97
31
function end ( ) {
98
- stream . queue ( transform ( data , filepath , options ) ) ;
32
+ stream . queue ( espowerSource ( data , filepath , options ) ) ;
99
33
stream . queue ( null ) ;
100
34
}
101
35
0 commit comments