-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Detecting server imports on the client #2442
base: main
Are you sure you want to change the base?
Changes from all commits
62257a1
1d038d8
4409c34
92f11f7
153ece5
01e431e
c1569bb
ab9488d
1da01dc
dcc2119
52653be
ba85650
e42b9fa
577de0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { type Plugin } from "vite"; | ||
import path from "path"; | ||
|
||
export function detectServerImports(): Plugin { | ||
return { | ||
name: 'wasp-detect-server-imports', | ||
transform(code, filePath) { | ||
const isInDotWaspFolder = filePath.includes("/.wasp/"); | ||
|
||
// We don't want to check for server imports in the Wasp | ||
// framework code. | ||
if (isInDotWaspFolder) { | ||
return; | ||
} | ||
|
||
const imports = getImportsFromCode(code); | ||
|
||
for (const imp of imports) { | ||
if (imp.moduleName.startsWith("wasp/server")) { | ||
throw new Error(getServerImportErrorMessage(imp, filePath)); | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
type Import = { | ||
importStatement: string; | ||
moduleName: string; | ||
} | ||
|
||
const importStatementRegex = /\s*import\s+(?:(?:[\w*\s{},]*)\s+from\s+)?(['"`])([^'"`]+)\1\s*/g; | ||
|
||
function* getImportsFromCode(code: string): Generator<Import> { | ||
const matches = code.matchAll(importStatementRegex); | ||
for (const match of matches) { | ||
yield { | ||
importStatement: match[0].trim(), | ||
moduleName: match[2], | ||
}; | ||
} | ||
} | ||
|
||
function getServerImportErrorMessage(imp: Import, filePath: string): string { | ||
return `Client module "${getRelativeFilePath(filePath)}" imports server code: | ||
|
||
${imp.importStatement} | ||
|
||
This is not supported in the client code.`; | ||
} | ||
|
||
const waspProjectDirAbsPath = getWaspProjectDirAbsPathFromCwd(); | ||
|
||
function getRelativeFilePath(filePath: string): string { | ||
return filePath.replace(waspProjectDirAbsPath, ""); | ||
} | ||
|
||
// We are not passing the waspProjectDir path from Haskell because | ||
// our e2e tests stop working. Becuase we need to absolute path of the | ||
// Wasp project directory, it contains things like the username of the | ||
// user running the tests, which is different on different machines. | ||
function getWaspProjectDirAbsPathFromCwd(): string { | ||
const webAppDirAbsPath = process.cwd(); | ||
const waspProjectDirAbsPath = path.join(webAppDirAbsPath, "../../../"); | ||
return waspProjectDirAbsPath; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't dive deep, just wanted to check, since this is regex, is there a chance of false positives? If so, in which situations, and are we ok with that? If not, can we do it better?