diff --git a/README.md b/README.md index 6115cba..cc762c3 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,8 @@ bundle.toString(); // console.log( answer ); // }()); -// options are as per `s.generateMap()` above +// options are as per `s.generateMap()` above except that `options.includeContent` can also +// be a function with the signature `function(source: { filename: string, content: string }): boolean` var map = bundle.generateMap({ file: 'bundle.js', includeContent: true, diff --git a/index.d.ts b/index.d.ts index 251701c..f390ed3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,6 +10,16 @@ export interface SourceMapOptions { includeContent: boolean; } + +export interface BundledSourceFileRecord { + filename: string | null; + content: string; +} + +export interface BundleSourceMapOptions extends SourceMapOptions { + includeContent: boolean | ((source: BundledSourceFileRecord) => boolean); +} + export type SourceMapSegment = | [number] | [number, number, number, number] @@ -42,8 +52,8 @@ export class Bundle { addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle; append(str: string, options?: BundleOptions): Bundle; clone(): Bundle; - generateMap(options?: Partial): SourceMap; - generateDecodedMap(options?: Partial): DecodedSourceMap; + generateMap(options?: Partial): SourceMap; + generateDecodedMap(options?: Partial): DecodedSourceMap; getIndentString(): string; indent(indentStr?: string): Bundle; indentExclusionRanges: ExclusionRange | Array; diff --git a/src/Bundle.js b/src/Bundle.js index 76fa317..ff7609c 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -147,7 +147,9 @@ export default class Bundle { return options.file ? getRelativePath(options.file, source.filename) : source.filename; }), sourcesContent: this.uniqueSources.map(source => { - return options.includeContent ? source.content : null; + const includeContent = typeof options.includeContent === 'function' ? options.includeContent(source) : options.includeContent; + + return includeContent ? source.content : null; }), names, mappings: mappings.raw diff --git a/test/MagicString.Bundle.js b/test/MagicString.Bundle.js index 05883e9..07d583e 100644 --- a/test/MagicString.Bundle.js +++ b/test/MagicString.Bundle.js @@ -495,6 +495,32 @@ var template = (function () { column: 16 }); }); + + it('supports a function for includeContent', () => { + const b = new MagicString.Bundle(); + + const files = { + 'one.js': new MagicString('function one () {}', { filename: 'one.js' }), + 'two.js': new MagicString('function two () {}', { filename: 'two.js' }), + }; + + b.addSource(files['one.js']); + b.addSource(files['two.js']); + + const map = b.generateMap({ + file: 'output.js', + source: 'input.js', + includeContent(source) { + assert.ok(files[source.filename]); + assert.equal(files[source.filename].original, source.content); + + return source.filename === 'one.js'; + } + }); + + assert.equal(map.sourcesContent[0], files['one.js'].original); + assert.equal(map.sourcesContent[1], null); + }); }); describe('indent', () => {