Skip to content

Commit

Permalink
Prevent glob importing from failing with empty dir
Browse files Browse the repository at this point in the history
In some circumstances it can be useful to already import the contents of
a directory (e.g. `@import 'objects/**/*.scss'`) although the directory
is empty, because there might be files added later in the process. Up
until now this was not possible and node-sass was throwing an error.
With this patch, empty directories are ignored and no error is thrown.

Fixes #161
  • Loading branch information
maoberlehner committed Jan 13, 2018
1 parent 4bfa6a2 commit 53bf377
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/node-sass-glob-importer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export = function globImporter() {

const filePaths = resolveGlobUrl(url, includePaths);

if (filePaths.length) {
if (filePaths) {
const contents = filePaths
.map((x: string) => `@import '${x}';`)
.join(`\n`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe(`resolveGlobUrl()`, () => {
const result = resolveGlobUrl(`test/url`);

expect(globMock.hasMagic).toBeCalled();
expect(result).toEqual([]);
expect(result).toEqual(null);
});

test(`It should return found glob file paths.`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export function resolveGlobUrlFactory(
.split(`\\`).join(`/`));
});
});

return [...filePaths];
}

return [...filePaths];
return null;
};
}
2 changes: 1 addition & 1 deletion packages/node-sass-magic-importer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export = function magicImporter(userOptions?: IMagicImporterOptions) {
filterPrefix = `${url.split(` from `)[0]} from `;
}

if (globFilePaths.length) {
if (globFilePaths) {
const contents = globFilePaths
.filter((x) => !store.has(getStoreId(x, selectorFilters, nodeFilters)))
.map((x) => `@import '${filterPrefix}${x}';`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type IResolveGlobUrl = (
url: string,
includePaths?: string[],
) => string[];
) => string[]|null;
Empty file added test/files/empty-dir/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions test/files/glob-import-empty-dir.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'empty-dir/*.scss';
11 changes: 11 additions & 0 deletions test/glob-importer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ describe(`globImporter()`, () => {
expect(result).toBe(expectedResult);
});

test(`Should not fail when dir is empty.`, () => {
const expectedResult = ``;

const result = sass.renderSync({
file: `test/files/glob-import-empty-dir.scss`,
importer: globImporter(),
}).css.toString();

expect(result).toBe(expectedResult);
});

test(`It should import glob files via CLI.`, async () => {
// tslint:disable-next-line max-line-length
const cmd = `node node_modules/.bin/node-sass --importer packages/node-sass-glob-importer/dist/cli.js test/files/glob-import.scss`;
Expand Down

1 comment on commit 53bf377

@Sebastian-Fitzner
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thx a lot!

Please sign in to comment.