Skip to content

Commit

Permalink
feat: support SASS-PATH env variable (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Apr 17, 2020
1 parent ddeff88 commit 8376179
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/getSassOptions.js
Expand Up @@ -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;
}
Expand Down
54 changes: 54 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -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):
Expand Down
33 changes: 33 additions & 0 deletions test/loader.test.js
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/sass/sass_path-env.sass
@@ -0,0 +1,2 @@
@import 'file'
@import 'other'
2 changes: 2 additions & 0 deletions test/sass/sass_path/file.sass
@@ -0,0 +1,2 @@
.foo
color: red
2 changes: 2 additions & 0 deletions test/sass/sass_path_other/other.sass
@@ -0,0 +1,2 @@
.bar
color: blue
2 changes: 2 additions & 0 deletions test/scss/sass_path-env.scss
@@ -0,0 +1,2 @@
@import 'file';
@import 'other';
3 changes: 3 additions & 0 deletions test/scss/sass_path/file.scss
@@ -0,0 +1,3 @@
.foo {
color: red;
}
3 changes: 3 additions & 0 deletions test/scss/sass_path_other/other.scss
@@ -0,0 +1,3 @@
.bar {
color: blue;
}

0 comments on commit 8376179

Please sign in to comment.