From 24c91c576470acef3aaa015e0de2e724cd5ee4e7 Mon Sep 17 00:00:00 2001 From: Kyle Fiegener Date: Thu, 3 Oct 2019 20:07:00 -0700 Subject: [PATCH 1/3] Import project .env and fix up SASS_PATH --- package.json | 2 ++ src/index.ts | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/package.json b/package.json index 5cbc0fb..2dcc099 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "trailingComma": "all" }, "dependencies": { + "dotenv": "^8.1.0", "icss-utils": "^4.1.1", "less": "^3.9.0", "lodash": "^4.17.14", @@ -57,6 +58,7 @@ "sass": "^1.22.4" }, "devDependencies": { + "@types/dotenv": "^6.1.1", "@types/jest": "^24.0.15", "@types/less": "^3.0.0", "@types/lodash": "^4.14.136", diff --git a/src/index.ts b/src/index.ts index bb52c37..bd07908 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { Options } from './options'; import { createLogger } from './helpers/logger'; import * as postcss from 'postcss'; import * as postcssIcssSelectors from 'postcss-icss-selectors'; +import * as dotenv from 'dotenv'; const removePlugin = postcss.plugin('remove-mixins', () => (css) => { css.walkRules((rule) => { @@ -184,6 +185,26 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { }; } + // apply .env file at project root to current process environment ++ TODO: Instead, manually open .env and parse just the SASS_PATH part + const projectDir = info.project.getCurrentDirectory(); + dotenv.config({ path: path.resolve(projectDir, '.env') }); + + // manually convert relative paths in SASS_PATH to absolute, lest they be resolved relative to process.cwd which would almost certainly be wrong + if (process.env.SASS_PATH) { + const sassPaths = process.env.SASS_PATH.split(path.delimiter); + + for ( + var i = 0, currPath = sassPaths[i]; + i < sassPaths.length; + currPath = sassPaths[++i] + ) { + if (path.isAbsolute(currPath)) continue; + sassPaths[i] = path.resolve(projectDir, currPath); // resolve relative path against project directory + } + // update SASS_PATH with new paths + process.env.SASS_PATH = sassPaths.join(path.delimiter); + } + return info.languageService; } From 08a52947928e386c5988c37cf83c617ac436bd15 Mon Sep 17 00:00:00 2001 From: Kyle Fiegener Date: Fri, 4 Oct 2019 16:01:24 -0700 Subject: [PATCH 2/3] Parse .env instead of importing it --- package.json | 2 -- src/index.ts | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 2dcc099..5cbc0fb 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "trailingComma": "all" }, "dependencies": { - "dotenv": "^8.1.0", "icss-utils": "^4.1.1", "less": "^3.9.0", "lodash": "^4.17.14", @@ -58,7 +57,6 @@ "sass": "^1.22.4" }, "devDependencies": { - "@types/dotenv": "^6.1.1", "@types/jest": "^24.0.15", "@types/less": "^3.0.0", "@types/lodash": "^4.14.136", diff --git a/src/index.ts b/src/index.ts index bd07908..0e35add 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,6 @@ import { Options } from './options'; import { createLogger } from './helpers/logger'; import * as postcss from 'postcss'; import * as postcssIcssSelectors from 'postcss-icss-selectors'; -import * as dotenv from 'dotenv'; const removePlugin = postcss.plugin('remove-mixins', () => (css) => { css.walkRules((rule) => { @@ -29,6 +28,8 @@ function getPostCssConfig(dir: string) { } } +const sassPathRegex = /^SASS_PATH=(.+)/m; + function init({ typescript: ts }: { typescript: typeof ts_module }) { let _isCSS: isCSSFn; @@ -185,23 +186,29 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { }; } - // apply .env file at project root to current process environment ++ TODO: Instead, manually open .env and parse just the SASS_PATH part const projectDir = info.project.getCurrentDirectory(); - dotenv.config({ path: path.resolve(projectDir, '.env') }); + const dotenvPath = path.resolve(projectDir, '.env'); // MAYBE TODO: custom .env file name/path in Options? + + // Manually open .env and parse just the SASS_PATH part, + // because we don't *need* to apply the full .env to this environment, + // and we are not sure doing so wouldn't have side effects + const dotenv = fs.readFileSync(dotenvPath, { encoding: 'utf8' }); + const sassPathMatch = sassPathRegex.exec(dotenv); - // manually convert relative paths in SASS_PATH to absolute, lest they be resolved relative to process.cwd which would almost certainly be wrong - if (process.env.SASS_PATH) { - const sassPaths = process.env.SASS_PATH.split(path.delimiter); + if (sassPathMatch && sassPathMatch[1]) { + const sassPaths = sassPathMatch[1].split(path.delimiter); + // Manually convert relative paths in SASS_PATH to absolute, + // lest they be resolved relative to process.cwd which would almost certainly be wrong for ( var i = 0, currPath = sassPaths[i]; i < sassPaths.length; currPath = sassPaths[++i] ) { if (path.isAbsolute(currPath)) continue; - sassPaths[i] = path.resolve(projectDir, currPath); // resolve relative path against project directory + sassPaths[i] = path.resolve(projectDir, currPath); // resolve path relative to project directory } - // update SASS_PATH with new paths + // join modified array and assign to environment SASS_PATH process.env.SASS_PATH = sassPaths.join(path.delimiter); } From f4c645a4348a124cc129673c96c7a3c242718899 Mon Sep 17 00:00:00 2001 From: Kyle Fiegener Date: Fri, 4 Oct 2019 18:11:31 -0700 Subject: [PATCH 3/3] Forgot try/catch around fs.readFileSync :disappointed_relieved: --- src/index.ts | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0e35add..d2e440a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -192,25 +192,27 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) { // Manually open .env and parse just the SASS_PATH part, // because we don't *need* to apply the full .env to this environment, // and we are not sure doing so wouldn't have side effects - const dotenv = fs.readFileSync(dotenvPath, { encoding: 'utf8' }); - const sassPathMatch = sassPathRegex.exec(dotenv); - - if (sassPathMatch && sassPathMatch[1]) { - const sassPaths = sassPathMatch[1].split(path.delimiter); - - // Manually convert relative paths in SASS_PATH to absolute, - // lest they be resolved relative to process.cwd which would almost certainly be wrong - for ( - var i = 0, currPath = sassPaths[i]; - i < sassPaths.length; - currPath = sassPaths[++i] - ) { - if (path.isAbsolute(currPath)) continue; - sassPaths[i] = path.resolve(projectDir, currPath); // resolve path relative to project directory + try { + const dotenv = fs.readFileSync(dotenvPath, { encoding: 'utf8' }); + const sassPathMatch = sassPathRegex.exec(dotenv); + + if (sassPathMatch && sassPathMatch[1]) { + const sassPaths = sassPathMatch[1].split(path.delimiter); + + // Manually convert relative paths in SASS_PATH to absolute, + // lest they be resolved relative to process.cwd which would almost certainly be wrong + for ( + var i = 0, currPath = sassPaths[i]; + i < sassPaths.length; + currPath = sassPaths[++i] + ) { + if (path.isAbsolute(currPath)) continue; + sassPaths[i] = path.resolve(projectDir, currPath); // resolve path relative to project directory + } + // join modified array and assign to environment SASS_PATH + process.env.SASS_PATH = sassPaths.join(path.delimiter); } - // join modified array and assign to environment SASS_PATH - process.env.SASS_PATH = sassPaths.join(path.delimiter); - } + } catch {} return info.languageService; }