forcedotcom / lightning-language-server

LWC and Aura Language Servers - shipped as part of the Salesforce VSCode Extensions
https://forcedotcom.github.io/salesforcedx-vscode/
BSD 3-Clause "New" or "Revised" License
33 stars 49 forks source link

feat: automatically updating `tsconfig.json`'s `paths` mapping for core workspaces. #587

Open rui-rayqiu opened 7 months ago

rui-rayqiu commented 7 months ago

What does this PR do?

Impact: this PR only has impact on core workspaces for Salesforce internal users working on core. For external users working on SFDX workspaces, this PR should have no impact.

This PR adds a new feature for automatically updating tsconfig.json's paths mapping for core workspaces.

LWC team is working on supporting TypeScript for authoring LWCs on core. In order for TypeScript compiler to work with LWC’s folder structure, we need to add a series of paths entries in tsconfig.json that re-map imports of LWC components to relative file paths of the imported components. For example, if component one/cmpA imports force/wireUtils, the import statement in one/cmpA will be like import { someUtil } from ‘force/wireUtils’; and for TypeScript to know where to look for force/wireUtils we need to add a path entry in tsconfig.json as the following.

    "compilerOptions": {
        "paths": {
            "force/wireUtils": ["../ui-force-components/modules/force/wireUtils/wireUtils"],
        }
    }

Maintaining this mapping in tsconfig.json manually is not convenient and a less ideal DX for developers on core. This PR adds a new indexer in the lwc-language-server that maintains a mapping between LWCs and their paths in tsconfig.json, automatically updating the tsconfig.json file when language server is initialized and whenever a LWC TypeScript file is changed. Thus developers won't need to create entries for paths manually when creating new LWCs in TypeScript.

This new feature will update tsconfig.json file for the paths attribute for all the core modules in the workspace when language server is initialized. Then whenever a LWC TS file is changed, all the imports are analyzed and any necessary LWC imports will be added to tsconfig.json automatically.

Here is a short demo video link on this feature. In the video, the test workspace lightning-language-server/test-workspace/core-like-workspace/coreTS/core (created in this PR) is opened in VSCode, there are two modules ui-force-components and ui-global-components in the workspace. Both tsconfig.json files for them don't have a paths entries. After the indexer's init function is executed, both tsconfig.json files are updated with the paths mappings. After that, if I add an import statement in a ts file, the tsconfig.json file is also updated to have the mapping for the new import.

Testing this locally will require this PR for adding TS files to file watcher.

What issues does this PR fix or reference?

@W-14958700@