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

Method addSource

src/Bundle.ts:65–105  ·  view source on GitHub ↗

* Adds the specified source to the bundle, which can either be a `MagicString` object directly, * or an options object that holds a magic string `content` property and optionally provides * a `filename` for the source within the bundle, as well as an optional `ignoreList` hint * (which defa

(source: MagicString | BundleSourceDescription)

Source from the content-addressed store, hash-verified

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)) {
92 this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length
93 this.uniqueSources.push({ filename: source.filename, content: source.content.original })
94 }
95 else {
96 const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]]
97 if (source.content.original !== uniqueSource.content) {
98 throw new Error(`Illegal source: same filename (${source.filename}), different contents`)
99 }
100 }
101 }
102
103 this.sources.push(source)
104 return this
105 }
106
107 append(str: string, options?: BundleOptions): this {
108 this.addSource({

Callers 3

appendMethod · 0.95
cloneMethod · 0.95
Bundle.test.tsFile · 0.45

Calls 1

isObjectFunction · 0.50

Tested by

no test coverage detected