Giter Site home page Giter Site logo

Comments (14)

mr-piratman avatar mr-piratman commented on May 16, 2024 25

this is working for me
https://www.npmjs.com/package/serverless-tscpaths

just
$ yarn add serverless-tscpaths -D

and in your serverless.yml

plugins:
  - serverless-plugin-typescript
  - serverless-tscpaths # should be after serverless-plugin-typescript

from serverless-plugin-typescript.

brothatru avatar brothatru commented on May 16, 2024 5

If you are using serverless-webpack (installed by default if you used sls create --template aws-nodejs-typescript)

In your webpack.config.js

// webpack.config.js
resolve: {
  extensions: ['.ts'],
  alias: {
    '@app': path.resolve(__dirname, './src'),
  },
},

// tsconfig.json
"baseUrl": "./src/",
"paths": {
    "@app/*": ["./*"]
},

Found in this thread

from serverless-plugin-typescript.

trungtin avatar trungtin commented on May 16, 2024 3

As I can see from that issue, this will be non-fixed from typescript team.

One alternative is using something like https://github.com/ilearnio/module-alias/, but because of each function is compiled separately so is there a way to prepend the register code require('module-alias/register') before every function?

from serverless-plugin-typescript.

kaveet avatar kaveet commented on May 16, 2024 2

I spent a bit of time looking into this, and here are the options as I see them:

  1. Use relative imports

  2. Switch to a bundler like Webpack with known support for module aliases and their resolution (e.g. serverless-webpack)

  3. Use something like module-alias in your functions to resolve these paths at runtime

    Workable, but you face issues like duplicating the aliases across two configs and remembering to register the modules in every file

  4. Rewrite aliased paths to relative ones in the emitted code

    There are several projects doing this for TypeScript, but many are outdated: tspath

I took a stab at implementing 3 and 4 as complementary Serverless plugins to this one (they feel out-of-scope for this plugin). I do think this is possible to solve for via plugins that rewrite the emitted code, but decided I'd personally rather use Webpack for the sake of time and stability. If anyone wants to pick up where I left off, here's what I learned:

  • Number 3 is certainly easier to implement, but comes with its own set of pitfalls (https://github.com/ilearnio/module-alias/#known-incompatibilities)
  • Hook into the same Serverless lifecycle events as this plugin does, but register your plugin after this one. You might have to do some juggling with the build paths to write out the modified code correctly
  • Read tsconfig.json into your plugin to eliminate duplicate configuration of aliases, especially if using something like module-alias

from serverless-plugin-typescript.

jonnyasmar avatar jonnyasmar commented on May 16, 2024 1

So, I solved this problem in a pretty trivial manner...

I got rid of serverless-plugin-typescript and created a bundle of my TS using Webpack. The resulting JS bundle is then deployed via sls deploy.

Simple solution, but doesn't leverage this plugin at all.

from serverless-plugin-typescript.

pie6k avatar pie6k commented on May 16, 2024

I have the same issue

from serverless-plugin-typescript.

leejh3224 avatar leejh3224 commented on May 16, 2024

I found out that baseUrl option in tsconfig.json also doesn't work.

So I replaced all the import statement with relative syntax :(

from serverless-plugin-typescript.

manuabhijit avatar manuabhijit commented on May 16, 2024

So, I solved this problem in a pretty trivial manner...

I got rid of serverless-plugin-typescript and created a bundle of my TS using Webpack. The resulting JS bundle is then deployed via sls deploy.

Simple solution, but doesn't leverage this plugin at all.

Please be more detailed. I am facing the same issue.

from serverless-plugin-typescript.

JackCuthbert avatar JackCuthbert commented on May 16, 2024

My understanding is that this issue is related to TypeScript and not this plugin. Typescript does indeed support module path aliases during compilation, but will not emit these aliased paths, see microsoft/TypeScript#10866.

Please feel free to re-open a new issue if you think it's still relevant 😄

from serverless-plugin-typescript.

ericmaicon avatar ericmaicon commented on May 16, 2024

@trungtin have you found a solution?

from serverless-plugin-typescript.

trungtin avatar trungtin commented on May 16, 2024

@ericmaicon nope, I switch to serverless-webpack for the moment.

from serverless-plugin-typescript.

joshdura avatar joshdura commented on May 16, 2024

Has anyone found a solution to this?

from serverless-plugin-typescript.

joaodfmota avatar joaodfmota commented on May 16, 2024

I'm using aliases with
https://github.com/AnomalyInnovations/serverless-bundle

from serverless-plugin-typescript.

karltaylor avatar karltaylor commented on May 16, 2024

I'm using aliases with
https://github.com/AnomalyInnovations/serverless-bundle

Can you elaborate @joaodfmota? I'm trying to find a simple solution, I've added the following to serverless.yml to no success

custom:
  bundle:
    stats: false
    aliases:
      - src: "./"

from serverless-plugin-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.