Skip to content

Commit 49bf9f9

Browse files
committed
fix(importer): return null when the override file could not be found
Adhere to the node-sass API contract and return a null when the importer isn't applying its own changes. BREAKING CHANGE: existing behaviour might change if there were other importers being applied after this one.
1 parent ca3d0d5 commit 49bf9f9

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import fs from 'fs';
22
import path from 'path';
33
import {URL} from 'url';
44

5-
export default function (options = {}) {
6-
return function (url, prev) {
5+
export default function environmentImporterGenerator(options = {}) {
6+
return function environmentImporter(url, prev) {
77
let candidates;
88
try {
99
candidates = resolveCandidates(url, prev, options, this.options);
@@ -14,16 +14,15 @@ export default function (options = {}) {
1414
// Find the first entry which exists.
1515
let fileName = candidates.find(fileExists);
1616

17-
if (!fileName) {
17+
if (fileName) {
1818
// Could not resolve it, return the original url.
1919
return {
20-
file: url,
20+
file: fileName,
2121
};
2222
}
2323

24-
return {
25-
file: fileName,
26-
};
24+
// If an importer does not want to handle a particular path, it should return null.
25+
return null;
2726
};
2827
}
2928

@@ -33,7 +32,7 @@ export default function (options = {}) {
3332
* @param url
3433
* @return {boolean}
3534
*/
36-
export function isURL(url) {
35+
function isURL(url) {
3736
try {
3837
new URL(url);
3938
} catch (_) {
@@ -55,7 +54,7 @@ export function isURL(url) {
5554
* @param sassOptions.includePaths {string} The include path.
5655
* @return {[]} List of file candidates.
5756
*/
58-
export function resolveCandidates(url, prev, options, sassOptions) {
57+
function resolveCandidates(url, prev, options, sassOptions) {
5958
if (isURL(url)) {
6059
// Nothing to do.
6160
return [];
@@ -92,7 +91,7 @@ export function resolveCandidates(url, prev, options, sassOptions) {
9291
* @param fileName
9392
* @return {function(*): [string, string]}
9493
*/
95-
export function applyEnvironment(fileName) {
94+
function applyEnvironment(fileName) {
9695
return (environment) => {
9796
const dirname = path.dirname(fileName);
9897
// Only scss files will be suggested by this importer to enforce a convention contrary to:
@@ -115,7 +114,7 @@ export function applyEnvironment(fileName) {
115114
* @param path
116115
* @return {boolean}
117116
*/
118-
export function fileExists(path) {
117+
function fileExists(path) {
119118
try {
120119
fs.statSync(path);
121120
return true;

test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('conditionalImporter', () => {
8383
});
8484

8585
expect(result.css.toString().trim())
86-
.toBe('@import http://fonts.googleapis.com/css?family=Droid+Sans;@import url(theme);@import landscape screen and (orientation: landscape)');
86+
.toBe('@import "http://fonts.googleapis.com/css?family=Droid+Sans";@import url(theme);@import "landscape" screen and (orientation: landscape)');
8787
});
8888

8989
test('resolve with a custom resolver', function () {

0 commit comments

Comments
 (0)