使用Grunt的CSS版本号

时间:2016-09-14 03:49:27

标签: css npm gruntjs

我正在使用Grunt与php并使用软件包来版本化我的css,日期戳为style.min.20160913230349.css

但是,如何找到一个将新字符串更新到我的php页面中的标题的包。 style.min.{grunt_this_string}.css

我正在寻找一个usemin,但看不出我是如何实现它的。

<link rel="stylesheet" media="all" href="/css/style.min.20160913230349.css" type="text/css"/>

Gruntfile.js

module.exports = function (grunt) {


  grunt.initConfig({


    /*=============================================
    =            Proxy server                    =
    =============================================*/
    php: {
      dist: {
        options: {
          hostname: 'sau.dev',
          base: 'website-v2', // Project root 
          keepalive: false,
          open: false
        }
      }
    },

    browserSync: {
      dev: {
        bsFiles: {
          src: [
            'website-v2/*.php',
            'website-v2/*/*.php',
            'website-v2/css/*.css'
          ]
        },
        options: {
          proxy: 'sau.dev',//<%= php.dist.options.hostname %>:<%= php.dist.options.port %>
              ghostMode: {
                  clicks: false,
                  forms: false,
                  scroll: true
              },
              logLevel: 'info',//client
              logConnections: true,
              watchTask: true,
              open: 'ui',
              xip: true,
        }
      }
    },

    watch: {
        html: {
          files: ['src/*.html'],
        }
    },

    cssmin: {
      dist: {
          files: {
             'website-v2/css/style.min.css': ['website-v2/css/all.css']
          }
      }
    },

    uglify: {
      dist: {
        files: {
          'website-v2/js/main.min.js': ['website-v2/js/jquery-     1.9.1.min.js','jquery-ui.min.js','main.js','featherlight.js']
        }
      }
    },

    assets_versioning: {
      options: {
        tag: 'date',
        dateFormat: 'YYYYMMDDHH',
        timezoneOffset: 10
      },
      dist: {
        options: {
          tasks: ['cssmin:dist']
        }
      },
    },

  });

  //grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-browser-sync');
  grunt.loadNpmTasks('grunt-php');
  grunt.loadNpmTasks('grunt-assets-versioning');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-string-replace');

  //grunt.loadNpmTasks('usemin');



  //Static Server
  grunt.registerTask('dist', ['versioning']);

  //Proxy Server
  grunt.registerTask('server', ['php:dist','browserSync','watch']);
};

1 个答案:

答案 0 :(得分:1)

在了解您的问题后,我认为您需要创建assets.map

您需要在gruntfile.js中添加此内容。

options: {
    ...
    versionsMapFile: 'assets.json',
    ...
}

我想相信grunt-assets-versioning创建一个采用该日期的版本。

您需要在html中替换提供assest.json的版本。 我建议您使用grunt-string-replace

replace: {
  dist: {
    options: {
      patterns: [
        {
          match: 'style.min.css',
          replacement: replacement()
        }
      ]
    },
    files: [
      {src: ['src/assets/index.html'], dest: 'build/index.html'}
    ]
  }
}

...

grunt.registerTask('versioning', ['assets_versioning']);
grunt.registerTask('replace', ['string-replace']

... 

function replacements() {
    var projectFile = "assets.json";

    if (!grunt.file.exists(projectFile)) {
        grunt.log.error("file " + projectFile + " not found");
        return true;//return false to abort the execution
    }
    var project = grunt.file.readJSON(projectFile);//get file as json object
    var version = project[0].version;
    var replace = ''

    // continue with the algorithm that you need

    return replace;
}

我希望有所帮助。