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

Class Bundle

src/Bundle.ts:34–359  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected