diff --git a/src/getSassOptions.js b/src/getSassOptions.js index a78a5dfc..73ce1435 100644 --- a/src/getSassOptions.js +++ b/src/getSassOptions.js @@ -106,9 +106,14 @@ function getSassOptions(loaderContext, loaderOptions, content, implementation) { ? proxyCustomImporters(options.importer, resourcePath) : []; - options.includePaths = (options.includePaths || []).concat( - path.dirname(resourcePath) - ); + options.includePaths = [] + .concat(options.includePaths || []) + .concat( + process.env.SASS_PATH + ? process.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':') + : [] + ) + .concat(path.dirname(resourcePath)); return options; } diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 25bfa367..98aad299 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -224,6 +224,60 @@ SassError: expected \\"{\\".", exports[`loader should output an understandable error with a problem in "@use" (dart-sass) (scss): warnings 1`] = `Array []`; +exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (sass): css 1`] = ` +".foo { + color: red; +} + +.bar { + color: blue; +}" +`; + +exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (sass): errors 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (sass): warnings 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (scss): css 1`] = ` +".foo { + color: red; +} + +.bar { + color: blue; +}" +`; + +exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (scss): errors 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (dart-sass) (scss): warnings 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (sass): css 1`] = ` +".foo { + color: red; } + +.bar { + color: blue; } +" +`; + +exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (sass): errors 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (sass): warnings 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (scss): css 1`] = ` +".foo { + color: red; } + +.bar { + color: blue; } +" +`; + +exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (scss): errors 1`] = `Array []`; + +exports[`loader should respect the "SASS_PATH" environment variable (node-sass) (scss): warnings 1`] = `Array []`; + exports[`loader should throw an error with a explicit file and a file does not exist (dart-sass) (sass): errors 1`] = ` Array [ "ModuleBuildError: Module build failed (from ../src/cjs.js): diff --git a/test/loader.test.js b/test/loader.test.js index b9dd21dd..576c6e6a 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -655,6 +655,39 @@ describe('loader', () => { expect(getErrors(stats)).toMatchSnapshot('errors'); }); + it(`should respect the "SASS_PATH" environment variable (${implementationName}) (${syntax})`, async () => { + const OLD_SASS_PATH = process.env.SASS_PATH; + + process.env.SASS_PATH = + process.platform === 'win32' + ? `${path.resolve('test', syntax, 'sass_path')};${path.resolve( + 'test', + syntax, + 'sass_path_other' + )}` + : `${path.resolve('test', syntax, 'sass_path')}:${path.resolve( + 'test', + syntax, + 'sass_path_other' + )}`; + + const testId = getTestId('sass_path-env', syntax); + const options = { + implementation: getImplementationByName(implementationName), + }; + const compiler = getCompiler(testId, { loader: { options } }); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + const codeFromSass = getCodeFromSass(testId, options); + + expect(codeFromBundle.css).toBe(codeFromSass.css); + expect(codeFromBundle.css).toMatchSnapshot('css'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + + process.env.SASS_PATH = OLD_SASS_PATH; + }); + if (implementation === dartSass) { it(`should output an understandable error with a problem in "@use" (${implementationName}) (${syntax})`, async () => { const testId = getTestId('error-use', syntax); diff --git a/test/sass/sass_path-env.sass b/test/sass/sass_path-env.sass new file mode 100644 index 00000000..d11efd23 --- /dev/null +++ b/test/sass/sass_path-env.sass @@ -0,0 +1,2 @@ +@import 'file' +@import 'other' diff --git a/test/sass/sass_path/file.sass b/test/sass/sass_path/file.sass new file mode 100644 index 00000000..80288d3e --- /dev/null +++ b/test/sass/sass_path/file.sass @@ -0,0 +1,2 @@ +.foo + color: red diff --git a/test/sass/sass_path_other/other.sass b/test/sass/sass_path_other/other.sass new file mode 100644 index 00000000..16e90054 --- /dev/null +++ b/test/sass/sass_path_other/other.sass @@ -0,0 +1,2 @@ +.bar + color: blue diff --git a/test/scss/sass_path-env.scss b/test/scss/sass_path-env.scss new file mode 100644 index 00000000..68f5d397 --- /dev/null +++ b/test/scss/sass_path-env.scss @@ -0,0 +1,2 @@ +@import 'file'; +@import 'other'; diff --git a/test/scss/sass_path/file.scss b/test/scss/sass_path/file.scss new file mode 100644 index 00000000..a15c877a --- /dev/null +++ b/test/scss/sass_path/file.scss @@ -0,0 +1,3 @@ +.foo { + color: red; +} diff --git a/test/scss/sass_path_other/other.scss b/test/scss/sass_path_other/other.scss new file mode 100644 index 00000000..99b5418f --- /dev/null +++ b/test/scss/sass_path_other/other.scss @@ -0,0 +1,3 @@ +.bar { + color: blue; +}