简体   繁体   English

用于资产版本控制的Grunt插件

[英]Grunt plugin for assets versioning

I'm looking for a grunt plugin that will automatically change the references to static assets (js/css) inside an html file like this: 我正在寻找一个grunt插件,它会自动更改html文件中对静态资产(js / css)的引用,如下所示:

<link rel="stylesheet" type="text/css" href="style.css?v=12345678" />
<script src="script.js?v=12345678"></script>

I searched at the gruntjs.com/plugins -> "version", but it seems that all of them change the actual version of the files instead of references to them. 我搜索了gruntjs.com/plugins - >“version”,但似乎所有这些都改变了文件的实际版本而不是对它们的引用。

Am I missing it? 我错过了吗? Is there a plugin that can perform this task? 是否有可以执行此任务的插件?

For this I use grunt-filerev for the versionning and grunt-usemin for the automatic update of the references in source files. 为此,我使用grunt-filerev进行版本控制,使用grunt-usemin自动更新源文件中的引用。

These two modules works well together (usemin replacing references with a mapping provided by filerev) 这两个模块很好地协同工作(usemin用filerev提供的映射替换引用)

Hope this helps 希望这可以帮助

edit: a few code examples (only showing you what's interesting in your case): 编辑:一些代码示例(仅显示您的案例中有趣的内容):

I use usemin & filerev only when packaging my app (for deployment) : 我只在打包我的应用程序时使用usemin和filerev(用于部署):

In the head of my index.html, the following code tell usemin to take all the files between the build tag and agregate it into one named ie-shims.js 在我的index.html的头部,下面的代码告诉usemin获取build标记之间的所有文件并将其聚合成一个名为ie-shims.js

[...]
<!-- build:js /js/ie-shims.js -->
    <script src="/vendor/html5shiv/dist/html5shiv.js"></script>
    <script src="/vendor/respond/dest/respond.src.js"></script>
<!-- endbuild -->
[...]

Next, in my gruntfile.js, i have two node : 接下来,在我的gruntfile.js中,我有两个节点:

[...]
filerev: {
    options: {
        encoding: 'utf8',
        algorithm: 'md5',
        length: 8
    },
    source: {
        files: [{
            src: [
                'www/js/**/*.js',
                'www/assets/**/*.{jpg,jpeg,gif,png,ico}'
            ]
        }]
    }
},
useminPrepare: {
    html: 'src/index.html',
    options: {
        dest: 'www'
    }
},

// usemin has access to the revved files mapping through grunt.filerev.summary
usemin: {
    html: ['www/*.html'],
    css: ['www/css/**/*.css'],
    js: ['www/js/**/*.js'],
    options: {
        dirs: ['www'],
        assetsDirs: ['www'],
        patterns: {
            js: [
                [/["']([^:"']+\.(?:png|gif|jpe?g))["']/img, 'Image replacement in js files']
            ]
        }
    }
} [...]

Finally, I have a grunt task that put all that together : 最后,我有一个咕噜咕噜的任务,把所有这些放在一起:

grunt.registerTask('build', 'Build task, does everything', ['useminPrepare', 'filerev', 'usemin']);

Then, the generated HTML is like that : 然后,生成的HTML就像这样:

[...]
<script src="/js/ie-shims.9f790592.js"></script>
[...]

I found a neat post about keeping Grunt clean, that walks through an entire folder structure, Gruntfile.js config and task running, over at http://www.jayway.com/2014/01/20/clean-grunt/ . 我发现了一个关于保持Grunt干净的简洁帖子,它遍历整个文件夹结构,Gruntfile.js配置和任务运行,请访问http://www.jayway.com/2014/01/20/clean-grunt/ Your comment on the earlier answer is about folder structure, so it should also help with that, since the structure there doesn't have index.html file in the root either. 您对早期答案的评论是关于文件夹结构的,所以它也应该有帮助,因为那里的结构也没有index.html文件。

  1. Prep your html file as per the docs of grunt-usemin (and/or the post I linked) 根据grunt-usemin的文档(和/或我链接的帖子)准备你的html文件
  2. You need to add grunt-contrib-copy , so you can copy index_src.html and rename it to index.html ( used this for inspiration ), and proceed with the 'usemin' task on that. 您需要添加grunt-contrib-copy ,因此您可以复制index_src.html并将其重命名为index.html( 用于获取灵感 ),然后继续执行'usemin'任务。
  3. Change references to your assets to relative paths (ex. ../js/controller.js ) ../js/controller.js资产的引用更改为相对路径(例如../js/controller.js
  4. Then configure your Gruntfile.js like this: 然后像这样配置你的Gruntfile.js

     [...] useminPrepare: { html: 'templates/index.html', options: { dest: 'js' } }, // Concat - best to implement it this way for useminPrepare to inject the config concat: { options: { separator: ';' }, // dist configuration is provided by useminPrepare dist: {} }, // Uglify - best to implement it this way for useminPrepare to inject the config uglify: { // dist configuration is provided by useminPrepare dist: {} }, filerev: { options: { encoding: 'utf8', algorithm: 'md5', length: 20 }, source: { files: [{ src: [ 'js/**/*.js' ] }] } }, copy: { rename: { files: [ { cwd: 'templates', src: ['**/index_src.html'], dest: 'templates', rename: function(dest, src) { return dest + src.replace(/_src\\.html$/i, '.html'); } } ] } }, // usemin has access to the revved files mapping through grunt.filerev.summary usemin: { html: ['templates/index.html'], options: { assetsDirs: ['js'] } } [...] 

    I'm not a 100% sure about the regex to rename the file, but make a backup of the folder and give it a try. 我不是100%肯定重命名文件的正则表达式,但是备份文件夹并尝试一下。 Also, I'm answering as per the pastebin link you gave, which did not include any css files. 此外,我正在回答您给出的pastebin链接,其中不包含任何css文件。 If there are any, things get a bit complicated, so let me know. 如果有的话,事情会变得有点复杂,所以请告诉我。

  5. You could then use the grunt task Bixi suggested, but include your copy step (and concat & uglify) 然后你可以使用Bixi建议的grunt任务,但包括你的复制步骤(和concat&uglify)

     grunt.registerTask('build', 'Build task, does everything', [ 'useminPrepare', 'concat', 'uglify', 'copy:rename', 'filerev', 'usemin' ]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM