项目作者: fetch

项目描述 :
Node SASS Asset functions
高级语言: JavaScript
项目地址: git://github.com/fetch/node-sass-asset-functions.git
创建时间: 2015-07-16T11:26:16Z
项目社区:https://github.com/fetch/node-sass-asset-functions

开源协议:MIT License

下载


Node SASS Asset functions Build Status npmjs

To ease the transitioning from Compass to Libsass, this module provides some of Compass’ asset functions for node-sass

NB Please note that the functions option of node-sass is still experimental (>= v3.0.0).

Installation

  1. npm install --save[-dev] node-sass-asset-functions

Usage

Basic usage is as easy as setting the functions property:

  1. var sass = require('node-sass');
  2. var assetFunctions = require('node-sass-asset-functions');
  3. sass.render({
  4. functions: assetFunctions(),
  5. file: scss_filename,
  6. [, options..]
  7. }, function(err, result) { /*...*/ });

You can specify the paths of your resources using the following options (shown with defaults):

  1. {
  2. images_path: 'public/images',
  3. fonts_path: 'public/fonts',
  4. http_images_path: '/images',
  5. http_fonts_path: '/fonts'
  6. }

So if for example your images reside in public/img instead of images/images, you use it as follows:

  1. var sass = require('node-sass');
  2. var assetFunctions = require('node-sass-asset-functions');
  3. sass.render({
  4. functions: assetFunctions({
  5. images_path: 'public/img',
  6. http_images_path: '/img'
  7. }),
  8. file: scss_filename,
  9. [, options..]
  10. }, function(err, result) { /*...*/ });

Additional options

asset_host: a function which completes with a string used as asset host.

  1. sass.render({
  2. functions: assetFunctions({
  3. asset_host: function(http_path, done){
  4. done('http://assets.example.com');
  5. // or use the supplied path to calculate a host
  6. done('http://assets' + (http_path.length % 4) + '.example.com');
  7. }
  8. }),
  9. file: scss_filename,
  10. [, options..]
  11. }, function(err, result) { /*...*/ });

asset_cache_buster: a function to rewrite the asset path

When this function returns a string, it’s set as the query of the path. When returned an object, path and query will be used.

  1. sass.render({
  2. functions: assetFunctions({
  3. asset_cache_buster: function(http_path, real_path, done){
  4. done('v=123');
  5. }
  6. }),
  7. file: scss_filename,
  8. [, options..]
  9. }, function(err, result) { /*...*/ });
A more advanced example:

Here we include the file’s hexdigest in the path, using the hexdigest module.

For example, /images/myimage.png would become /images/myimage-8557f1c9b01dd6fd138ba203e6a953df6a222af3.png.

  1. var path = require('path')
  2. , fs = require('fs')
  3. , hexdigest = require('hexdigest');
  4. sass.render({
  5. functions: assetFunctions({
  6. asset_cache_buster: function(http_path, real_path, done){
  7. hexdigest(real_path, 'sha1', function(err, digest) {
  8. if (err) {
  9. // an error occurred, maybe the file doesn't exists.
  10. // Calling `done` without arguments will result in an unmodified path.
  11. done();
  12. } else {
  13. var extname = path.extname(http_path)
  14. , basename = path.basename(http_path, extname);
  15. var new_name = basename + '-' + digest + extname;
  16. done({path: path.join(path.dirname(http_path), new_name), query: null});
  17. }
  18. });
  19. }
  20. }),
  21. file: scss_filename,
  22. [, options..]
  23. }, function(err, result) { /*...*/ });

Available functions

  • image-url($filename: null, $only_path: false)
  • image-width($filename: null)
  • image-height($filename: null)
  • font-url($filename: null, $only-path: false)
  • font-files($filenames...)
  • and more to come

Usage with Grunt

Using this module with Grunt is just as easy:

  1. var assetFunctions = require('node-sass-asset-functions');
  2. module.exports = function(grunt){
  3. grunt.initConfig({
  4. // ...
  5. sass: {
  6. options: {
  7. functions: assetFunctions()
  8. },
  9. dist: {
  10. 'public/stylesheets/application.css': 'app/assets/stylesheets/application.css.scss'
  11. }
  12. }
  13. // ...
  14. });
  15. };

See also

node-sass-css-importer: Import CSS files into node-sass, just like sass-css-importer did for Compass

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request