serverless-plugin-typescript icon indicating copy to clipboard operation
serverless-plugin-typescript copied to clipboard

Build shared .ts files

Open joshua-holly89 opened this issue 7 years ago • 3 comments
trafficstars

Hi!

I have shared code in my project, e.g. a folder structure like this: /shared shared1.ts shared2.ts /microservice1 /node_modules package.json microservice1.ts tsconfig.json serverless.yml /microservice2 /node_modules package.json microservice2.ts tsconfig.json serverless.yml

According to what I read it is not possible to pass a YAML file via command line to serverless deploy / offline start. So the current working directory has to be inside "/microservice1" or "/microservice2".

This results in not being able to build with error: 'File '...shared1.ts' is not under 'rootDir' '...microservice1'. 'rootDir' is expected to contain all source files.',

I also can not set rootDir in tsconfig.json to "../" since it is overwritten with default './'. So the only work around I see is to work with symlinks which is IMHO a little hacky.

So is there a possibility to build these shared files?

joshua-holly89 avatar Feb 16 '18 00:02 joshua-holly89

Is there a solution/workaround for this?

andrewoh531 avatar Aug 13 '19 12:08 andrewoh531

I only found a workaround:

  • Install serverless in the root folder of "/shared", "microservice1" and "microservice2"
  • in the package.json folder in "microservice1" and "microservice2" I have such a npm script "node ../node_modules/serverless/bin/serverless offline start"

joshua-holly89 avatar Aug 13 '19 16:08 joshua-holly89

Thanks @joshinator. I considered that path but my project has a backend, frontend, lambdas and shared folder and so I didn't want to corrupt the project by enveloping it with serverless configurations at the root.

The workaround I used was to use yarn link for the shared package and treat it like an independent package. I personally think this is a slightly cleaner approach.

~Unfortunately this only works for sls invoke local but errors out when attempting to run sls package. Looking to see if it can be fixed.~ This didn't work with serverless-plugin-typescript version 1.1.9. Downgrading to version 1.1.7 worked. Related to this issue.

andrewoh531 avatar Aug 14 '19 10:08 andrewoh531