From ad7760abf23e7960b371043ed397273fb1be147e Mon Sep 17 00:00:00 2001 From: John Harvey Date: Fri, 9 Mar 2018 22:46:43 +0000 Subject: [PATCH] Add support for _index files (#2456) Closes #690 --- doc-src/SASS_CHANGELOG.md | 5 +++++ doc-src/SASS_REFERENCE.md | 8 ++++++++ lib/sass/importers/filesystem.rb | 6 ++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc-src/SASS_CHANGELOG.md b/doc-src/SASS_CHANGELOG.md index b96bb1d7c6..2edc6c5ec4 100644 --- a/doc-src/SASS_CHANGELOG.md +++ b/doc-src/SASS_CHANGELOG.md @@ -3,6 +3,11 @@ * Table of contents {:toc} +## 3.6.0 (Unreleased) + +* Add support for importing an `_index.scss` or `_index.sass` file when + importing a directory. + ## 3.5.6 (Unreleased) * `var()` may now be passed in place of multiple arguments to `rgb()`, `rgba()`, diff --git a/doc-src/SASS_REFERENCE.md b/doc-src/SASS_REFERENCE.md index 89e9be3cad..4f2e18c40d 100644 --- a/doc-src/SASS_REFERENCE.md +++ b/doc-src/SASS_REFERENCE.md @@ -1349,6 +1349,14 @@ Note that you may not include a partial and a non-partial with the same name in the same directory. For example, `_colors.scss` may not exist alongside `colors.scss`. +#### Index Files + +If you write a file with the special name `_index.scss` or `_index.sass`, it +will be loaded if you import the directory that contains it. For example, if you +have `dir/_index.scss`, you can write `@import "dir";` and it will load your +file. However, if you have a file named `_dir.scss` *and* a file named +`dir/_index.scss`, `_dir.scss` will take precedence. + #### Nested `@import` Although most of the time it's most useful to just have `@import`s diff --git a/lib/sass/importers/filesystem.rb b/lib/sass/importers/filesystem.rb index 754dad9c50..d4d02dbe46 100644 --- a/lib/sass/importers/filesystem.rb +++ b/lib/sass/importers/filesystem.rb @@ -154,7 +154,9 @@ def find_real_file(dir, name, options) [Sass::Util.cleanpath(full_path).to_s, s] end end.flatten(1) - return if found.empty? + if found.empty? && split(name)[2].nil? && File.directory?("#{dir}/#{name}") + return find_real_file("#{dir}/#{name}", "index", options) + end if found.size > 1 && !@same_name_warnings.include?(found.first.first) found.each {|(f, _)| @same_name_warnings << f} @@ -202,7 +204,7 @@ def split(name) def _find(dir, name, options) full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options)) - return unless full_filename && File.readable?(full_filename) + return unless full_filename && File.file?(full_filename) && File.readable?(full_filename) # TODO: this preserves historical behavior, but it's possible # :filename should be either normalized to the native format