@@ -16,12 +16,25 @@ import { StylesheetLanguage, StylesheetPluginOptions } from './stylesheet-plugin
1616
1717let sassService : SassCompiler | undefined ;
1818let sassServicePromise : Promise < SassCompiler > | undefined ;
19+ let resolutionCache : MemoryCache < URL | null > | undefined ;
20+ let packageRootCache : MemoryCache < string | null > | undefined ;
1921
2022function isSassException ( error : unknown ) : error is Exception {
2123 return ! ! error && typeof error === 'object' && 'sassMessage' in error ;
2224}
2325
26+ export function resetSassWorkerPoolCaches ( ) : void {
27+ resolutionCache ?. clear ( ) ;
28+ packageRootCache ?. clear ( ) ;
29+ if ( sassService ) {
30+ sassService . clearCache ( ) ;
31+ } else if ( sassServicePromise ) {
32+ void sassServicePromise . then ( ( service ) => service . clearCache ( ) ) ;
33+ }
34+ }
35+
2436export function shutdownSassWorkerPool ( ) : void {
37+ resetSassWorkerPoolCaches ( ) ;
2538 if ( sassService ) {
2639 void sassService . close ( ) ;
2740 sassService = undefined ;
@@ -91,14 +104,15 @@ async function compileString(
91104 }
92105 }
93106
94- // Cache is currently local to individual compile requests.
95- // Caching follows Sass behavior where a given url will always resolve to the same value
96- // regardless of its importer's path.
107+ // Caching follows Sass behavior where a given package url will always resolve to the same value
108+ // regardless of its importer's path. Relative paths are qualified with the containing URL.
97109 // A null value indicates that the cached resolution attempt failed to find a location and
98110 // later stage resolution should be attempted. This avoids potentially expensive repeat
99111 // failing resolution attempts.
100- const resolutionCache = new MemoryCache < URL | null > ( ) ;
101- const packageRootCache = new MemoryCache < string | null > ( ) ;
112+ resolutionCache ??= new MemoryCache < URL | null > ( ) ;
113+ packageRootCache ??= new MemoryCache < string | null > ( ) ;
114+ const currentResolutionCache = resolutionCache ;
115+ const currentPackageRootCache = packageRootCache ;
102116 const warnings : PartialMessage [ ] = [ ] ;
103117 const { silenceDeprecations, futureDeprecations, fatalDeprecations } = options . sass ?? { } ;
104118
@@ -116,8 +130,12 @@ async function compileString(
116130 quietDeps : true ,
117131 importers : [
118132 {
119- findFileUrl : ( url , options ) =>
120- resolutionCache . getOrCreate ( url , async ( ) => {
133+ findFileUrl : ( url , options ) => {
134+ const cacheKey = url . startsWith ( 'pkg:' )
135+ ? url
136+ : `${ options . containingUrl ?. href ?? '' } :${ url } ` ;
137+
138+ return currentResolutionCache . getOrCreate ( cacheKey , async ( ) => {
121139 const result = await resolveUrl ( url , options ) ;
122140 if ( result . path ) {
123141 return pathToFileURL ( result . path ) ;
@@ -128,12 +146,16 @@ async function compileString(
128146
129147 // Caching package root locations is particularly beneficial for `@material/*` packages
130148 // which extensively use deep imports.
131- const packageRoot = await packageRootCache . getOrCreate ( packageName , async ( ) => {
132- // Use the required presence of a package root `package.json` file to resolve the location
133- const packageResult = await resolveUrl ( packageName + '/package.json' , options ) ;
149+ const packageRootKey = `${ options . containingUrl ?. href ?? '' } :${ packageName } ` ;
150+ const packageRoot = await currentPackageRootCache . getOrCreate (
151+ packageRootKey ,
152+ async ( ) => {
153+ // Use the required presence of a package root `package.json` file to resolve the location
154+ const packageResult = await resolveUrl ( packageName + '/package.json' , options ) ;
134155
135- return packageResult . path ? dirname ( packageResult . path ) : null ;
136- } ) ;
156+ return packageResult . path ? dirname ( packageResult . path ) : null ;
157+ } ,
158+ ) ;
137159
138160 // Package not found could be because of an error or the specifier is intended to be found
139161 // via a later stage of the resolution process (`loadPaths`, etc.).
@@ -145,7 +167,8 @@ async function compileString(
145167
146168 // Not found
147169 return null ;
148- } ) ,
170+ } ) ;
171+ } ,
149172 } ,
150173 ] ,
151174 logger : {
0 commit comments