-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Enable npm workspace for user project and generated code #3159
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
3e2f284
e094a41
dda141b
1e8ad7d
544a5a9
7fd0290
c1a1da0
ac9bd17
a7d71d7
7a6441f
c3883b9
ca9bb9d
4ea1332
25b7e1c
ac348fe
d4c84a9
636434b
2173f34
063e210
e267c5b
641e6cd
6ef08f7
be519d2
3bf16e6
2fdca35
1a83660
99767ec
f2100ff
820ed7a
cca26c3
94e673f
a8b11c3
800688c
9454f77
beb8846
317c2f6
256c4ac
a931703
4657896
95db53f
0a691b3
13e891f
06d4bf3
7385ad1
b005124
2b7dcf4
7ae0b78
aeba0c0
c34d38e
45febe1
3036606
3ed37c7
6a6868f
e776b53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,47 @@ | ||
module Wasp.Generator.NpmWorkspaces | ||
( serverPackageName, | ||
toWorkspacesField, | ||
webAppPackageName, | ||
workspaces, | ||
workspaceGlobs, | ||
) | ||
where | ||
|
||
import Control.Exception (Exception (displayException)) | ||
import Data.List (sort) | ||
import StrongPath (Dir, Path, Posix, Rel, fromRelDirP, relDirToPosix, reldirP, (</>)) | ||
import Data.Either (fromRight) | ||
import StrongPath (Dir, Path, Path', Posix, Rel, (</>)) | ||
import qualified StrongPath as SP | ||
import qualified System.FilePath.Posix as FP.Posix | ||
import Wasp.AppSpec (AppSpec, isBuild) | ||
import Wasp.Project.Common (WaspProjectDir, buildDirInDotWaspDir, dotWaspDirInWaspProjectDir, generatedCodeDirInDotWaspDir) | ||
import Wasp.Generator.Common (ProjectRootDir) | ||
import Wasp.Project.Common | ||
( WaspProjectDir, | ||
buildDirInDotWaspDir, | ||
dotWaspDirInWaspProjectDir, | ||
generatedCodeDirInDotWaspDir, | ||
) | ||
|
||
-- | Returns the list of workspaces that should be included in the generated package.json file. Each | ||
-- | Returns the list of workspaces that should be included in the user's `package.json` file. Each | ||
-- workspace is a glob that matches all packages in a certain directory. | ||
-- The path syntax here is always POSIX, because Windows syntax does not allow globs, and `npm` | ||
-- prefers it. | ||
-- | ||
-- Order doesn't matter, but we sort the packages to ensure a deterministic order. Otherwise, we | ||
-- might inadvertently change the order and the compiler will complain about it in user's projects. | ||
workspaces :: [Path Posix (Rel WaspProjectDir) (Dir ())] | ||
workspaces = | ||
sort | ||
[ globFor generatedCodeDirInDotWaspDir, | ||
globFor buildDirInDotWaspDir | ||
-- TODO: Add SDK as a workspace: | ||
-- Currently a overly-zealous resolution makes an incompatible resolution for `@types/react` | ||
-- that would make the workspace installation fail. | ||
-- Review when we upgrade React 19 (#2482). | ||
] | ||
-- The glob syntax is POSIX-path-like, but not actually a path, so it's represented as a String. | ||
workspaceGlobs :: [String] | ||
workspaceGlobs = | ||
FP.Posix.dropTrailingPathSeparator . SP.fromRelDirP | ||
<$> [ makeGlobFromProjectRoot $ dotWaspDirInWaspProjectDir </> generatedCodeDirInDotWaspDir, | ||
makeGlobFromProjectRoot $ dotWaspDirInWaspProjectDir </> buildDirInDotWaspDir | ||
-- TODO: Add SDK as a workspace (#3233) | ||
] | ||
where | ||
globFor dir = | ||
forceRelDirToPosix (dotWaspDirInWaspProjectDir </> dir) | ||
</> packageWildcard | ||
packageWildcard = [reldirP|*|] | ||
makeGlobFromProjectRoot :: Path' (Rel WaspProjectDir) (Dir ProjectRootDir) -> Path Posix (Rel WaspProjectDir) (Dir a) | ||
makeGlobFromProjectRoot projectRootDir = (forceRelDirToPosix projectRootDir) </> packageWildcard | ||
|
||
forceRelDirToPosix = either nonPosixError id . relDirToPosix | ||
nonPosixError exception = | ||
error $ | ||
unlines | ||
[ "This should never happen: our paths should always be POSIX-compatible, but they're not.", | ||
displayException exception | ||
] | ||
-- We force this to be POSIX because Windows-style paths do not accept wildcard characters. | ||
packageWildcard = [SP.reldirP|*|] | ||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also something that probably doesn't need to be in StrongPath. When we're constructing wildcards, we're already in string teritorry, so there's no point in pretending we're dealing with real paths, discussing Posix etc. |
||
|
||
toWorkspacesField :: [Path Posix (Rel WaspProjectDir) (Dir ())] -> [String] | ||
toWorkspacesField = | ||
-- While the trailing slashes do not matter, we drop them because they will be user-visible in | ||
-- their `package.json`, and it is more customary without them. | ||
fmap (FP.Posix.dropTrailingPathSeparator . fromRelDirP) | ||
forceRelDirToPosix inputDir = fromRight (makeNonPosixError inputDir) $ SP.relDirToPosix inputDir | ||
makeNonPosixError inputDir = | ||
error $ | ||
"This should never happen: our paths should always be POSIX-compatible, but they're not. (Received: " | ||
++ show inputDir | ||
++ ")" | ||
|
||
serverPackageName :: AppSpec -> String | ||
serverPackageName = workspacePackageName "server" | ||
|
@@ -57,6 +50,8 @@ webAppPackageName :: AppSpec -> String | |
webAppPackageName = workspacePackageName "webapp" | ||
|
||
workspacePackageName :: String -> AppSpec -> String | ||
workspacePackageName baseName spec = "@wasp.sh/generated-" ++ baseName ++ "-" ++ mode | ||
workspacePackageName baseName spec = "@wasp.sh/generated-" ++ baseName ++ "-" ++ modeName | ||
where | ||
mode = if isBuild spec then "build" else "dev" | ||
modeName | ||
| isBuild spec = "build" | ||
| otherwise = "dev" |
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.
Do we lose something by not separating dev dependency conflicts from regular dependency conflicts?