lwc
lwc copied to clipboard
Making @lwc/module-resolver capable to resolve directory without namespaces
Is your feature request related to a problem? Please describe.
SFDX project structures are different than standard LWC project structures. Because of this, it is currently cumbersome to use @lwc/module-resolver to resolve LWC modules from an SFDX project.
- Standard LWC project structure:
src
└── modules/
├── ui/
│ ├── button/
│ │ ├── button.js
│ │ └── button.html
│ └── icon/
│ └── icon.js
└── shared/
└── utils/
└── utils.js
- Standard SFDX project structure:
sfdx-project-dir
└── sfdx-package-dir
└── main
└── default
└── lwc
├── button/
│ ├── button.js
│ └── button.html
└── icon/
└── icon.js
One of the key differences to observe here is that the SFDX structure doesn't have a namespace folder containing the LWC modules. The LWC modules are contained under the lwc directory even if the namespace they map to is c.
Because of this, a directory module record can't be passed to the module resolver. The only way to resolve those LWC modules is to list the folders under the lwc directory and map each directory to an alias module record.
Describe the solution you'd like
We could extend the directory module record record to accept an optional namespace property that would be used as a common module namespace for a given directory.
export interface DirModuleRecord {
dir: string;
namespace?: string;
}
Going back to the SDFX repro structure example, the module resolver could be invoked this way to resolve the modules.
import { resolveModule } from '@lwc/module-resolver';
const root = process.cwd();
resolveModule('c/button', root, {
modules: [{
dir: 'sfdx-project-dir/sfdx-package-dir/main/default/lwc',
namespace: 'c'
}],
})
Describe alternatives you've considered
Another approach would be to define a new module record type to handle SFDX folder format. But I think that reusing the existing directory module record is way more elegant.
Additional context
Currently, the sfdx-lwc-jest is using a custom LWC module resolver to accommodate SFDX project structure. On top of requirement extra maintenance from the LWC team, this custom resolver is really slow (https://github.com/salesforce/sfdx-lwc-jest/issues/161). Using the standard LWC modules resolver would increase the maintainability and improve the module resolution performance.
/cc @diervo @trevor-bliss
What's the state of this? I feel like it would be an easy fix and would help a lot with ecosystem compatibility. I could open a PR if there's bandwidth to review it.
We have plans internally to update module resolution and make it more flexible. I'd recommend leaving this for now.
@wjhsf thanks for the update, I'll keep that in mind.