1
- import { dirname , join } from "path" ;
2
1
import type { DocumentContext } from "vscode-css-languageservice" ;
3
- import { URI } from "vscode-uri" ;
4
- import { FileSystemProvider } from "../../shared/file-system" ;
5
- import type { NodeFileSystem } from "../../shared/node-file-system" ;
6
-
7
- /*---------------------------------------------------------------------------------------------
8
- * Copyright (c) Microsoft Corporation. All rights reserved.
9
- * Licensed under the MIT License.
10
- * See https://github.com/microsoft/vscode/blob/main/LICENSE.txt for license information.
11
- *--------------------------------------------------------------------------------------------*/
12
- function getModuleNameFromPath ( path : string ) {
13
- // If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first isntance of '/'
14
- if ( path . startsWith ( "@" ) ) {
15
- return path . slice ( 0 , Math . max ( 0 , path . indexOf ( "/" , path . indexOf ( "/" ) + 1 ) ) ) ;
16
- }
17
-
18
- return path . slice ( 0 , Math . max ( 0 , path . indexOf ( "/" ) ) ) ;
19
- }
20
-
21
- function resolvePathToModule (
22
- moduleName : string ,
23
- relativeTo : string ,
24
- fs : FileSystemProvider ,
25
- ) : string | undefined {
26
- // Resolve the module relative to the document. We can't use `require` here as the code is webpacked.
27
- // fsPath use is OK here since we never reach this function unless the URI is a file://.
28
- const documentFolder = dirname ( URI . parse ( relativeTo ) . fsPath ) ;
29
-
30
- // Assume this path exists. If not, let VS Code deal with the "404" and have the user fix a typo or install node_modules.
31
- const packPath = join (
32
- documentFolder ,
33
- "node_modules" ,
34
- moduleName ,
35
- "package.json" ,
36
- ) ;
37
-
38
- if ( Object . prototype . hasOwnProperty . call ( fs , "existsSync" ) ) {
39
- if ( ( fs as NodeFileSystem ) . existsSync ( packPath ) ) {
40
- return URI . file ( packPath ) . toString ( ) ;
41
- }
42
- }
43
-
44
- return undefined ;
45
- }
46
-
47
- function resolve ( from : string , to : string ) : string {
48
- const resolvedUrl = new URL ( to , new URL ( from , "resolve://" ) ) ;
49
- if ( resolvedUrl . protocol === "resolve:" ) {
50
- // `from` is a relative URL.
51
- const { pathname, search, hash } = resolvedUrl ;
52
- return pathname + search + hash ;
53
- }
54
- return resolvedUrl . toString ( ) ;
55
- }
2
+ import { URI , Utils } from "vscode-uri" ;
56
3
57
4
export function buildDocumentContext (
58
5
documentUri : string ,
59
6
workspaceRoot : URI ,
60
- fs : FileSystemProvider ,
61
7
) : DocumentContext {
62
8
function getRootFolder ( ) : string | undefined {
63
9
let folderURI = workspaceRoot . toString ( ) ;
@@ -83,26 +29,8 @@ export function buildDocumentContext(
83
29
return folderUri + ref . slice ( 1 ) ;
84
30
}
85
31
}
86
-
87
- // Following [css-loader](https://github.com/webpack-contrib/css-loader#url)
88
- // and [sass-loader's](https://github.com/webpack-contrib/sass-loader#imports)
89
- // convention, if an import path starts with ~ then use node module resolution
90
- // *unless* it starts with "~/" as this refers to the user's home directory.
91
- if ( ref . startsWith ( "~" ) && ref [ 1 ] !== "/" ) {
92
- ref = ref . slice ( 1 ) ;
93
- if ( base . startsWith ( "file://" ) ) {
94
- const moduleName = getModuleNameFromPath ( ref ) ;
95
- const modulePath = resolvePathToModule ( moduleName , base , fs ) ;
96
- if ( modulePath ) {
97
- const pathWithinModule = ref . slice (
98
- Math . max ( 0 , moduleName . length + 1 ) ,
99
- ) ;
100
- return resolve ( modulePath , pathWithinModule ) ;
101
- }
102
- }
103
- }
104
-
105
- return resolve ( base , ref ) ;
32
+ base = base . substr ( 0 , base . lastIndexOf ( "/" ) + 1 ) ;
33
+ return Utils . resolvePath ( URI . parse ( base ) , ref ) . toString ( ) ;
106
34
} ,
107
35
} ;
108
36
}
0 commit comments