2016-09-14 55 views
2

我使用Grunt与PHP和我使用一个软件包来版本我的css与日期戳为style.min.20160913230349.css与Grunt的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']); 
}; 
+0

你能给我一个'gruntfile.js'吗? – slorenzo

+0

您是否用'files:{'website-v2/css/style.min.css':['website-v2/css/all.css']}''和'grunt.registerTask('dist',[' cssmin:dist','assets_versioning']);'?像https://www.npmjs.com/package/grunt-assets-versioning#versioning-using-a-date – slorenzo

+0

我可以正确使用这些,但我需要更新我的链接在标签动态更新和引用新文件和路径名称 – ottz0

回答

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; 
} 

我希望有帮助。

+0

我想你已经把我放在了正确的方向 – ottz0