Skip to content

Commit 68d76c9

Browse files
authored
fix: avoid mangling code from incorrect magic-string usage (#7397)
1 parent d23f4ff commit 68d76c9

File tree

10 files changed

+78
-33
lines changed

10 files changed

+78
-33
lines changed

packages/vite/src/node/optimizer/scan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ async function transformGlob(
503503
resolve
504504
)
505505
s.prepend(importsString)
506-
s.overwrite(expStart, endIndex, exp)
506+
s.overwrite(expStart, endIndex, exp, { contentOnly: true })
507507
}
508508
return s.toString()
509509
}

packages/vite/src/node/plugins/asset.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
100100
const file = getAssetFilename(hash, config) || this.getFileName(hash)
101101
chunk.viteMetadata.importedAssets.add(cleanUrl(file))
102102
const outputFilepath = config.base + file + postfix
103-
s.overwrite(match.index, match.index + full.length, outputFilepath)
103+
s.overwrite(match.index, match.index + full.length, outputFilepath, {
104+
contentOnly: true
105+
})
104106
}
105107

106108
if (s) {

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
5151
index + exp.length,
5252
`new URL(import.meta.globEagerDefault(${JSON.stringify(
5353
pattern
54-
)})[${rawUrl}], self.location)`
54+
)})[${rawUrl}], self.location)`,
55+
{ contentOnly: true }
5556
)
5657
continue
5758
}
@@ -70,7 +71,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
7071
s.overwrite(
7172
index,
7273
index + exp.length,
73-
`new URL(${JSON.stringify(builtUrl)}, self.location)`
74+
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
75+
{ contentOnly: true }
7476
)
7577
}
7678
if (s) {

packages/vite/src/node/plugins/define.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
130130
const start = match.index
131131
const end = start + match[0].length
132132
const replacement = '' + replacements[match[1]]
133-
s.overwrite(start, end, replacement)
133+
s.overwrite(start, end, replacement, { contentOnly: true })
134134
}
135135

136136
if (!hasReplaced) {

packages/vite/src/node/plugins/html.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
266266
s.overwrite(
267267
src!.value!.loc.start.offset,
268268
src!.value!.loc.end.offset,
269-
`"${config.base + url.slice(1)}"`
269+
`"${config.base + url.slice(1)}"`,
270+
{ contentOnly: true }
270271
)
271272
}
272273

@@ -340,7 +341,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
340341
s.overwrite(
341342
p.value.loc.start.offset,
342343
p.value.loc.end.offset,
343-
`"${config.base + url.slice(1)}"`
344+
`"${config.base + url.slice(1)}"`,
345+
{ contentOnly: true }
344346
)
345347
}
346348
}
@@ -370,7 +372,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
370372
s.overwrite(
371373
styleNode.loc.start.offset,
372374
styleNode.loc.end.offset,
373-
`"__VITE_INLINE_CSS__${cleanUrl(id)}_${inlineModuleIndex}__"`
375+
`"__VITE_INLINE_CSS__${cleanUrl(id)}_${inlineModuleIndex}__"`,
376+
{ contentOnly: true }
374377
)
375378
}
376379

@@ -430,7 +433,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
430433
s.overwrite(
431434
value.loc.start.offset,
432435
value.loc.end.offset,
433-
`"${url}"`
436+
`"${url}"`,
437+
{ contentOnly: true }
434438
)
435439
} catch (e) {
436440
if (e.code !== 'ENOENT') {
@@ -442,9 +446,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
442446
// emit <script>import("./aaa")</script> asset
443447
for (const { start, end, url } of scriptUrls) {
444448
if (!isExcludedUrl(url)) {
445-
s.overwrite(start, end, await urlToBuiltUrl(url, id, config, this))
449+
s.overwrite(
450+
start,
451+
end,
452+
await urlToBuiltUrl(url, id, config, this),
453+
{ contentOnly: true }
454+
)
446455
} else if (checkPublicFile(url, config)) {
447-
s.overwrite(start, end, config.base + url.slice(1))
456+
s.overwrite(start, end, config.base + url.slice(1), {
457+
contentOnly: true
458+
})
448459
}
449460
}
450461

@@ -599,7 +610,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
599610
s.overwrite(
600611
match.index,
601612
match.index + full.length,
602-
cssTransformedCode
613+
cssTransformedCode,
614+
{ contentOnly: true }
603615
)
604616
}
605617
if (s) {

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
365365
resolve
366366
)
367367
str().prepend(importsString)
368-
str().overwrite(expStart, endIndex, exp)
368+
str().overwrite(expStart, endIndex, exp, { contentOnly: true })
369369
imports.forEach((url) => {
370370
url = url.replace(base, '/')
371371
importedUrls.add(url)
@@ -472,7 +472,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
472472
str().overwrite(
473473
expStart,
474474
expEnd,
475-
`import('${url}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))`
475+
`import('${url}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))`,
476+
{ contentOnly: true }
476477
)
477478
} else {
478479
const exp = source.slice(expStart, expEnd)
@@ -483,17 +484,24 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
483484
index
484485
)
485486
if (rewritten) {
486-
str().overwrite(expStart, expEnd, rewritten)
487+
str().overwrite(expStart, expEnd, rewritten, {
488+
contentOnly: true
489+
})
487490
} else {
488491
// #1439 export * from '...'
489-
str().overwrite(start, end, url)
492+
str().overwrite(start, end, url, { contentOnly: true })
490493
}
491494
}
492495
rewriteDone = true
493496
}
494497
}
495498
if (!rewriteDone) {
496-
str().overwrite(start, end, isDynamicImport ? `'${url}'` : url)
499+
str().overwrite(
500+
start,
501+
end,
502+
isDynamicImport ? `'${url}'` : url,
503+
{ contentOnly: true }
504+
)
497505
}
498506
})
499507
}
@@ -533,7 +541,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
533541
isExplicitImportRequired(url.slice(1, -1))
534542
) {
535543
needQueryInjectHelper = true
536-
str().overwrite(start, end, `__vite__injectQuery(${url}, 'import')`)
544+
str().overwrite(
545+
start,
546+
end,
547+
`__vite__injectQuery(${url}, 'import')`,
548+
{ contentOnly: true }
549+
)
537550
}
538551
}
539552
}
@@ -589,7 +602,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
589602
ssr
590603
)
591604
normalizedAcceptedUrls.add(normalized)
592-
str().overwrite(start, end, JSON.stringify(normalized))
605+
str().overwrite(start, end, JSON.stringify(normalized), {
606+
contentOnly: true
607+
})
593608
}
594609

595610
// update the module graph for HMR analysis.

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
165165
insertPreload
166166
)
167167
str().prepend(importsString)
168-
str().overwrite(expStart, endIndex, exp)
168+
str().overwrite(expStart, endIndex, exp, { contentOnly: true })
169169
if (!isEager) {
170170
needPreloadHelper = true
171171
}
@@ -176,7 +176,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
176176
needPreloadHelper = true
177177
const original = source.slice(expStart, expEnd)
178178
const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)`
179-
str().overwrite(expStart, expEnd, replacement)
179+
str().overwrite(expStart, expEnd, replacement, { contentOnly: true })
180180
}
181181

182182
// Differentiate CSS imports that use the default export from those that
@@ -191,7 +191,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
191191
!(bareImportRE.test(specifier) && !specifier.includes('/'))
192192
) {
193193
const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)
194-
str().overwrite(start, end, dynamicIndex > -1 ? `'${url}'` : url)
194+
str().overwrite(start, end, dynamicIndex > -1 ? `'${url}'` : url, {
195+
contentOnly: true
196+
})
195197
}
196198
}
197199

@@ -223,7 +225,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
223225
s.overwrite(
224226
match.index,
225227
match.index + isModernFlag.length,
226-
isModern
228+
isModern,
229+
{ contentOnly: true }
227230
)
228231
}
229232
return {
@@ -303,7 +306,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
303306
hasRemovedPureCssChunk = true
304307
}
305308

306-
s.overwrite(expStart, expEnd, 'Promise.resolve({})')
309+
s.overwrite(expStart, expEnd, 'Promise.resolve({})', {
310+
contentOnly: true
311+
})
307312
}
308313
}
309314
}
@@ -330,7 +335,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
330335
// main chunk is removed
331336
(hasRemovedPureCssChunk && deps.size > 0)
332337
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
333-
: `[]`
338+
: `[]`,
339+
{ contentOnly: true }
334340
)
335341
}
336342
}

packages/vite/src/node/plugins/workerImportMetaUrl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
155155
url = injectQuery(url, WORKER_FILE_ID)
156156
url = injectQuery(url, `type=${workerType}`)
157157
}
158-
s.overwrite(urlIndex, urlIndex + exp.length, JSON.stringify(url))
158+
s.overwrite(urlIndex, urlIndex + exp.length, JSON.stringify(url), {
159+
contentOnly: true
160+
})
159161
}
160162
if (s) {
161163
return {

packages/vite/src/node/server/middlewares/indexHtml.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ const processNodeUrl = (
6464
s.overwrite(
6565
node.value!.loc.start.offset,
6666
node.value!.loc.end.offset,
67-
`"${config.base + url.slice(1)}"`
67+
`"${config.base + url.slice(1)}"`,
68+
{ contentOnly: true }
6869
)
6970
} else if (
7071
url.startsWith('.') &&
@@ -82,7 +83,8 @@ const processNodeUrl = (
8283
`"${path.posix.join(
8384
path.posix.relative(originalUrl, '/'),
8485
url.slice(1)
85-
)}"`
86+
)}"`,
87+
{ contentOnly: true }
8688
)
8789
}
8890
}
@@ -123,7 +125,8 @@ const devHtmlHook: IndexHtmlTransformHook = async (
123125
s.overwrite(
124126
node.loc.start.offset,
125127
node.loc.end.offset,
126-
`<script type="module" src="${modulePath}"></script>`
128+
`<script type="module" src="${modulePath}"></script>`,
129+
{ contentOnly: true }
127130
)
128131
}
129132

packages/vite/src/node/ssr/ssrTransform.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ export async function ssrTransform(
158158
s.overwrite(
159159
node.start,
160160
node.start + 14 /* 'export default'.length */,
161-
`${ssrModuleExportsKey}.default =`
161+
`${ssrModuleExportsKey}.default =`,
162+
{ contentOnly: true }
162163
)
163164
}
164165
}
@@ -207,14 +208,16 @@ export async function ssrTransform(
207208
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
208209
}
209210
} else {
210-
s.overwrite(id.start, id.end, binding)
211+
s.overwrite(id.start, id.end, binding, { contentOnly: true })
211212
}
212213
},
213214
onImportMeta(node) {
214-
s.overwrite(node.start, node.end, ssrImportMetaKey)
215+
s.overwrite(node.start, node.end, ssrImportMetaKey, { contentOnly: true })
215216
},
216217
onDynamicImport(node) {
217-
s.overwrite(node.start, node.start + 6, ssrDynamicImportKey)
218+
s.overwrite(node.start, node.start + 6, ssrDynamicImportKey, {
219+
contentOnly: true
220+
})
218221
if (node.type === 'ImportExpression' && node.source.type === 'Literal') {
219222
dynamicDeps.add(node.source.value as string)
220223
}

0 commit comments

Comments
 (0)