Packaging multiple Typescript projects depending on the same common local module
I tried @matt-mccutchen's approach, but unfortunately, I couldn't get that to work with the VSTS build tasks due to the fact that these tasks require commonjs
:
"compilerOptions": {
"module": "commonjs",
"target": "es6",
But I did find a solution that works for me.
In the Tasks
folder I've added a tsconfig.json
which defines my default settings and includes the files from the Common library:
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"strict": false,
"strictNullChecks": false,
"removeComments": true
},
"files": [
"./Common/uuidv5.d.ts",
"./Common/Common.ts"
]
}
Then in each task I created a tsconfig.json
which sets the output folder to the current folder for that project and which inherits from the tsconfig.json
in the Tasks
folder:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./",
"sourceRoot": "./"
},
"files": [
"InstallExtension.ts"
]
}
This results in:
- MyExtension
- package.json // containing all dev-dependencies
- tslint.json
- Tasks
- tsconfig.json // Including Common by default
- Common
- common.ts // containing functionality shared across tasks
- package.json // containing all runtime dependencies for Common
- tsconfig.json // containing build configuration for just the common files, inherits from ..\Task\tsconfig.json
- My1stTask
- package.json // containing all prod-dependencies for the task
- task.ts // containing task implementation
- tsconfig.json // containing build configuration for the task, inherits from ..\Task\tsconfig.json
- ...
- ...
- My6thTask
- package.json // containing all prod-dependencies
- task.ts // containing task implementation
- tsconfig.json // containing build configuration for the task, inherits from ..\Task\tsconfig.json
When compiling a task the following
- My6thTask
- Common
- Common.js // Compiled common
- My6thTask
- task.js // Compiled task
- package.json // containing all prod-dependencies
- task.ts // containing task implementation
- task.json // defining the task UI
- tsconfig.json // containing build configuration for the task
The only thing I had to add to task.ts
is the following:
///<reference path="../Common/Common.ts"/>
import * as common from "../Common/Common";
And to change the execution handler in the task.json to point to the new location:
"execution": {
"Node": {
"target": "InstallExtension/InstallExtension.js", // was: InstallExtension.js
"argumentFormat": ""
}
}
And all seems fine :D. Combined with using glob-exec
I've been able to reduce the build time to less than a minute when building clean:
"initdev:npm": "npm install & glob-exec --parallel --foreach \"Tasks/*/tsconfig.json\" -- \"cd {{file.dir}} && npm install\"",
"compile:tasks": "glob-exec \"Tasks/*/tsconfig.json\" -- \"tsc -b {{files.join(' ')}}\"",
"lint:tasks": "glob-exec --parallel --foreach \"Tasks/*/tsconfig.json\" -- \"tslint -p {{file}}\"",