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

tsconfig-paths safe for production? #59

Open
bduron opened this issue Jul 31, 2018 · 8 comments
Open

tsconfig-paths safe for production? #59

bduron opened this issue Jul 31, 2018 · 8 comments

Comments

@bduron
Copy link

bduron commented Jul 31, 2018

Hi, is tsconfig-path safe to use in production?

And what about perfomance impact of runtime module resolution?

Do you have any insight on these topics?

Thank you for solving this annoying absolute path resolution problem!

@josecolella
Copy link

josecolella commented Sep 5, 2018

@bduron I've been using tsconfig-paths in a production environment, and I have had no problems with this module.

With regards to performance impact, I would minimal to none. I use tsconfig path in conjunction with webpack and basically substitutes the path with the path:

__webpack_require__(/*! @app/controllers/auth/login */ "./src/controllers/auth/login/index.ts");

I hope this helps. I don't have exact numbers for performance impact, but seeing the generated code, it just swaps out the path.

@sriramr98
Copy link

sriramr98 commented Feb 1, 2020

Any insights on how to use tsconfig-paths with tsc when building the project? tsconfig-paths works well with node and ts-node but not with tsc

@jonathangaldino
Copy link

jonathangaldino commented May 11, 2020

I ask the same, @sriramr98 . Tsc is able to map the and resolve the paths when building but, when I run the main file inside the /dist folder (my outDir), it complains about the paths.

@sriramr98
Copy link

sriramr98 commented May 11, 2020

@jonathangaldino after struggling a lot, the best way I found to achieve this is to use webpack in production and use the tsconfig-paths-webpack-plugin to make it work in production. It seems like a simple enough integration with webpack. I have settled with this approach for now .

@jonathangaldino
Copy link

jonathangaldino commented May 11, 2020

@jonathangaldino after struggling a lot, the best way I found to achieve this is to use webpack in production and use the tsconfig-paths-webpack-plugin to make it work in production. It seems like a simple enough integration with webpack. I have settled with this approach for now .

Do you have an example repo?

@mprinc
Copy link
Contributor

mprinc commented Oct 10, 2020

Any insights on how to use tsconfig-paths with tsc when building the project? tsconfig-paths works well with node and ts-node but not with tsc

Hi @sriramr98, can you provide a repo to reproduce the problem? Usually, the problem comes from the fact that after building your paths are different, according to the outDir parameter in the tsconfig.json

@jonaskello
Copy link
Member

jonaskello commented Oct 11, 2020

Not sure I really understand the questions but I'll try an answer:

When using tsconfig-paths-webpack-plugin the paths are actually replaced in the output so you can run that output with node and it should work.

When using tsc the paths are not replaced in the output. If you want to run the output with node you must load tsconfig-paths when starting node so the paths can be resolved at run-time. See the docs:

node -r tsconfig-paths/register main.js

You need to do this also in production. The alternative would be to replace the paths that tsc outputs. If I recall correctly there is some other library that does path replacement of tsc compilations. Don't remember the name of it but you should be able to google that :-)

@cadesalaberry
Copy link

cadesalaberry commented Jul 23, 2022

For the record, I ran into the same issue and ended up using the tsc-alias package to replace the alias names with their relative path in the outDir folder.

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "lib": [
            "ES6"
        ],
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "declaration": true,
        "composite": true,
        "rootDir": "./src",
        "outDir": "./dist",
        "baseUrl": "./src", // Make sure we can replace it by "dist" in the production code (with TS_NODE_BASEURL)
        "paths": {
            "@helpers/*": ["helpers/*"],
            "@middlewares/*": ["middlewares/*"],
            "@topics/*": ["topics/*"],
            "@app": ["app"]
        }
    },

    "ts-node": {
        "require": ["tsconfig-paths/register"]
    },

    "exclude": [
        "node_modules",
        "**/dist/*"
    ],

    "include": [
        "src/**/*",
    ],
}

package.json

{
  ...
  "main": "dist/index.js",
  "engines": {
    "node": "16"
  },
  "scripts": {
    "yarn": "yarn install --frozen-lockfile",
    "yarn:production": "yarn install --frozen-lockfile --production",
    "start:compiled": "TS_NODE_BASEURL=./dist node dist/index.js",
    "start:dev": "ts-node src",
    "build": "rm -rf ./dist && rm -f tsconfig.tsbuildinfo && npx tsc && tsc-alias -p tsconfig.json",
    "test": "jest"
  },
  "devDependencies": {
    ...
    "tsconfig-paths": "^3.9.0",
    "ts-jest": "^28.0.5",
    "ts-node": "^10.8.2",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    ...
    "tsc-alias": "^1.7.0"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants