diff --git a/lib/sassc.rb b/lib/sassc.rb index 6d5bd74b..c3143020 100644 --- a/lib/sassc.rb +++ b/lib/sassc.rb @@ -1,6 +1,30 @@ # frozen_string_literal: true module SassC + # The global load paths for Sass files. This is meant for plugins and + # libraries to register the paths to their Sass stylesheets to that they may + # be `@imported`. This load path is used by every instance of {Sass::Engine}. + # They are lower-precedence than any load paths passed in via the + # {file:SASS_REFERENCE.md#load_paths-option `:load_paths` option}. + # + # If the `SASS_PATH` environment variable is set, + # the initial value of `load_paths` will be initialized based on that. + # The variable should be a colon-separated list of path names + # (semicolon-separated on Windows). + # + # Note that files on the global load path are never compiled to CSS + # themselves, even if they aren't partials. They exist only to be imported. + # + # @example + # SassC.load_paths << File.dirname(__FILE__ + '/sass') + # @return [Array] + def self.load_paths + @load_paths ||= if ENV['SASS_PATH'] + ENV['SASS_PATH'].split(SassC::Util.windows? ? ';' : ':') + else + [] + end + end end require_relative "sassc/version" diff --git a/lib/sassc/engine.rb b/lib/sassc/engine.rb index ea01477e..150ce022 100644 --- a/lib/sassc/engine.rb +++ b/lib/sassc/engine.rb @@ -132,8 +132,8 @@ def output_style end def load_paths - paths = @options[:load_paths] - paths.join(":") if paths + paths = (@options[:load_paths] || []) + SassC.load_paths + paths.join(":") if paths.any? end end end diff --git a/test/engine_test.rb b/test/engine_test.rb index 33a1f26f..6169e5cf 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -198,6 +198,31 @@ def test_load_paths ).render end + def test_global_load_paths + temp_dir("included_1") + temp_dir("included_2") + + temp_file("included_1/import_parent.scss", "$s: 30px;") + temp_file("included_2/import.scss", "@import 'import_parent'; $size: $s;") + temp_file("styles.scss", "@import 'import.scss'; .hi { width: $size; }") + + ::SassC.load_paths << "included_1" + ::SassC.load_paths << "included_2" + + assert_equal ".hi {\n width: 30px; }\n", Engine.new( + File.read("styles.scss"), + ).render + ::SassC.load_paths.clear + end + + def test_env_load_paths + expected_load_paths = [ "included_1", "included_2" ] + ::SassC.instance_eval { @load_paths = nil } + ENV['SASS_PATH'] = expected_load_paths.join(':') + assert_equal expected_load_paths, ::SassC.load_paths + ::SassC.load_paths.clear + end + def test_load_paths_not_configured temp_file("included_1/import_parent.scss", "$s: 30px;") temp_file("included_2/import.scss", "@import 'import_parent'; $size: $s;")