-
-
Notifications
You must be signed in to change notification settings - Fork 433
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
ts-loader can't find typings for npm TS module when allowJS is true #586
Comments
Hey @bsouthga, I've had a quick look but nothing leaped out at me. I'm afraid I don't have time to look into this right now but I'm happy to support you as you do. There's very little magic around the https://github.com/TypeStrong/ts-loader/blob/master/src/instances.ts#L96 I'd advise cloning a copy of ts-loader, |
will do, thanks for the quick feedback! |
So I dug into this a little bit, and it looks like the issue is here: Lines 146 to 150 in 80e0349
When allowJS is true, resolutionResult.resolvedFileName !== tsResolutionResult.resolvedFileName Meaning the returned resolution result has: resolutionResult.isExternalLibraryImport === undefined As a fix, I added a helper function and changed the line above as follows: function isBundledTypeDeclaration(resolvedFileName: string, tsResolvedFileName: string) {
return resolvedFileName.replace(/\.js$/, '.d.ts') === tsResolvedFileName;
}
// lines omitted...
if (resolutionResult) {
if (resolutionResult.resolvedFileName === tsResolutionResult.resolvedFileName ||
isBundledTypeDeclaration(resolutionResult.resolvedFileName, tsResolutionResult.resolvedFileName)) {
resolutionResult.isExternalLibraryImport = tsResolutionResult.isExternalLibraryImport;
}
} else {
resolutionResult = tsResolutionResult;
}
}
return resolutionResult;
} It seems like a bit of a hack, as its possible that the bundled declaration files are not named the same or in the same directory as the js. Any alternate thoughts? |
First up, well done for identifying the problem!
Yeah - it's a massive hack 😉 Couple of ideas - can you give me some example values for the 2 values in the expression below?:
Are we dealing with a filename or a full path? What I'm wondering is, are there other rules we can use here instead of the one you proposed. Idea: Assuming we have full paths then we can presumably check for the presence of
Would that work? |
yeah, switching the conditional to if (resolutionResult) {
if (resolutionResult.resolvedFileName === tsResolutionResult.resolvedFileName ||
/node_modules\/.*\.d\.ts/.test(tsResolutionResult.resolvedFileName)) {
resolutionResult.isExternalLibraryImport = tsResolutionResult.isExternalLibraryImport;
}
} else {
resolutionResult = tsResolutionResult;
} solves the issue as well, for reference logging console.log({
loader: resolutionResult.resolvedFileName,
ts: tsResolutionResult.resolvedFileName
}); produces
|
seems to cause issues with the tests though, I'll keep poking around |
Cool - let me know what your findings are. Also some of the tests are supersensitive so it could be worth checking the nature of the errors. (comparison test pack in particular) Test "failures" aren't always failures - if you follow me.... |
Oh and feel free to submit a PR with your change. That will allow me to see the nature of the failing tests and I can probably advise on whether it's a meaningful failure. We're trying to move (where possible) to using the execution test pack as it's less flaky than the comparison test pack. (There's some test scenarios that require the comparison test pack though) |
Hey @bsouthga, just wondering how your investigations went? |
I've tried to add a speculative test for this here: #590 It's based on your repro - might need some tweaking to get working. It's more complicated than I'd like as it depends on babel due to EventRank using ES2015 features. Longer term it might made for a clearer test to base our execution test on this one: https://github.com/TypeStrong/ts-loader/tree/master/test/execution-tests/1.8.2_babel-allowSyntheticDefaultImports (I'm pretty sure babel can be stripped out here as well - I don't think it's required) Hopefully this can get the ball rolling though. The test fails - hopefully for the reason we expect. We now just need to apply the speculative fix and see if that resolves matters. |
Closed with #590 |
Minimal repro repository: https://github.com/bsouthga/ts-loader-types-error-example
First off, thanks for all the great work you guys do on
ts-loader
, its an important part of our build stack!Issue:
Versions:
Given a JS entry file:
And an imported typescript file (importing this library):
And a webpack config as follows:
The project will compile fine under
tsc
, but webpack returns the following error:The text was updated successfully, but these errors were encountered: