Skip to content

Commit

Permalink
Enable aliases in the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Jan 15, 2025
1 parent 4d3f95d commit a4ab2dd
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 16 deletions.
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",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./operations/*": "./dist/operations/*",
{=! Used by our code, uncodumented (but accessible) for users. =}
Expand Down
6 changes: 6 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
// 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,
"baseUrl": "{= baseUrl =}",
"paths": {
{=# paths =}
"{= key =}": [{=# value =}"{= . =}"{=/ value =}],
{=/ paths =}
}
// todo(filip): Only works with common js, see https://www.typescriptlang.org/tsconfig#paths and daily-article.
// "paths": {
// "@wasp/*": [
Expand Down
2 changes: 2 additions & 0 deletions waspc/src/Wasp/AppSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import Wasp.AppSpec.Query (Query)
import Wasp.AppSpec.Route (Route)
import Wasp.Env (EnvVar)
import Wasp.ExternalConfig.PackageJson (PackageJson)
import Wasp.ExternalConfig.TsConfig (TsConfig)
import Wasp.Node.Version (oldestWaspSupportedNodeVersion)
import Wasp.Project.Common (WaspProjectDir)
import Wasp.Project.Db.Migrations (DbMigrationsDir)
Expand All @@ -69,6 +70,7 @@ data AppSpec = AppSpec
prismaSchema :: Psl.Schema.Schema,
-- | The contents of the package.json file found in the root directory of the wasp project.
packageJson :: PackageJson,
tsConfig :: TsConfig,
-- | Absolute path to the directory containing the wasp project.
waspProjectDir :: Path' Abs (Dir WaspProjectDir),
-- | List of external code files (they are referenced/used in the declarations).
Expand Down
5 changes: 4 additions & 1 deletion waspc/src/Wasp/ExternalConfig/TsConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Data.Aeson
parseJSON,
)
import qualified Data.Aeson as Aeson
import Data.Map (Map)
import GHC.Generics (Generic)

data TsConfig = TsConfig
Expand All @@ -31,7 +32,9 @@ data CompilerOptions = CompilerOptions
lib :: !(Maybe [String]),
allowJs :: !(Maybe Bool),
typeRoots :: !(Maybe [String]),
outDir :: !(Maybe String)
outDir :: !(Maybe String),
baseUrl :: !(Maybe String),
paths :: !(Maybe (Map String [String]))
}
deriving (Show, Generic)

Expand Down
21 changes: 17 additions & 4 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import Control.Concurrent (newChan)
import Control.Concurrent.Async (concurrently)
import Data.Aeson (object)
import Data.Aeson.Types ((.=))
import qualified Data.Map as M
import Data.Maybe (isJust, mapMaybe)
import qualified Data.Text as T
import StrongPath
import qualified StrongPath as SP
import System.Exit (ExitCode (..))
Expand All @@ -26,6 +28,7 @@ import qualified Wasp.AppSpec.App.Dependency as AS.Dependency
import qualified Wasp.AppSpec.ExternalFiles as EC
import Wasp.AppSpec.Valid (getLowestNodeVersionUserAllows, isAuthEnabled)
import qualified Wasp.AppSpec.Valid as AS.Valid
import qualified Wasp.ExternalConfig.TsConfig as TC
import Wasp.Generator.Common
( ProjectRootDir,
makeJsonWithEntityData,
Expand Down Expand Up @@ -101,7 +104,7 @@ genSdk spec =
genFileCopy [relfile|dev/index.ts|],
genClientConfigFile,
genServerConfigFile spec,
genTsConfigJson,
genTsConfigJson spec,
genServerUtils spec,
genPackageJson spec,
genDbClient spec
Expand Down Expand Up @@ -260,17 +263,27 @@ genClientConfigFile = return $ C.mkTmplFdWithData relConfigFilePath tmplData

-- todo(filip): remove this duplication, we have almost the same thing in the
-- ServerGenerator.
genTsConfigJson :: Generator FileDraft
genTsConfigJson = do
genTsConfigJson :: AppSpec -> Generator FileDraft
genTsConfigJson spec = do
return $
C.mkTmplFdWithDstAndData
[relfile|tsconfig.json|]
[relfile|tsconfig.json|]
( Just $
object
[ "majorNodeVersion" .= show (SV.major NodeVersion.oldestWaspSupportedNodeVersion)
[ "majorNodeVersion" .= show (SV.major NodeVersion.oldestWaspSupportedNodeVersion),
"baseUrl" .= baseUrl,
"paths" .= paths
]
)
where
baseUrl = TC.baseUrl $ TC.compilerOptions $ AS.tsConfig spec
paths =
fmap (map pathEntryToObject . M.toList)
. TC.paths
. TC.compilerOptions
$ AS.tsConfig spec
pathEntryToObject (key, value) = object ["key" .= T.pack key, "value" .= value]

depsRequiredForAuth :: AppSpec -> [AS.Dependency.Dependency]
depsRequiredForAuth spec = maybe [] (const authDeps) maybeAuth
Expand Down
5 changes: 1 addition & 4 deletions waspc/src/Wasp/Generator/SdkGenerator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ sdkTemplatesDirInTemplatesDir :: Path' (Rel TemplatesDir) (Dir SdkTemplatesDir)
sdkTemplatesDirInTemplatesDir = [reldir|sdk/wasp|]

extSrcDirInSdkRootDir :: Path' (Rel SdkRootDir) (Dir GeneratedExternalCodeDir)
extSrcDirInSdkRootDir = [reldir|ext-src|]
extSrcDirInSdkRootDir = [reldir|src|]

relDirToRelFileP :: Path Posix (Rel d) Dir' -> Path Posix (Rel d) File'
relDirToRelFileP path = fromJust $ SP.parseRelFileP $ removeTrailingSlash $ SP.fromRelDirP path
Expand All @@ -73,9 +73,6 @@ relDirToRelFileP path = fromJust $ SP.parseRelFileP $ removeTrailingSlash $ SP.f
makeSdkImportPath :: Path Posix (Rel SdkRootDir) File' -> Path Posix (Rel s) File'
makeSdkImportPath path = [reldirP|wasp|] </> path

extCodeDirInSdkRootDir :: Path' (Rel SdkRootDir) Dir'
extCodeDirInSdkRootDir = [reldir|ext-src|]

clientTemplatesDirInSdkTemplatesDir :: Path' (Rel SdkTemplatesDir) (Dir ClientTemplatesDir)
clientTemplatesDirInSdkTemplatesDir = [reldir|client|]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,5 @@ extImportToJsImport extImport@(EI.ExtImport extImportName extImportPath) =
}
where
importPath = C.makeSdkImportPath $ dropExtensionFromImportPath $ extCodeDirP </> SP.castRel extImportPath
extCodeDirP = fromJust $ SP.relDirToPosix C.extCodeDirInSdkRootDir
extCodeDirP = fromJust $ SP.relDirToPosix C.extSrcDirInSdkRootDir
importName = GJI.extImportNameToJsImportName extImportName
10 changes: 5 additions & 5 deletions waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import qualified Wasp.Generator.WebSocket as AS.WS
genWebSockets :: AppSpec -> Generator [FileDraft]
genWebSockets spec
| AS.WS.areWebSocketsUsed spec =
sequence
[ genWebSocketServerIndex spec,
genFileCopy [relfile|client/webSocket/index.ts|],
genWebSocketProvider spec
]
sequence
[ genWebSocketServerIndex spec,
genFileCopy [relfile|client/webSocket/index.ts|],
genWebSocketProvider spec
]
| otherwise = return []
where
genFileCopy = return . C.mkTmplFd
Expand Down
2 changes: 2 additions & 0 deletions waspc/src/Wasp/Project/Analyze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ constructAppSpec waspDir options externalConfigs parsedPrismaSchema decls = do
clientEnvVars <- readDotEnvClient waspDir

let packageJsonContent = EC._packageJson externalConfigs
let tsConfigContent = EC._tsConfig externalConfigs

let appSpec =
AS.AppSpec
{ AS.decls = decls,
AS.prismaSchema = parsedPrismaSchema,
AS.packageJson = packageJsonContent,
AS.tsConfig = tsConfigContent,
AS.waspProjectDir = waspDir,
AS.externalCodeFiles = externalCodeFiles,
AS.externalPublicFiles = externalPublicFiles,
Expand Down

0 comments on commit a4ab2dd

Please sign in to comment.