Comments (14)
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.
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/*": ["./*"]
},
from serverless-plugin-typescript.
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.
I spent a bit of time looking into this, and here are the options as I see them:
-
Use relative imports
-
Switch to a bundler like Webpack with known support for module aliases and their resolution (e.g. serverless-webpack)
-
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
-
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.
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.
I have the same issue
from serverless-plugin-typescript.
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.
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.
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.
@trungtin have you found a solution?
from serverless-plugin-typescript.
@ericmaicon nope, I switch to serverless-webpack for the moment.
from serverless-plugin-typescript.
Has anyone found a solution to this?
from serverless-plugin-typescript.
I'm using aliases with
https://github.com/AnomalyInnovations/serverless-bundle
from serverless-plugin-typescript.
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)
- Deployed package contains deleted files
- Cannot locate handler error message is not sufficient enough HOT 1
- Feat. Add option for not compiling with tsc
- Use custom typescript compiler
- TypeError: Cannot read properties of undefined (reading 'split') HOT 5
- Using multiple runtimes and explicit artifact, receive Cannot access package artifact error HOT 4
- Never throws type errors HOT 1
- Example is missing type information HOT 1
- Serverless Offline does not compiles tsx files
- Uploading source maps to sentry during build HOT 2
- [BUG] This plugin borks something with package artifacts HOT 2
- Error [ERR_MODULE_NOT_FOUND]: Cannot find module HOT 4
- Fix for only one function deploy
- [BUG] Not able to deploy with serverless v2. HOT 2
- ESM Support for Node 18-plus? (.mts -> .mjs) HOT 1
- [BUG - Since 2.1.5] Compilation of tsconfig include files does not honour tsConfigFileLocation in serverless
- Throws EISDIR error when running deploy HOT 1
- Add support to new AWS Lambda Runtime nodejs20.x
- How to Trigger `.build` Directory Recompile / Watch with non *.ts Files HOT 2
- Package 'individually=true' not working when setup at function level and 'false' at service level HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serverless-plugin-typescript.