|
| 1 | +// Ensure we have working sourcemaps when the app runs in a browser |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const sm = require('source-map'); |
| 6 | + |
| 7 | +const PRAGMA = '//# sourceMappingURL='; |
| 8 | +const DATA = 'data:application/json;charset=utf-8;base64,'; |
| 9 | + |
| 10 | +function read(...f) { |
| 11 | + return fs.readFileSync(require.resolve(path.join(__dirname, ...f)), 'utf-8'); |
| 12 | +} |
| 13 | + |
| 14 | +function parseSourceMap(content) { |
| 15 | + const sourcemapLine = content.split(/\r?\n/).find(l => l.startsWith(PRAGMA)); |
| 16 | + if (!sourcemapLine) { |
| 17 | + throw new Error(`no ${PRAGMA} found in ${content}`); |
| 18 | + } |
| 19 | + return JSON.parse(Buffer.from(sourcemapLine.slice(PRAGMA.length + DATA.length), 'base64')); |
| 20 | +} |
| 21 | + |
| 22 | +function readSourceMap(...f) { |
| 23 | + return JSON.parse(fs.readFileSync(require.resolve(path.join(__dirname, ...f)))); |
| 24 | +} |
| 25 | + |
| 26 | +function find(text, s = 'ts1') { |
| 27 | + const lines = text.split(/\r?\n/); |
| 28 | + for (let line = 1; line <= lines.length; line++) { |
| 29 | + const column = lines[line - 1].indexOf(s); |
| 30 | + if (column >= 0) { |
| 31 | + return {line, column}; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function asserts(pos) { |
| 37 | + // This doesn't work because the output dir is different from input |
| 38 | + // so it actually starts with a bunch of '/../..' |
| 39 | + // expect(pos.source).toBe('index.js'); |
| 40 | + |
| 41 | + expect(pos.source.endsWith('index.js')).toBeTruthy(); |
| 42 | + expect(pos.line).toBe(6); // one-based |
| 43 | + expect(pos.column).toBe(20); // zero-based |
| 44 | +} |
| 45 | + |
| 46 | +describe('application sourcemaps in the browser', () => { |
| 47 | + it('should work after rollup', async () => { |
| 48 | + const content = read('app_chunks', 'index.js'); |
| 49 | + const rawSourceMap = parseSourceMap(content); |
| 50 | + await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => { |
| 51 | + asserts(consumer.originalPositionFor(find(content))); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should work after terser', async () => { |
| 56 | + const content = read('app_chunks.min', 'index.js'); |
| 57 | + const rawSourceMap = readSourceMap('app_chunks.min', 'index.js.map'); |
| 58 | + await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => { |
| 59 | + asserts(consumer.originalPositionFor(find(content))); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should work after babel', async () => { |
| 64 | + const content = read('app_chunks_es5', 'index.js'); |
| 65 | + const rawSourceMap = parseSourceMap(content); |
| 66 | + await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => { |
| 67 | + asserts(consumer.originalPositionFor(find(content))); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should work after babel+terser', async () => { |
| 72 | + const content = read('app_chunks_es5.min', 'index.js'); |
| 73 | + const rawSourceMap = readSourceMap('app_chunks_es5.min', 'index.js.map'); |
| 74 | + await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => { |
| 75 | + asserts(consumer.originalPositionFor(find(content))); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments