Skip to content

Commit 8f72934

Browse files
committed
[css] resolve modules without require
1 parent 3460fb9 commit 8f72934

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ suite('Links', () => {
5454
}
5555

5656
function getTestResource(path: string) {
57-
return Uri.file(resolve(__dirname, '../../test/linkTestFixtures', path)).toString();
57+
return Uri.file(resolve(__dirname, '../../test/linksTestFixtures', path)).toString();
5858
}
5959

6060
test('url links', function () {
@@ -66,4 +66,14 @@ suite('Links', () => {
6666
[{ offset: 29, value: '"hello.html"', target: getTestResource('hello.html') }], testUri, folders
6767
);
6868
});
69+
70+
test('url links', function () {
71+
72+
let testUri = getTestResource('about.css');
73+
let folders = [{ name: 'x', uri: getTestResource('') }];
74+
75+
assertLinks('html { background-image: url("~foo/hello.html|")',
76+
[{ offset: 29, value: '"~foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
77+
);
78+
});
6979
});

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { DocumentContext } from 'vscode-css-languageservice';
77
import { endsWith, startsWith } from '../utils/strings';
88
import * as url from 'url';
99
import { WorkspaceFolder } from 'vscode-languageserver';
10+
import URI from 'vscode-uri';
11+
import { join, dirname } from 'path';
12+
import { existsSync } from 'fs';
1013

1114
function getModuleNameFromPath(path: string) {
1215
// If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first isntance of '/'
@@ -16,19 +19,14 @@ function getModuleNameFromPath(path: string) {
1619
return path.substring(0, path.indexOf('/'));
1720
}
1821

19-
function resolvePathToModule(_moduleName: string, _relativeTo: string) {
20-
// if we require.resolve('my-module') then it will follow the main property in the linked package.json
21-
// but we want the root of the module so resolve to the package.json and then trim
22-
let resolved;
23-
try {
24-
// resolved = require
25-
// .resolve(`${moduleName}/package.json`, { paths: [relativeTo] });
26-
throw new Error();
22+
function resolvePathToModule(_moduleName: string, _relativeTo: string): string | undefined {
23+
// 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');
26+
if (existsSync(packPath)) {
27+
return URI.file(packPath).toString();
2728
}
28-
catch (ex) {
29-
return null;
30-
}
31-
return resolved.slice(0, -12); // remove trailing `package.json`
29+
return undefined;
3230
}
3331

3432
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
@@ -59,7 +57,7 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
5957
// and [sass-loader's](https://github.com/webpack-contrib/sass-loader#imports)
6058
// convention, if an import path starts with ~ then use node module resolution
6159
// *unless* it starts with "~/" as this refers to the user's home directory.
62-
if (ref[0] === '~' && ref[1] !== '/') {
60+
if (ref[0] === '~' && ref[1] !== '/' && startsWith(base, 'file://')) {
6361
const moduleName = getModuleNameFromPath(ref.substring(1));
6462
const modulePath = resolvePathToModule(moduleName, base);
6563
if (modulePath) {

0 commit comments

Comments
 (0)