3 ms·
The best option right now for keeping relative imports clean is using the `paths` compiler option to create aliases. This works most of the time but in some cas
by michaeldwan 8y ago
The best option right now for keeping relative imports clean is using the `paths` compiler option to create aliases. This works most of the time but in some cases typescript won't rewrite paths in the javascript output so modules can't be resolved at runtime, so you'll need to use webpack or another tool in your build script to rewrite them.
See: https://decembersoft.com/posts/say-goodbye-to-relative-paths-in-typescript-imports/ https://decembersoft.com/posts/say-goodbye-to-relative-paths...
- danenania 8y agoCool, that looks like it could work for client-side code that gets run through webpack, but what about for a node-friendly library?
- frankpf 8y agoI use tsconfig's `paths` in combination with the `require-rewrite` npm module (https://github.com/IUnknown68/require-rewrite https://github.com/IUnknown68/require-rewrite). Here's an example: // tsconfig.json ... "paths": { "#src/*": ["src/*"], } ... // package.json ... "requireRewrite": { "map": [ [ "#src/", "dist/src/" ], ] } ... // src/path1/x.ts import { X } from '#src/path2/y.ts`
- johnny_reilly 8y agoIf you're using webpack with ts-loader you can use https://www.npmjs.com/package/tsconfig-paths-webpack-plugin https://www.npmjs.com/package/tsconfig-paths-webpack-plugin to create aliases in webpack. awesome-typescript-loader has the same functionality available too I believe