Spent a couple hours trying to figure out why my n...
# help
a
Spent a couple hours trying to figure out why my new dependency injection via Typescript decorators isn't working. Works in unit tests, but tracked down that metadata isn't getting emitted and it's esbuild's fault. I see a few threads about
emitDecoratorMetadata
here in slack and means of providing entire custom esbuild configurations. What is the easiest way to just get this metadata emitted under SST?
Tried Configure esbuild plugins . The first build attempts outputs :
Copy code
Building function src/stacks/cognito/pre-signup-lambda.handler

Error: There was a problem transpiling the Lambda handler: 
    at Object.bundle (/Users/adam/dev/casa/node_modules/@serverless-stack/core/dist/runtime/handler/node.js:211:23)
    at Object.bundle (/Users/adam/dev/casa/node_modules/@serverless-stack/core/dist/runtime/handler/handler.js:19:16)
    at new Function (/Users/adam/dev/casa/node_modules/@serverless-stack/resources/src/Function.ts:361:39)
    at new CognitoStack (/Users/adam/dev/casa/cloud/src/stacks/cognito/cognito.stack.ts:40:23)
    at Object.main (/Users/adam/dev/casa/cloud/src/stacks/index.ts:51:24)
    at Object.<anonymous> (/Users/adam/dev/casa/cloud/.build/run.js:94:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
The example in the PR that added support for esbuild plugins uses a different plugin in the test code, but that code no longer exists in the current version.
k
This worked for me:
Add a esbuild.js file in the root
Ignore the mjml thing that is something specific to my need
Than add this config to your functions
I am currently using the onica injector for my project also so let me know what you are changing it to really interested to see :)
Just noticed that you tried exactly this, weird that it is not working for you 😅
a
What do you use for DI? Im using inversify.
Would like to know more how you implemented DI. @Adam Fanello
a
@Kujtim Hoxha This PR has what I'm trying out. https://github.com/onicagroup/sailplane/pull/126 @Adrián Mouly That project ☝️
k
@Adam Fanello how does your tsconfig look like?
a
Project config
Root config
k
Hmm can you try to add the experimental options to your project level config I can see that you are probably inheriting from the root level but just guessing
a
I think the fact that it works for unit test, using ts-jest, means that it is properly configured.
k
Yeah very interesting…
a
Still stuck on this.
Copy code
export default function main(app: <http://sst.App|sst.App>): void {
  /*
   * App-level configuration
   */
  app.setDefaultFunctionProps({
    runtime: "nodejs14.x",
    architecture: Architecture.ARM_64,
    logRetention: RetentionDays.ONE_MONTH,
    memorySize: 256,
    tracing: Tracing.DISABLED, // x-ray
    bundle: {
      esbuildConfig: {
        keepNames: true, // preserve for @sailplane/injector
        plugins: "./esbuild-plugins.js",
      },
    },
  });
esbuild-plugins.js:
Copy code
const { esbuildDecorators } = require("@anatine/esbuild-decorators");
module.exports = [esbuildDecorators()];
Still same failure as start of thread. @Frank @thdxr? Guidance appreciated.
Added some console logging before the stack trace and found that the path to my plugins file was incorrect - needs to be relative to package root, not the source. Thus
Copy code
plugins: "./src/stacks/esbuild-plugins.js",
The thrown error when the build spawn fails tries to output stderr and stdout, but they are showing up as empty. The lack of a clue to the problem was the challenge here.
t
I wasn't sure whether to make it relative to srcpath or root, we're somewhat inconsistent about this
a
Either choice is valid. Real issue is lack of feedback. I see your code is trying, but stderr and stdout aren't returning anything.
There are two nearly identical plugins with a common maintainer. I can't really tell the difference, other then my build with neither takes 13 seconds, with esbuild-plugin-tsc takes 22 seconds, and with esbuild-decorators (the newer plugin) takes 27 seconds. 🤷
Ugh. esbuild-plugin-tsc apparently obtains its speed advantage by failing to emit the metadata.
f
Hey Adam, just checking in see if u got the decorators to work in the end.
a
Yes. 👍
Basically the way you have it documented. The relative path was my biggest blocker and I played with other plugins to see if something were faster; ended up sticking with esbuild-decorators
f
Ah I see. Just added a check for it and throws friendlier msg when it’s not found https://github.com/serverless-stack/serverless-stack/pull/1326
a
Helpful. 👍
f
haha sorry we couldn’t help u in time.. 🙏