Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generateDecodedMap and expose SourceMap class #134

Merged
merged 1 commit into from Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion index.d.ts
Expand Up @@ -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[];
Expand All @@ -28,6 +38,7 @@ export class Bundle {
append(str: string, options?: BundleOptions): Bundle;
clone(): Bundle;
generateMap(options?: Partial<SourceMapOptions>): SourceMap;
generateDecodedMap(options?: Partial<SourceMapOptions>): DecodedSourceMap;
getIndentString(): string;
indent(indentStr?: string): Bundle;
indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
Expand Down Expand Up @@ -64,6 +75,7 @@ export default class MagicString {
appendRight(index: number, content: string): MagicString;
clone(): MagicString;
generateMap(options?: Partial<SourceMapOptions>): SourceMap;
generateDecodedMap(options?: Partial<SourceMapOptions>): DecodedSourceMap;
getIndentString(): string;

indent(options?: IndentOptions): MagicString;
Expand Down
14 changes: 9 additions & 5 deletions src/Bundle.js
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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;
Expand All @@ -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 () {
Expand Down
17 changes: 9 additions & 8 deletions 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';
Expand Down Expand Up @@ -126,7 +127,7 @@ MagicString.prototype = {
return cloned;
},

generateMap ( options ) {
generateDecodedMap ( options ) {
options = options || {};

const sourceIndex = 0;
Expand All @@ -145,25 +146,25 @@ 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 );
}

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 () {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -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';
8 changes: 1 addition & 7 deletions src/utils/Mappings.js
@@ -1,5 +1,3 @@
import { encode } from 'sourcemap-codec';

export default function Mappings ( hires ) {
let generatedCodeLine = 0;
let generatedCodeColumn = 0;
Expand All @@ -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,
Expand Down Expand Up @@ -81,8 +79,4 @@ export default function Mappings ( hires ) {

generatedCodeColumn += lines[lines.length - 1].length;
};

this.encode = () => {
return encode(this.raw);
};
}
3 changes: 2 additions & 1 deletion 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;
Expand All @@ -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 = {
Expand Down