Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build ts to js #8

Open
ak1103dev opened this issue Oct 13, 2020 · 5 comments
Open

build ts to js #8

ak1103dev opened this issue Oct 13, 2020 · 5 comments
Labels
enhancement New feature or request hacktoberfest help wanted Extra attention is needed

Comments

@ak1103dev
Copy link
Contributor

I use absolute path for import library or utilities.
When I build ts to js, it cannot run js file because import with absolute path not found.
I think use webpack for build ts to js

@ak1103dev ak1103dev added enhancement New feature or request help wanted Extra attention is needed hacktoberfest labels Oct 13, 2020
@geraldsamosir
Copy link

@ak1103dev have you check the version of node is is it support for typescript?

@kasipavankumar
Copy link

Greetings.

If I'm not wrong, we can add paths to tsconfig.json which should resolve imports of the respective modules at build time.

For example:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
        "app/*": ["app/*"],
        "config/*": ["app/_config/*"]
    },
}

Reference

@kasipavankumar
Copy link

Greetings, again.

After digging deeper, it can be said that adding paths to tsconfig.json cannot solve this issue.

The Problem

Typescript compiler's expected behavior of not mapping import aliases was a growing debate on it's repository (#10866) but was closed under working as intended. Mapping import aliases by tsc was never intended in the first place.

The objective of this repository is to make use of Typescript with NodeJS and so features like import aliases to improve productivity, readability and maintainability of the codebase. So, using tsc for build process, renders the whole import aliases thing pointless. As @ak1103dev mentioned, using module bundler like Webpack is the way to go.

I can see the use of tsconfig-paths to handle mapping of import aliases during runtime in development, which I tried to use in the build script but that doesn't work.

"start": "ts-node-dev --respawn -r tsconfig-paths/register ./app/server.ts",

Possible Solution

I was able to solve this issue and make the production (build) version running without any errors with Webpack using ts-loader and tsconfig-paths-webpack-plugin.

Caveats (or not?)

Performance

Since Webpack is used to generate the production bundle, it crunches everything in a single (and possibly minified) file which then can be executed using node build/server.js. This can have performance benefits because, server has to handle only a single minified file.

And, if we think of a scenario where tsc had a feature of mapping import aliases, the production bundle would be divided in modules as written during development this means on every request, the modules have to be required (imports & exports) which can decrease (depending on how powerful the production server is) performance when handling requests.


@ak1103dev, please let me know your thoughts on this. I would be glad to make a PR using the Webpack solution.

@ak1103dev
Copy link
Contributor Author

@kasipavankumar the script is not error for running. but I want to build ts to js then run start js file for production.

@kasipavankumar
Copy link

Yes, sir. I get your point. I've tried to reproduce the issue locally.

There were two issues I've encountered:

  1. On npm run build, TypeScript compiler throws error of module not found.
  2. Even after using paths in tsconfig.json, the absolute are not being resolved in the production bundle.

So, I used Webpack to correctly resolve the relative imports at the build time.
On doing so, npm run build && npm run start:prod works properly without any import errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants