|
| 1 | +import type { Compiler } from 'webpack'; |
| 2 | +import { sources } from 'webpack'; |
| 3 | + |
| 4 | +export interface FixSourceMapUrlPluginOptions { |
| 5 | + outputPath: string; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Ensures sourceMappingURL points to the actual file:// location on device/emulator. |
| 10 | + * Handles Webpack 5 asset sources (string/Buffer/Source objects). |
| 11 | + */ |
| 12 | +export default class FixSourceMapUrlPlugin { |
| 13 | + constructor(private readonly options: FixSourceMapUrlPluginOptions) {} |
| 14 | + |
| 15 | + apply(compiler: Compiler) { |
| 16 | + const wp: any = (compiler as any).webpack; |
| 17 | + const hasProcessAssets = |
| 18 | + !!wp?.Compilation?.PROCESS_ASSETS_STAGE_DEV_TOOLING && |
| 19 | + !!(compiler as any).hooks?.thisCompilation; |
| 20 | + |
| 21 | + const leadingCharacter = process.platform === 'win32' ? '/' : ''; |
| 22 | + |
| 23 | + const toStringContent = (content: any): string => { |
| 24 | + if (typeof content === 'string') return content; |
| 25 | + if (Buffer.isBuffer(content)) return content.toString('utf-8'); |
| 26 | + if (content && typeof content.source === 'function') { |
| 27 | + const inner = content.source(); |
| 28 | + if (typeof inner === 'string') return inner; |
| 29 | + if (Buffer.isBuffer(inner)) return inner.toString('utf-8'); |
| 30 | + try { |
| 31 | + return String(inner); |
| 32 | + } catch { |
| 33 | + return ''; |
| 34 | + } |
| 35 | + } |
| 36 | + try { |
| 37 | + return String(content); |
| 38 | + } catch { |
| 39 | + return ''; |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const processFile = (filename: string, compilation: any) => { |
| 44 | + if (!(filename.endsWith('.mjs') || filename.endsWith('.js'))) return; |
| 45 | + |
| 46 | + // Support both legacy compilation.assets and v5 Asset API |
| 47 | + let rawSource: any; |
| 48 | + if (typeof (compilation as any).getAsset === 'function') { |
| 49 | + const assetObj = (compilation as any).getAsset(filename); |
| 50 | + if (assetObj && assetObj.source) { |
| 51 | + rawSource = (assetObj.source as any).source |
| 52 | + ? (assetObj.source as any).source() |
| 53 | + : (assetObj.source as any)(); |
| 54 | + } |
| 55 | + } |
| 56 | + if ( |
| 57 | + rawSource === undefined && |
| 58 | + (compilation as any).assets && |
| 59 | + (compilation as any).assets[filename] |
| 60 | + ) { |
| 61 | + const asset = (compilation as any).assets[filename]; |
| 62 | + rawSource = typeof asset.source === 'function' ? asset.source() : asset; |
| 63 | + } |
| 64 | + |
| 65 | + let source = toStringContent(rawSource); |
| 66 | + // Replace sourceMappingURL to use file:// protocol pointing to actual location |
| 67 | + source = source.replace( |
| 68 | + /\/\/\# sourceMappingURL=(.+\.map)/g, |
| 69 | + `//# sourceMappingURL=file://${leadingCharacter}${this.options.outputPath}/$1`, |
| 70 | + ); |
| 71 | + |
| 72 | + // Prefer Webpack 5 updateAsset with RawSource when available |
| 73 | + const RawSourceCtor = |
| 74 | + wp?.sources?.RawSource || (sources as any)?.RawSource; |
| 75 | + if ( |
| 76 | + typeof (compilation as any).updateAsset === 'function' && |
| 77 | + RawSourceCtor |
| 78 | + ) { |
| 79 | + (compilation as any).updateAsset(filename, new RawSourceCtor(source)); |
| 80 | + } else { |
| 81 | + (compilation as any).assets[filename] = { |
| 82 | + source: () => source, |
| 83 | + size: () => source.length, |
| 84 | + }; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + if (hasProcessAssets) { |
| 89 | + compiler.hooks.thisCompilation.tap( |
| 90 | + 'FixSourceMapUrlPlugin', |
| 91 | + (compilation: any) => { |
| 92 | + const stage = wp.Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING; |
| 93 | + compilation.hooks.processAssets.tap( |
| 94 | + { name: 'FixSourceMapUrlPlugin', stage }, |
| 95 | + (assets: Record<string, any>) => { |
| 96 | + Object.keys(assets).forEach((filename) => |
| 97 | + processFile(filename, compilation), |
| 98 | + ); |
| 99 | + }, |
| 100 | + ); |
| 101 | + }, |
| 102 | + ); |
| 103 | + } else { |
| 104 | + // Fallback for older setups: use emit (may log deprecation in newer webpack) |
| 105 | + compiler.hooks.emit.tap('FixSourceMapUrlPlugin', (compilation: any) => { |
| 106 | + Object.keys((compilation as any).assets).forEach((filename) => |
| 107 | + processFile(filename, compilation), |
| 108 | + ); |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments