Skip to content

Commit

Permalink
Make Wasp TS Config projects work with project references (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic authored Jan 24, 2025
1 parent 51f1e90 commit d91b412
Show file tree
Hide file tree
Showing 26 changed files with 62 additions and 42 deletions.
4 changes: 2 additions & 2 deletions waspc/data/Generator/templates/react-app/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{={= =}=}}
{
"extends": "@tsconfig/vite-react/tsconfig.json",
"compilerOptions": {
Expand All @@ -14,7 +15,6 @@
"src"
],
"references": [
// TODO: It would be better to inject this knowledge from Haskell.
{ "path": "../../../tsconfig.json" }
{ "path": "{= srcTsConfigPath =}" }
]
}

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.

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.

2 changes: 1 addition & 1 deletion waspc/examples/todoApp/main.wasp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app todoApp {
wasp: {
version: "^0.15.0"
version: "^0.16.0"
},
title: "ToDo App",
// head: [],
Expand Down
6 changes: 1 addition & 5 deletions waspc/headless-test/examples/todoApp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@
// Source 2: https://github.com/testing-library/jest-dom/issues/546#issuecomment-1889884843
"node_modules/@types"
],
"outDir": ".wasp/out/user",
"paths" : {
"@util": ["./src/util.js"],
"@components/*": ["./src/components/*"],
}
"outDir": ".wasp/out/user"
},
"include": [
"src"
Expand Down
7 changes: 4 additions & 3 deletions waspc/src/Wasp/AppSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
import Data.List (find)
import Data.Maybe (fromMaybe, isJust)
import Data.Text (Text)
import StrongPath (Abs, Dir, File', Path', Rel, (</>))
import StrongPath (Abs, Dir, File, File', Path', Rel, (</>))
import Wasp.AppSpec.Action (Action)
import Wasp.AppSpec.Api (Api)
import Wasp.AppSpec.ApiNamespace (ApiNamespace)
Expand All @@ -49,7 +49,7 @@ import Wasp.AppSpec.Route (Route)
import Wasp.Env (EnvVar)
import Wasp.ExternalConfig.PackageJson (PackageJson)
import Wasp.Node.Version (oldestWaspSupportedNodeVersion)
import Wasp.Project.Common (WaspProjectDir)
import Wasp.Project.Common (SrcTsConfigFile, WaspProjectDir)
import Wasp.Project.Db.Migrations (DbMigrationsDir)
import qualified Wasp.Psl.Ast.Schema as Psl.Schema
import qualified Wasp.SemanticVersion as SV
Expand Down Expand Up @@ -89,7 +89,8 @@ data AppSpec = AppSpec
-- | Connection URL for a database used during development. If provided, generated app will
-- make sure to use it when run in development mode.
devDatabaseUrl :: Maybe String,
customViteConfigPath :: Maybe (Path' (Rel WaspProjectDir) File')
customViteConfigPath :: Maybe (Path' (Rel WaspProjectDir) File'),
srcTsConfigPath :: Path' (Rel WaspProjectDir) (File SrcTsConfigFile)
}

-- TODO: Make this return "Named" declarations?
Expand Down
20 changes: 18 additions & 2 deletions waspc/src/Wasp/Generator/WebAppGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Data.Maybe (fromJust)
import qualified FilePath.Extra as FP.Extra
import StrongPath
( Dir,
File,
File',
Path,
Path',
Expand Down Expand Up @@ -56,7 +57,7 @@ import Wasp.JsImport
makeJsImport,
)
import qualified Wasp.Node.Version as NodeVersion
import Wasp.Project.Common (dotWaspDirInWaspProjectDir, generatedCodeDirInDotWaspDir)
import Wasp.Project.Common (SrcTsConfigFile, dotWaspDirInWaspProjectDir, generatedCodeDirInDotWaspDir, waspProjectDirFromAppComponentDir)
import qualified Wasp.Project.Common as Project
import Wasp.Util ((<++>))

Expand All @@ -65,7 +66,7 @@ genWebApp spec = do
sequence
[ genFileCopy [relfile|README.md|],
genFileCopy [relfile|tsconfig.json|],
genFileCopy [relfile|tsconfig.app.json|],
genAppTsConfigJson spec,
genFileCopy [relfile|tsconfig.node.json|],
genFileCopy [relfile|netlify.toml|],
genPackageJson spec (npmDepsForWasp spec),
Expand All @@ -80,6 +81,21 @@ genWebApp spec = do
where
genFileCopy = return . C.mkTmplFd

genAppTsConfigJson :: AppSpec -> Generator FileDraft
genAppTsConfigJson spec = do
return $
C.mkTmplFdWithDstAndData
(C.asTmplFile [relfile|tsconfig.app.json|])
(C.asWebAppFile [relfile|tsconfig.app.json|])
( Just $
object
[ "srcTsConfigPath" .= SP.fromRelFile srcTsConfigPath
]
)
where
srcTsConfigPath :: Path' (Rel C.WebAppRootDir) (File SrcTsConfigFile) =
waspProjectDirFromAppComponentDir </> AS.srcTsConfigPath spec

genDotEnv :: AppSpec -> Generator [FileDraft]
-- Don't generate .env if we are building for production, since .env is to be used only for
-- development.
Expand Down
4 changes: 3 additions & 1 deletion waspc/src/Wasp/Project/Analyze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ constructAppSpec waspDir options externalConfigs parsedPrismaSchema decls = do
clientEnvVars <- readDotEnvClient waspDir

let packageJsonContent = EC._packageJson externalConfigs
srcTsConfigPath = EC._srcTsConfigPath externalConfigs

let appSpec =
AS.AppSpec
Expand All @@ -102,7 +103,8 @@ constructAppSpec waspDir options externalConfigs parsedPrismaSchema decls = do
AS.userDockerfileContents = maybeUserDockerfileContents,
AS.configFiles = configFiles,
AS.devDatabaseUrl = devDbUrl,
AS.customViteConfigPath = customViteConfigPath
AS.customViteConfigPath = customViteConfigPath,
AS.srcTsConfigPath = srcTsConfigPath
}

return $ runValidation ASV.validateAppSpec appSpec
Expand Down
21 changes: 15 additions & 6 deletions waspc/src/Wasp/Project/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ module Wasp.Project.Common
getSrcTsConfigInWaspProjectDir,
srcTsConfigInWaspLangProject,
srcTsConfigInWaspTsProject,
waspProjectDirFromAppComponentDir,
)
where

import StrongPath (Abs, Dir, File, File', Path', Rel, reldir, relfile, toFilePath, (</>))
import System.Directory (doesFileExist)
import Wasp.AppSpec.ExternalFiles (SourceExternalCodeDir, SourceExternalPublicDir)
import qualified Wasp.Generator.Common
import qualified Wasp.Generator.Common as G.Common

type CompileError = String

Expand Down Expand Up @@ -64,25 +65,33 @@ nodeModulesDirInWaspProjectDir :: Path' (Rel WaspProjectDir) (Dir NodeModulesDir
nodeModulesDirInWaspProjectDir = [reldir|node_modules|]

-- | NOTE: If you change the depth of this path, also update @waspProjectDirFromProjectRootDir@ below.
-- TODO: Hm this has different name than it has in Generator.
generatedCodeDirInDotWaspDir :: Path' (Rel DotWaspDir) (Dir Wasp.Generator.Common.ProjectRootDir)
generatedCodeDirInDotWaspDir :: Path' (Rel DotWaspDir) (Dir G.Common.ProjectRootDir)
-- TODO: We sometimes call this directory "ProjectRootDir" and sometimes
-- "GeneratedCodeDir". We should unify the naming (the latter is the beter
-- name).
generatedCodeDirInDotWaspDir = [reldir|out|]

-- | NOTE: If you change the depth of this path, also update @waspProjectDirFromProjectRootDir@ below.
buildDirInDotWaspDir :: Path' (Rel DotWaspDir) (Dir Wasp.Generator.Common.ProjectRootDir)
buildDirInDotWaspDir :: Path' (Rel DotWaspDir) (Dir G.Common.ProjectRootDir)
buildDirInDotWaspDir = [reldir|build|]

-- TODO: This backwards relative path relies on multiple forward relative path
-- definitions. We should find a better way to express it (e.g., by somehow
-- calculating it from existing definitions)
waspProjectDirFromAppComponentDir :: G.Common.AppComponentRootDir d => Path' (Rel d) (Dir WaspProjectDir)
waspProjectDirFromAppComponentDir = [reldir|../../../|]

-- | NOTE: This path is calculated from the values of @dotWaspDirInWaspProjectDir@,
-- @generatedCodeDirInDotWaspDir@ and @buildDirInDotWaspDir@., which are the three functions just above.
-- Also, it assumes @generatedCodeDirInDotWaspDir@ and @buildDirInDotWaspDir@ have same depth.
-- If any of those change significantly (their depth), this path should be adjusted.
waspProjectDirFromProjectRootDir :: Path' (Rel Wasp.Generator.Common.ProjectRootDir) (Dir WaspProjectDir)
waspProjectDirFromProjectRootDir :: Path' (Rel G.Common.ProjectRootDir) (Dir WaspProjectDir)
waspProjectDirFromProjectRootDir = [reldir|../../|]

dotWaspRootFileInWaspProjectDir :: Path' (Rel WaspProjectDir) File'
dotWaspRootFileInWaspProjectDir = [relfile|.wasproot|]

dotWaspInfoFileInGeneratedCodeDir :: Path' (Rel Wasp.Generator.Common.ProjectRootDir) File'
dotWaspInfoFileInGeneratedCodeDir :: Path' (Rel G.Common.ProjectRootDir) File'
dotWaspInfoFileInGeneratedCodeDir = [relfile|.waspinfo|]

packageJsonInWaspProjectDir :: Path' (Rel WaspProjectDir) (File PackageJsonFile)
Expand Down
10 changes: 6 additions & 4 deletions waspc/src/Wasp/Project/ExternalConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ import Wasp.Project.ExternalConfig.TsConfig (analyzeSrcTsConfigFile)

data ExternalConfigs = ExternalConfigs
{ _packageJson :: P.PackageJson,
_tsConfig :: T.TsConfig
_tsConfig :: T.TsConfig,
_srcTsConfigPath :: Path' (Rel WaspProjectDir) (File SrcTsConfigFile)
}
deriving (Show)

analyzeExternalConfigs ::
Path' Abs (Dir WaspProjectDir) ->
Path' (Rel WaspProjectDir) (File SrcTsConfigFile) ->
IO (Either [CompileError] ExternalConfigs)
analyzeExternalConfigs waspDir srcTsConfigFile = runExceptT $ do
analyzeExternalConfigs waspDir srcTsConfigPath = runExceptT $ do
packageJsonContent <- ExceptT $ analyzePackageJsonFile waspDir
tsConfigContent <- ExceptT $ analyzeSrcTsConfigFile waspDir srcTsConfigFile
tsConfigContent <- ExceptT $ analyzeSrcTsConfigFile waspDir srcTsConfigPath

return $
ExternalConfigs
{ _packageJson = packageJsonContent,
_tsConfig = tsConfigContent
_tsConfig = tsConfigContent,
_srcTsConfigPath = srcTsConfigPath
}
2 changes: 1 addition & 1 deletion waspc/src/Wasp/Project/WaspFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ findWaspFile waspDir = do
. (waspDir </>)
<$> find ((suffix `isSuffixOf`) . fromRelFile) files

fileNotFoundMessage = "Couldn't find the *.wasp or a *.wasp.ts file in the " ++ fromAbsDir waspDir ++ " directory"
fileNotFoundMessage = "Couldn't find a *.wasp or a *.wasp.ts file in directory " ++ fromAbsDir waspDir ++ " ."
bothFilesFoundMessage =
"Found both *.wasp and *.wasp.ts files in the project directory. "
++ "You must choose how you want to define your app (using Wasp or TypeScript) and only keep one of them."
Expand Down
1 change: 0 additions & 1 deletion waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ library
Wasp.Generator.NpmInstall
Wasp.Generator.NpmInstall.Common
Wasp.Generator.NpmInstall.InstalledNpmDepsLog
Wasp.Generator.ImportPathAlias
Wasp.Generator.SdkGenerator
Wasp.Generator.SdkGenerator.Auth.AuthFormsG
Wasp.Generator.SdkGenerator.Auth.EmailAuthG
Expand Down

0 comments on commit d91b412

Please sign in to comment.