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

Method addSource

src/Bundle.ts:81–121  ·  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

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 })
108 }
109 else {
110 const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]]
111 if (source.content.original !== uniqueSource.content) {
112 throw new MagicStringError(
113 `duplicate filename "${source.filename}" with different content, use unique filenames`,
114 )
115 }
116 }
117 }
118
119 this.sources.push(source)
120 return this
121 }
122
123 append(str: string, options?: BundleOptions): this {
124 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