Skip to content

Commit c9f17cf

Browse files
PsiRadishmrmckeb
authored andcommitted
Add SASS_PATH support (mrmckeb#51)
1 parent 3b3260f commit c9f17cf

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function getPostCssConfig(dir: string) {
2828
}
2929
}
3030

31+
const sassPathRegex = /^SASS_PATH=(.+)/m;
32+
3133
function init({ typescript: ts }: { typescript: typeof ts_module }) {
3234
let _isCSS: isCSSFn;
3335

@@ -184,6 +186,34 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
184186
};
185187
}
186188

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+
187217
return info.languageService;
188218
}
189219

0 commit comments

Comments
 (0)