Add timestamps to compiled sass/scss

2019-02-09 05:59发布

Is it possible to automatically add a timestamp on the compiled CSS file using SASS? e.g.

/* CSS Compiled on: {date+time} */
...compiled css goes here...

I've checked the SASS and Compass docs but no mention of such a feature.

2条回答
仙女界的扛把子
2楼-- · 2019-02-09 06:31

If you are using the command line version of SCSS you can do the following:

  1. Install sass-timestamp

    gem install sass-timestamp

  2. Use it within your code like (see documentation for more information)

    /* Compiled on #{timestamp()} */

  3. Require it on the command line

    scss -r sass-timestamp ...

  4. Output will be

    /* Compiled on 2015-02-02 13:01:40 +0800 */

Note: Use #{unix_timestamp()} for a unix timestamp

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-09 06:46

I don't know of any built-in SASS or Compass feature for this, but it's not too hard to add a custom Ruby function to do it. (See the section entitled "Adding Custom Functions" in the SASS reference here.)

Your file with the Ruby function would look like this (let's call it "timestamp.rb"):

module Sass::Script::Functions
    def timestamp()
        return Sass::Script::String.new(Time.now.to_s)
    end
end

And you'd reference the new timestamp function in your SASS file like this:

/*!
 * CSS Compiled on: #{timestamp()}
 */

You just need to make sure your "timestamp.rb" file is loaded when you compile the SASS, either by requiring it from a Compass config file, or by using the --require parameter with the SASS command line. When all is said and done, you should get output like the following:

/*
 * CSS Compiled on: 2012-10-23 08:53:03 -0400
 */
查看更多
登录 后发表回答