Skip to content

Commit 0dfc04a

Browse files
authored
fix(devtools): avoid direct import.meta access in rspack (#349)
* fix(devtools): avoid direct import.meta access in rspack * chore(changeset): add patch note for rspack import.meta fix
1 parent c6bbb90 commit 0dfc04a

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.changeset/rspack-import-meta.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools': patch
3+
---
4+
5+
Fix Rspack compatibility by avoiding direct `import.meta` access patterns and add a regression test to prevent reintroduction.

packages/devtools/src/components/source-inspector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const SourceInspector = () => {
8080
e.stopPropagation()
8181

8282
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
83-
const baseUrl = new URL(import.meta?.env?.BASE_URL ?? '/', location.origin)
83+
const baseUrl = new URL(import.meta.env?.BASE_URL ?? '/', location.origin)
8484
const url = new URL(
8585
`__tsd/open-source?source=${encodeURIComponent(
8686
highlightState.dataSource,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { readdirSync, readFileSync } from 'node:fs'
2+
import { extname, join } from 'node:path'
3+
import { describe, it } from 'vitest'
4+
5+
const sourceRoot = join(process.cwd(), 'src')
6+
const sourceExtensions = new Set(['.ts', '.tsx'])
7+
const forbiddenPatterns = [
8+
/import\.meta\?\./,
9+
/typeof\s+import\.meta(?!\.)/,
10+
/=\s*import\.meta(?!\.)/,
11+
]
12+
13+
const getSourceFiles = (dir: string): Array<string> => {
14+
const entries = readdirSync(dir, { withFileTypes: true })
15+
return entries.flatMap((entry) => {
16+
const path = join(dir, entry.name)
17+
if (entry.isDirectory()) {
18+
return getSourceFiles(path)
19+
}
20+
if (sourceExtensions.has(extname(path))) {
21+
return [path]
22+
}
23+
return []
24+
})
25+
}
26+
27+
describe('import.meta compatibility', () => {
28+
it('avoids direct import.meta access that breaks Rspack parsing', () => {
29+
const violations = getSourceFiles(sourceRoot).flatMap((path) => {
30+
const content = readFileSync(path, 'utf8')
31+
const lines = content.split('\n')
32+
return lines.flatMap((line, index) => {
33+
const hasViolation = forbiddenPatterns.some((pattern) =>
34+
pattern.test(line),
35+
)
36+
return hasViolation ? [`${path}:${index + 1}`] : []
37+
})
38+
})
39+
40+
if (violations.length > 0) {
41+
throw new Error(
42+
[
43+
'Found direct `import.meta` usage in devtools source.',
44+
'Rspack only supports property access on `import.meta`.',
45+
...violations,
46+
].join('\n'),
47+
)
48+
}
49+
})
50+
})

0 commit comments

Comments
 (0)