Skip to content

Commit 1f70038

Browse files
committed
Write tests
1 parent 8780152 commit 1f70038

3 files changed

Lines changed: 77 additions & 14 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
"@commitlint/config-conventional": "^19.8.1",
6060
"@commitlint/types": "^19.8.1",
6161
"@eslint/js": "^9.32.0",
62+
"@jridgewell/sourcemap-codec": "^1.5.5",
63+
"@jridgewell/trace-mapping": "^0.3.31",
6264
"@stylistic/eslint-plugin": "^5.2.2",
6365
"@types/node": "^24.2.0",
6466
"@vitest/coverage-v8": "^3.2.4",

pnpm-lock.yaml

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

src/__tests__/plugin.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getThreadPoolSize,
99
getValidBundleList,
1010
Log,
11+
composeSourcemaps,
1112
isEnabledFeature,
1213
isEnableThreadPool,
1314
isEnableAutoExcludesNodeModules,
@@ -19,6 +20,8 @@ import {
1920
import {BundleList, Config} from "../type";
2021
import {isArray, isFunction, isFileNameExcluded} from '../utils/is';
2122
import { Worker } from 'node:worker_threads';
23+
import { encode } from '@jridgewell/sourcemap-codec';
24+
import { TraceMap, originalPositionFor, sourceContentFor } from '@jridgewell/trace-mapping';
2225

2326
// Mock WORKER_FILE_PATH
2427
vi.stubGlobal('WORKER_FILE_PATH', './worker.js');
@@ -237,6 +240,58 @@ describe('getChunkName', () => {
237240
});
238241
});
239242

243+
describe('composeSourcemaps', () => {
244+
it('should compose maps and preserve the original source content', () => {
245+
const originalSource = 'const message = "hi";\nconsole.log(message);\n';
246+
const intermediateSource = 'const message="hi";\nconsole.log(message);\n';
247+
248+
const bundlerMap: Rollup.SourceMapInput = {
249+
version: 3,
250+
file: 'bundle.js',
251+
sources: ['original.ts'],
252+
sourcesContent: [originalSource],
253+
names: [],
254+
mappings: encode([
255+
[[0, 0, 0, 0]],
256+
[[0, 0, 1, 0]],
257+
]),
258+
};
259+
260+
const obfuscatorMap: Rollup.SourceMapInput = {
261+
version: 3,
262+
file: 'bundle-obf.js',
263+
sources: ['bundle.js'],
264+
sourcesContent: [intermediateSource],
265+
names: [],
266+
mappings: encode([
267+
[[0, 0, 0, 0]],
268+
[[0, 0, 1, 0]],
269+
]),
270+
};
271+
272+
const logSpy = vi.fn();
273+
274+
const composed = composeSourcemaps(bundlerMap, obfuscatorMap, logSpy);
275+
276+
expect(logSpy).toHaveBeenCalledWith('composing source maps...');
277+
expect(composed).not.toBeNull();
278+
279+
const trace = new TraceMap(composed as any);
280+
281+
const firstLine = originalPositionFor(trace, { line: 1, column: 0 });
282+
expect(firstLine.source).toBe('original.ts');
283+
expect(firstLine.line).toBe(1);
284+
expect(firstLine.column).toBe(0);
285+
286+
const secondLine = originalPositionFor(trace, { line: 2, column: 0 });
287+
expect(secondLine.source).toBe('original.ts');
288+
expect(secondLine.line).toBe(2);
289+
290+
const recoveredSource = sourceContentFor(trace, firstLine.source as string);
291+
expect(recoveredSource).toBe(originalSource);
292+
});
293+
});
294+
240295
describe('CodeSizeAnalyzer', () => {
241296
let analyzer: CodeSizeAnalyzer;
242297
const mockLog = new Log(true);

0 commit comments

Comments
 (0)