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

Make Wasp work with tsconfig path aliases #2457

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
382dc59
Implement project references prototype
sodic Jan 9, 2025
79924a0
Add project reference to user code from server
sodic Jan 10, 2025
4d3f95d
Push todoApp/tsconfig updated for project references
sodic Jan 10, 2025
a4ab2dd
Enable aliases in the SDK
sodic Jan 15, 2025
3fd1830
Make path aliases fully work but its ugly
sodic Jan 16, 2025
91b6f80
Clean some things up
sodic Jan 17, 2025
ae5bcaf
Add clarifying comment for rootDir
sodic Jan 17, 2025
cf3cd30
Merge branch 'filip-project-references' into filip-path-aliases
sodic Jan 17, 2025
cf1dc52
Move path aliases logic into Haskell
sodic Jan 17, 2025
611e2c0
Remove naming and remove unused code
sodic Jan 17, 2025
804852b
Improve tsconfig error messages
sodic Jan 17, 2025
302f7cf
Small refactors and error message improvements
sodic Jan 17, 2025
fe2c7c3
Remove redundant space from template
sodic Jan 17, 2025
c1cd0da
Add small refactors
sodic Jan 20, 2025
74f3567
Change [String] back to FieldPath
sodic Jan 20, 2025
09731ab
Update e2e tests
sodic Jan 20, 2025
ef1008b
Add path aliases to waspc/examples/todoApp
sodic Jan 20, 2025
7f9e078
Merge branch 'main' into filip-project-references
sodic Jan 20, 2025
2b66ac8
Merge branch 'filip-project-references' into filip-path-aliases
sodic Jan 20, 2025
9cf176e
Update waspc.cabal, e2e-tests, and todoApp to v0.16.0
sodic Jan 20, 2025
b51a56f
Fix formatting
sodic Jan 20, 2025
f36b33a
Update headless tests for 0.16.0
sodic Jan 20, 2025
6cf7730
Remove redundant testing file
sodic Jan 20, 2025
871ea87
Remove another redundant testing file
sodic Jan 20, 2025
dda6c0a
Remove more redundant testing files
sodic Jan 20, 2025
2d4c5c9
Update e2e tests
sodic Jan 21, 2025
e9ad9c3
Merge branch 'filip-project-references' into filip-path-aliases
sodic Jan 21, 2025
f390cd1
Update headless tests and waspc.cabal
sodic Jan 21, 2025
e790ebd
Merge branch 'filip-project-references' into filip-path-aliases
sodic Jan 21, 2025
e3f89ad
Merge branch 'main' into filip-path-aliases
sodic Jan 21, 2025
73a96fa
Merge branch 'main' into filip-path-aliases
sodic Jan 27, 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
25 changes: 16 additions & 9 deletions waspc/data/Cli/templates/skeleton/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// =============================== IMPORTANT =================================
// This file is mainly used for Wasp IDE support.
//
// This file is only used for Wasp IDE support. You can change it to configure
// your IDE checks, but none of these options will affect the TypeScript
// compiler. Proper TS compiler configuration in Wasp is coming soon :)
// Wasp will compile your code with slightly different (less strict) compilerOptions.
// You can increase the configuration's strictness (e.g., by adding
// "noUncheckedIndexedAccess": true), but you shouldn't reduce it (e.g., by
// adding "strict": false). Just keep in mind that this will only affect your
// IDE support, not the actual compilation.
//
// Full TypeScript configurability is coming very soon :)
{
"compilerOptions": {
"module": "esnext",
// Needed because this is used as a project reference.
"composite": true,
"target": "esnext",
// We're bundling all code in the end so this is the most appropriate option,
// it's also important for autocomplete to work properly.
Expand All @@ -20,6 +27,7 @@
"dom.iterable",
"esnext"
],
"skipLibCheck": true,
"allowJs": true,
"typeRoots": [
// This is needed to properly support Vitest testing with jest-dom matchers.
Expand All @@ -32,10 +40,9 @@
// Source 2: https://github.com/testing-library/jest-dom/issues/546#issuecomment-1889884843
"node_modules/@types"
],
// Since this TS config is used only for IDE support and not for
// compilation, the following directory doesn't exist. We need to specify
// it to prevent this error:
// https://stackoverflow.com/questions/42609768/typescript-error-cannot-write-file-because-it-would-overwrite-input-file
"outDir": ".wasp/phantom"
}
"outDir": ".wasp/out/user"
},
"include": [
"src"
]
}
20 changes: 20 additions & 0 deletions waspc/data/Generator/templates/react-app/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "@tsconfig/vite-react/tsconfig.json",
"compilerOptions": {
// Temporary loosen the type checking until we can address all the errors.
"jsx": "preserve",
"allowJs": true,
"strict": false,
"skipLibCheck": true,
// Allow importing pages with the .tsx extension.
"allowImportingTsExtensions": true,
"noEmit": true,
},
"include": [
"src"
],
"references": [
// TODO: It would be better to inject this knowledge from Haskell.
{ "path": "../../../tsconfig.json" }
]
}
20 changes: 4 additions & 16 deletions waspc/data/Generator/templates/react-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
{
"extends": "@tsconfig/vite-react/tsconfig.json",
"compilerOptions": {
// Temporary loosen the type checking until we can address all the errors.
"jsx": "preserve",
"allowJs": true,
"strict": false,
// Allow importing pages with the .tsx extension.
"allowImportingTsExtensions": true,
},
"include": [
"src"
],
"files": [],
"references": [
{
"path": "./tsconfig.node.json"
}
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" },
]
}
}
10 changes: 7 additions & 3 deletions waspc/data/Generator/templates/react-app/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"noEmit": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
},
"include": ["vite.config.ts", "./src/ext-src/vite.config.ts"]
}
"include": [
"vite.config.ts",
"./src/ext-src/vite.config.ts"
]
}
10 changes: 9 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,9 @@
import { mergeConfig } from "vite";
import react from "@vitejs/plugin-react";
import { defaultExclude } from "vitest/config"
import { resolveProjectPath } from "wasp/dev"
import tsconfigPaths from 'vite-tsconfig-paths'


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

const defaultViteConfig = {
base: "{= baseDir =}",
plugins: [react()],
plugins: [
tsconfigPaths({
projects: ["./", resolveProjectPath("./")]
}),
react()
],
optimizeDeps: {
exclude: ['wasp']
},
Expand Down
1 change: 1 addition & 0 deletions waspc/data/Generator/templates/sdk/wasp/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { join as joinPaths } from 'path'
* .wasp/out/web-app directory. This function resolves a project root dir path
* to be relative to the `web-app` directory i.e. `../../../projectDirPath`.
*/
// TODO: The relative path should come from Haskell
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll either solve this for the next RC or create an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's old code, I think it's okay if we just create an issue for this.

export function resolveProjectPath(path: string): string {
return joinPaths('../../../', path)
}
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/sdk/wasp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{=! expose it here (which leaks it to our users). We could avoid this by =}
{=! using relative imports inside SDK code (instead of library imports), =}
{=! but I didn't have time to implement it. =}
"./ext-src/*": "./dist/ext-src/*.js",
"./src/*": "./dist/src/*.js",
Copy link
Contributor Author

@sodic sodic Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the output folder instead of renaming aliases, seemed less fiddly. This way, the generated code's structure matches the original.

It also has a nice side effect of showing SDK build errors with correct file mappings:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "renaming aliases", does that mean you'd search & replace src with ext-src in the SDK version of the files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, both there and in tsconfig paths.

{=! Used by our code, uncodumented (but accessible) for users. =}
"./operations/*": "./dist/operations/*",
{=! Used by our code, uncodumented (but accessible) for users. =}
Expand Down
11 changes: 5 additions & 6 deletions waspc/data/Generator/templates/sdk/wasp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@
// While this is great, we still want to dig deeper at some point to understand
// better why Vite HMR misbehaves when the SDK is recompiled: https://github.com/wasp-lang/wasp/issues/1934
"incremental": true,
// todo(filip): Only works with common js, see https://www.typescriptlang.org/tsconfig#paths and daily-article.
// "paths": {
// "@wasp/*": [
// "./*.js"
// ]
// }
"paths": {
{=# paths =}
"{= path =}": ["{= lookupLocation =}"],
{=/ paths =}
}
Comment on lines +47 to +50
Copy link
Contributor Author

@sodic sodic Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the user doesn't define any aliases, this file still has an empty object.

I thought that was cleaner than doing the conditional check, but feel free to disagree :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the compiler source code it doesn't seem like it will cause any issues 👍

},
"include": [
"."
Expand Down
12 changes: 11 additions & 1 deletion waspc/data/Generator/templates/server/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{={= =}=}}
import esbuild from 'rollup-plugin-esbuild'
import alias from '@rollup/plugin-alias';
import resolve from '@rollup/plugin-node-resolve';

export default [
createBundle('src/server.ts', 'bundle/server.js'),
Expand All @@ -17,12 +19,20 @@ function createBundle(inputFilePath, outputFilePath) {
sourcemap: true,
},
plugins: [
resolve(),
alias({
entries: [
{=# aliases =}
{ find: '{= find =}', replacement: '{= replacement =}' },
{=/ aliases =}
]
Comment on lines +24 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: Just like paths in sdk/tsconfig, this field's value is an empty array when the user doesn't define any aliases.

}),
esbuild({
target: 'esnext',
}),
],
// We don't want to bundle any of the node_module deps
// as we want to keep them as external dependencies
external: (id) => !/^[./]/.test(id),
external: /node_modules/,
Copy link
Contributor Author

@sodic sodic Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo: leave a note about resolution, wasp, and bundling dependencies.

Todo: leave a note about why we're using alias and not some of the other plugins (check notes).

@infomiho I plan to explain this in detail. It's all in my notes.

}
}
29 changes: 12 additions & 17 deletions waspc/data/Generator/templates/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@
"compilerOptions": {
// Overriding this until we implement more complete TypeScript support.
"strict": false,
// When left unspecified, the 'rootDir' defaults to the longest common path of
// all non-declaration input files. TypeScript mimics the source structure
// inside the output directory (i.e., 'dist').

// This property currently doesn't matter because we haven't been running
// TSC on server code since https://github.com/wasp-lang/wasp/pull/1714.
//
// Since Wasp apps can (but don't have to) import user files from
// '../../../src', it makes the rootDir's default value inconsistent:
// - When the app doesn't import user files (e.g., the user didn't specify
// any operations), the rootDir is set to the server source dir (/src).
// - When the app imports user files (as is the case for most Wasp apps),
// the rootDir is set to the Wasp project root (../../../).
//
// Our build script (in package.json) requires a consistent structure of
// the output directory, which is why we always make sure to point the rootDir
// to the Wasp project root (../../../).
//
// See this comment for more details: https://github.com/wasp-lang/wasp/pull/1584#discussion_r1404019301
"rootDir": "../../../",
// When we start running TSC again (after we fix all current errors and
// make project references work for the TS config), then I believe the
// correct configuration is "rootDir": "." (the project reference should
// take care of the user code), but we should double-check.
"rootDir": ".",
// Overriding this because we want to use top-level await
"module": "esnext",
"target": "es2017",
Expand All @@ -36,5 +28,8 @@
},
"include": [
"src"
],
"references": [
{ "path": "../../../tsconfig.json" }
]
}
}
29 changes: 15 additions & 14 deletions 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.

Loading
Loading