Reputation: 1535
I am currently trying to build my ts files into a single ts files. The issue I'm getting is that my code below isn't doing what I thought it would. I used sourceRoot to attempt to set the only place it could get the source from but that didn't work. I have also tried putting a . infront of the directory but it still pulls from everywhere :(
{
"compilerOptions": {
"target": "ES5",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"out": "www/js/app.js",
"sourceMap": true,
"sourceRoot": "www/app"
}
}
all files including those not inside of www/app build :(
for now I've moved back to manually specifying the files:
{
"compilerOptions": {
"target": "ES5",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"out": "www/js/app.js",
"sourceMap": true
},
"files": [
"./www/app/app.ts",
"./www/app/menu/menuController.ts",
"./www/app/playlists/playlistsController.ts"
]
}
is it possible to restrict the source directories to be only www/app?
Thanks
Upvotes: 26
Views: 62937
Reputation: 74800
This question is a bit older, I know. But some of the TypeScript compiler options mentioned here are not necessarily helpful to solve the question. For people searching rootDir
or similar (like me), it may be helpful to clarify the mentioned and the solution-relevant options.
✅ Use outFile
❌ Don't use out
(deprecated)
Background:
If you want to to build to a destination directory, choose outDir
. See compiler options for further infos.
✅ Use files
/include
/exclude
in tsconfig
❌ Don't use rootDir
Explanation:
The compiler finds all input files by
file
/include
/exclude
propertiesimport
statements ///<reference .. />
(should not matter so much anymore)If those options are not specified, all files in your TypeScript project root (given by tsconfig.json) will be included. The import
ed modules are automatically included by the compiler, regardless of file
/include
/exclude
- have a look at their FAQ. All input files combined are the envelope of files it will build. How to configure file
/include
/exclude
, see tsconfig.json docs, consider also @TSV's answer.
rootDir
controls the output directory structure alongside with outDir
, it is not used to specify the compiler input. Its usage is (quote):
For every input file (i.e. a .ts/.tsx file) it needs to generate an matching output (a .js/.jsx file). To figure out the file path of the generated output file it will chop off the "rootDir" from the input, then prepend the "outDir" to it.
Consequently rootDir
needs a directory that includes all your input sources from above, otherwise you get
error TS6059: File is not under 'rootDir' .. 'rootDir' is expected to contain all source files
If rootDir
is omitted, the compiler automatically calculates a suitable directory by considering all input files at hand. So it doesn't necessarily have to be set.
This option is only relevant with sourcemaps/debugging and can be omitted for the scope of the question. It is used, when your sources files are at a different location at runtime than at design time. The compiler will adjust the paths to the sources in the sourcemap file to match the paths at runtime (compiler options).
Hope, that clarifies things a bit.
Upvotes: 27
Reputation: 1145
Yes,it is possible. Please use rootDir
like 'rootDir': 'app'
, if www
is your root dir of your application.
rootDir
description from typescript compiler options:
Specifies the root directory of input files.
Upvotes: 24
Reputation: 51
The rootDir
is just used to control the output directory structure.
The output directory structure will be similar to your rootDir
's directory.
U can use the glob-like file patterns
to restrict your source directories:
* matches zero or more characters (excluding directory separators)
? matches any one character (excluding directory separators)
**/ recursively matches any subdirectory
Upvotes: 5
Reputation: 7641
According to the tsconfig schema:
"If no 'files' property is present in a tsconfig.json, the compiler defaults to including all files the containing directory and subdirectories. When a 'files' property is specified, only those files are included."
"If no 'files' property is present in a tsconfig.json, but an 'exclude' property is present, the compiler will exclude the files and folders specified in the 'exclude' property."
According to the description:
The "files" property cannot be used in conjunction with the "exclude" property. If both are specified then the "files" property takes precedence.
The issue 'support globs in tsconfig.json files property (or just file/directory exclusions)' reflects current situation.
Upvotes: 12