Skip to content

Commit

Permalink
Allow Sass to import files even if they don’t have an item
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Dec 1, 2018
1 parent 9cbd2ab commit 459d701
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
11 changes: 6 additions & 5 deletions nanoc/lib/nanoc/filters/sass/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def find_relative(name, base_identifier, options)
return unless raw_filename

item = raw_filename_to_item(raw_filename)
# it doesn't make sense to import a file, from Nanoc's content if the corresponding item has been deleted
raise "unable to map #{raw_filename} to any item" if item.nil?

filter.depend_on([item])
content = item ? item.raw_content : File.read(raw_filename)
filename = item ? item.identifier.to_s : raw_filename

filter.depend_on([item]) if item

options[:syntax] = syntax
options[:filename] = item.identifier.to_s
options[:filename] = filename
options[:importer] = self
::Sass::Engine.new(item.raw_content, options)
::Sass::Engine.new(content, options)
end

def find(identifier, options)
Expand Down
29 changes: 28 additions & 1 deletion nanoc/spec/nanoc/filters/sass_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@
rep.raw_paths = rep.paths = { last: [Dir.getwd + '/output/style/main.sass'] }
end
end

let(:item_main_sourcemap_rep) do
Nanoc::Int::ItemRep.new(item_main, :sourcemap).tap do |rep|
rep.raw_paths = rep.paths = { last: [Dir.getwd + '/output/style/main.sass.map'] }
end
end

let(:item_main_view) { Nanoc::CompilationItemView.new(item_main, view_context) }
let(:item_main_default_rep_view) { Nanoc::CompilationItemRepView.new(item_main_default_rep, view_context) }
let(:item_main_sourcemap_rep_view) { Nanoc::CompilationItemRepView.new(item_main_sourcemap_rep, view_context) }
Expand Down Expand Up @@ -122,9 +124,11 @@
reps << item_main_sourcemap_rep
end
end

let(:dependency_tracker) { Nanoc::Int::DependencyTracker.new(dependency_store) }
let(:dependency_store) { Nanoc::Int::DependencyStore.new(empty_items, empty_layouts, config) }
let(:compilation_context) { double(:compilation_context) }

let(:snapshot_repo) do
Nanoc::Int::SnapshotRepo.new.tap do |repo|
repo.set(reps[item_blue].first, :last, Nanoc::Int::TextualContent.new('.blue { color: blue }'))
Expand Down Expand Up @@ -225,7 +229,7 @@
end
end

context 'importing a file for which an item does not exist' do
context 'importing (using regular load paths) a file for which an item does not exist' do
before { File.write('_external.scss', 'body { font: 100%; }') }

context 'load_path set' do
Expand All @@ -248,6 +252,29 @@
end
end

context 'importing (using the importer) a file for which an item does not exist' do
before { File.write('_external.scss', 'body { font: 100%; }') }

context 'load_path set' do
it 'can import by relative path' do
expect(sass.setup_and_run('@import "../../_external"', load_paths: ['.']))
.to match(/\Abody\s+\{\s*font:\s+100%;?\s*\}\s*\z/)
end

it 'creates no dependency' do
expect { sass.setup_and_run('@import external', load_paths: ['.']) }
.to create_dependency_from(item_main_view).onto([instance_of(Nanoc::Int::ItemCollection)])
end
end

context 'load_path not set' do
it 'cannot import by relative path' do
expect { sass.setup_and_run('@import external') }
.to raise_error(::Sass::SyntaxError, /File to import not found/)
end
end
end

context 'importing by identifier or pattern' do
it 'can import by identifier' do
expect(sass.setup_and_run('@import /style/colors/blue.*'))
Expand Down
22 changes: 22 additions & 0 deletions nanoc/spec/nanoc/regressions/gh_1378_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

describe 'GH-1378', site: true, stdio: true do
before do
FileUtils.mkdir_p('content')
File.write('outside.scss', 'p { color: red; }')
File.write('content/style.scss', '@import "../outside.scss";')

File.write('Rules', <<~EOS)
compile '/*' do
filter :sass, syntax: :scss
write ext: 'css'
end
EOS
end

example do
expect { Nanoc::CLI.run([]) }
.not_to change { File.file?('output/test.md') }
.from(false)
end
end

0 comments on commit 459d701

Please sign in to comment.