Skip to content

Commit

Permalink
esm - restore ability to require.resolve with paths (#227934)
Browse files Browse the repository at this point in the history
* esm - restore ability to `require.resolve` with `paths`

* fill in paths
  • Loading branch information
bpasero authored Sep 9, 2024
1 parent 3ab41c2 commit 6abee1b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/vs/workbench/api/node/extHostExtensionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ class NodeModuleRequireInterceptor extends RequireInterceptor {
return originalLookup.call(this, applyAlternatives(request), parent);
};

const originalResolveFilename = node_module._resolveFilename;
node_module._resolveFilename = function resolveFilename(request: string, parent: unknown, isMain: boolean, options?: { paths?: string[] }) {
if (request === 'vsda' && Array.isArray(options?.paths) && options.paths.length === 0) {
// ESM: ever since we moved to ESM, `require.main` will be `undefined` for extensions
// Some extensions have been using `require.resolve('vsda', { paths: require.main.paths })`
// to find the `vsda` module in our app root. To be backwards compatible with this pattern,
// we help by filling in the `paths` array with the node modules paths of the current module.
options.paths = node_module._nodeModulePaths(import.meta.dirname);
}
return originalResolveFilename.call(this, request, parent, isMain, options);
};

const applyAlternatives = (request: string) => {
for (const alternativeModuleName of that._alternatives) {
const alternative = alternativeModuleName(request);
Expand Down

0 comments on commit 6abee1b

Please sign in to comment.