grunt-ts
grunt-ts copied to clipboard
TypeScript needs a `baseDir` option
Say we have the JS structure:
ts
a.ts
foo
| b.ts
| c.ts
js
a.js
foo
| b.js
| c.js
If we compile with all three files with outDir set to js
its all okay. But if we only compile b.ts
or c.ts
with outDir set to js
then we would get:
js
| b.js
| c.js
Workaround is to create a temporary file under ts
called ignoreBaseDirFile.ts
and pass it to the tsc
compiler
Reported to TS team : https://typescript.codeplex.com/workitem/2297
Instead of having the user specify the baseDir
manually we can simply expand src
and find the most common dir and assume that as the baseDir
when fast compiling.
Love the idea! I think we should also have a way to override that for safety. In a large enough Org I can see people getting confused if the basedir changes on them.
In a large enough Org I can see people getting confused if the basedir changes on them.
v1.9.2 has baseDir
determined if user hasn't specified.
This is cool!
Maybe make it a short dot file name? Like.baseDir.ts
as ignoreBaseDirFile.ts
is so bulky and regular.
When I use this I end up with a ignoreBaseDirFile.js.map
, ignoreBaseDirFile.js
and ignoreBaseDirFile.d.ts
in my output directory.
Maybe we can delete those after compilation?
Like.baseDir.ts as ignoreBaseDirFile.ts is so bulky and regular.
My bad. I'll do .baseDir.d.ts
and see how that goes (or revert to .baseDir.ts
) . I'll put it in a minor version update (since the user options don't change).
Another Idea :
A better solution would be to figure out the correct outDir
option based on the changed files e.g in the given sample above it can be js/foo
. But that involves more work than I want to do right now. I'd rather wait and see typescript have a native baseDir
option
Cool, all fine by me.
I'm not really using it explicitly (atm), just as-is with the fast option.
I'll do .baseDir.d.ts
a .d.ts
does not play a role in determining the outDir inside the compiler (I sort of suspected that). Falling back to .baseDir.ts
I'm a little confused. From the first post above, when I've set fast:always
, I expected that your workaround would result in a JS folder tree that mirrors the TS tree (as it does when fast:never
is set). But instead, I just get the following when I modify b.ts
(newly generated files indicated):
ts
a.ts
foo
| .baseDir.ts [new]
| b.js [new]
| b.js.map [new]
| b.ts
| c.ts
js
a.js
foo
| b.js
| c.js
I was expecting the b.js file would be replaced in the JS file tree. What have I missed?
@marcuswhit baseDir
(source) is only useful if an outDir
(destination) is specified
@basarat Ok, it seems with baseDir
correctly set, the generated JS is placed in the correct target JS folder, but it's also created in the TS folder, as below. Given a full build doesn't create the extra js files in the TS folder, why does the fast compile do so?
ts
.baseDir.ts [new]
a.ts
foo
| b.js [new]
| b.js.map [new]
| b.ts
| c.ts
js
a.js
foo
| b.js [new]
| b.js.map [new]
| c.js
why does the fast compile do so
It shouldn't. Are you sure its grunt-ts
and not visualstudio / webstorm doing that? Can you share the sample?
Ah yes, my dumb mistake. Seems VS is compiling on save, even though I've explicitly set the option not to.
Next question - I have a grunt-watch task which monitors my TS folder and then kicks off my regular ts:dev task. I'm not using your inbuilt watch option. Should fast compilation work?
- When I set
fast:'watch'
it always clears the cache and recompiles everything. - When I set
fast:'always'
, it does correctly compile just the changed file, but only works when the class has no dependencies. When a class has dependencies on other classes, it doesn't seem to know about previously compiled classes, so throws a bunch of errors complaining that the dependencies don't exist.
What have I missed?
Thanks.
Should fast compilation work?
yes.
I am assuming you've already see https://github.com/grunt-ts/grunt-ts/issues/96#issuecomment-38987023
When I set fast:'watch' it always clears the cache and recompiles everything.
It should only do it the first time after you start grunt
When I set fast:'always', it does correctly compile just the changed file, but only works when the class has no dependencies
It only compiles the changed file and does no dependency tracking. This is a deliberate decision because if it did dependency tracking it seems very likely that it will end up compiling everything since it is after all one project (a tree with all files as leaves).
Doesn't work for me. I have setup like above and grunt-ts ends up creating:
ts/foo/.baseDir.ts
js/.baseDir.ts
js/b.js
Seems like it works when I create empty.ts
.
This isn't working for me either; and I haven't found a workaround :(
I think it may be because my original typescript code is in a symlinked directory... but don't have time to explore a lot more right now.
I have:
ldixon@l:~/code/uproxy/uproxy-lib$ ls -l build/typescript-src/
total 0
lrwxrwxrwx 1 ldixon eng 22 Aug 28 23:38 arraybuffers -> ../../src/arraybuffers
lrwxrwxrwx 1 ldixon eng 16 Aug 28 23:38 crypto -> ../../src/crypto
lrwxrwxrwx 1 ldixon eng 17 Aug 28 23:38 freedom -> ../../src/freedom
...
And every time I compile the code in array buffers with ts rule like this:
ts: {
arraybuffers: {
src: ['build/typescript-src/arraybuffers/**/*.ts',
'!**/*.d.ts',
'!**/.baseDir.ts'],
outDir: 'build/',
options: {
compile: true,
baseDir: 'build/typescript-src/',
sourceRoot: 'build/typescript-src/',
module: 'amd',
comments: true,
noImplicitAny: true
}
}
}
Then I end up with:
-rw-r----- 1 ldixon eng 2938 Aug 28 23:55 accumulator.js
-rw-r----- 1 ldixon eng 1660 Aug 28 23:55 accumulator.js.map
-rw-r----- 1 ldixon eng 4773 Aug 28 23:55 arraybuffers.js
-rw-r----- 1 ldixon eng 4863 Aug 28 23:55 arraybuffers.js.map
-rw-r----- 1 ldixon eng 7814 Aug 28 23:55 arraybuffers.spec.js
-rw-r----- 1 ldixon eng 7733 Aug 28 23:55 arraybuffers.spec.js.map
drwxr-x--- 2 ldixon eng 4096 Aug 28 23:55 typescript-src
where I was expecting:
drwxr-x--- 2 ldixon eng 4096 Aug 28 23:55 arraybuffers
drwxr-x--- 2 ldixon eng 4096 Aug 28 23:55 typescript-src
Will revert back to grunt-typescript for now...
@iislucas It think its because you are excluding .baseDir.ts
explicitly i.e. you have '!**/.baseDir.ts'
:
src: ['build/typescript-src/arraybuffers/**/*.ts',
'!**/*.d.ts',
'!**/.baseDir.ts'],
I no longer have this problem, though I'm not sure what was wrong before.
Empty baseDir file is annoying. Is there a way to prevent baseDir from being created? If no, can you at least add the hidden
file attribute to it so is not visible on the file system (NTFS, Windows)?
can you at least add the hidden file attribute to it so is not visible on the file system (NTFS, Windows)
you can open a new issue for this so that someone can tackle it
@basarat thanks; that was the issue.
I still find this all a bit awkward... I think I have a suggestion for proposed behaviour that might help with this and a related issue:
I have my code in a directory called src
. e.g. src/arraybuffers/accumulater.ts
I want to compile it all into build
to get output of build/arraybuffers/accumulater.js
. In the shell, I can do this by a cd src && tsc */*.ts
But in grunt-ts, there is no way to specify a cwd
as one can for most other grunt rules. Adding a cwd
parameter like the grunt-contrib-copy
and others (see https://github.com/gruntjs/grunt-contrib-copy#usage-examples) would help give programatic control over where output files end up.
Make sense?
@iislucas you can specify baseDir
in options. It is same as effectively cwd
.
Great! Probably good to add it to the README: I did a search there (in the readme), but the only occurrence of the string baseDir
seemed to be unrelated, about the .baseDir.ts file
Actually, that doesn't work for me, issue filed here: https://github.com/TypeStrong/grunt-ts/issues/201
I just got linked to here from a .basedir.ts
file popping up during options: out
builds. Is there anything happening on the issue?
I'm planning to eliminate it in a future patch if someone passes the new rootDir option to tsc. But it's a matter of getting to it.
This would be nice to get rid of this indeed as it introduced a bug.
When using the grunt-ts noLib: true
parameter, and properly declaring a lib in my main ts file, I get the following output when running grunt ts -v
:
Running "ts" task
Running "ts:app" (ts) task Verifying property ts.app exists in config...OK Files: src/app.ts -> . Compiling...
Fast Compile >>src/app.ts
Using tsc v1.8.10 "~/Projets/green-box-express/src/app.ts" "~/Projets/green-box-express/src/.baseDir.ts" --removeComments --noLib --target ES6 --module commonjs --outDir . error TS2318: Cannot find global type 'Array'. error TS2318: Cannot find global type 'Boolean'. error TS2318: Cannot find global type 'Function'. error TS2318: Cannot find global type 'IArguments'. error TS2318: Cannot find global type 'Iterable'. error TS2318: Cannot find global type 'IterableIterator'. error TS2318: Cannot find global type 'Iterator'. error TS2318: Cannot find global type 'Number'. error TS2318: Cannot find global type 'Object'. error TS2318: Cannot find global type 'RegExp'. error TS2318: Cannot find global type 'String'. error TS2318: Cannot find global type 'Symbol'. error TS2318: Cannot find global type 'TemplateStringsArray'. error TS2468: Cannot find global value 'Symbol'.
14 non-emit-preventing type warnings
Error: tsc return code: 2 Warning: Task "ts:app" failed. Use --force to continue.Aborted due to warnings.
And this is due to the file .baseDir.ts being passed to the compiler while not containing the library declaration.
To fix this one could add something like /// <reference path="../node_modules/typescript/lib/lib.es6.d.ts" />
to the .baseDir.ts file, but for a beginner in TS like me, it means wasting more than an hour debugging this issue...
Is there anything happening on the issue?