Skip to content

Commit a212cd5

Browse files
Copilothi-ogawa
andauthored
fix(rsc): fix false detection of import.meta.viteRsc API inside comments and strings (#938)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
1 parent b2c00ed commit a212cd5

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

packages/plugin-rsc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"estree-walker": "^3.0.3",
4545
"magic-string": "^0.30.19",
4646
"periscopic": "^4.0.2",
47+
"strip-literal": "^3.1.0",
4748
"turbo-stream": "^3.1.0",
4849
"vitefu": "^1.1.1"
4950
},

packages/plugin-rsc/src/plugin.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { scanBuildStripPlugin } from './plugins/scan'
5555
import { validateImportPlugin } from './plugins/validate-import'
5656
import { vitePluginFindSourceMapURL } from './plugins/find-source-map-url'
5757
import { parseCssVirtual, toCssVirtual, parseIdQuery } from './plugins/shared'
58+
import { stripLiteral } from 'strip-literal'
5859

5960
const isRolldownVite = 'rolldownVersion' in vite
6061

@@ -702,10 +703,11 @@ export default function vitePluginRsc(
702703
if (!code.includes('import.meta.viteRsc.loadModule')) return
703704
const { server } = manager
704705
const s = new MagicString(code)
705-
for (const match of code.matchAll(
706+
for (const match of stripLiteral(code).matchAll(
706707
/import\.meta\.viteRsc\.loadModule\(([\s\S]*?)\)/dg,
707708
)) {
708-
const argCode = match[1]!.trim()
709+
const [argStart, argEnd] = match.indices![1]!
710+
const argCode = code.slice(argStart, argEnd).trim()
709711
const [environmentName, entryName] = evalValue(`[${argCode}]`)
710712
let replacement: string
711713
if (
@@ -973,10 +975,11 @@ export default assetsManifest.bootstrapScriptContent;
973975
assert(this.environment.name !== 'client')
974976
const output = new MagicString(code)
975977

976-
for (const match of code.matchAll(
978+
for (const match of stripLiteral(code).matchAll(
977979
/import\s*\.\s*meta\s*\.\s*viteRsc\s*\.\s*loadBootstrapScriptContent\(([\s\S]*?)\)/dg,
978980
)) {
979-
const argCode = match[1]!.trim()
981+
const [argStart, argEnd] = match.indices![1]!
982+
const argCode = code.slice(argStart, argEnd).trim()
980983
const entryName = evalValue(argCode)
981984
assert(
982985
entryName,
@@ -2087,11 +2090,12 @@ function vitePluginRscCss(
20872090
const output = new MagicString(code)
20882091
let importAdded = false
20892092

2090-
for (const match of code.matchAll(
2093+
for (const match of stripLiteral(code).matchAll(
20912094
/import\.meta\.viteRsc\.loadCss\(([\s\S]*?)\)/dg,
20922095
)) {
20932096
const [start, end] = match.indices![0]!
2094-
const argCode = match[1]!.trim()
2097+
const [argStart, argEnd] = match.indices![1]!
2098+
const argCode = code.slice(argStart, argEnd).trim()
20952099
let importer = id
20962100
if (argCode) {
20972101
const argValue = evalValue<string>(argCode)

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)