Is it possible to include a json file when using TypeScript with a tsconfig.json file?
JsonTypescriptJson Problem Overview
Is there any way to have the TypeScript compiler also copy content files that don't have a ts or tsx extension to the output directory?
For example, my tsconfig.json looks like:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": true,
"outDir": "../../dist",
"types": [
]
},
"include": [
"file1.ts",
"config.json"
],
"exclude": [
"../../node_modules"
]
}
I would like the config.json
to end up in my ../../dist directory, but this isn't happening. Is there no concept of a content file for TypeScript?
Json Solutions
Solution 1 - Json
This can be achieved by setting "resolveJsonModule": true
in the compiler options and adding "source-path/**/*.json"
to the include
array in the tsconfig.json
Solution 2 - Json
I was having the same problem and the solution for this was to add:
"source-path/**/*.json"
keeping into "includes" array:
"source-path/**/*"
Example:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"resolveJsonModule": true,
"incremental": true
},
"include":[
"src/**/*",
"src/**/*.json",
],
}
Solution 3 - Json
One workaround is to make the json file a normal js file, require/import it normally, and add "allowJs" : true
to "compilerOptions" in tsconfig.json.
Solution 4 - Json
No, the tsc
command's job is only to compile .ts files. It doesn't have the capabilities to do build-related tasks that a build system like gulp, grunt or webpack would handle.
For more information, see this similar question: https://stackoverflow.com/questions/38708200/angular-2-typescript-compiler-copy-html-and-css-files