Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<SourceMapOptions>): SourceMap;
generateDecodedMap(options?: Partial<SourceMapOptions>): DecodedSourceMap;
generateMap(options?: Partial<BundleSourceMapOptions>): SourceMap;
generateDecodedMap(options?: Partial<BundleSourceMapOptions>): DecodedSourceMap;
getIndentString(): string;
indent(indentStr?: string): Bundle;
indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
Expand Down
4 changes: 3 additions & 1 deletion src/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/MagicString.Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down