Skip to content

Commit 23c9259

Browse files
authoredOct 7, 2022
chore: update magic-string (#10364)
1 parent 34f5be7 commit 23c9259

11 files changed

+45
-83
lines changed
 

‎packages/vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"http-proxy": "^1.18.1",
9898
"json5": "^2.2.1",
9999
"launch-editor-middleware": "^2.6.0",
100-
"magic-string": "^0.26.5",
100+
"magic-string": "^0.26.6",
101101
"micromatch": "^4.0.5",
102102
"mlly": "^0.5.16",
103103
"mrmime": "^1.0.1",

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ export function renderAssetUrlInJS(
9595
typeof replacement === 'string'
9696
? JSON.stringify(replacement).slice(1, -1)
9797
: `"+${replacement.runtime}+"`
98-
s.overwrite(match.index, match.index + full.length, replacementString, {
99-
contentOnly: true
100-
})
98+
s.update(match.index, match.index + full.length, replacementString)
10199
}
102100

103101
// Replace __VITE_PUBLIC_ASSET__5aa0ddc0__ with absolute paths
@@ -119,9 +117,7 @@ export function renderAssetUrlInJS(
119117
typeof replacement === 'string'
120118
? JSON.stringify(replacement).slice(1, -1)
121119
: `"+${replacement.runtime}+"`
122-
s.overwrite(match.index, match.index + full.length, replacementString, {
123-
contentOnly: true
124-
})
120+
s.update(match.index, match.index + full.length, replacementString)
125121
}
126122

127123
return s

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
6161
// target so we use the global location here. It can be
6262
// window.location or self.location in case it is used in a Web Worker.
6363
// @see https://developer.mozilla.org/en-US/docs/Web/API/Window/self
64-
s.overwrite(
64+
s.update(
6565
index,
6666
index + exp.length,
67-
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`,
68-
{ contentOnly: true }
67+
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`
6968
)
7069
continue
7170
}
@@ -111,11 +110,10 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
111110
)
112111
builtUrl = url
113112
}
114-
s.overwrite(
113+
s.update(
115114
index,
116115
index + exp.length,
117-
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
118-
{ contentOnly: true }
116+
`new URL(${JSON.stringify(builtUrl)}, self.location)`
119117
)
120118
}
121119
if (s) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
139139
const start = match.index
140140
const end = start + match[0].length
141141
const replacement = '' + replacements[match[1]]
142-
s.overwrite(start, end, replacement, { contentOnly: true })
142+
s.update(start, end, replacement)
143143
}
144144

145145
if (!hasReplaced) {

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

+7-21
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,10 @@ export function overwriteAttrValue(
216216
}
217217
const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0
218218
const valueOffset = valueStart.index! + valueStart[0].length - 1
219-
s.overwrite(
219+
s.update(
220220
sourceCodeLocation.startOffset + valueOffset + wrapOffset,
221221
sourceCodeLocation.endOffset - wrapOffset,
222-
newValue,
223-
{ contentOnly: true }
222+
newValue
224223
)
225224
return s
226225
}
@@ -487,11 +486,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
487486
js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`
488487
const hash = getHash(cleanUrl(id))
489488
// will transform in `applyHtmlTransforms`
490-
s.overwrite(
489+
s.update(
491490
styleNode.sourceCodeLocation!.startOffset,
492491
styleNode.sourceCodeLocation!.endOffset,
493-
`__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`,
494-
{ contentOnly: true }
492+
`__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`
495493
)
496494
}
497495

@@ -546,16 +544,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
546544
// emit <script>import("./aaa")</script> asset
547545
for (const { start, end, url } of scriptUrls) {
548546
if (!isExcludedUrl(url)) {
549-
s.overwrite(
550-
start,
551-
end,
552-
await urlToBuiltUrl(url, id, config, this),
553-
{ contentOnly: true }
554-
)
547+
s.update(start, end, await urlToBuiltUrl(url, id, config, this))
555548
} else if (checkPublicFile(url, config)) {
556-
s.overwrite(start, end, toOutputPublicFilePath(url), {
557-
contentOnly: true
558-
})
549+
s.update(start, end, toOutputPublicFilePath(url))
559550
}
560551
}
561552

@@ -780,12 +771,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
780771
s ||= new MagicString(result)
781772
const { 0: full, 1: scopedName } = match
782773
const cssTransformedCode = htmlProxyResult.get(scopedName)!
783-
s.overwrite(
784-
match.index,
785-
match.index + full.length,
786-
cssTransformedCode,
787-
{ contentOnly: true }
788-
)
774+
s.update(match.index, match.index + full.length, cssTransformedCode)
789775
}
790776
if (s) {
791777
result = s.toString()

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

+8-21
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
350350
if (!rewriteDone) {
351351
let rewrittenUrl = JSON.stringify(file)
352352
if (!isDynamicImport) rewrittenUrl = rewrittenUrl.slice(1, -1)
353-
str().overwrite(start, end, rewrittenUrl, {
354-
contentOnly: true
355-
})
353+
str().update(start, end, rewrittenUrl)
356354
}
357355
}
358356
}
@@ -374,9 +372,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
374372
!(bareImportRE.test(specifier) && !specifier.includes('/'))
375373
) {
376374
const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)
377-
str().overwrite(start, end, isDynamicImport ? `'${url}'` : url, {
378-
contentOnly: true
379-
})
375+
str().update(start, end, isDynamicImport ? `'${url}'` : url)
380376
}
381377
}
382378

@@ -405,12 +401,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
405401
const s = new MagicString(code)
406402
let match: RegExpExecArray | null
407403
while ((match = re.exec(code))) {
408-
s.overwrite(
409-
match.index,
410-
match.index + isModernFlag.length,
411-
isModern,
412-
{ contentOnly: true }
413-
)
404+
s.update(match.index, match.index + isModernFlag.length, isModern)
414405
}
415406
return {
416407
code: s.toString(),
@@ -505,9 +496,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
505496
hasRemovedPureCssChunk = true
506497
}
507498

508-
s.overwrite(expStart, expEnd, 'Promise.resolve({})', {
509-
contentOnly: true
510-
})
499+
s.update(expStart, expEnd, 'Promise.resolve({})')
511500
}
512501
}
513502
}
@@ -582,11 +571,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
582571
)
583572
}
584573

585-
s.overwrite(
574+
s.update(
586575
markerStartPos,
587576
markerStartPos + preloadMarkerWithQuote.length,
588-
`[${renderedDeps.join(',')}]`,
589-
{ contentOnly: true }
577+
`[${renderedDeps.join(',')}]`
590578
)
591579
rewroteMarkerStartPos.add(markerStartPos)
592580
}
@@ -598,11 +586,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
598586
let markerStartPos = code.indexOf(preloadMarkerWithQuote)
599587
while (markerStartPos >= 0) {
600588
if (!rewroteMarkerStartPos.has(markerStartPos)) {
601-
s.overwrite(
589+
s.update(
602590
markerStartPos,
603591
markerStartPos + preloadMarkerWithQuote.length,
604-
'void 0',
605-
{ contentOnly: true }
592+
'void 0'
606593
)
607594
}
608595

‎packages/vite/src/node/plugins/worker.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
356356
typeof replacement === 'string'
357357
? JSON.stringify(replacement).slice(1, -1)
358358
: `"+${replacement.runtime}+"`
359-
s.overwrite(
360-
match.index,
361-
match.index + full.length,
362-
replacementString,
363-
{
364-
contentOnly: true
365-
}
366-
)
359+
s.update(match.index, match.index + full.length, replacementString)
367360
}
368361
}
369362
return result()

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
141141
builtUrl = injectQuery(builtUrl, WORKER_FILE_ID)
142142
builtUrl = injectQuery(builtUrl, `type=${workerType}`)
143143
}
144-
s.overwrite(
144+
s.update(
145145
urlIndex,
146146
urlIndex + exp.length,
147-
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
148-
{ contentOnly: true }
147+
`new URL(${JSON.stringify(builtUrl)}, self.location)`
149148
)
150149
}
151150

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,10 @@ const devHtmlHook: IndexHtmlTransformHook = async (
184184
if (module) {
185185
server?.moduleGraph.invalidateModule(module)
186186
}
187-
s.overwrite(
187+
s.update(
188188
node.sourceCodeLocation!.startOffset,
189189
node.sourceCodeLocation!.endOffset,
190-
`<script type="module" src="${modulePath}"></script>`,
191-
{ contentOnly: true }
190+
`<script type="module" src="${modulePath}"></script>`
192191
)
193192
}
194193

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,10 @@ async function ssrTransformScript(
196196
)
197197
} else {
198198
// anonymous default exports
199-
s.overwrite(
199+
s.update(
200200
node.start,
201201
node.start + 14 /* 'export default'.length */,
202-
`${ssrModuleExportsKey}.default =`,
203-
{ contentOnly: true }
202+
`${ssrModuleExportsKey}.default =`
204203
)
205204
}
206205
}
@@ -247,16 +246,14 @@ async function ssrTransformScript(
247246
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
248247
}
249248
} else {
250-
s.overwrite(id.start, id.end, binding, { contentOnly: true })
249+
s.update(id.start, id.end, binding)
251250
}
252251
},
253252
onImportMeta(node) {
254-
s.overwrite(node.start, node.end, ssrImportMetaKey, { contentOnly: true })
253+
s.update(node.start, node.end, ssrImportMetaKey)
255254
},
256255
onDynamicImport(node) {
257-
s.overwrite(node.start, node.start + 6, ssrDynamicImportKey, {
258-
contentOnly: true
259-
})
256+
s.update(node.start, node.start + 6, ssrDynamicImportKey)
260257
if (node.type === 'ImportExpression' && node.source.type === 'Literal') {
261258
dynamicDeps.add(node.source.value as string)
262259
}

‎pnpm-lock.yaml

+12-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.