@@ -6,7 +6,6 @@ import * as espree from "espree";
6
6
import fs from "fs" ;
7
7
import path from "path" ;
8
8
import { LoaderContext } from "webpack" ;
9
- import { SourceMapGenerator , StartOfSourceMap } from "source-map" ;
10
9
11
10
import { AddonOptionsWebpack } from "../types" ;
12
11
@@ -26,39 +25,17 @@ type RawSourceMap = {
26
25
} ;
27
26
28
27
function sanitizeSourceMap ( rawSourceMap : RawSourceMap | string ) : RawSourceMap {
29
- if ( typeof rawSourceMap === 'string' ) return JSON . parse ( rawSourceMap ) ;
30
- const { sourcesContent, ...sourceMap } = rawSourceMap ?? { } ;
31
-
32
- // JSON parse/stringify trick required for istanbul to accept the SourceMap
33
- return JSON . parse ( JSON . stringify ( sourceMap ) ) ;
34
- }
35
-
36
- function createIdentitySourceMap (
37
- file : string ,
38
- source : string ,
39
- option : StartOfSourceMap
40
- ) {
41
- const gen = new SourceMapGenerator ( option ) ;
42
- const tokens = espree . tokenize ( source , { loc : true , ecmaVersion : "latest" } ) ;
43
-
44
- tokens . forEach ( ( token : any ) => {
45
- const loc = token . loc . start ;
46
- gen . addMapping ( {
47
- source : file ,
48
- original : loc ,
49
- generated : loc ,
50
- } ) ;
51
- } ) ;
52
-
53
- return JSON . parse ( gen . toString ( ) ) ;
28
+ return rawSourceMap === "string" ? JSON . parse ( rawSourceMap ) : rawSourceMap ;
54
29
}
55
30
56
31
export default function (
57
32
this : LoaderContext < Options > ,
58
33
source : string ,
59
34
sourceMap ?: RawSourceMap
60
35
) {
61
- let map = sourceMap ?? getInlineSourceMap . call ( this , source ) ;
36
+ let map = sourceMap
37
+ ? sanitizeSourceMap ( sourceMap )
38
+ : getInlineSourceMap . call ( this , source ) ;
62
39
const options = this . getOptions ( ) ;
63
40
const callback = this . async ( ) ;
64
41
@@ -70,26 +47,7 @@ export default function (
70
47
// Instrument the code
71
48
const instrumenter = options . instrumenter ;
72
49
73
- const combinedSourceMap = sanitizeSourceMap ( sourceMap ) ;
74
-
75
- const code = instrumenter . instrumentSync (
76
- source ,
77
- this . resourcePath ,
78
- combinedSourceMap as any
79
- ) ;
80
-
81
- const identitySourceMap = sanitizeSourceMap (
82
- createIdentitySourceMap ( this . resourcePath , source , {
83
- file : combinedSourceMap . file ,
84
- sourceRoot : combinedSourceMap . sourceRoot ,
85
- } )
86
- ) ;
87
-
88
- instrumenter . instrumentSync (
89
- source ,
90
- this . resourcePath ,
91
- identitySourceMap as any
92
- ) ;
50
+ const code = instrumenter . instrumentSync ( source , this . resourcePath , map ) ;
93
51
94
52
const lastSourceMap = instrumenter . lastSourceMap ( ) ;
95
53
0 commit comments