1.0.3 • Published 3 years ago
tsc-alias-2 v1.0.3
tsc-alias-2
Alias tsc's (typescript's) output, including file extensions and node_modules aliasing! You can run your code directly in node or the browser with NO BUNDLING! Each output file maps exactly to one source file.
How
npm i -g tsc-alias-2 # or
npm i --save-dev tsc-alias-2You probably want outDir and rootDir in your tsconfig.json:
{
    "compilerOptions": {
        "outDir": "./dist/",
        "rootDir": "./src/"
    }
}Then just run tsc && tsc-alias-2 instead of your build step. It will map paths specified in the tsconfig as well.
Using the result
For the browser, put this in your index.html:
<script type="module">
    import { whatever } from './dist/index.mjs'
    alert(whatever())
</script>For node, it's just a
node ./dist/index.mjsWhy
Chrome performance profiling doesn't work very well with a bundle + sourcemap. It works much better with separate source files. I needed detailed perf on some code, so I made this.
- tsc-aliasdoesn't point to- node_modulespaths so you can't run in the browser at all! Node doesn't work either because the extensions were wrong. So- tsc-alias-2also renames- *.jsto- *.mjsso node expects it to be a module.
- digital-loukoum/tsc-esmseemed to have some bugs. This is largely based on it.
Library
You can also use import it if you have a js build script or something:
const tscAlias2 = require('tsc-alias-2')
// Checks the tsconfig and does subs:
tscAlias2.main()
// Pass in args directly instead:
tscAlias2.replaceAllImports('./dist/', [{ pattern: '@/*', replacement: './' }])1.0.3
3 years ago