11import type { File , Store } from '../store'
22import {
3- MagicString ,
43 babelParse ,
54 extractIdentifiers ,
65 isInDestructureAssignment ,
@@ -103,7 +102,28 @@ function processChildFiles(
103102}
104103
105104function processModule ( store : Store , src : string , filename : string ) {
106- const s = new MagicString ( src )
105+ // edits by offsets in `src`, sorted and disjoint. Each edit is applied
106+ // against the edits made before it: a replaced range drops the edits inside
107+ // it, whichever is made first, and inserts at one offset keep call order.
108+ const edits : { start : number ; end : number ; text : string } [ ] = [ ]
109+ function replace ( start : number , end : number , text : string ) {
110+ let i = edits . length
111+ while ( i && edits [ i - 1 ] . end > start ) i --
112+ const next = edits [ i ]
113+ if ( next && next . start <= start && end <= next . end && next . start < end ) {
114+ return
115+ }
116+ let covered = 0
117+ while (
118+ i + covered < edits . length &&
119+ edits [ i + covered ] . start < end &&
120+ edits [ i + covered ] . end <= end
121+ ) {
122+ covered ++
123+ }
124+ edits . splice ( i , covered , { start, end, text } )
125+ }
126+ const insert = ( offset : number , text : string ) => replace ( offset , offset , text )
107127
108128 const ast = babelParse ( src , {
109129 sourceFilename : filename ,
@@ -136,19 +156,23 @@ function processModule(store: Store, src: string, filename: string) {
136156 importedFiles . add ( filename )
137157 const id = `__import_${ importedFiles . size } __`
138158 importToIdMap . set ( filename , id )
139- s . appendLeft (
159+ insert (
140160 node . start ! ,
141161 `const ${ id } = ${ modulesKey } [${ JSON . stringify ( filename ) } ]\n` ,
142162 )
143163 return id
144164 }
145165
146166 function defineExport ( name : string , local = name ) {
147- s . append ( `\n${ exportKey } (${ moduleKey } , "${ name } ", () => ${ local } )` )
167+ insert (
168+ src . length ,
169+ `\n${ exportKey } (${ moduleKey } , "${ name } ", () => ${ local } )` ,
170+ )
148171 }
149172
150173 // 0. instantiate module
151- s . prepend (
174+ insert (
175+ 0 ,
152176 `const ${ moduleKey } = ${ modulesKey } [${ JSON . stringify (
153177 filename ,
154178 ) } ] = { [Symbol.toStringTag]: "Module" }\n\n`,
@@ -176,7 +200,7 @@ function processModule(store: Store, src: string, filename: string) {
176200 idToImportMap . set ( spec . local . name , importId )
177201 }
178202 }
179- s . remove ( node . start ! , node . end ! )
203+ replace ( node . start ! , node . end ! , '' )
180204 }
181205 }
182206 }
@@ -200,7 +224,7 @@ function processModule(store: Store, src: string, filename: string) {
200224 }
201225 }
202226 }
203- s . remove ( node . start ! , node . declaration . start ! )
227+ replace ( node . start ! , node . declaration . start ! , '' )
204228 } else if ( node . source && node . source . value . startsWith ( './' ) ) {
205229 // export { foo, bar } from './foo'
206230 const importId = defineImport ( node , node . source . value )
@@ -210,15 +234,15 @@ function processModule(store: Store, src: string, filename: string) {
210234 `${ importId } .${ ( spec as ExportSpecifier ) . local . name } ` ,
211235 )
212236 }
213- s . remove ( node . start ! , node . end ! )
237+ replace ( node . start ! , node . end ! , '' )
214238 } else {
215239 // export { foo, bar }
216240 for ( const spec of node . specifiers ) {
217241 const local = ( spec as ExportSpecifier ) . local . name
218242 const binding = idToImportMap . get ( local )
219243 defineExport ( ( spec . exported as Identifier ) . name , binding || local )
220244 }
221- s . remove ( node . start ! , node . end ! )
245+ replace ( node . start ! , node . end ! , '' )
222246 }
223247 }
224248
@@ -229,23 +253,29 @@ function processModule(store: Store, src: string, filename: string) {
229253 // export default function foo() {}
230254 // export default class A {}
231255 const { name } = node . declaration . id
232- s . remove ( node . start ! , node . start ! + 15 )
233- s . append ( `\n${ exportKey } (${ moduleKey } , "default", () => ${ name } )` )
256+ replace ( node . start ! , node . start ! + 15 , '' )
257+ insert (
258+ src . length ,
259+ `\n${ exportKey } (${ moduleKey } , "default", () => ${ name } )` ,
260+ )
234261 } else {
235262 // anonymous default exports
236- s . overwrite ( node . start ! , node . start ! + 14 , `${ moduleKey } .default =` )
263+ replace ( node . start ! , node . start ! + 14 , `${ moduleKey } .default =` )
237264 }
238265 }
239266
240267 // export * from './foo'
241268 if ( node . type === 'ExportAllDeclaration' ) {
242269 const importId = defineImport ( node , node . source . value )
243- s . remove ( node . start ! , node . end ! )
244- s . append ( `\nfor (const key in ${ importId } ) {
270+ replace ( node . start ! , node . end ! , '' )
271+ insert (
272+ src . length ,
273+ `\nfor (const key in ${ importId } ) {
245274 if (key !== 'default') {
246275 ${ exportKey } (${ moduleKey } , key, () => ${ importId } [key])
247276 }
248- }` )
277+ }` ,
278+ )
249279 }
250280 }
251281
@@ -265,7 +295,7 @@ function processModule(store: Store, src: string, filename: string) {
265295 ! ( parent as any ) . inPattern ||
266296 isInDestructureAssignment ( parent , parentStack )
267297 ) {
268- s . appendLeft ( id . end ! , `: ${ binding } ` )
298+ insert ( id . end ! , `: ${ binding } ` )
269299 }
270300 } else if (
271301 parent &&
@@ -276,10 +306,10 @@ function processModule(store: Store, src: string, filename: string) {
276306 declaredConst . add ( id . name )
277307 // locate the top-most node containing the class declaration
278308 const topNode = parentStack [ 1 ]
279- s . prependRight ( topNode . start ! , `const ${ id . name } = ${ binding } ;\n` )
309+ insert ( topNode . start ! , `const ${ id . name } = ${ binding } ;\n` )
280310 }
281311 } else {
282- s . overwrite ( id . start ! , id . end ! , binding )
312+ replace ( id . start ! , id . end ! , binding )
283313 }
284314 } )
285315 }
@@ -292,8 +322,8 @@ function processModule(store: Store, src: string, filename: string) {
292322 const arg = parent . arguments [ 0 ]
293323 if ( arg . type === 'StringLiteral' && arg . value . startsWith ( './' ) ) {
294324 hasDynamicImport = true
295- s . overwrite ( node . start ! , node . start ! + 6 , dynamicImportKey )
296- s . overwrite (
325+ replace ( node . start ! , node . start ! + 6 , dynamicImportKey )
326+ replace (
297327 arg . start ! ,
298328 arg . end ! ,
299329 JSON . stringify ( arg . value . replace ( / ^ \. \/ + / , 'src/' ) ) ,
@@ -303,8 +333,16 @@ function processModule(store: Store, src: string, filename: string) {
303333 } ,
304334 } )
305335
336+ let code = ''
337+ let offset = 0
338+ for ( const edit of edits ) {
339+ code += src . slice ( offset , edit . start ) + edit . text
340+ offset = edit . end
341+ }
342+ code += src . slice ( offset )
343+
306344 return {
307- code : s . toString ( ) ,
345+ code,
308346 importedFiles,
309347 hasDynamicImport,
310348 }
0 commit comments