Skip to content

Commit 8376179

Browse files
feat: support SASS-PATH env variable (#836)
1 parent ddeff88 commit 8376179

File tree

9 files changed

+109
-3
lines changed

9 files changed

+109
-3
lines changed

src/getSassOptions.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ function getSassOptions(loaderContext, loaderOptions, content, implementation) {
106106
? proxyCustomImporters(options.importer, resourcePath)
107107
: [];
108108

109-
options.includePaths = (options.includePaths || []).concat(
110-
path.dirname(resourcePath)
111-
);
109+
options.includePaths = []
110+
.concat(options.includePaths || [])
111+
.concat(
112+
process.env.SASS_PATH
113+
? process.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':')
114+
: []
115+
)
116+
.concat(path.dirname(resourcePath));
112117

113118
return options;
114119
}

test/__snapshots__/loader.test.js.snap

+54
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,60 @@ SassError: expected \\"{\\".",
224224

225225
exports[`loader should output an understandable error with a problem in "@use" (dart-sass) (scss): warnings 1`] = `Array []`;
226226

227+
exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (sass): css 1`] = `
228+
".foo {
229+
color: red;
230+
}
231+
232+
.bar {
233+
color: blue;
234+
}"
235+
`;
236+
237+
exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (sass): errors 1`] = `Array []`;
238+
239+
exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (sass): warnings 1`] = `Array []`;
240+
241+
exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (scss): css 1`] = `
242+
".foo {
243+
color: red;
244+
}
245+
246+
.bar {
247+
color: blue;
248+
}"
249+
`;
250+
251+
exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (scss): errors 1`] = `Array []`;
252+
253+
exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (scss): warnings 1`] = `Array []`;
254+
255+
exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (sass): css 1`] = `
256+
".foo {
257+
color: red; }
258+
259+
.bar {
260+
color: blue; }
261+
"
262+
`;
263+
264+
exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (sass): errors 1`] = `Array []`;
265+
266+
exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (sass): warnings 1`] = `Array []`;
267+
268+
exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (scss): css 1`] = `
269+
".foo {
270+
color: red; }
271+
272+
.bar {
273+
color: blue; }
274+
"
275+
`;
276+
277+
exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (scss): errors 1`] = `Array []`;
278+
279+
exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (scss): warnings 1`] = `Array []`;
280+
227281
exports[`loader should throw an error with a explicit file and a file does not exist (dart-sass) (sass): errors 1`] = `
228282
Array [
229283
"ModuleBuildError: Module build failed (from ../src/cjs.js):

test/loader.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,39 @@ describe('loader', () => {
655655
expect(getErrors(stats)).toMatchSnapshot('errors');
656656
});
657657

658+
it(`should respect the "SASS_PATH" environment variable (${implementationName}) (${syntax})`, async () => {
659+
const OLD_SASS_PATH = process.env.SASS_PATH;
660+
661+
process.env.SASS_PATH =
662+
process.platform === 'win32'
663+
? `${path.resolve('test', syntax, 'sass_path')};${path.resolve(
664+
'test',
665+
syntax,
666+
'sass_path_other'
667+
)}`
668+
: `${path.resolve('test', syntax, 'sass_path')}:${path.resolve(
669+
'test',
670+
syntax,
671+
'sass_path_other'
672+
)}`;
673+
674+
const testId = getTestId('sass_path-env', syntax);
675+
const options = {
676+
implementation: getImplementationByName(implementationName),
677+
};
678+
const compiler = getCompiler(testId, { loader: { options } });
679+
const stats = await compile(compiler);
680+
const codeFromBundle = getCodeFromBundle(stats, compiler);
681+
const codeFromSass = getCodeFromSass(testId, options);
682+
683+
expect(codeFromBundle.css).toBe(codeFromSass.css);
684+
expect(codeFromBundle.css).toMatchSnapshot('css');
685+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
686+
expect(getErrors(stats)).toMatchSnapshot('errors');
687+
688+
process.env.SASS_PATH = OLD_SASS_PATH;
689+
});
690+
658691
if (implementation === dartSass) {
659692
it(`should output an understandable error with a problem in "@use" (${implementationName}) (${syntax})`, async () => {
660693
const testId = getTestId('error-use', syntax);

test/sass/sass_path-env.sass

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import 'file'
2+
@import 'other'

test/sass/sass_path/file.sass

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.foo
2+
color: red

test/sass/sass_path_other/other.sass

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.bar
2+
color: blue

test/scss/sass_path-env.scss

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import 'file';
2+
@import 'other';

test/scss/sass_path/file.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: red;
3+
}

test/scss/sass_path_other/other.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bar {
2+
color: blue;
3+
}

0 commit comments

Comments
 (0)