sass-import_once 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTEyNDNkMmE1YTQwN2QwMzk5MWViYTRhMjhmMDYxMDBlYzQ2YjY2NA==
5
+ data.tar.gz: !binary |-
6
+ YjhjMjc0NGRkNjJhMzY3M2JkNzgzMWJiMjY1YTRkMGYwYTNiMjI0Yw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZTliNDgwNWM1YWU2Yzk1NjIxM2NiOWRkOTM4ZDM4M2VkNTM2NjdjODBiMGRh
10
+ YTQ2MjUwYzY1YWQwOTFkM2ZkMmVlZDVmNTBhNTE5Mzc1NWZiZjQ5MDYxYjM2
11
+ YWQ4YzlmN2E4MDJjNGIyMzVjNmNhYjVkYjc5M2QyNGJmNTAxMTk=
12
+ data.tar.gz: !binary |-
13
+ YjMzMGY4MDA1ZjdmMTAyM2VkMzU4NWY3ZmNjMGEyMmJhMGQwM2QwMzMwNWM3
14
+ OWMyZjY3OGNjMzViYmQ3OWFhZjY0NDM5YTE2MTJkNmI2ZTAwMzBiYTNhNzdm
15
+ YWEyNDI3NThiNmE0ZDVmN2FlNjI4MmNiN2U4ZGJhOWQ1N2U3NmM=
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ Initial release.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sass-import_once.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'sass'
@@ -0,0 +1,22 @@
1
+ # Sass Import Once Plugin
2
+
3
+ Sass import_once changes the behaviour of the sass @import directive making never require the same file twice.
4
+
5
+ ## Installation
6
+
7
+ $ gem install sass-import_once
8
+
9
+ ## Use with the Sass command line
10
+
11
+ $ sass -r sass-import_once --watch sass_dir:css_dir
12
+
13
+ ## Use with compass
14
+
15
+ Add the following to your compass configuration:
16
+
17
+ $ gem install 'compass-import_once'
18
+ require 'compass-import_once'
19
+
20
+ ## Stylesheet Syntax
21
+
22
+ The same as always :)
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
@@ -0,0 +1 @@
1
+ require 'sass/import_once'
@@ -0,0 +1,7 @@
1
+ module Sass
2
+ module ImportOnce
3
+ end
4
+ end
5
+
6
+ require 'sass/import_once/importer';
7
+ require 'sass/import_once/monkey_patches';
@@ -0,0 +1,141 @@
1
+ class Sass::ImportOnce::Importer < ::Sass::Importers::Filesystem
2
+
3
+ attr_accessor :root, :staleness_check, :imported, :original_filename
4
+
5
+ @@DEBUG = false
6
+
7
+ # Creates a new filesystem importer that imports files relative to a given path.
8
+ #
9
+ # @param root [String] The root path.
10
+ # This importer will import files relative to this path.
11
+ def initialize(root)
12
+ @imported = []
13
+ super(root)
14
+ end
15
+
16
+ # Same as Sass
17
+ COLORS = { :red => 31, :green => 32, :yellow => 33 }
18
+
19
+ def color(color, str)
20
+ raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
21
+
22
+ # Almost any real Unix terminal will support color,
23
+ # so we just filter for Windows terms (which don't set TERM)
24
+ # and not-real terminals, which aren't ttys.
25
+ return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
26
+ return "\e[#{COLORS[color]}m#{str}\e[0m"
27
+ end
28
+
29
+ def self.DEBUG=(value)
30
+ @@DEBUG = value
31
+ end
32
+
33
+ def debug(text, level = 1)
34
+ if @@DEBUG
35
+ puts "__ DEBUG: " + text.to_s
36
+ end
37
+ end
38
+
39
+ def update_imported_list(options)
40
+ @original_filename ||= options[:original_filename]
41
+
42
+ if @original_filename != options[:original_filename]
43
+ @imported = []
44
+ @original_filename = options[:original_filename]
45
+ end
46
+ end
47
+
48
+ def staleness_check?(options)
49
+ # as per sass/importers/filesystem.rb : 135, 148
50
+ # quote: "If options[:_line] exists, we're here due to an actual import in an import_node
51
+ # Otherwise, we're here via StalenessChecker."
52
+ # But it's not true, place two @import's to file A inside a file B and both will have _line set.
53
+ # So, unfortunately, for now, we'll have to check the stack
54
+
55
+ Kernel.caller.each {|call|
56
+ return true if call.include? "staleness_checker"
57
+ }
58
+
59
+ return false
60
+ end
61
+
62
+ # @see Base#find_relative
63
+ def find_relative(name, base, options)
64
+ update_imported_list options
65
+ debug color(:yellow, " finding relative ") + color(:green, name)
66
+ just_checking = staleness_check? options
67
+
68
+ # get the real file
69
+ real_file, syntax = Sass::Util.destructure(find_real_file(File.dirname(base), name, options))
70
+
71
+ if !real_file
72
+ raise color(:red, "Could not find a RELATIVE file '#{name}'. Imported at #{options[:original_filename]}:#{options[:_line]}")
73
+ return nil
74
+ end
75
+
76
+ unless just_checking
77
+
78
+ if @imported.include? real_file
79
+ debug color(:red, " already included")
80
+
81
+ return empty(options)
82
+ end
83
+
84
+ else
85
+ debug " Just checking"
86
+ end
87
+
88
+ f = _find(File.dirname(base), name, options)
89
+
90
+ if f
91
+ debug color(:yellow, " imported: ") + color(:green, real_file) unless just_checking
92
+ @imported << real_file unless just_checking
93
+ end
94
+
95
+ return f
96
+ end
97
+
98
+ # @see Base#find
99
+ def find(name, options)
100
+ debug color(:red, "find called")
101
+ update_imported_list options
102
+ debug "finding absolute " + color(:green, name)
103
+ just_checking = staleness_check? options
104
+
105
+ # get the real file
106
+ real_file, syntax = Sass::Util.destructure(find_real_file(@root, name, options))
107
+
108
+ if !real_file
109
+ raise color(:red, "Could not find file '#{name}'. Imported at #{options[:original_filename]}:#{options[:_line]}")
110
+ return nil
111
+ end
112
+
113
+ unless just_checking
114
+
115
+ if @imported.include? real_file
116
+ debug color(:red, "already included")
117
+ return empty(options)
118
+ end
119
+
120
+ else
121
+ debug "Just checking", 2
122
+ end
123
+
124
+ f = _find(@root, name, options)
125
+
126
+ if f
127
+ debug " imported: " + color(:green, real_file) unless just_checking
128
+ @imported << real_file unless just_checking
129
+ else
130
+ debug color(:red, "NOT FOUND!")
131
+ end
132
+
133
+ return f
134
+ end
135
+
136
+ protected
137
+
138
+ def empty(options)
139
+ return Sass::Engine.new("", options)
140
+ end
141
+ end
@@ -0,0 +1,11 @@
1
+ require 'sass/exec'
2
+
3
+ class Sass::Engine
4
+ alias old_initialize initialize
5
+
6
+ def initialize(template, options={})
7
+ options[:filesystem_importer] = Sass::ImportOnce::Importer
8
+
9
+ old_initialize(template, options)
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Sass
2
+ module ImportOnce
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/sass/import_once/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "sass-import_once"
6
+ s.version = Sass::ImportOnce::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["The Blacksmith (a.k.a. Saulo Vallory)"]
9
+ s.email = ["me@saulovallory.com"]
10
+ s.homepage = "http://theblacksmithhq.com/"
11
+ s.summary = %q{Makes Sass @import directives only import a file once.}
12
+ s.description = %q{
13
+ Sass ImportOnce Importer
14
+
15
+ This gem will MODIFY the default behaviour of Sass.
16
+
17
+ Sass @import directive imports the file requested everytime it's called
18
+ this has been default behaviour, as far as I know, from the beginning of
19
+ Sass's life. But sometimes, or most of the time, this behaviour isn't
20
+ desireable. When working with modularized sass styles and sass frameworks
21
+ out there we usually want a file to be imported only once.
22
+ }
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- test/*`.split("\n")
26
+ s.require_paths = ["lib"]
27
+
28
+ s.add_runtime_dependency 'sass', '>= 3.1'
29
+
30
+ end
@@ -0,0 +1,3 @@
1
+ #i.was.imported {
2
+ color: red;
3
+ }
@@ -0,0 +1,13 @@
1
+ // Let's import a file
2
+ @import "partials/imported";
3
+
4
+ // add some rules
5
+ $blue: #3bbfce;
6
+
7
+ div.bleue {
8
+ background-color: $blue;
9
+ }
10
+
11
+ // And import the same file again
12
+ // even if we change the way we reference the file, it shouldn't be imported
13
+ @import "partials/imported.scss";
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'sass'
3
+ require 'sass-import_once'
4
+
5
+ class SassImportOnceTest < Test::Unit::TestCase
6
+
7
+ def test_will_import_file_once
8
+ css = render_file("test.scss")
9
+ assert_equal 1, css.scan(/#i\.was\.imported/).count
10
+ end
11
+
12
+ private
13
+ def render_file(filename)
14
+ fixtures_dir = File.expand_path("fixtures", File.dirname(__FILE__))
15
+ full_filename = File.expand_path(filename, fixtures_dir)
16
+ syntax = File.extname(full_filename)[1..-1].to_sym
17
+ engine = Sass::Engine.new(File.read(full_filename),
18
+ :syntax => syntax,
19
+ :filename => full_filename,
20
+ :cache => false,
21
+ :read_cache => false,
22
+ :load_paths => [fixtures_dir])
23
+ engine.render
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sass-import_once
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - The Blacksmith (a.k.a. Saulo Vallory)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sass
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ description: ! "\n Sass ImportOnce Importer\n\n This gem will MODIFY the default
28
+ behaviour of Sass.\n\n Sass @import directive imports the file requested everytime
29
+ it's called\n this has been default behaviour, as far as I know, from the beginning
30
+ of\n Sass's life. But sometimes, or most of the time, this behaviour isn't \n
31
+ \ desireable. When working with modularized sass styles and sass frameworks\n
32
+ \ out there we usually want a file to be imported only once.\n "
33
+ email:
34
+ - me@saulovallory.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - CHANGELOG.markdown
41
+ - Gemfile
42
+ - README.markdown
43
+ - Rakefile
44
+ - lib/sass-import_once.rb
45
+ - lib/sass/import_once.rb
46
+ - lib/sass/import_once/importer.rb
47
+ - lib/sass/import_once/monkey_patches.rb
48
+ - lib/sass/import_once/version.rb
49
+ - sass-import_once.gemspec
50
+ - test/fixtures/partials/_imported.scss
51
+ - test/fixtures/test.scss
52
+ - test/sass_import_once_test.rb
53
+ homepage: http://theblacksmithhq.com/
54
+ licenses: []
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.1.10
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Makes Sass @import directives only import a file once.
76
+ test_files:
77
+ - test/fixtures/partials/_imported.scss
78
+ - test/fixtures/test.scss
79
+ - test/sass_import_once_test.rb