import SASS partial over http instead of filesyste

2019-03-16 22:20发布

问题:

I have a rails project where I want to dynamically create a sass file based on user variables, (as a way for users to customize the site). I need to @import that file into the site's main sass file through http, since it's dynamically generated and not actually written to the filesystem.

How do I configure the :load_paths directive to tell SASS to look for that file over http instead of through the filesystem?

The documentation says it's possible but I can't find any examples online. http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#custom_importers

回答1:

This documentation is discussing the fact that you can implement your own importer; HTTP is being used as an example. Fortunately, it is not too difficult to do so.

Here, I've implemented a simple HTTP importer: https://gist.github.com/1111803

It doesn't cache as aggressively as it could, and you should be aware that Sass will use it in addition to the filesystem to look for all imports (if you use a framework like Compass, there are many of these). If you need more performance, you should probably cache the failures in this case. Still, it seems to work in my testing.

You can use it simply by requiring the sass_http.rb file and then adding it to the load path:

require 'sass_http'
Sass::Plugin.options[:load_paths] ||= []
Sass::Plugin.options[:load_paths] << Sass::Importers::HTTP.new("http://stylesheets.example.com/")

The path currently must refer to a directory (i.e. end in a slash). You should get all of the usual debugging information (with HTTP, rather than filesystem, paths).