TypeScript: Exclude option is not respected in tsc
TypeScript Version: 1.8.7 1.8.0 1.7.5
Code
{
"exclude": ["node_modules"]
}
Expected behavior:
Shouldn’t build any .ts files in node_modules.
Actual behavior:
Builds files in node_modules. If target: es6 I get lots of SyntaxErrors in node.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 17 (7 by maintainers)
Commits related to this issue
- fix(typescript): Avoids packaging TypeScript sources to avoid .d.ts conflicts Per https://github.com/Microsoft/TypeScript/issues/7432 the TypeScript compiler will attempt to compile the TypeScript so... — committed to zsims/pact-node by zsims 6 years ago
- fix(typescript): Avoids packaging TypeScript sources to avoid .d.ts conflicts Per https://github.com/Microsoft/TypeScript/issues/7432 the TypeScript compiler will attempt to compile the TypeScript so... — committed to zsims/pact-node by zsims 6 years ago
I see your point, and I understand the reason. Still I believe something should be done.
So this becomes really inconvenient with node_modules written in typescript. I don’t want typescript touching my node_modules folder. Generating
.d.tsfiles doesn’t help either. Isn’t there a way to avoid this? Or should I not publish source files to npm?One thing to note,
excludeis about the compiler automatically loading all files from your folder when you runtscwith no file list. This does not impact in any shape of form howimportstatements are resolved. if the compiler seesimport * from "mod", and--moduleResolution nodethen it will try to findmodinnode_modulesand if it found it it will include it.if this is not what you want, use
--moduleResolution classic.I’ve seen this be problematic when you are using npm link. When npm link is used you will see both d.ts and .ts files in the node_modules folder for your particular dependency. Then the compiler will try and resolve the imports (and compile them) which would overwrite the d.ts files, which are input files.
I’ve been able to get around this by doing an npm pack (which honors the .npmignore file) and then extracting, and then linking the extracted package. Kind of a pain, but it works I guess.
if you exclude a folder (e.g.
node_modules) it will not be included in the compilation. However, if you have an import (e.g.import .. from "@angular\core") it is going to load the required file from the excluded folder. So i guess it is all about the errors you are getting. what are they…As I mentioned in https://github.com/Microsoft/TypeScript/issues/7432#issuecomment-193891886, and as mentioned in https://github.com/Microsoft/TypeScript/wiki/tsconfig.json:
excludeis just a convenience. it saves you from writing the names of all your files on the command linetsc a.ts b.ts c.ts ....Now you have an
import './b', this might have a side effect, i.e. a module augmentation (see https://github.com/Microsoft/TypeScript/wiki/What’s-new-in-TypeScript#augmenting-globalmodule-scope-from-modules) and that needs to be visible in your system. moreover, if you use any imported values e.g.import {a} from 'b', the compiler needs to know whatameans to be able to give you any meanigful type checking.