Last active
October 19, 2016 21:36
-
-
Save sebastiandeutsch/d253cb655bb2457c9ca54b65e63f3664 to your computer and use it in GitHub Desktop.
I'm trying to implement css modules for haml. I've added a processor for the attributes to archive that: https://github.com/sebastiandeutsch/haml/blob/feature/process_attributes_and_values/lib/haml/compiler.rb#L417. See implementation of the processor below. Now I have the problem that I need to set the context for the processor within a file on…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="test__container___1qISr"> | |
Hello World! | |
<div class="test__container___1qISr"> | |
Test | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"stylesheets/application.css": { | |
"container": [ | |
"application__container___1q3ub" | |
] | |
}, | |
"stylesheets/index.css": { | |
"container": [ | |
"index__container___Az6C0" | |
] | |
}, | |
"stylesheets/test.css": { | |
"container": [ | |
"test__container___1qISr" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CssModulesProcessor | |
def call(attr, value, options) | |
puts "********** CssModulesProcessor ************** called" | |
puts options.inspect | |
puts options[:css_module_context] | |
puts options.filename | |
puts "********** /CssModulesProcessor ************* called" | |
file = File.read(File.join(Rails.root, 'webpack', 'dist', 'css_classes.json')) | |
css_modules = JSON.parse(file) | |
if attr == 'class' | |
if css_modules[options[:css_module_context]].present? | |
value = css_modules[options[:css_module_context]][value] if css_modules[options[:css_module_context]].key?(value) | |
end | |
end | |
return attr, value | |
end | |
end | |
Haml::Template.options[:attr_processor] = CssModulesProcessor.new | |
Haml::Template.options[:css_module_context] = "stylesheets/application.css" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="index__container___Az6C0"> | |
Hello World! | |
<div class="test__container___1qISr"> | |
Test | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Haml::Template.options[:css_module_context] = "stylesheets/index.css" | |
.container | |
Hello World! | |
- Haml::Template.options[:css_module_context] = "stylesheets/test.css" | |
.container | |
Test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment