typescript-register icon indicating copy to clipboard operation
typescript-register copied to clipboard

Do not cache current working directory

Open tkrotoff opened this issue 10 years ago • 6 comments

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 ./).

tkrotoff avatar Jul 06 '15 20:07 tkrotoff

Do you have a stack trace of how it fails?

pspeter3 avatar Jul 06 '15 20:07 pspeter3

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);
};

tkrotoff avatar Jul 06 '15 20:07 tkrotoff

@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.

tkrotoff avatar Jul 06 '15 20:07 tkrotoff

Ah ok, so we can't cache the current working directory, that makes sense.

pspeter3 avatar Jul 06 '15 21:07 pspeter3

@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.

pspeter3 avatar Jul 06 '15 21:07 pspeter3

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.

tkrotoff avatar Jul 06 '15 23:07 tkrotoff