@@ -35,32 +35,34 @@ class WebpackObfuscator {
35
35
36
36
compiler . hooks . emit . tap ( pluginName , ( compilation : compilation . Compilation ) => {
37
37
let identifiersPrefixCounter : number = 0 ;
38
-
38
+ let sourcemap_output : { [ index : string ] : string } = { } ;
39
39
compilation . chunks . forEach ( chunk => {
40
40
chunk . files . forEach ( ( fileName : string ) => {
41
+ if ( this . options . sourceMap && fileName . toLowerCase ( ) . endsWith ( '.map' ) ) {
42
+ let src_name = fileName . toLowerCase ( ) . substr ( 0 , fileName . length - 4 ) ;
43
+ if ( ! this . shouldExclude ( src_name ) ) {
44
+ const transferredSourceMap = transferSourceMap ( {
45
+ fromSourceMap : sourcemap_output [ src_name ] ,
46
+ toSourceMap : compilation . assets [ fileName ] . source ( )
47
+ } ) ;
48
+ let final_sourcemap = JSON . parse ( transferredSourceMap ) ;
49
+ final_sourcemap [ 'sourcesContent' ] = JSON . parse ( compilation . assets [ fileName ] . source ( ) ) [ 'sourcesContent' ] ;
50
+ compilation . assets [ fileName ] = new RawSource ( JSON . stringify ( final_sourcemap ) ) ;
51
+ }
52
+ return ;
53
+ }
41
54
if ( ! fileName . toLowerCase ( ) . endsWith ( '.js' ) || this . shouldExclude ( fileName ) ) {
42
55
return ;
43
56
}
44
57
const asset = compilation . assets [ fileName ]
45
58
const { inputSource, inputSourceMap } = this . extractSourceAndSourceMap ( asset ) ;
46
- const { obfuscatedSource, obfuscationSourceMap } = this . obfuscate ( inputSource , identifiersPrefixCounter ) ;
59
+ const { obfuscatedSource, obfuscationSourceMap } = this . obfuscate ( inputSource , fileName , identifiersPrefixCounter ) ;
47
60
48
61
if ( this . options . sourceMap && inputSourceMap ) {
49
- const transferredSourceMap = transferSourceMap ( {
50
- fromSourceMap : obfuscationSourceMap ,
51
- toSourceMap : inputSourceMap
52
- } ) ;
53
-
54
- compilation . assets [ fileName ] = new SourceMapSource (
55
- obfuscatedSource ,
56
- fileName ,
57
- transferredSourceMap ,
58
- inputSource ,
59
- inputSourceMap
60
- ) ;
61
- } else {
62
- compilation . assets [ fileName ] = new RawSource ( obfuscatedSource ) ;
62
+ sourcemap_output [ fileName ] = obfuscationSourceMap ;
63
63
}
64
+ compilation . assets [ fileName ] = new RawSource ( obfuscatedSource ) ;
65
+
64
66
65
67
identifiersPrefixCounter ++ ;
66
68
} ) ;
@@ -86,12 +88,14 @@ class WebpackObfuscator {
86
88
87
89
private obfuscate (
88
90
javascript : string ,
91
+ fileName : string ,
89
92
identifiersPrefixCounter : number
90
93
) : { obfuscatedSource : string , obfuscationSourceMap : string } {
91
94
const obfuscationResult = JavaScriptObfuscator . obfuscate (
92
95
javascript ,
93
96
{
94
97
identifiersPrefix : `${ WebpackObfuscator . baseIdentifiersPrefix } ${ identifiersPrefixCounter } ` ,
98
+ sourceMapFileName : fileName + '.map' ,
95
99
...this . options
96
100
}
97
101
) ;
0 commit comments