Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with incorrect output path for declaration files #822

Merged
merged 3 commits into from Sep 3, 2018
Merged

Fixed issue with incorrect output path for declaration files #822

merged 3 commits into from Sep 3, 2018

Conversation

JonWallsten
Copy link
Contributor

@JonWallsten JonWallsten commented Aug 17, 2018

In regards of: #190

I'll try to explain the fix:
Below is the code where we prepare the relative path for the d.ts files so we can add them to Webpack's compilation assets.

const assetPath = path.relative(
        compilation.compiler.context,
        declarationFile.name
      );
compilation.assets[assetPath] = {
        source: () => declarationFile.text,
        size: () => declarationFile.text.length
      };

Example
Project root: /home/user/jon/projects/a/
[webpack.main]: index.ts
[webpack.output.path]: /home/user/jon/projects/a/dist/
[compilerOptions.declarationDir]: ./dist/typings/

[Scope variables]
[compilation.compiler.context]: /home/user/jon/projects/a/
[compilation.compiler.outputPath]: /home/user/jon/projects/a/dist/

[Other]
All relative assets in [compilation.assets] will have [compilation.compiler.outputPath] as base it seems.

Results
What will happen here is that path.relative will return a relative path with compiler.context (project root) as a base. So when TypeScript compiler outputs /home/user/jon/projects/a/dist/index.d.ts (declarationDir is relative to the project root), path.relative will strip the project root and we will end up with dist/index.d.ts. It looks correct, right? Well, if we would store the compiled files relative to the project root it would. The issue is that compilation.assets seems to equal webpack.output.path.
So we'll end up with this:

/home/user/jon/projects/a/dist/index.js
/home/user/jon/projects/a/dist/dist/index.d.ts

By changing compilation.compiler.context -> compilation.compiler.outputPath we will get a path relative to the output folder instead.

So if we have the same output from the TypeScript compiler: /home/user/jon/projects/a/dist/index.d.ts and we strip away /home/user/jon/projects/a/dist/ instead, we end up with index.d.ts. The result would be:

/home/user/jon/projects/a/dist/index.js
/home/user/jon/projects/a/dist/index.d.ts

Note that this might be a breaking change since people probably has workarounds or have adapted to this way. Myself included.

@JonWallsten JonWallsten changed the title Added correct relative path Fixed issue with incorrect path for d.ts files Aug 17, 2018
@JonWallsten JonWallsten changed the title Fixed issue with incorrect path for d.ts files Fixed issue with incorrect output path for declaration files Aug 17, 2018
@johnnyreilly
Copy link
Member

I really appreciate the detailed PR - that's very helpful indeed!

Note that this might be a breaking change since people probably has workarounds or have adapted to this way. Myself included.

Could you provide some details around what workarounds people might be making use of?

@JonWallsten
Copy link
Contributor Author

JonWallsten commented Aug 18, 2018

I just mean that people have adapted to use a path relative to the output folder instead of the root.
My project for example:
The packages where I still use ATL have './dist/typings'
The packages where I use TS-loader have just 'typings'
Now they will end up in the same output folder. The problem with the ts-loader however is that when I run tsc manually, for example when I compile the files for testing, the output folder will be /typings/ instead of /dist/typings/. And as you know you can't unfortunately have different tsconfig files in the same project. That's how I found the issue I replied to.

I thought I had during the night was that maybe we can combine the old solution and the new one. If you start your declarationDir with dot-slash, './output', we could use the project root as base. And if you just use 'output' for example it could be relative to the webpack output folder. Still a bit confusing though since tsc wouldn't output the files in the same folder.

@johnnyreilly
Copy link
Member

Cool. Essentially I'm trying to evaluate how "breaking" this change would be. If it's not really that break-y then I'd consider treating this as a fix version-wise. Alternatively we'd have to do a major version bump which I'm less keen on, but if it's appropriate then that's what it should be. Do you have a view?

@JonWallsten
Copy link
Contributor Author

Yeah, a major seems unnecessary for this small fix. But still I might require action from the user. I'm not sure what the conventional thing to do is.
I'm not in a hurry with this fix, so take your time and let me know. Whatever works for me!

@johnnyreilly
Copy link
Member

Thanks chap. I'm not going to be online much for the next couple of weeks; I'll probably look to ship this with #817 when the time comes.

@JonWallsten
Copy link
Contributor Author

Sound good! Just ping me when the time comes!

@gluons
Copy link

gluons commented Aug 27, 2018

Thank you very much for this fix. 😍

I've tried this PR. It works properly. (Both with outDir and declarationDir options in tsconfig.json) 🎉

But it works only if I set outDir (or declarationDir) in tsconfig.json (e.g. dist).

With outDir
Image copied from #190


If I forget to set outDir (or declarationDir), all declaration files will be placed at the same place as source files.

Without outDir
Image copied from #190


Maybe we need note for reminding users to set outDir or declarationDir.




It should be great if outDir/declarationDir isn't required. But this behavior is OK for me.

@JonWallsten
Copy link
Contributor Author

@gluons I would have to say that it's intended (but maybe unwanted in some cases?). It's TypeScripts default behavior to do this. If you don't add outDir or declarationDir it might be intended, and you could get annoyed by a warning, since it's in the context of TypeScript isn't wrong. What does @johnnyreilly think?

@johnnyreilly
Copy link
Member

It's TypeScripts default behavior to do this.

Then that's what ts-loader should do 😁

As a general rule of thumb, ts-loader aims to be a drop in replacement for tsc behaviour wise; only deviating if there's specific cause. I don't see that in this case so I think it's just fine.

BTW I'm hoping to get this merged and shipped either next week or the week after.

@gluons
Copy link

gluons commented Aug 29, 2018

OK. I get it.
It's actually fixed now. 🎉

@johnnyreilly
Copy link
Member

Hey @JonWallsten,

I'm just heading back from holidays. I've been thinking; although this is a minor change, you're right that it's technically a breaking change. As much as I don't want to bump the version number; ts-loader seeks to be a good semver citizen. So it's time to bite the bullet.

Please could you:

  1. Update the version in the package.json to 5.0.0
  2. Add a corresponding entry to the CHANGELOG.md

Then I'll close my eyes, weep a bit and push out the major version. Cheers!

@JonWallsten
Copy link
Contributor Author

JonWallsten commented Sep 3, 2018

@johnnyreilly Sure. I'll fix it before nightfall! And don't worry, it seems like it's fashion to bump majors all the time now anyways ;) I mean, look at Chrome 68!

@johnnyreilly johnnyreilly merged commit 1cc576d into TypeStrong:master Sep 3, 2018
@JonWallsten JonWallsten deleted the fix-declaration-output-wrong-folder branch September 3, 2018 11:25
@johnnyreilly
Copy link
Member

Thanks for your work @JonWallsten!

Released with https://github.com/TypeStrong/ts-loader/releases/tag/v5.0.0

@JonWallsten
Copy link
Contributor Author

Perfect! No worries! Happy to help for my own cause! ;)

@Jogai
Copy link

Jogai commented Sep 5, 2018

I mean, look at Chrome 68!

You mean 69?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants