Skip to content
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"prettier": "^3.9.6",
"redis": "^6.2.1",
"signal-polyfill": "^0.2.2",
"typescript": "^5.9.0",
"typescript": "^7.0.2",
"vite": "^8.3.4",
"vitest": "^4.1.11",
"wrangler": "^4.129.0",
Expand Down
213 changes: 207 additions & 6 deletions pnpm-lock.yaml

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

73 changes: 37 additions & 36 deletions scripts/check-browser-imports.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import fs from "node:fs"
import path from "node:path"
import process from "node:process"
import ts from "typescript"
import {
isExportDeclaration,
isImportDeclaration,
isStringLiteral,
SyntaxKind,
} from "typescript/unstable/ast"
import { withTypeScriptSources } from "./typescript-source.mjs"

const repositoryRoot = path.resolve(import.meta.dirname, "..")
const browserSafeRoots = [
Expand All @@ -22,49 +27,45 @@ const browserSafeRoots = [
]
const forbiddenPackages = new Set(["mysql2", "pg", "redis", "ws"])

const violations = []
const visited = new Set()
const roots = process.argv.slice(2).length > 0 ? process.argv.slice(2) : browserSafeRoots
for (const root of roots) visit(path.resolve(repositoryRoot, root))
withTypeScriptSources(({ getSourceFile }) => {
const violations = []
const visited = new Set()
const roots = process.argv.slice(2).length > 0 ? process.argv.slice(2) : browserSafeRoots
for (const root of roots) visit(path.resolve(repositoryRoot, root))

if (violations.length > 0) {
process.stderr.write(`${violations.join("\n")}\n`)
process.exitCode = 1
}
if (violations.length > 0) {
process.stderr.write(`${violations.join("\n")}\n`)
process.exitCode = 1
}

function visit(filePath) {
if (visited.has(filePath)) return
visited.add(filePath)
const source = ts.createSourceFile(
filePath,
fs.readFileSync(filePath, "utf8"),
ts.ScriptTarget.Latest,
true,
)
for (const statement of source.statements) {
const specifier = valueImportSpecifier(statement)
if (specifier === undefined) continue
if (specifier.startsWith("node:") || forbiddenPackages.has(specifier)) {
violations.push(
`${path.relative(repositoryRoot, filePath)} imports ${specifier}, which the browser cannot load`,
)
continue
function visit(filePath) {
if (visited.has(filePath)) return
visited.add(filePath)
const source = getSourceFile(filePath)
for (const statement of source.statements) {
const specifier = valueImportSpecifier(statement)
if (specifier === undefined) continue
if (specifier.startsWith("node:") || forbiddenPackages.has(specifier)) {
violations.push(
`${path.relative(repositoryRoot, filePath)} imports ${specifier}, which the browser cannot load`,
)
continue
}
if (!specifier.startsWith(".")) continue
visit(resolveRelativeImport({ from: filePath, specifier }))
}
if (!specifier.startsWith(".")) continue
visit(resolveRelativeImport({ from: filePath, specifier }))
}
}
})

function valueImportSpecifier(statement) {
if (ts.isImportDeclaration(statement)) {
if (statement.importClause?.isTypeOnly) return undefined
if (!ts.isStringLiteral(statement.moduleSpecifier)) return undefined
if (isImportDeclaration(statement)) {
if (statement.importClause?.phaseModifier === SyntaxKind.TypeKeyword) return undefined
if (!isStringLiteral(statement.moduleSpecifier)) return undefined
return statement.moduleSpecifier.text
}
if (ts.isExportDeclaration(statement)) {
if (isExportDeclaration(statement)) {
if (statement.isTypeOnly) return undefined
if (!statement.moduleSpecifier || !ts.isStringLiteral(statement.moduleSpecifier))
return undefined
if (!statement.moduleSpecifier || !isStringLiteral(statement.moduleSpecifier)) return undefined
return statement.moduleSpecifier.text
}
return undefined
Expand Down
Loading