|
1 | | -import webpack from 'webpack'; |
2 | | -import { BacktracePluginV4 } from './BacktracePluginV4'; |
3 | | -import { BacktracePluginV5 } from './BacktracePluginV5'; |
4 | | - |
5 | | -let BacktracePlugin: typeof BacktracePluginV4 | typeof BacktracePluginV5; |
6 | | - |
7 | | -const version = process.env.WEBPACK_VERSION ?? webpack.version[0]; |
8 | | -switch (version) { |
9 | | - case '4': |
10 | | - BacktracePlugin = BacktracePluginV4; |
11 | | - break; |
12 | | - case '5': |
13 | | - BacktracePlugin = BacktracePluginV5; |
14 | | - break; |
15 | | - default: |
16 | | - throw new Error(`Webpack version ${version} is not supported.`); |
17 | | -} |
| 1 | +import { DebugIdGenerator, SourceMapUploader, SourceProcessor } from '@backtrace/sourcemap-tools'; |
| 2 | +import path from 'path'; |
| 3 | +import webpack, { WebpackPluginInstance } from 'webpack'; |
| 4 | +import { BacktracePluginOptions } from './models/BacktracePluginOptions'; |
| 5 | + |
| 6 | +export class BacktracePlugin implements WebpackPluginInstance { |
| 7 | + private readonly _sourceMapProcessor: SourceProcessor; |
| 8 | + private readonly _sourceMapUploader?: SourceMapUploader; |
| 9 | + |
| 10 | + constructor(public readonly options?: BacktracePluginOptions) { |
| 11 | + this._sourceMapProcessor = new SourceProcessor(options?.debugIdGenerator ?? new DebugIdGenerator()); |
| 12 | + |
| 13 | + this._sourceMapUploader = |
| 14 | + options?.sourceMapUploader ?? (options?.uploadUrl ? new SourceMapUploader(options.uploadUrl) : undefined); |
| 15 | + } |
| 16 | + |
| 17 | + public apply(compiler: webpack.Compiler) { |
| 18 | + compiler.hooks.afterEmit.tapPromise(BacktracePlugin.name, async (compilation) => { |
| 19 | + const logger = compilation.getLogger(BacktracePlugin.name); |
| 20 | + if (!compilation.outputOptions.path) { |
| 21 | + logger.error('skipping everything because outputOptions.path is not set, a bug?'); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const entries: [string, string, string][] = []; |
| 26 | + |
| 27 | + for (const asset in compilation.assets) { |
| 28 | + if (!asset.match(/\.(c|m)?jsx?$/)) { |
| 29 | + logger.debug(`[${asset}] skipping processing, extension does not match`); |
| 30 | + continue; |
| 31 | + } |
18 | 32 |
|
19 | | -export { BacktracePlugin }; |
| 33 | + const map = asset + '.map'; |
| 34 | + if (!compilation.assets[map]) { |
| 35 | + logger.debug(`[${asset}] skipping processing, map file not found`); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + const assetPath = path.join(compilation.outputOptions.path, asset); |
| 40 | + const sourceMapPath = path.join(compilation.outputOptions.path, map); |
| 41 | + |
| 42 | + logger.debug(`adding asset ${assetPath} with sourcemap ${sourceMapPath}`); |
| 43 | + entries.push([asset, assetPath, sourceMapPath]); |
| 44 | + } |
| 45 | + |
| 46 | + logger.log(`received ${entries.length} files for processing`); |
| 47 | + |
| 48 | + for (const [asset, sourcePath, sourceMapPath] of entries) { |
| 49 | + logger.time(`[${asset}] process source and sourcemap`); |
| 50 | + |
| 51 | + let debugId: string; |
| 52 | + try { |
| 53 | + debugId = await this._sourceMapProcessor.processSourceAndSourceMapFiles(sourcePath, sourceMapPath); |
| 54 | + logger.timeEnd(`[${asset}] process source and sourcemap`); |
| 55 | + } catch (err) { |
| 56 | + logger.timeEnd(`[${asset}] process source and sourcemap`); |
| 57 | + logger.error(`[${asset}] process source and sourcemap failed:`, err); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if (this._sourceMapUploader) { |
| 62 | + logger.time(`[${asset}] upload sourcemap`); |
| 63 | + try { |
| 64 | + await this._sourceMapUploader.upload(sourceMapPath, debugId); |
| 65 | + logger.timeEnd(`[${asset}] upload sourcemap`); |
| 66 | + } catch (err) { |
| 67 | + logger.timeEnd(`[${asset}] upload sourcemap`); |
| 68 | + logger.error(`[${asset}] upload sourcemap failed:`, err); |
| 69 | + } |
| 70 | + logger.log(`[${asset}] file processed and sourcemap uploaded`); |
| 71 | + } else { |
| 72 | + logger.log(`[${asset}] file processed`); |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
0 commit comments