diff --git a/src/Bundle.ts b/src/Bundle.ts index 08b27f4d..4dc88ae8 100644 --- a/src/Bundle.ts +++ b/src/Bundle.ts @@ -28,6 +28,21 @@ interface UniqueSource { content: string } +export interface BundledSourceFileRecord { + filename: string + content: string +} + +export interface BundleSourceMapOptions extends Omit { + /** + * Whether to include the original content of each source in the map's `sourcesContent` array. + * Can also be a function that receives the source's `filename` and `content` and returns + * whether to include it, allowing per-source control (for example, omitting content for + * sources that are otherwise loadable at runtime, such as public http(s) urls). + */ + includeContent?: boolean | ((source: BundledSourceFileRecord) => boolean) +} + export interface DecodedSourceMapOrMissingContent extends Omit { sourcesContent: Array } @@ -133,7 +148,7 @@ export default class Bundle { return bundle as this } - generateDecodedMap(options: SourceMapOptions = {}): DecodedSourceMapOrMissingContent { + generateDecodedMap(options: BundleSourceMapOptions = {}): DecodedSourceMapOrMissingContent { const names = [] let x_google_ignoreList this.sources.forEach((source) => { @@ -214,7 +229,11 @@ 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, @@ -224,7 +243,7 @@ export default class Bundle { } generateMap( - options?: SourceMapOptions, + options?: BundleSourceMapOptions, ): Omit & { sourcesContent: Array } { return new SourceMap(this.generateDecodedMap(options)) as Omit & { sourcesContent: Array diff --git a/src/index.ts b/src/index.ts index 7f35617c..44fe6be4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import MagicStringError from './MagicStringError.ts' import SourceMap from './SourceMap.ts' export { Bundle, MagicString as default, MagicString, MagicStringError, SourceMap } -export type { BundleOptions } from './Bundle.ts' +export type { BundledSourceFileRecord, BundleOptions, BundleSourceMapOptions } from './Bundle.ts' export type { ExclusionRange, IndentOptions, diff --git a/test/Bundle.test.ts b/test/Bundle.test.ts index 7f296e98..4739c810 100644 --- a/test/Bundle.test.ts +++ b/test/Bundle.test.ts @@ -719,6 +719,32 @@ describe('bundle', () => { column: 16, }) }) + + it('supports a function for includeContent', () => { + const b = new Bundle() + + const files: Record = { + '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', () => {