@@ -28,6 +28,8 @@ function getPostCssConfig(dir: string) {
28
28
}
29
29
}
30
30
31
+ const sassPathRegex = / ^ S A S S _ P A T H = ( .+ ) / m;
32
+
31
33
function init ( { typescript : ts } : { typescript : typeof ts_module } ) {
32
34
let _isCSS : isCSSFn ;
33
35
@@ -184,6 +186,34 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
184
186
} ;
185
187
}
186
188
189
+ const projectDir = info . project . getCurrentDirectory ( ) ;
190
+ const dotenvPath = path . resolve ( projectDir , '.env' ) ; // MAYBE TODO: custom .env file name/path in Options?
191
+
192
+ // Manually open .env and parse just the SASS_PATH part,
193
+ // because we don't *need* to apply the full .env to this environment,
194
+ // and we are not sure doing so wouldn't have side effects
195
+ try {
196
+ const dotenv = fs . readFileSync ( dotenvPath , { encoding : 'utf8' } ) ;
197
+ const sassPathMatch = sassPathRegex . exec ( dotenv ) ;
198
+
199
+ if ( sassPathMatch && sassPathMatch [ 1 ] ) {
200
+ const sassPaths = sassPathMatch [ 1 ] . split ( path . delimiter ) ;
201
+
202
+ // Manually convert relative paths in SASS_PATH to absolute,
203
+ // lest they be resolved relative to process.cwd which would almost certainly be wrong
204
+ for (
205
+ var i = 0 , currPath = sassPaths [ i ] ;
206
+ i < sassPaths . length ;
207
+ currPath = sassPaths [ ++ i ]
208
+ ) {
209
+ if ( path . isAbsolute ( currPath ) ) continue ;
210
+ sassPaths [ i ] = path . resolve ( projectDir , currPath ) ; // resolve path relative to project directory
211
+ }
212
+ // join modified array and assign to environment SASS_PATH
213
+ process . env . SASS_PATH = sassPaths . join ( path . delimiter ) ;
214
+ }
215
+ } catch { }
216
+
187
217
return info . languageService ;
188
218
}
189
219
0 commit comments