|
1 | 1 | import MagicString, { MagicStringOptions } from 'magic-string';
|
2 |
| -import { diff_match_patch, DIFF_DELETE, DIFF_INSERT } from 'diff-match-patch'; |
| 2 | +import { log } from './log'; |
3 | 3 |
|
4 |
| -export function buildMagicString( |
| 4 | +export async function buildMagicString( |
5 | 5 | from: string,
|
6 | 6 | to: string,
|
7 | 7 | options?: MagicStringOptions
|
8 |
| -): MagicString { |
| 8 | +): Promise<MagicString | null> { |
| 9 | + let diff_match_patch, DIFF_DELETE: number, DIFF_INSERT: number; |
| 10 | + try { |
| 11 | + const dmpPkg = await import('diff-match-patch'); |
| 12 | + diff_match_patch = dmpPkg.diff_match_patch; |
| 13 | + DIFF_INSERT = dmpPkg.DIFF_INSERT; |
| 14 | + DIFF_DELETE = dmpPkg.DIFF_DELETE; |
| 15 | + } catch (e) { |
| 16 | + log.error.once( |
| 17 | + 'Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.' |
| 18 | + ); |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
9 | 22 | const dmp = new diff_match_patch();
|
10 | 23 | const diffs = dmp.diff_main(from, to);
|
11 | 24 | dmp.diff_cleanupSemantic(diffs);
|
@@ -38,8 +51,8 @@ export function buildMagicString(
|
38 | 51 | return m;
|
39 | 52 | }
|
40 | 53 |
|
41 |
| -export function buildSourceMap(from: string, to: string, filename?: string) { |
| 54 | +export async function buildSourceMap(from: string, to: string, filename?: string) { |
42 | 55 | // @ts-ignore
|
43 |
| - const m = buildMagicString(from, to, { filename }); |
44 |
| - return m.generateDecodedMap({ source: filename, hires: true, includeContent: false }); |
| 56 | + const m = await buildMagicString(from, to, { filename }); |
| 57 | + return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null; |
45 | 58 | }
|
0 commit comments