Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update magic-string #10364

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"http-proxy": "^1.18.1",
"json5": "^2.2.1",
"launch-editor-middleware": "^2.6.0",
"magic-string": "^0.26.5",
"magic-string": "^0.26.6",
"micromatch": "^4.0.5",
"mlly": "^0.5.16",
"mrmime": "^1.0.1",
Expand Down
8 changes: 2 additions & 6 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ export function renderAssetUrlInJS(
typeof replacement === 'string'
? JSON.stringify(replacement).slice(1, -1)
: `"+${replacement.runtime}+"`
s.overwrite(match.index, match.index + full.length, replacementString, {
contentOnly: true
})
s.update(match.index, match.index + full.length, replacementString)
}

// Replace __VITE_PUBLIC_ASSET__5aa0ddc0__ with absolute paths
Expand All @@ -119,9 +117,7 @@ export function renderAssetUrlInJS(
typeof replacement === 'string'
? JSON.stringify(replacement).slice(1, -1)
: `"+${replacement.runtime}+"`
s.overwrite(match.index, match.index + full.length, replacementString, {
contentOnly: true
})
s.update(match.index, match.index + full.length, replacementString)
}

return s
Expand Down
10 changes: 4 additions & 6 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
// target so we use the global location here. It can be
// window.location or self.location in case it is used in a Web Worker.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Window/self
s.overwrite(
s.update(
index,
index + exp.length,
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`,
{ contentOnly: true }
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`
)
continue
}
Expand Down Expand Up @@ -111,11 +110,10 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
)
builtUrl = url
}
s.overwrite(
s.update(
index,
index + exp.length,
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
{ contentOnly: true }
`new URL(${JSON.stringify(builtUrl)}, self.location)`
)
}
if (s) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
const start = match.index
const end = start + match[0].length
const replacement = '' + replacements[match[1]]
s.overwrite(start, end, replacement, { contentOnly: true })
s.update(start, end, replacement)
}

if (!hasReplaced) {
Expand Down
28 changes: 7 additions & 21 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,10 @@ export function overwriteAttrValue(
}
const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0
const valueOffset = valueStart.index! + valueStart[0].length - 1
s.overwrite(
s.update(
sourceCodeLocation.startOffset + valueOffset + wrapOffset,
sourceCodeLocation.endOffset - wrapOffset,
newValue,
{ contentOnly: true }
newValue
)
return s
}
Expand Down Expand Up @@ -487,11 +486,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`
const hash = getHash(cleanUrl(id))
// will transform in `applyHtmlTransforms`
s.overwrite(
s.update(
styleNode.sourceCodeLocation!.startOffset,
styleNode.sourceCodeLocation!.endOffset,
`__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`,
{ contentOnly: true }
`__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`
)
}

Expand Down Expand Up @@ -546,16 +544,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
// emit <script>import("./aaa")</script> asset
for (const { start, end, url } of scriptUrls) {
if (!isExcludedUrl(url)) {
s.overwrite(
start,
end,
await urlToBuiltUrl(url, id, config, this),
{ contentOnly: true }
)
s.update(start, end, await urlToBuiltUrl(url, id, config, this))
} else if (checkPublicFile(url, config)) {
s.overwrite(start, end, toOutputPublicFilePath(url), {
contentOnly: true
})
s.update(start, end, toOutputPublicFilePath(url))
}
}

Expand Down Expand Up @@ -780,12 +771,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
s ||= new MagicString(result)
const { 0: full, 1: scopedName } = match
const cssTransformedCode = htmlProxyResult.get(scopedName)!
s.overwrite(
match.index,
match.index + full.length,
cssTransformedCode,
{ contentOnly: true }
)
s.update(match.index, match.index + full.length, cssTransformedCode)
}
if (s) {
result = s.toString()
Expand Down
29 changes: 8 additions & 21 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
if (!rewriteDone) {
let rewrittenUrl = JSON.stringify(file)
if (!isDynamicImport) rewrittenUrl = rewrittenUrl.slice(1, -1)
str().overwrite(start, end, rewrittenUrl, {
contentOnly: true
})
str().update(start, end, rewrittenUrl)
}
}
}
Expand All @@ -374,9 +372,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
!(bareImportRE.test(specifier) && !specifier.includes('/'))
) {
const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)
str().overwrite(start, end, isDynamicImport ? `'${url}'` : url, {
contentOnly: true
})
str().update(start, end, isDynamicImport ? `'${url}'` : url)
}
}

Expand Down Expand Up @@ -405,12 +401,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const s = new MagicString(code)
let match: RegExpExecArray | null
while ((match = re.exec(code))) {
s.overwrite(
match.index,
match.index + isModernFlag.length,
isModern,
{ contentOnly: true }
)
s.update(match.index, match.index + isModernFlag.length, isModern)
}
return {
code: s.toString(),
Expand Down Expand Up @@ -505,9 +496,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
hasRemovedPureCssChunk = true
}

s.overwrite(expStart, expEnd, 'Promise.resolve({})', {
contentOnly: true
})
s.update(expStart, expEnd, 'Promise.resolve({})')
}
}
}
Expand Down Expand Up @@ -582,11 +571,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
)
}

s.overwrite(
s.update(
markerStartPos,
markerStartPos + preloadMarkerWithQuote.length,
`[${renderedDeps.join(',')}]`,
{ contentOnly: true }
`[${renderedDeps.join(',')}]`
)
rewroteMarkerStartPos.add(markerStartPos)
}
Expand All @@ -598,11 +586,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
let markerStartPos = code.indexOf(preloadMarkerWithQuote)
while (markerStartPos >= 0) {
if (!rewroteMarkerStartPos.has(markerStartPos)) {
s.overwrite(
s.update(
markerStartPos,
markerStartPos + preloadMarkerWithQuote.length,
'void 0',
{ contentOnly: true }
'void 0'
)
}

Expand Down
9 changes: 1 addition & 8 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
typeof replacement === 'string'
? JSON.stringify(replacement).slice(1, -1)
: `"+${replacement.runtime}+"`
s.overwrite(
match.index,
match.index + full.length,
replacementString,
{
contentOnly: true
}
)
s.update(match.index, match.index + full.length, replacementString)
}
}
return result()
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
builtUrl = injectQuery(builtUrl, WORKER_FILE_ID)
builtUrl = injectQuery(builtUrl, `type=${workerType}`)
}
s.overwrite(
s.update(
urlIndex,
urlIndex + exp.length,
`new URL(${JSON.stringify(builtUrl)}, self.location)`,
{ contentOnly: true }
`new URL(${JSON.stringify(builtUrl)}, self.location)`
)
}

Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@ const devHtmlHook: IndexHtmlTransformHook = async (
if (module) {
server?.moduleGraph.invalidateModule(module)
}
s.overwrite(
s.update(
node.sourceCodeLocation!.startOffset,
node.sourceCodeLocation!.endOffset,
`<script type="module" src="${modulePath}"></script>`,
{ contentOnly: true }
`<script type="module" src="${modulePath}"></script>`
)
}

Expand Down
13 changes: 5 additions & 8 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,10 @@ async function ssrTransformScript(
)
} else {
// anonymous default exports
s.overwrite(
s.update(
node.start,
node.start + 14 /* 'export default'.length */,
`${ssrModuleExportsKey}.default =`,
{ contentOnly: true }
`${ssrModuleExportsKey}.default =`
)
}
}
Expand Down Expand Up @@ -247,16 +246,14 @@ async function ssrTransformScript(
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
}
} else {
s.overwrite(id.start, id.end, binding, { contentOnly: true })
s.update(id.start, id.end, binding)
}
},
onImportMeta(node) {
s.overwrite(node.start, node.end, ssrImportMetaKey, { contentOnly: true })
s.update(node.start, node.end, ssrImportMetaKey)
},
onDynamicImport(node) {
s.overwrite(node.start, node.start + 6, ssrDynamicImportKey, {
contentOnly: true
})
s.update(node.start, node.start + 6, ssrDynamicImportKey)
if (node.type === 'ImportExpression' && node.source.type === 'Literal') {
dynamicDeps.add(node.source.value as string)
}
Expand Down
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.