Skip to content

Commit 24c91c5

Browse files
committed
Import project .env and fix up SASS_PATH
1 parent 8698439 commit 24c91c5

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"trailingComma": "all"
4848
},
4949
"dependencies": {
50+
"dotenv": "^8.1.0",
5051
"icss-utils": "^4.1.1",
5152
"less": "^3.9.0",
5253
"lodash": "^4.17.14",
@@ -57,6 +58,7 @@
5758
"sass": "^1.22.4"
5859
},
5960
"devDependencies": {
61+
"@types/dotenv": "^6.1.1",
6062
"@types/jest": "^24.0.15",
6163
"@types/less": "^3.0.0",
6264
"@types/lodash": "^4.14.136",

src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Options } from './options';
99
import { createLogger } from './helpers/logger';
1010
import * as postcss from 'postcss';
1111
import * as postcssIcssSelectors from 'postcss-icss-selectors';
12+
import * as dotenv from 'dotenv';
1213

1314
const removePlugin = postcss.plugin('remove-mixins', () => (css) => {
1415
css.walkRules((rule) => {
@@ -184,6 +185,26 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
184185
};
185186
}
186187

188+
// apply .env file at project root to current process environment ++ TODO: Instead, manually open .env and parse just the SASS_PATH part
189+
const projectDir = info.project.getCurrentDirectory();
190+
dotenv.config({ path: path.resolve(projectDir, '.env') });
191+
192+
// manually convert relative paths in SASS_PATH to absolute, lest they be resolved relative to process.cwd which would almost certainly be wrong
193+
if (process.env.SASS_PATH) {
194+
const sassPaths = process.env.SASS_PATH.split(path.delimiter);
195+
196+
for (
197+
var i = 0, currPath = sassPaths[i];
198+
i < sassPaths.length;
199+
currPath = sassPaths[++i]
200+
) {
201+
if (path.isAbsolute(currPath)) continue;
202+
sassPaths[i] = path.resolve(projectDir, currPath); // resolve relative path against project directory
203+
}
204+
// update SASS_PATH with new paths
205+
process.env.SASS_PATH = sassPaths.join(path.delimiter);
206+
}
207+
187208
return info.languageService;
188209
}
189210

0 commit comments

Comments
 (0)