MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / Bundle

Class Bundle

src/Bundle.ts:50–415  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50export default class Bundle {
51 /** @internal */
52 declare intro: string
53 /** @internal */
54 declare separator: string
55 /** @internal */
56 declare sources: BundleSourceDescription[]
57 /** @internal */
58 declare uniqueSources: UniqueSource[]
59 /** @internal */
60 declare uniqueSourceIndexByFilename: Record<string, number>
61 declare indentExclusionRanges: ExclusionRange | ExclusionRange[] | undefined
62
63 constructor(options: BundleOptions = {}) {
64 this.intro = options.intro || ''
65 this.separator = options.separator !== undefined ? options.separator : '\n'
66 this.sources = []
67 this.uniqueSources = []
68 this.uniqueSourceIndexByFilename = {}
69 }
70
71 /**
72 * Adds the specified source to the bundle, which can either be a `MagicString` object directly,
73 * or an options object that holds a magic string `content` property and optionally provides
74 * a `filename` for the source within the bundle, as well as an optional `ignoreList` hint
75 * (which defaults to `false`). The `filename` is used when constructing the source map for the
76 * bundle, to identify this `source` in the source map's `sources` field. The `ignoreList` hint
77 * is used to populate the `x_google_ignoreList` extension field in the source map, which is a
78 * mechanism for tools to signal to debuggers that certain sources should be ignored by default
79 * (depending on user preferences).
80 */
81 addSource(source: MagicString | BundleSourceDescription): this {
82 if (source instanceof MagicString) {
83 return this.addSource({
84 content: source,
85 filename: source.filename,
86 separator: this.separator,
87 })
88 }
89
90 if (!isObject(source) || !source.content) {
91 throw new MagicStringError('addSource() requires a `content` property that is a MagicString')
92 }
93
94 ['filename', 'ignoreList', 'indentExclusionRanges', 'separator'].forEach((option) => {
95 if (!hasOwnProp.call(source, option))
96 source[option] = source.content[option]
97 })
98
99 if (source.separator === undefined) {
100 // TODO there's a bunch of this sort of thing, needs cleaning up
101 source.separator = this.separator
102 }
103
104 if (source.filename) {
105 if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
106 this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length
107 this.uniqueSources.push({ filename: source.filename, content: source.content.original })

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected