Skip to content

Refactor module resolution logic to have configurable goal extensions #9430

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

Closed
wants to merge 9 commits into from

Conversation

weswigham
Copy link
Member

Fixes #9427

@weswigham
Copy link
Member Author

@billti You should probably take a quick look at this.
@rbuckton I've used your suggestion for this area of code from my other PR, so you should probably take a look, too.

"moduleResolution": "node",
"resolvedInputFiles": [
"lib.d.ts",
"node_modules/@types/m1/index.d.ts",
Copy link
Member

Choose a reason for hiding this comment

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

So this file doesn't exist, but it's referenced?

Copy link
Member Author

@weswigham weswigham Jun 29, 2016

Choose a reason for hiding this comment

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

Hah. It didn't get committed because it's in a node_modules folder. That's funny. And annoying. Let me fix that.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, see the last line I added to the gitignore file on my prior merge to master. That nearly got me too.

jsonContent = jsonText ? <{ typings?: string, types?: string, main?: string }>JSON.parse(jsonText) : {};
function getPackageEntry(packageJson: any, key: string, tag: string, state: ModuleResolutionState) {
const value = packageJson[key];
if (typeof value === tag) {
Copy link
Contributor

Choose a reason for hiding this comment

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

when do we need something that is not a string?

Copy link
Member Author

Choose a reason for hiding this comment

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

At this point, never. This was mostly @rbuckton's suggestion. I can specialize the function, if you'd prefer it.

Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed the possibility of augmenting the package.json for extensions, which could leverage this if we decided to lean in that direction.

@billti
Copy link
Member

billti commented Jun 29, 2016

This seems like a lot more code change than we need. I was thinking just something like the below in program.ts @ 781 would be sufficient, which is a minor change to existing logic. Anywhere this has issues?

            if (baseName !== "node_modules") {
                // Try to load source from the package 
                const packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state);
                if (packageResult && hasTypeScriptFileExtension(packageResult)) {
                    // Always prefer TypeScript source (.ts, .tsx, .d.ts) shipped with the package
                    return packageResult;
                }
                else {
                    // Else prefer a types package over non-TypeScript results (e.g. JavaScript files)
                    const typesResult = loadModuleFromNodeModulesFolder(combinePaths("@types", moduleName), directory, failedLookupLocations, state);
                    if (typesResult || packageResult) {
                        return typesResult || packageResult;
                    }
                }
            }

@billti
Copy link
Member

billti commented Jun 29, 2016

I just tested the above simple change and that seems to work fine too. I'll push some commits to the (slightly) related change at #9421 for review.

@@ -1134,10 +1134,33 @@ namespace ts {
*/
export const supportedTypeScriptExtensions = [".ts", ".tsx", ".d.ts"];
export const supportedJavascriptExtensions = [".js", ".jsx"];
const allSupportedExtensions = supportedTypeScriptExtensions.concat(supportedJavascriptExtensions);

export function getExtensionsForGoal(goal: ResolutionGoal): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

ResolutionGoal seems to have some similarities to the ExtensionPriority enum used as part of glob resolution.

Copy link
Member Author

Choose a reason for hiding this comment

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

Merged! ExtensionPriority overlays nicely onto what I used to call ResolutionGoal (now since renamed).

@weswigham
Copy link
Member Author

weswigham commented Jun 30, 2016

@billti I have ulterior motives for making this a little more complicated: I need to make it easy (in the near future) to specify that I want to load js files only for resolving modules in the extension PR (which is where the first draft of this code came from).

Additionally, we were passing a handful of flags around to control the loading of js and jsx/tsx - this now unifies them. And, thanks to a little bit more effort, is now merged with the ExtensionPriority enum used by globbing.

@billti
Copy link
Member

billti commented Jun 30, 2016

Right, I was figuring as much. But we need this fix for 2.0, which has already snapped for stabilization. I'd rather keep it as simple as possible, especially as the extensibility stuff is very early on at this point and may go through more changes. That said, I'll let @mhegazy make the call on which pull request to take.

@weswigham
Copy link
Member Author

On a related note, Travis finally finished testing this PR and it's now lit up green. :D

@mhegazy
Copy link
Contributor

mhegazy commented Jun 30, 2016

I do agree with @billti for 2.0 i think #9421 is sufficient. we ca take these other changes separately, and no need to lump them here.

@weswigham weswigham changed the title Look for js files after @types files Refactor module resolution logic to have configurable goal extensions Jul 5, 2016
@weswigham
Copy link
Member Author

@mhegazy Now that we've cut a 2.0 beta and have a PR out to bump the version tag in master to 2.1, can we look into merging this refactoring again?

@weswigham
Copy link
Member Author

@vladima Could you look at this PR?

@mhegazy
Copy link
Contributor

mhegazy commented Dec 30, 2016

This is stale by now. sorry for the delay. closing.

@mhegazy mhegazy closed this Dec 30, 2016
@mhegazy mhegazy deleted the fix-lookup-priority branch November 2, 2017 21:02
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants