mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-22 11:19:32 +00:00
Node module resolution for CSS import
Add support for `@import "~bootstrap/dist/css/bootstrap";` as per https://github.com/webpack-contrib/sass-loader#imports Fixes Microsoft/vscode-css-languageservice#136
This commit is contained in:
@@ -8,6 +8,14 @@ import { endsWith, startsWith } from '../utils/strings';
|
||||
import * as url from 'url';
|
||||
import { WorkspaceFolder } from 'vscode-languageserver';
|
||||
|
||||
function getModuleNameFromPath(path: string) {
|
||||
// If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first isntance of '/'
|
||||
if (path[0] === '@') {
|
||||
return path.substring(0, path.indexOf('/', path.indexOf('/') + 1));
|
||||
}
|
||||
return path.substring(0, path.indexOf('/'));
|
||||
}
|
||||
|
||||
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
|
||||
function getRootFolder(): string | undefined {
|
||||
for (let folder of workspaceFolders) {
|
||||
@@ -32,6 +40,12 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ref[0] === '~' && ref[1] !== '/') {
|
||||
const moduleName = getModuleNameFromPath(ref.substring(1));
|
||||
const modulePath = require.resolve(moduleName, { paths: [base] });
|
||||
const pathInModule = ref.substring(moduleName.length + 2);
|
||||
return url.resolve(modulePath, pathInModule);
|
||||
}
|
||||
return url.resolve(base, ref);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user