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

Added support for TypeScript declaration map #821

Merged
merged 4 commits into from Aug 17, 2018
Merged

Added support for TypeScript declaration map #821

merged 4 commits into from Aug 17, 2018

Conversation

JonWallsten
Copy link
Contributor

@JonWallsten JonWallsten commented Aug 15, 2018

@johnnyreilly: Please check this out and let me know if you want any changes or if I did something wrong creating the tests.

Fix for #820

@johnnyreilly
Copy link
Member

First of all, thanks for the PR!

Question; is there overlap with the work @andrewbranch is doing in this PR?

#817

He's been busy beavering away for the last week or so and it seems that there might be commonality here. @andrewbranch can you comment on the work @JonWallsten has done.

I'm wondering if it make sense for you guys to collaborate on this rather than having related PRs merged separately.

@JonWallsten
Copy link
Contributor Author

JonWallsten commented Aug 15, 2018

@johnnyreilly I was actually checking out his PR earlier today. I don't think they collide. He should be able to merge this without conflicts. My changes are minor as you can see. I only allowed the regex to include .d.ts.map as well and made sure that the provideDeclarationFilesToWebpack function handles multiple output files (d.ts and d.ts.map). We should be able to merge this before his. And I guess project references and declarationMap are totally unrelated. They just happens to be new features in 3.0.x.
But please correct me if I'm wrong. I don't feel like I have the full picture of TS 3.0 yet.

src/instances.ts Outdated
@@ -205,11 +205,11 @@ function successfulTypeScriptInstance(
let normalizedFilePath: string;
try {
const filesToLoad = loaderOptions.onlyCompileBundledFiles
? configParseResult.fileNames.filter(fileName =>
? configParseResult.fileNames.filter((fileName: string) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the type annotation necessary? Presumably the type can be inferred?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got an error in VS code, that's why I fixed it. But now when I check the tsconfig I can't find "noImplicitAny", so I'm not actually sure why I got that warning. VSCode has been a little shaky when it comes to errors since last release though. I don't have a problem removing them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

src/instances.ts Outdated
dtsDtsxRegex.test(fileName)
)
: configParseResult.fileNames;
filesToLoad.forEach(filePath => {
filesToLoad.forEach((filePath: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the type annotation necessary? Presumably the type can be inferred?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@johnnyreilly
Copy link
Member

But please correct me if I'm wrong. I don't feel like I have the full picture of TS 3.0 yet.

Me neither 😄

size: () => declarationFile.text.length
};
const declarationFiles: typescript.OutputFile[] = outputFiles
.filter((outputFile: typescript.OutputFile) => outputFile.name.match(constants.dtsDtsxRegex));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can (outputFile: typescript.OutputFile) just be outputFile? Again type inference should work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are correct. I reinstalled VS Code, and it works now. I'm removing these.

.filter((outputFile: typescript.OutputFile) => outputFile.name.match(constants.dtsDtsxRegex));

if (Array.isArray(declarationFiles)) {
declarationFiles.forEach(file => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can file be declarationFile please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Fixed!

src/constants.ts Outdated
@@ -12,7 +12,7 @@ export const ScriptTargetES2015 = 2;
export const ModuleKindCommonJs = 1;

export const tsTsxRegex = /\.ts(x?)$/i;
export const dtsDtsxRegex = /\.d\.ts(x?)$/i;
export const dtsDtsxRegex = /\.d\.ts(x?)(\.map)?$/i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be dtsDtsxRegex be dtsDtsxOrDtsDtsxMapRegex please? It's verbose but hopefully clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! I'll fix!

const declarationFiles: typescript.OutputFile[] = outputFiles
.filter((outputFile: typescript.OutputFile) => outputFile.name.match(constants.dtsDtsxRegex));

if (Array.isArray(declarationFiles)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you could tell me about this part some more? This is the crux of the PR really; previously the code assumed a single d.ts file. Now it's multiple; presumably that's allowing for the map files as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly! Due to the regex change, getEmitOutput will return both d.ts and d.ts.map when declarationMap is true. It felt logical to handle declaration & map files together since they are linked together inside the d.ts file, and besides the map file can't live without the declaration file. When it comes to performance loss for using a forEach instead of a pop() when it's only one should not a problem since we're not using a million and millions of files (i hope).

outputFile.name.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);

if (Array.isArray(declarationFiles)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Array.isArray the right thing to do here? Presumably this will always be true as the result of the filter will always be an Array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is correct. But forEach will handle that. I'm only doing the array check to make sure forEach won't throw and error.

Edit: I see your point now. The original line was declarationFiles !== undefined. So I assumed (maybe wrongly) that I could also be undefined, even though I was surprised about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter will always return an array. So I removed the check. It's safe to assume that we will always have an array, but it might be empty.

Removed unnecessary typings
Removed unnecessary array check
@johnnyreilly
Copy link
Member

Looks good. I'll plan to merge and ship this shortly; thanks for your work!

@JonWallsten
Copy link
Contributor Author

JonWallsten commented Aug 16, 2018

Perfect! Pleasure's all mine. I really want the feature. I'm glad it was easy to implement. Good work of the code base.

@johnnyreilly johnnyreilly merged commit c8e3fec into TypeStrong:master Aug 17, 2018
@JonWallsten JonWallsten deleted the support-for-declaration-map branch August 17, 2018 15:04
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

2 participants