-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support preexisisting inline source maps.
If tsickle is passed sources that include inline source maps (eg ts 'sources' produced by ngc), compose those source maps with the source maps produced by running tsickle.
- Loading branch information
1 parent
aae914d
commit e1db360
Showing
4 changed files
with
183 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {SourceMapConsumer, SourceMapGenerator} from 'source-map'; | ||
|
||
const INLINE_SOURCE_MAP_REGEX = | ||
new RegExp('^//# sourceMappingURL=data:application/json;base64,(.*)$', 'm'); | ||
|
||
export function containsInlineSourceMap(source: string): boolean { | ||
return getInlineSourceMapCount(source) > 0; | ||
} | ||
|
||
export function getInlineSourceMapCount(source: string): number { | ||
const match = source.match(INLINE_SOURCE_MAP_REGEX); | ||
if (match) { | ||
// The INLINE_SOURCE_MAP_REGEX has a capture group, but we only want to count real matches | ||
return match.length / 2; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
export function extractInlineSourceMap(source: string): string { | ||
const result = INLINE_SOURCE_MAP_REGEX.exec(source)!; | ||
const base64EncodedMap = result[1]; | ||
return Buffer.from(base64EncodedMap, 'base64').toString('utf8'); | ||
} | ||
|
||
export function removeInlineSourceMap(source: string): string { | ||
if (INLINE_SOURCE_MAP_REGEX.test(source)) { | ||
return source.replace(INLINE_SOURCE_MAP_REGEX, ''); | ||
} | ||
return source; | ||
} | ||
|
||
export function setInlineSourceMap(source: string, sourceMap: string): string { | ||
const encodedSourceMap = Buffer.from(sourceMap, 'utf8').toString('base64'); | ||
if (INLINE_SOURCE_MAP_REGEX.test(source)) { | ||
return source.replace( | ||
INLINE_SOURCE_MAP_REGEX, | ||
`//# sourceMappingURL=data:application/json;base64,${encodedSourceMap}`); | ||
} else { | ||
return `${source} | ||
//# sourceMappingURL=data:application/json;base64,${encodedSourceMap}`; | ||
} | ||
} | ||
|
||
export function sourceMapConsumerToGenerator(sourceMapConsumer: SourceMapConsumer): | ||
SourceMapGenerator { | ||
return SourceMapGenerator.fromSourceMap(sourceMapConsumer); | ||
} | ||
|
||
/** | ||
* Tsc identifies source files by their relative path to the output file. Since | ||
* there's no easy way to identify these relative paths when tsickle generates its | ||
* own source maps, we patch them with the file name from the tsc source maps | ||
* before composing them. | ||
*/ | ||
export function sourceMapGeneratorToConsumerWithFileName( | ||
sourceMapGenerator: SourceMapGenerator, fileName: string): SourceMapConsumer { | ||
const rawSourceMap = sourceMapGenerator.toJSON(); | ||
rawSourceMap.sources = [fileName]; | ||
rawSourceMap.file = fileName; | ||
return new SourceMapConsumer(rawSourceMap); | ||
} | ||
|
||
export function sourceMapTextToConsumer(sourceMapText: string): SourceMapConsumer { | ||
const sourceMapJson: any = sourceMapText; | ||
return new SourceMapConsumer(sourceMapJson); | ||
} | ||
|
||
export function sourceMapTextToGenerator(sourceMapText: string): SourceMapGenerator { | ||
const sourceMapJson: any = sourceMapText; | ||
return SourceMapGenerator.fromSourceMap(this.sourceMapTextToConsumer(sourceMapJson)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.