Adrián Mouly
08/07/2021, 7:59 AMpackage.json
file today, due most of them have totally different dependencies between each other.
How is supposed that I can do the same with SST?
For example, I have a bunch of lambdas under the user
service that uses the jsonwebtoken
dependency. Of course I don’t want to share that dependency with every other service.
Based on mono-repo examples, all shared a package.json
on the root, at the same level as the sst.json
file, then all the projects are built with the same dependencies, I guess.
Anybody else has any thoughts?thdxr
08/07/2021, 11:52 AMFrank
Frank
/
sst.json
libs/ <- CDK code
services/
users/
package.json
listUsers.js
getUser.js
posts/
package.json
listPosts.js
getPost.js
Frank
listUsers
and getUser
, make sure you set the srcPath
to users
Frank
listPosts
and getPost
, set the srcPath
to posts
Sam Hulick
08/07/2021, 3:48 PMSam Hulick
08/07/2021, 4:18 PMyarn init
at the root, and inside every “sub-repo” folder (let’s say you have services/service1
and services/service2
). then at the root level, you edit package.json and add "workspaces": ["services/*"]
yarn workspaces list
run at the root level will show the list of all the projects in services
. to add a package to service1, just do yarn workspace service1 add some-package
(e.g. yarn workspace <workspace name> <command>
)
the package you add will be written to service1's package.json, but it will physically exist in node_modules
at the root level (it’s hoisted up to the monorepo root). and inside service1
, you’d just import the package as if it existed in that current folder. yarn will figure out where to find itSam Hulick
08/07/2021, 4:22 PM$ yarn init -y
$ yarn set version berry
$ yarn config set nodeLinker node-modules # definitely do this, otherwise yarn will default to PnP mode which doesn't work with SST
$ yarn plugin import typescript # auto adds @types/* for packages you add
$ yarn plugin import interactive-tools # provides "yarn upgrade-interactive" command
and finally (if you do use Yarn 2), be sure to add the proper entries to your .gitignore (the second list, since you’re not using zero-installs): https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignoredAdrián Mouly
08/07/2021, 11:44 PMAdrián Mouly
08/07/2021, 11:44 PMthdxr
08/08/2021, 1:59 AMAdrián Mouly
08/08/2021, 2:13 AMAdrián Mouly
08/08/2021, 2:14 AMSam Hulick
08/08/2021, 2:45 AMAshishkumar Pandey
08/08/2021, 3:33 AMSam Hulick
08/08/2021, 5:48 PM.yarn/cache
instead), and you commit all of the package info to your repository. so while the repo can be large, the benefit is that your CI/CD doesn’t need to run yarn install --immutable
since the packages are already there and at the proper version. but lots of frameworks like SST & Serverless Framework don’t support this yet. there’s an esbuild plugin that enables PnP (https://github.com/yarnpkg/berry/tree/master/packages/esbuild-plugin-pnp) which does indeed work, but doesn’t seem to work within an SST project for some reasonAshishkumar Pandey
08/08/2021, 6:12 PMthdxr
08/08/2021, 6:13 PMthdxr
08/08/2021, 6:13 PMAshishkumar Pandey
08/08/2021, 6:13 PMSam Hulick
08/08/2021, 10:58 PMthdxr
08/08/2021, 11:00 PMthdxr
08/08/2021, 11:00 PMSam Hulick
08/08/2021, 11:00 PMthdxr
08/09/2021, 7:13 PMberry
now points to yarn 3thdxr
08/09/2021, 7:13 PMSam Hulick
08/09/2021, 7:16 PMyarn set version 2
might be a better idea. not sure how stable 3.x isSam Hulick
08/09/2021, 7:19 PMSam Hulick
08/09/2021, 7:19 PMthdxr
08/09/2021, 7:22 PMAshishkumar Pandey
08/09/2021, 7:30 PMSam Hulick
08/09/2021, 7:31 PMthdxr
08/09/2021, 7:32 PMtsc
does not seem compatible with yarn pnpthdxr
08/09/2021, 7:32 PMAshishkumar Pandey
08/09/2021, 7:33 PMAshishkumar Pandey
08/09/2021, 7:33 PMyarn plugin import typescript
, would that help?thdxr
08/09/2021, 7:34 PMyarn pnpify tsc
Sam Hulick
08/09/2021, 7:34 PMrequire('./.pnp.cjs').setup()
which makes it possible to `require`/`import` dependencies that would normally be found in node_modulesSam Hulick
08/09/2021, 7:35 PMthdxr
08/09/2021, 7:36 PMtsc
directly when running sst start
to make sure type checking passesSam Hulick
08/09/2021, 7:37 PMthdxr
08/09/2021, 7:37 PMthdxr
08/09/2021, 7:38 PMSam Hulick
08/09/2021, 7:38 PMSam Hulick
08/09/2021, 7:38 PMAshishkumar Pandey
08/09/2021, 7:39 PMAdrián Mouly
08/09/2021, 7:39 PMthdxr
08/09/2021, 7:42 PMAshishkumar Pandey
08/09/2021, 7:45 PM