diff --git a/index.d.ts b/index.d.ts index 9d762db..ec23bed 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,7 +10,17 @@ export interface SourceMapOptions { includeContent: boolean; } -export interface SourceMap { +export interface DecodedSourceMap { + file: string; + sources: string[]; + sourcesContent: string[]; + names: string[]; + mappings: number[][][]; +} + +export class SourceMap { + constructor(properties: DecodedSourceMap); + version: string; file: string; sources: string[]; @@ -28,6 +38,7 @@ export class Bundle { append(str: string, options?: BundleOptions): Bundle; clone(): Bundle; generateMap(options?: Partial): SourceMap; + generateDecodedMap(options?: Partial): DecodedSourceMap; getIndentString(): string; indent(indentStr?: string): Bundle; indentExclusionRanges: ExclusionRange | Array; @@ -64,6 +75,7 @@ export default class MagicString { appendRight(index: number, content: string): MagicString; clone(): MagicString; generateMap(options?: Partial): SourceMap; + generateDecodedMap(options?: Partial): DecodedSourceMap; getIndentString(): string; indent(options?: IndentOptions): MagicString; diff --git a/src/Bundle.js b/src/Bundle.js index 8a11683..02ee934 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -80,7 +80,7 @@ Bundle.prototype = { return bundle; }, - generateMap ( options = {} ) { + generateDecodedMap ( options = {} ) { const names = []; this.sources.forEach( source => { Object.keys( source.content.storedNames ).forEach( name => { @@ -114,7 +114,7 @@ Bundle.prototype = { if ( source.filename ) { if ( chunk.edited ) { - mappings.addEdit( sourceIndex, chunk.content, chunk.original, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1 ); + mappings.addEdit( sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1 ); } else { mappings.addUneditedChunk( sourceIndex, chunk, magicString.original, loc, magicString.sourcemapLocations ); } @@ -132,7 +132,7 @@ Bundle.prototype = { } }); - return new SourceMap({ + return { file: ( options.file ? options.file.split( /[\/\\]/ ).pop() : null ), sources: this.uniqueSources.map( source => { return options.file ? getRelativePath( options.file, source.filename ) : source.filename; @@ -141,8 +141,12 @@ Bundle.prototype = { return options.includeContent ? source.content : null; }), names, - mappings: mappings.encode() - }); + mappings: mappings.raw + }; + }, + + generateMap ( options ) { + return new SourceMap(this.generateDecodedMap( options )); }, getIndentString () { diff --git a/src/MagicString.js b/src/MagicString.js index ea3b36e..1b435c9 100644 --- a/src/MagicString.js +++ b/src/MagicString.js @@ -1,3 +1,4 @@ +import { encode } from 'sourcemap-codec'; import Chunk from './Chunk.js'; import SourceMap from './utils/SourceMap.js'; import guessIndent from './utils/guessIndent.js'; @@ -126,7 +127,7 @@ MagicString.prototype = { return cloned; }, - generateMap ( options ) { + generateDecodedMap ( options ) { options = options || {}; const sourceIndex = 0; @@ -145,7 +146,7 @@ MagicString.prototype = { if ( chunk.intro.length ) mappings.advance( chunk.intro ); if ( chunk.edited ) { - mappings.addEdit( sourceIndex, chunk.content, chunk.original, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1 ); + mappings.addEdit( sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1 ); } else { mappings.addUneditedChunk( sourceIndex, chunk, this.original, loc, this.sourcemapLocations ); } @@ -153,17 +154,17 @@ MagicString.prototype = { if ( chunk.outro.length ) mappings.advance( chunk.outro ); }); - if ( DEBUG ) this.stats.time( 'generateMap' ); - const map = new SourceMap({ + return { file: ( options.file ? options.file.split( /[\/\\]/ ).pop() : null ), sources: [ options.source ? getRelativePath( options.file || '', options.source ) : null ], sourcesContent: options.includeContent ? [ this.original ] : [ null ], names, - mappings: mappings.encode() - }); - if ( DEBUG ) this.stats.timeEnd( 'generateMap' ); + mappings: mappings.raw + }; + }, - return map; + generateMap ( options ) { + return new SourceMap(this.generateDecodedMap( options )); }, getIndentString () { diff --git a/src/index.js b/src/index.js index 594d8db..44c15ef 100644 --- a/src/index.js +++ b/src/index.js @@ -2,3 +2,4 @@ import MagicString from './MagicString.js'; export default MagicString; export { default as Bundle } from './Bundle.js'; +export { default as SourceMap} from './utils/SourceMap.js'; diff --git a/src/utils/Mappings.js b/src/utils/Mappings.js index fead3a9..bcd1377 100644 --- a/src/utils/Mappings.js +++ b/src/utils/Mappings.js @@ -1,5 +1,3 @@ -import { encode } from 'sourcemap-codec'; - export default function Mappings ( hires ) { let generatedCodeLine = 0; let generatedCodeColumn = 0; @@ -9,7 +7,7 @@ export default function Mappings ( hires ) { let pending = null; - this.addEdit = ( sourceIndex, content, original, loc, nameIndex ) => { + this.addEdit = ( sourceIndex, content, loc, nameIndex ) => { if ( content.length ) { const segment = [ generatedCodeColumn, @@ -81,8 +79,4 @@ export default function Mappings ( hires ) { generatedCodeColumn += lines[lines.length - 1].length; }; - - this.encode = () => { - return encode(this.raw); - }; } diff --git a/src/utils/SourceMap.js b/src/utils/SourceMap.js index f3dde82..3117b88 100644 --- a/src/utils/SourceMap.js +++ b/src/utils/SourceMap.js @@ -1,4 +1,5 @@ import btoa from './btoa.js'; +import { encode } from 'sourcemap-codec'; export default function SourceMap ( properties ) { this.version = 3; @@ -7,7 +8,7 @@ export default function SourceMap ( properties ) { this.sources = properties.sources; this.sourcesContent = properties.sourcesContent; this.names = properties.names; - this.mappings = properties.mappings; + this.mappings = encode(properties.mappings); } SourceMap.prototype = {