Skip to content

Commit 815d94e

Browse files
authored
Merge pull request microsoft#81555 from penx/feature/node-module-resolution-for-css-import-parent-folders
CSS - Search in parent folders for node module resolution
2 parents 869929e + 0fffab3 commit 815d94e

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

extensions/css-language-features/server/src/test/links.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@ suite('Links', () => {
7676
[{ offset: 29, value: '"~foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
7777
);
7878
});
79+
80+
test('node module subfolder resolving', function () {
81+
82+
let testUri = getTestResource('subdir/about.css');
83+
let folders = [{ name: 'x', uri: getTestResource('') }];
84+
85+
assertLinks('html { background-image: url("~foo/hello.html|")',
86+
[{ offset: 29, value: '"~foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
87+
);
88+
});
7989
});

extensions/css-language-features/server/src/utils/documentContext.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ function getModuleNameFromPath(path: string) {
1919
return path.substring(0, path.indexOf('/'));
2020
}
2121

22-
function resolvePathToModule(_moduleName: string, _relativeTo: string): string | undefined {
22+
function resolvePathToModule(_moduleName: string, _relativeToFolder: string, _rootFolder: string | undefined): string | undefined {
2323
// resolve the module relative to the document. We can't use `require` here as the code is webpacked.
24-
const documentFolder = dirname(URI.parse(_relativeTo).fsPath);
25-
const packPath = join(documentFolder, 'node_modules', _moduleName, 'package.json');
24+
25+
const packPath = join(_relativeToFolder, 'node_modules', _moduleName, 'package.json');
2626
if (existsSync(packPath)) {
2727
return URI.file(packPath).toString();
28+
} else if (_rootFolder && _relativeToFolder.startsWith(_rootFolder) && (_relativeToFolder.length !== _rootFolder.length)) {
29+
return resolvePathToModule(_moduleName, dirname(_relativeToFolder), _rootFolder);
2830
}
2931
return undefined;
3032
}
@@ -61,7 +63,13 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
6163
ref = ref.substring(1);
6264
if (startsWith(base, 'file://')) {
6365
const moduleName = getModuleNameFromPath(ref);
64-
const modulePath = resolvePathToModule(moduleName, base);
66+
const rootFolderUri = getRootFolder();
67+
let rootFolder;
68+
if (rootFolderUri) {
69+
rootFolder = URI.parse(rootFolderUri).fsPath;
70+
}
71+
const documentFolder = dirname(URI.parse(base).fsPath);
72+
const modulePath = resolvePathToModule(moduleName, documentFolder, rootFolder);
6573
if (modulePath) {
6674
const pathWithinModule = ref.substring(moduleName.length + 1);
6775
return url.resolve(modulePath, pathWithinModule);

0 commit comments

Comments
 (0)