Skip to content

Commit 8780152

Browse files
committed
Compose sourcemaps after obfuscation
1 parent b6136ab commit 8780152

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"version": "auto-changelog"
5252
},
5353
"dependencies": {
54+
"@jridgewell/remapping": "^2.3.5",
5455
"javascript-obfuscator": "^4.1.1"
5556
},
5657
"devDependencies": {
@@ -88,4 +89,4 @@
8889
"esbuild"
8990
]
9091
}
91-
}
92+
}

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path';
55
import os from 'node:os';
66
import { gzipSync } from 'node:zlib';
77
import javascriptObfuscator from 'javascript-obfuscator';
8+
import remapping from '@jridgewell/remapping';
89

910
import type { BundleList, Config, FormatSizeResult, ObfuscationResult, SizeResult } from '../type';
1011
import { isBoolean, isFileNameExcluded, isObject } from './is';
@@ -105,6 +106,20 @@ export function getChunkName(id: string, manualChunks: string[]): string {
105106
return VENDOR_MODULES;
106107
}
107108

109+
export function composeSourcemaps(map1: Rollup.SourceMapInput | null, map2: Rollup.SourceMapInput | null, log?: (msg: string) => void): Rollup.SourceMapInput | null {
110+
if (!map1) return map2;
111+
if (!map2) return map1;
112+
113+
log?.('composing source maps...');
114+
115+
const composed = remapping(
116+
[map2 as remapping.SourceMapInput, map1 as remapping.SourceMapInput],
117+
() => null,
118+
);
119+
120+
return composed as Rollup.SourceMapInput;
121+
}
122+
108123
export class ObfuscatedFilesRegistry {
109124
private static instance: ObfuscatedFilesRegistry;
110125
private obfuscatedFiles: Set<string> = new Set();
@@ -167,17 +182,19 @@ export function obfuscateBundle(finalConfig: Config, fileName: string, bundleIte
167182
sourceMapFileName: `${fileName}.map`,
168183
}
169184
: finalConfig.options;
170-
const obfuscationResult = javascriptObfuscator.obfuscate(bundleItem.code, fileSpecificOptions);
185+
const obfuscated = javascriptObfuscator.obfuscate(bundleItem.code, fileSpecificOptions);
171186
_log.info(`obfuscation complete for ${fileName}.`);
172187

173188
registry.markAsObfuscated(fileName);
174189
_log.info(`added ${fileName} to obfuscated files registry`);
175190

176-
const sourceMap = obfuscationResult.getSourceMap();
177-
178191
return {
179-
code: obfuscationResult.getObfuscatedCode(),
180-
map: sourceMap ? JSON.parse(sourceMap) : null,
192+
code: obfuscated.getObfuscatedCode(),
193+
map: composeSourcemaps(
194+
JSON.parse(JSON.stringify(bundleItem.map) || 'null'), // strip methods
195+
JSON.parse(obfuscated.getSourceMap() || 'null'),
196+
_log.info.bind(_log),
197+
),
181198
};
182199
}
183200

src/worker/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parentPort } from 'node:worker_threads';
22
import javascriptObfuscator, { ObfuscatorOptions } from 'javascript-obfuscator';
3-
import { Log, ObfuscatedFilesRegistry } from '../utils';
3+
import { composeSourcemaps, Log, ObfuscatedFilesRegistry } from '../utils';
44
import type { ObfuscationResult, WorkerMessage } from '../type';
55

66
if (parentPort) {
@@ -40,7 +40,11 @@ if (parentPort) {
4040
results.push({
4141
fileName,
4242
obfuscatedCode: obfuscated.getObfuscatedCode(),
43-
map: JSON.parse(obfuscated.getSourceMap() || 'null'),
43+
map: composeSourcemaps(
44+
JSON.parse(JSON.stringify(bundleItem.map) || 'null'), // strip methods
45+
JSON.parse(obfuscated.getSourceMap() || 'null'),
46+
_log.info.bind(_log),
47+
),
4448
});
4549
}
4650

0 commit comments

Comments
 (0)