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

Detecting server imports on the client #2442

Merged
merged 28 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
62257a1
Detect server imports WIP
infomiho Jan 8, 2025
1d038d8
Use generator. Cleanup.
infomiho Jan 9, 2025
4409c34
Add relative file path generation
infomiho Jan 14, 2025
92f11f7
e2e tests
infomiho Jan 14, 2025
153ece5
Fix todoApp server import
infomiho Jan 14, 2025
01e431e
"Going into production" docs update (#2373)
infomiho Jan 9, 2025
c1569bb
Fixes deployment docs links
infomiho Jan 9, 2025
ab9488d
Update client env vars import in docs
infomiho Jan 9, 2025
1da01dc
Update config.yml
Martinsos Jan 10, 2025
dcc2119
Deployment docs extras (#2443)
infomiho Jan 12, 2025
52653be
Add `react-dom` and `react-router-dom` deps validation (#2445)
infomiho Jan 13, 2025
ba85650
Validate env vars with Zod (#2362)
infomiho Jan 15, 2025
e42b9fa
Project dir computation change
infomiho Jan 17, 2025
577de0c
Merge branch 'main' into miho-detect-server-imports-on-client
infomiho Jan 20, 2025
1ac0354
Merge branch 'main' into miho-detect-server-imports-on-client
infomiho Feb 4, 2025
68767f9
Use `resolveId` hook instead of `transform`
infomiho Feb 6, 2025
7276406
Cleanup
infomiho Feb 6, 2025
5279cda
e2e tests
infomiho Feb 6, 2025
34fff43
Update path logic.
infomiho Feb 7, 2025
30e9ac3
Update tsconfig.node.json generation
infomiho Feb 7, 2025
4ad2972
e2e tests
infomiho Feb 7, 2025
2934492
PR comments
infomiho Feb 7, 2025
80cbb46
PR comments
infomiho Feb 7, 2025
dc17770
e2e tests
infomiho Feb 7, 2025
2f93d7e
e2e tests
infomiho Feb 8, 2025
fbadf45
Split Vite plugins into a separate module
infomiho Feb 8, 2025
f8515b7
PR comments
infomiho Feb 11, 2025
20d0d1e
e2e tests
infomiho Feb 11, 2025
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 waspc/data/Generator/templates/react-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.vite.json" },
]
}
16 changes: 16 additions & 0 deletions waspc/data/Generator/templates/react-app/tsconfig.vite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{={= =}=}}
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"noEmit": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
},
"include": [
{=# includePaths =}
"{= . =}",
{=/ includePaths =}
]
}
6 changes: 5 additions & 1 deletion waspc/data/Generator/templates/react-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { mergeConfig } from "vite";
import react from "@vitejs/plugin-react";
import { defaultExclude } from "vitest/config"
import { detectServerImports } from "./vite/detectServerImports"

{=# customViteConfig.isDefined =}
// Ignoring the TS error because we are importing a file outside of TS root dir.
Expand All @@ -15,7 +16,10 @@ const _waspUserProvidedConfig = {};

const defaultViteConfig = {
base: "{= baseDir =}",
plugins: [react()],
plugins: [
react(),
detectServerImports(),
],
optimizeDeps: {
exclude: ['wasp']
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{{={= =}=}}
import { type Plugin } from 'vite'
import path from 'path'

const waspProjectDirAbsPath = getWaspProjectDirAbsPathFromCwd()

export function detectServerImports(): Plugin {
return {
name: 'wasp-detect-server-imports',
enforce: 'pre',
resolveId(source, importer) {
if (!importer) {
return
}

const pathToUserCode = parsePathToUserCode(importer)
if (!pathToUserCode) {
return
}

if (isServerImport(source)) {
throw new Error(
`Server code cannot be imported in the client code. Import from "${source}" in "${pathToUserCode}" is not allowed.`
)
}
},
}
}

function isServerImport(moduleName: string): boolean {
return moduleName.startsWith('wasp/server')
}

type RelativePathToUserCode = string & { _brand: 'relativePathToUserCode' }

function parsePathToUserCode(
importerPath: string
): RelativePathToUserCode | null {
const importerPathRelativeToWaspProjectDir = path.relative(
waspProjectDirAbsPath,
importerPath
)
return importerPathRelativeToWaspProjectDir.startsWith('src/')
? (importerPathRelativeToWaspProjectDir as RelativePathToUserCode)
: null
}

// We can't pass the "waspProjectDir" path from Haskell because we need the absolute path:
// e.g. /Users/{username}/dev/wasp/waspc/examples/todoApp
// which contains machine specific info like the username which is different in the CI and locally.
// This breaks our e2e tests in the CI because the path is different.
function getWaspProjectDirAbsPathFromCwd(): string {
const webAppDirAbsPath = process.cwd()
return path.join(webAppDirAbsPath, '{= waspProjectDirFromWebAppDir =}')
}
3 changes: 2 additions & 1 deletion waspc/e2e-test/test-outputs/waspBuild-golden/files.manifest

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.

Loading
Loading