fix: keep the ignore-list hint and stored names when cloning (#329) · Rich-Harris/magic-string@ddf02ca · GitHub
Skip to content

Commit ddf02ca

Browse files
authored
fix: keep the ignore-list hint and stored names when cloning (#329)
1 parent 811de46 commit ddf02ca

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/Bundle.ts

Lines changed: 2 additions & 0 deletions

src/MagicString.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ export default class MagicString {
193193
* Does what you'd expect.
194194
*/
195195
clone(): this {
196-
const cloned = new MagicString(this.original, { filename: this.filename, offset: this.offset })
196+
const cloned = new MagicString(this.original, {
197+
filename: this.filename,
198+
ignoreList: this.ignoreList,
199+
offset: this.offset,
200+
})
197201

198202
let originalChunk = this.firstChunk
199203
let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone())
@@ -224,6 +228,7 @@ export default class MagicString {
224228
}
225229

226230
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations)
231+
cloned.storedNames = { ...this.storedNames }
227232

228233
cloned.intro = this.intro
229234
cloned.outro = this.outro

test/Bundle.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ describe('bundle', () => {
108108
assert.equal(b.toString(), '>>>abXXef\nghijkl<<<')
109109
assert.equal(clone.toString(), '>>>abcdef\nghijkl<<<')
110110
})
111+
it('should clone the ignore-list hint', () => {
112+
const b = new Bundle() as BundleInternals
113+
114+
b.addSource({ content: new MagicString('foo', { filename: 'foo.js' }), ignoreList: true })
115+
116+
const clone = b.clone() as BundleInternals
117+
118+
assert.strictEqual(clone.sources[0].ignoreList, true)
119+
assert.deepEqual(clone.generateMap({ includeContent: false }).x_google_ignoreList, [0])
120+
})
121+
122+
it('should clone indentExclusionRanges', () => {
123+
const b = new Bundle() as BundleInternals
124+
125+
b.addSource({
126+
content: new MagicString('foo', { filename: 'foo.js' }),
127+
indentExclusionRanges: [1, 2],
128+
})
129+
130+
const clone = b.clone() as BundleInternals
131+
132+
assert.deepEqual(clone.sources[0].indentExclusionRanges, [1, 2])
133+
})
111134
})
112135

113136
describe('generateMap', () => {

test/MagicString.test.ts

Lines changed: 34 additions & 0 deletions

0 commit comments

Comments
 (0)