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:
Alasdair McLeay
2019-03-18 11:47:17 +00:00
parent 0eaf7b7ed0
commit f0b9a60a8f

View File

@@ -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);
},
};