@@ -8,6 +8,32 @@ import type { URI } from 'vscode-uri'
88const textCache = new Map < string , Promise < string | undefined > > ( )
99const jsonCache = new Map < string , Promise < any > > ( )
1010
11+ export type CreateNpmFileSystemOptions = {
12+ getPackageLatestVersionUrl ?: ( pkgName : string ) => string
13+ getPackageDirectoryUrl ?: (
14+ pkgName : string ,
15+ pkgVersion : string ,
16+ pkgPath : string ,
17+ ) => string
18+ getPackageFileTextUrl ?: (
19+ path : string ,
20+ pkgName : string ,
21+ pkgVersion : string | undefined ,
22+ pkgPath : string ,
23+ ) => string
24+ }
25+
26+ const defaultUnpkgOptions : Required < CreateNpmFileSystemOptions > = {
27+ getPackageLatestVersionUrl : ( pkgName : string ) =>
28+ `https://unpkg.com/${ pkgName } @latest/package.json` ,
29+ getPackageDirectoryUrl : (
30+ pkgName : string ,
31+ pkgVersion : string ,
32+ pkgPath : string ,
33+ ) => `https://unpkg.com/${ pkgName } @${ pkgVersion } /${ pkgPath } /?meta` ,
34+ getPackageFileTextUrl : ( path : string ) => `https://unpkg.com/${ path } ` ,
35+ }
36+
1137export function createNpmFileSystem (
1238 getCdnPath = ( uri : URI ) : string | undefined => {
1339 if ( uri . path === '/node_modules' ) {
@@ -18,7 +44,14 @@ export function createNpmFileSystem(
1844 } ,
1945 getPackageVersion ?: ( pkgName : string ) => string | undefined ,
2046 onFetch ?: ( path : string , content : string ) => void ,
47+ options ?: CreateNpmFileSystemOptions ,
2148) : FileSystem {
49+ const {
50+ getPackageDirectoryUrl = defaultUnpkgOptions . getPackageDirectoryUrl ,
51+ getPackageFileTextUrl = defaultUnpkgOptions . getPackageFileTextUrl ,
52+ getPackageLatestVersionUrl = defaultUnpkgOptions . getPackageLatestVersionUrl ,
53+ } = options || { }
54+
2255 const fetchResults = new Map < string , Promise < string | undefined > > ( )
2356 const statCache = new Map < string , { type : FileType } > ( )
2457 const dirCache = new Map < string , [ string , FileType ] [ ] > ( )
@@ -128,7 +161,7 @@ export function createNpmFileSystem(
128161 if ( resolvedVersion === 'latest' ) {
129162 try {
130163 const data = await fetchJson < { version : string } > (
131- `https://unpkg.com/ ${ pkgName } @ ${ resolvedVersion } /package.json` ,
164+ getPackageLatestVersionUrl ( pkgName ) ,
132165 )
133166 if ( data ?. version ) {
134167 actualVersion = data . version
@@ -138,8 +171,7 @@ export function createNpmFileSystem(
138171 }
139172 }
140173
141- const endpoint = `https://unpkg.com/${ pkgName } @${ actualVersion } /${ pkgPath } /?meta`
142-
174+ const endpoint = getPackageDirectoryUrl ( pkgName , actualVersion , pkgPath )
143175 try {
144176 const data = await fetchJson < {
145177 files : {
@@ -202,7 +234,9 @@ export function createNpmFileSystem(
202234 if ( ( await _stat ( path ) ) ?. type !== ( 1 satisfies FileType . File ) ) {
203235 return
204236 }
205- const text = await fetchText ( `https://unpkg.com/${ path } ` )
237+ const text = await fetchText (
238+ getPackageFileTextUrl ( path , pkgName , _version , pkgFilePath ) ,
239+ )
206240 if ( text !== undefined ) {
207241 onFetch ?.( path , text )
208242 }
0 commit comments