Skip to content

Commit 08a5294

Browse files
committed
Parse .env instead of importing it
1 parent 24c91c5 commit 08a5294

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"trailingComma": "all"
4848
},
4949
"dependencies": {
50-
"dotenv": "^8.1.0",
5150
"icss-utils": "^4.1.1",
5251
"less": "^3.9.0",
5352
"lodash": "^4.17.14",
@@ -58,7 +57,6 @@
5857
"sass": "^1.22.4"
5958
},
6059
"devDependencies": {
61-
"@types/dotenv": "^6.1.1",
6260
"@types/jest": "^24.0.15",
6361
"@types/less": "^3.0.0",
6462
"@types/lodash": "^4.14.136",

src/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ 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';
1312

1413
const removePlugin = postcss.plugin('remove-mixins', () => (css) => {
1514
css.walkRules((rule) => {
@@ -29,6 +28,8 @@ function getPostCssConfig(dir: string) {
2928
}
3029
}
3130

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

@@ -185,23 +186,29 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
185186
};
186187
}
187188

188-
// apply .env file at project root to current process environment ++ TODO: Instead, manually open .env and parse just the SASS_PATH part
189189
const projectDir = info.project.getCurrentDirectory();
190-
dotenv.config({ path: path.resolve(projectDir, '.env') });
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+
const dotenv = fs.readFileSync(dotenvPath, { encoding: 'utf8' });
196+
const sassPathMatch = sassPathRegex.exec(dotenv);
191197

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);
198+
if (sassPathMatch && sassPathMatch[1]) {
199+
const sassPaths = sassPathMatch[1].split(path.delimiter);
195200

201+
// Manually convert relative paths in SASS_PATH to absolute,
202+
// lest they be resolved relative to process.cwd which would almost certainly be wrong
196203
for (
197204
var i = 0, currPath = sassPaths[i];
198205
i < sassPaths.length;
199206
currPath = sassPaths[++i]
200207
) {
201208
if (path.isAbsolute(currPath)) continue;
202-
sassPaths[i] = path.resolve(projectDir, currPath); // resolve relative path against project directory
209+
sassPaths[i] = path.resolve(projectDir, currPath); // resolve path relative to project directory
203210
}
204-
// update SASS_PATH with new paths
211+
// join modified array and assign to environment SASS_PATH
205212
process.env.SASS_PATH = sassPaths.join(path.delimiter);
206213
}
207214

0 commit comments

Comments
 (0)