tsc-util v1.4.2
tsc-util - Wraps Around tsc to Copy Declaration and JS Files
If you follow the practice of keep TypeScript source and output files in different directories (for example, the output goes to a dist folder), you might have run into the following situations:
- While you can write
*.tsfiles for the majority of the cases, some of the scripts are better written as*.jsand*.d.tsfiles. - When you run
tsc, you find that the*.tsfiles were copied, but none of the*.jsand*.d.tsfiles are. - You research online and found that
allowJsneeds to be enabled in order to also copy the*.jsfiles. - That works great, so you also try to enable
declarationin order to copy the*.d.tsfiles, but immediately this caused an error says thatdeclarationandallowUsare conflicting with each other and the issue is still open. - After you discovered the work around, it turns out that manually written
*.d.tswon't be copied.
This small utility is meant to support the development pattern of:
- Intermixing
*.tsand*.d.ts/*.jspair in the source directory. - Copy to an
distdirectory, with the*.d.tsand*.jscopied. - Any references to
distfoldr inside the*.jsfiles are normalized to removedist. - If you are using
ts-nodein the*.js, it's removed when copied todist. - If you are using
source-map-supportin the*.js, it's removed once copied todist.
As the *.d.ts and *.js files are manually copied, you do not need to enable allowJs in tsconfig.
Install
npm install -g tsc-util
Usage
Just run tsc-util (in place of tsc). Note that tsc-util currently doesn't handle any parameters that tsc does, i.e. it's not a pass through, even though it calls tsc for you.
There are two main patterns:
source-map-supportpattern - usedist/in theimport/require's.ts-nodepattern - use withoutdist/in theimport/require's.
source-map-support Usage Pattern
The source-map-support pattern is as follows:
- Try to include the files from the
distfolder, which includes the*.d.ts,*.js, and*.js.mapfiles. - use
source-map-supportto map back to the*.tsin the root folder.
With this pattern, you'll need to first compile dist folder before doing testing, and need to check in the dist folder if you need to npm install from git (instead of npm registry). The nice part of this pattern is that you are not including TypeScript as part of the run-time.
ts-node Usage Pattern
The ts-node pattern is as follows:
- Use
require('ts-node/register')in the start script. - all of the includes are referencing outside of the
distfolder, since we will be directly running viats-node.
With this pattern, you'll need to use ts-node as part of your development process. dist doesn't need to exist first prior to development, nor does dist needs to be checked in if you are npm install from git.