@@ -345,18 +345,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
345
345
const nodeStartWithLeadingWhitespace = (
346
346
node : DefaultTreeAdapterMap [ 'node' ] ,
347
347
) => {
348
- if ( node . sourceCodeLocation ! . startOffset === 0 )
349
- return node . sourceCodeLocation ! . startOffset
348
+ const startOffset = node . sourceCodeLocation ! . startOffset
349
+ if ( startOffset === 0 ) return 0
350
350
351
351
// Gets the offset for the start of the line including the
352
352
// newline trailing the previous node
353
353
const lineStartOffset =
354
- node . sourceCodeLocation ! . startOffset -
355
- node . sourceCodeLocation ! . startCol
356
- const line = s . slice (
357
- Math . max ( 0 , lineStartOffset ) ,
358
- node . sourceCodeLocation ! . startOffset ,
359
- )
354
+ startOffset - node . sourceCodeLocation ! . startCol
360
355
361
356
// <previous-line-node></previous-line-node>
362
357
// <target-node></target-node>
@@ -369,9 +364,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
369
364
//
370
365
// However, if there is content between our target node start and the
371
366
// previous newline, we cannot strip it out without risking content deletion.
372
- return line . trim ( )
373
- ? node . sourceCodeLocation ! . startOffset
374
- : lineStartOffset
367
+ let isLineEmpty = false
368
+ try {
369
+ const line = s . slice ( Math . max ( 0 , lineStartOffset ) , startOffset )
370
+ isLineEmpty = ! line . trim ( )
371
+ } catch {
372
+ // magic-string may throw if there's some content removed in the sliced string,
373
+ // which we ignore and assume the line is not empty
374
+ }
375
+
376
+ return isLineEmpty ? lineStartOffset : startOffset
375
377
}
376
378
377
379
// pre-transform
0 commit comments