TypeStrong / grunt-ts

A grunt task to manage your complete typescript development to production workflow
https://www.npmjs.com/package/grunt-ts
MIT License
330 stars 121 forks source link

Modules and OutDir #340

Closed shawnsparks closed 8 years ago

shawnsparks commented 8 years ago

How can I have the generated files output to a different directory than source files when using modules? I have tried both out and outDir, but neither work with modules. I simply don't want generated files sitting next to source files and must break source into multiple files. New to TypeScript so let me know if I'm missing something obvious.

shawnsparks commented 8 years ago

I solved most of the problem using this configuration. I had to add fast: 'never' because otherwise files in subdirectories like src/services/example.ts would get compiled to dist/example.ts. This would break imports. Is there a way to use outDir, modules, and fast compiling?

ts: {
  default: {
    src: [ 'src/**/*.ts' ],
    outDir: 'dist',
    options: {
      module: 'commonjs',
      fast: 'never'
    }
  }
}
nycdotnet commented 8 years ago

Hi, Fast shouldn't affect outDir so this sounds like a bug. Thanks for the report.

shawnsparks commented 8 years ago

Here appear to be a couple related issues: https://github.com/TypeStrong/grunt-ts/issues/77 and https://github.com/TypeStrong/grunt-ts/issues/139

  1. I find it odd that the .baseDir.ts file gets created in src/services and not src.
  2. tasks/modules/compile.js references options.baseDir, but tasks/modules/optionsResolver.js has nothing about baseDir. It's like the baseDir option was removed at some point in time.
nycdotnet commented 8 years ago

I did a rewrite of the options resolver last summer and it's likely I broke baseDir because it wasn't documented. #300 is related too. It's on the list of things I need to get to as well as fast being broken at least in certain situations. Thank you for the report and certainly PRs accepted. I'll try to spend some time on grunt-ts soon.

shawnsparks commented 8 years ago

So I believe the problem from #139 is how baseDir is calculated when not explicitly set. My repo is very similar to what's described in that issue. I have my code in a src folder. I also have type_definitions/node.d.ts outside of src directory. This results in utils.findCommonPath(...) to return an empty string so options.baseDir is never defined. A .baseDir.ts file is never created. Ultimately, fast compilation creates an incorrect folder structure in dist (value of outDir). The code for this is in compile.ts lines 114-116 currently.

shawnsparks commented 8 years ago

baseDir option can't be explicitly set because it's missing from optionsResolver.ts line 13.

shawnsparks commented 8 years ago

So the really simple fix seems to be to add baseDir to optionsResolver.ts, and explicitly set it in gruntfile. It will still result in problems for people who include type definitions outside of src folder or otherwise have source code from two different paths in their repo and who don't explicitly set baseDir.

My thoughts are below. Let me know what you think and I can try to help with PRs.

  1. Submit pull request to allow baseDir to be explicitly set in config
  2. Submit pull request to throw error when baseDir isn't explicitly set, outDir is set, and utils.findCommonPath(...) returns an empty value.
  3. Submit pull request to delete .baseDir.ts from source folder, .baseDir.js and .baseDir.js.map from outDir in an attempt to clean up after grunt-ts. This appears to be new functionality that I think would be beneficial.
nycdotnet commented 8 years ago

I merged in the PR for number one. Thank you so much for your contribution.

I like your idea 2.

I think that idea 3 would be useful, but honestly I would prefer to just eliminate .baseDir.ts altogether. There is a rootDir feature natively supported by new TypeScript versions. #300 has some discussion on that topic.

Thanks again.

shawnsparks commented 8 years ago

Ok, I did not understand rootDir and that it replaces baseDir when using newer versions of TypeScript. I submitted #344. If you're happy with that, we can close this ticket as I'm assuming most people are using a new enough version of TypeScript for rootDir.

nycdotnet commented 8 years ago

Your fixes should be available in v5.4.0 - just deployed to npm. Please let me know if you experience further issues.

I am going to close this ticket and open a new one to investigate fast interfering with outDir.

Thanks so much!!!