|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -import { Compiler, Compilation, sources } from 'webpack'; |
| 3 | +import { Compiler, compilation } from 'webpack'; |
4 | 4 | import JavaScriptObfuscator, { ObfuscatorOptions } from 'javascript-obfuscator';
|
| 5 | +import { RawSource } from 'webpack-sources'; |
5 | 6 | import multimatch from 'multimatch';
|
6 | 7 | import { RawSourceMap } from 'source-map';
|
7 | 8 | const transferSourceMap = require("multi-stage-sourcemap").transfer;
|
@@ -41,55 +42,45 @@ export class WebpackObfuscatorPlugin {
|
41 | 42 |
|
42 | 43 | const pluginName = this.constructor.name;
|
43 | 44 |
|
44 |
| - compiler.hooks.compilation.tap(pluginName, (compilation: Compilation) => { |
45 |
| - compilation.hooks.processAssets.tap( |
46 |
| - { |
47 |
| - name: 'WebpackObfuscator', |
48 |
| - stage: Compilation.PROCESS_ASSETS_STAGE_DERIVED |
49 |
| - }, |
50 |
| - (assets) => { |
51 |
| - let identifiersPrefixCounter: number = 0; |
52 |
| - const sourcemapOutput: {[index:string]: string} = {}; |
53 |
| - |
54 |
| - compilation.chunks.forEach(chunk => { |
55 |
| - chunk.files.forEach((fileName: string) => { |
56 |
| - if (this.options.sourceMap && fileName.toLowerCase().endsWith('.map')) { |
57 |
| - let srcName = fileName.toLowerCase().substr(0, fileName.length - 4); |
58 |
| - |
59 |
| - if (!this.shouldExclude(srcName)) { |
60 |
| - const transferredSourceMap = transferSourceMap({ |
61 |
| - fromSourceMap: sourcemapOutput[srcName], |
62 |
| - toSourceMap: compilation.assets[fileName].source() |
63 |
| - }); |
64 |
| - const finalSourcemap = JSON.parse(transferredSourceMap); |
65 |
| - |
66 |
| - finalSourcemap['sourcesContent'] = JSON.parse( |
67 |
| - assets[fileName].source().toString() |
68 |
| - )['sourcesContent']; |
69 |
| - assets[fileName] = new sources.RawSource(JSON.stringify(finalSourcemap), false); |
70 |
| - } |
71 |
| - |
72 |
| - return; |
73 |
| - } |
74 |
| - |
75 |
| - if (!fileName.toLowerCase().endsWith('.js') || this.shouldExclude(fileName)) { |
76 |
| - return; |
77 |
| - } |
78 |
| - |
79 |
| - const asset = compilation.assets[fileName] |
80 |
| - const { inputSource, inputSourceMap } = this.extractSourceAndSourceMap(asset); |
81 |
| - const { obfuscatedSource, obfuscationSourceMap } = this.obfuscate(inputSource, fileName, identifiersPrefixCounter); |
82 |
| - |
83 |
| - if (this.options.sourceMap && inputSourceMap) { |
84 |
| - sourcemapOutput[fileName] = obfuscationSourceMap; |
85 |
| - } |
86 |
| - |
87 |
| - assets[fileName] = new sources.RawSource(obfuscatedSource, false); |
88 |
| - identifiersPrefixCounter++; |
89 |
| - }); |
90 |
| - }); |
91 |
| - } |
92 |
| - ); |
| 45 | + compiler.hooks.emit.tap(pluginName, (compilation: compilation.Compilation) => { |
| 46 | + let identifiersPrefixCounter: number = 0; |
| 47 | + const sourcemapOutput: {[index:string]: string} = {}; |
| 48 | + |
| 49 | + compilation.chunks.forEach(chunk => { |
| 50 | + chunk.files.forEach((fileName: string) => { |
| 51 | + if (this.options.sourceMap && fileName.toLowerCase().endsWith('.map')) { |
| 52 | + let srcName = fileName.toLowerCase().substr(0, fileName.length - 4); |
| 53 | + |
| 54 | + if (!this.shouldExclude(srcName)) { |
| 55 | + const transferredSourceMap = transferSourceMap({ |
| 56 | + fromSourceMap: sourcemapOutput[srcName], |
| 57 | + toSourceMap: compilation.assets[fileName].source() |
| 58 | + }); |
| 59 | + const finalSourcemap = JSON.parse(transferredSourceMap); |
| 60 | + |
| 61 | + finalSourcemap['sourcesContent'] = JSON.parse(compilation.assets[fileName].source())['sourcesContent']; |
| 62 | + compilation.assets[fileName] = new RawSource(JSON.stringify(finalSourcemap)); |
| 63 | + } |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (!fileName.toLowerCase().endsWith('.js') || this.shouldExclude(fileName)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const asset = compilation.assets[fileName] |
| 73 | + const { inputSource, inputSourceMap } = this.extractSourceAndSourceMap(asset); |
| 74 | + const { obfuscatedSource, obfuscationSourceMap } = this.obfuscate(inputSource, fileName, identifiersPrefixCounter); |
| 75 | + |
| 76 | + if (this.options.sourceMap && inputSourceMap) { |
| 77 | + sourcemapOutput[fileName] = obfuscationSourceMap; |
| 78 | + } |
| 79 | + |
| 80 | + compilation.assets[fileName] = new RawSource(obfuscatedSource); |
| 81 | + identifiersPrefixCounter++; |
| 82 | + }); |
| 83 | + }); |
93 | 84 | });
|
94 | 85 | }
|
95 | 86 |
|
|
0 commit comments