Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ interface UniqueSource {
content: string
}

export interface BundledSourceFileRecord {
filename: string
content: string
}

export interface BundleSourceMapOptions extends Omit<SourceMapOptions, 'includeContent'> {
/**
* 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<DecodedSourceMap, 'sourcesContent'> {
sourcesContent: Array<string | null>
}
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand All @@ -224,7 +243,7 @@ export default class Bundle {
}

generateMap(
options?: SourceMapOptions,
options?: BundleSourceMapOptions,
): Omit<SourceMap, 'sourcesContent'> & { sourcesContent: Array<string | null> } {
return new SourceMap(this.generateDecodedMap(options)) as Omit<SourceMap, 'sourcesContent'> & {
sourcesContent: Array<string | null>
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions test/Bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,32 @@ describe('bundle', () => {
column: 16,
})
})

it('supports a function for includeContent', () => {
const b = new Bundle()

const files: Record<string, MagicString> = {
'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