Do not cache current working directory
Layout:
./client/gulpfile.ts
./server/gulpfile.ts
# Contains "scripts": {"build": "gulp --cwd client build && gulp --cwd server build"}
# so one can run `npm run build` to build both client and server projects
./package.json
gulp --cwd client build or gulp --cwd server build will fail because process.cwd() is ./ instead of ./client or ./server.
I've tried typescript-require on the same layout and their process.cwd() points to the proper path (./client or ./server and not ./).
Do you have a stack trace of how it fails?
After some investigations, I understand why.
require.extensions[".ts"] = function() {
console.log('process.cwd() inside require.extensions[".ts"]', process.cwd()); // Works
}
console.log('process.cwd() outside require.extensions[".ts"]', process.cwd()); // Fails
You need to call process.cwd inside your req(module, filename) function.
If you call process.cwd outside like you do, process.cwd does not inherit its value from gulp.
And this is exactly what typescript-require do:
require.extensions['.ts'] = function(module) {
var jsname = compileTS(module); // ==> Contains call to process.cwd()
runJS(jsname, module);
};
@pspeter3 Obviously there is no stack trace: it is not a crash, just process.cwd() that does not contain the proper path in some cases.
Here the solution: move defaultCompilerOptions inside function req():
function req(module: Module, filename: string): void {
var defaultCompilerOptions: typescript.CompilerOptions = {
module: typescript.ModuleKind.CommonJS,
outDir: getCachePath(process.cwd()),
target: typescript.ScriptTarget.ES5
};
var options = compilerOptions(defaultCompilerOptions);
[...]
}
[...]
function compilerOptions(defaultCompilerOptions: Object): typescript.CompilerOptions {
return env(Config.COMPILER_OPTIONS, defaultCompilerOptions, toOptions);
}
Since I don't know how to run my own modified version (see https://github.com/pspeter3/typescript-register/issues/9), I've manually modified node_modules/typescript-register/index.js from my project.
Ah ok, so we can't cache the current working directory, that makes sense.
@tkrotoff if you want to submit a pull request to call process.cwd() at compile time, I'll happily accept it. Otherwise I'll get to it soon.
I've patched my fork: https://github.com/tkrotoff/typescript-register/commit/ec46ab007a924297e40a56af00b012385fc15ae8#diff-ed009b6b86b017532ef0489c77de5100 Quick and dirty. I guess you'll implement a clean solution with a unit test for it.