diff --git a/tools/webpack-plugin/src/index.ts b/tools/webpack-plugin/src/index.ts index 995f0b8d..9e0de13c 100644 --- a/tools/webpack-plugin/src/index.ts +++ b/tools/webpack-plugin/src/index.ts @@ -1,3 +1,4 @@ import { BacktracePlugin } from './BacktracePlugin'; export { BacktracePlugin }; export default BacktracePlugin; +export { BacktracePluginOptions } from '@backtrace/sourcemap-tools'; diff --git a/tools/webpack-plugin/tests/__mocks__/TestDebugIdGenerator.ts b/tools/webpack-plugin/tests/__mocks__/TestDebugIdGenerator.ts deleted file mode 100644 index 0da08676..00000000 --- a/tools/webpack-plugin/tests/__mocks__/TestDebugIdGenerator.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { DebugIdGenerator, SOURCEMAP_DEBUG_ID_KEY } from '@backtrace/sourcemap-tools'; - -export class TestDebugIdGenerator implements DebugIdGenerator { - public generateSourceSnippet(): string { - return `console.log("Source snippet");`; - } - - public generateSourceComment(): string { - return `//# Source comment`; - } - - public addSourceMapKey(sourceMap: T): T & { [SOURCEMAP_DEBUG_ID_KEY]: string } { - return { - ...sourceMap, - [SOURCEMAP_DEBUG_ID_KEY]: 'Source map key', - }; - } - - public static testForSourceSnippet(content: string) { - expect(content).toContain(new TestDebugIdGenerator().generateSourceSnippet()); - } - - public static testForSourceComment(content: string) { - expect(content).toContain(new TestDebugIdGenerator().generateSourceComment()); - } - - public static testForSourceMapKey(sourceMap: object | string) { - if (typeof sourceMap === 'string') { - sourceMap = JSON.parse(sourceMap); - } - - const expected = new TestDebugIdGenerator().addSourceMapKey({}); - expect(sourceMap).toMatchObject(expected); - } -} diff --git a/tools/webpack-plugin/tests/e2e/createE2ETest.ts b/tools/webpack-plugin/tests/e2e/createE2ETest.ts index 242bd4af..1a7f2507 100644 --- a/tools/webpack-plugin/tests/e2e/createE2ETest.ts +++ b/tools/webpack-plugin/tests/e2e/createE2ETest.ts @@ -1,8 +1,8 @@ import { Ok, SourceProcessor, SymbolUploader, ZipArchive } from '@backtrace/sourcemap-tools'; import assert from 'assert'; import crypto from 'crypto'; -import fs from 'fs'; import path from 'path'; +import { Readable } from 'stream'; import webpack from 'webpack'; import { asyncWebpack, expectSuccess, getFiles, removeDir, webpackModeTest } from './helpers'; @@ -19,9 +19,7 @@ export function createE2ETest(configBuilder: (mode: webpack.Configuration['mode' } function mockProcessor() { - return jest - .spyOn(SourceProcessor.prototype, 'processSourceAndSourceMapFiles') - .mockImplementation(async (_, __, debugId) => Ok({ debugId: debugId ?? 'debugId' } as never)); + return jest.spyOn(SourceProcessor.prototype, 'processSourceAndSourceMapFiles'); } function mockZipArchiveAppend() { @@ -34,7 +32,7 @@ export function createE2ETest(configBuilder: (mode: webpack.Configuration['mode' let zipArchiveAppendSpy: ReturnType; beforeAll(async () => { - jest.resetAllMocks(); + jest.restoreAllMocks(); uploadSpy = mockUploader(); processSpy = mockProcessor(); @@ -50,44 +48,35 @@ export function createE2ETest(configBuilder: (mode: webpack.Configuration['mode' result = webpackResult; }, 120000); - it('should call SourceProcessor for every emitted source file and sourcemap pair', async () => { + it('should call SourceProcessor for every emitted source file', async () => { const outputDir = result.compilation.outputOptions.path; assert(outputDir); const jsFiles = await getFiles(outputDir, /.js$/); expect(jsFiles.length).toBeGreaterThan(0); - const processedPairs = processSpy.mock.calls.map( - ([p1, p2]) => [path.resolve(p1), p2 ? path.resolve(p2) : undefined] as const, - ); - for (const file of jsFiles) { - const content = await fs.promises.readFile(file, 'utf8'); - const matches = [...content.matchAll(/^\/\/# sourceMappingURL=(.+)$/gm)]; - expect(matches.length).toEqual(1); - const [, sourceMapPath] = matches[0]; - - expect(processedPairs).toContainEqual([ - path.resolve(file), - path.resolve(path.dirname(file), sourceMapPath), - ]); - } + const processedFiles = processSpy.mock.calls + .map(([sourcePath]) => path.resolve(sourcePath)) + .sort((a, b) => a.localeCompare(b)); + + expect(processedFiles).toEqual(jsFiles.sort((a, b) => a.localeCompare(b))); }); it('should append every emitted sourcemap to archive', async () => { const outputDir = result.compilation.outputOptions.path; assert(outputDir); - const mapFiles = await getFiles(outputDir, /.js.map$/); + const mapFiles = (await getFiles(outputDir, /.js.map$/)).map((f) => path.basename(f)); expect(mapFiles.length).toBeGreaterThan(0); - const uploadedFiles = zipArchiveAppendSpy.mock.calls.map((c) => path.resolve(c[1] as string)); + const uploadedFiles = zipArchiveAppendSpy.mock.calls.map(([name]) => name); for (const file of mapFiles) { - expect(uploadedFiles).toContain(path.resolve(file)); + expect(uploadedFiles).toContainEqual(expect.stringContaining(file)); } }); it('should upload archive', async () => { - expect(uploadSpy).toBeCalledWith(expect.any(ZipArchive)); + expect(uploadSpy).toHaveBeenCalledWith(expect.any(Readable)); }); }); } diff --git a/tools/webpack-plugin/tests/e2e/helpers.ts b/tools/webpack-plugin/tests/e2e/helpers.ts index f835a954..7e9e14d9 100644 --- a/tools/webpack-plugin/tests/e2e/helpers.ts +++ b/tools/webpack-plugin/tests/e2e/helpers.ts @@ -2,7 +2,6 @@ import fs from 'fs'; import path from 'path'; import webpack from 'webpack'; import { BacktracePlugin, BacktracePluginOptions } from '../../src'; -import { TestDebugIdGenerator } from '../__mocks__/TestDebugIdGenerator'; export interface BaseConfigOptions { tsconfigPath?: string; @@ -62,18 +61,6 @@ export function expectSuccess(stats?: webpack.Stats): asserts stats is webpack.S } } -export async function expectSourceSnippet(content: string) { - TestDebugIdGenerator.testForSourceSnippet(content); -} - -export async function expectSourceComment(content: string) { - TestDebugIdGenerator.testForSourceComment(content); -} - -export async function expectSourceMapSnippet(content: string) { - TestDebugIdGenerator.testForSourceMapKey(content); -} - export async function getFiles(dir: string, test?: RegExp) { const files = (await fs.promises.readdir(dir)).map((f) => path.join(dir, f)); if (test) { diff --git a/tools/webpack-plugin/tsconfig.build.json b/tools/webpack-plugin/tsconfig.build.json index 7993723f..e8bfc757 100644 --- a/tools/webpack-plugin/tsconfig.build.json +++ b/tools/webpack-plugin/tsconfig.build.json @@ -2,7 +2,8 @@ "extends": "./tsconfig.json", "compilerOptions": { "rootDir": "./src", - "outDir": "./lib" + "outDir": "./lib", + "lib": [] }, "exclude": ["node_modules", "tests", "lib"] } diff --git a/tools/webpack-plugin/tsconfig.json b/tools/webpack-plugin/tsconfig.json index eb7bf768..14d6f934 100644 --- a/tools/webpack-plugin/tsconfig.json +++ b/tools/webpack-plugin/tsconfig.json @@ -1,5 +1,8 @@ { "extends": "../../tsconfig.base.json", + "compilerOptions": { + "lib": ["ES2020"] + }, "references": [ { "path": "../sourcemap-tools/tsconfig.json"