Gruntjs: grunt-usemin使用心得
grunt-usemin:
Replaces references to non-optimized scripts or stylesheets into a set of HTML files
usemin
exports 2 different tasks:
useminPrepare
prepares the configuration to transform specific construction (blocks) in the scrutinized file into a single line, targeting an optimized version of the filesusemin
replaces the blocks by the file they reference, and replaces all references to assets by their revisioned version if it is found on the disk. This target modifies the files it is working on.
Usually, useminPrepare
is launched first, then the steps of the transformation flow (for example, concat
, uglify
, and cssmin
), and then, in the end usemin
is launched.
我的工作目录
workspace/
——app/
——js/
——tpl/
Gruntfile.js配置
module.exports = function(grunt) { // Project configuration.
grunt.initConfig({ useminPrepare: {
html: ['app/tpl/**/*.html'],
options: {
// 测试发现这里指定的dest,是usemin引入资源的相对路径的开始
// 在usemin中设置assetsDirs,不是指定的相对路径
// List of directories where we should start to look for revved version of the assets referenced in the currently looked at file
dest: 'build/tpl' // string type
}
},
usemin: {
html: ['build/tpl/**/*.html'], // 注意此处是build/
options: {
assetsDirs: ['build/js']
}
},
copy: {
html: {
expand: true, // 需要该参数
cwd: 'app/',
src: ['tpl/**/*.html'], // 会把tpl文件夹+文件复制过去
dest: 'build'
}
} }); grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-usemin'); // 最后就是顺序了,没错concat,uglify在这里哦!
grunt.registerTask('default', [
'copy:html',
'useminPrepare',
'concat:generated',
'uglify:generated',
'usemin'
]); };
源html结构
<body>
<p>this is a grunt usemin</p> <script src="../js/globle.js"></script> <!-- build:js ../js/page.js -->
<script src="../js/libs.js"></script>
<script src="../js/page.js"></script>
<!-- endbuild -->
</body>
打包后的html
<body>
<p>this is a grunt usemin</p> <script src="../js/globle.js"></script> <script src="../js/page.js"></script>
</body>
感谢gruntjs,yeoman
https://github.com/yeoman/grunt-usemin
Gruntjs: grunt-usemin使用心得的更多相关文章
- Grunt usemin
yeoman/grunt-usemin 用来将 HTML 文件中(或者 templates/views)中没有优化的 script 和 stylesheets 替换为优化过的版本. usemin 暴露 ...
- Grunt usemin前端自动化打包流程
前端优化是尽量减少http请求,所以我们需要尽量合并压缩文件,然后调用压缩后的文件,比如多个css文件压缩成一个,多个js文件合并压缩等,usemin能够自动在html中使用压缩后的文件,达到上面的目 ...
- Grunt Part 2
Objectives and Outcomes In this exercise, you will continue to learn to use Grunt, the task runner. ...
- windows下安装node环境,以及grunt试水笔记
grunt,当下前端界知名度最高的工作流处理工具. 在一线的互联网公司,它早已经被用烂了,而我真正接触,是在去年年底... 期间还因为内心太杂分心玩乐而荒废学途,以致到最近才重拾学业,在这里BS一下自 ...
- 前端资料QQ群交流
转:https://github.com/jsfront/src/blob/master/qq.md 这本来是我QQ群内部的一份公共约定的日常交流规则,后来得到大伙的一致认可,并用实际行动来捍卫它,使 ...
- JavaScript资源分享
一. 资源教程: 综合类 前端知识体系 前端知识结构 Web前端开发大系概览 Web前端开发大系概览-中文版 Web Front-end Stack v2.2 En类资源汇总 免费的编程中文书籍索引 ...
- 终极教程【zhong】
just for a better future! 资源教程 aiim 综合类 前端知识体系 前端知识结构 Web前端开发大系概览 We ...
- Top JavaScript Frameworks, Libraries & Tools and When to Use Them
It seems almost every other week there is a new JavaScript library taking the web community by storm ...
- js 框架及uI框架
转载自:http://www.techweb.com.cn/network/system/2015-12-23/2245809.shtml 这篇文章主要描述现今流行的一些 Javascript web ...
随机推荐
- VBA 操作数字
第8章 操作数字 加.减.乘.除.平方与指数(^2 或者^n).平方根Sqr.正弦Sin.余弦Cos.正切Tan.反正切Atn.绝对值Abs 转换为整型数.长整型数.双精度型数和值 Cint当双精度型 ...
- yii2 GridView 下拉搜索实现案例教程
作者:白狼 出处:http://www.manks.top/article/yii2_gridview_dropdown_search本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章 ...
- 理解JavaScript中的原型继承(2)
两年前在我学习JavaScript的时候我就写过两篇关于原型继承的博客: 理解JavaScript中原型继承 JavaScript中的原型继承 这两篇博客讲的都是原型的使用,其中一篇还有我学习时的错误 ...
- Spring中@Autowired注解、@Resource注解的区别
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- android AsyncTask实例
.java package com.example.activitydemoay; import android.app.Activity; import android.content.Intent ...
- ssh升级
Openssh升级操作步骤(此方法仅供参考) 1 .开启telnet服务 未避免openssh升级失败,导致ssh无法连接,在升级前首先开启telnet服务. 首先要确定是否安装了telnet 修改配 ...
- sqlite3 shell的使用
sqlite的安装 1. 首先是下载sqlite,可以该页面下载:http://www.sqlite.org/download.html 当前的最新版本为:sqlite-shell-win32-x86 ...
- 证明你是你——快速开启Windows Azure多重身份验证
中国版Windows Azure的多重身份验证(Multi-Factor Authentication)功能已经开放.这个功能说白了就是要“证明你是你”.目前可以支持以下几种验证方式: 手机,短信验证 ...
- 《超级IP》:伪理论,没能比现有的市场营销理论更高明,只敢勉强去解释已经发生的事情,不敢去预测未来。2星。
超级IP是作者造出来的一个词.作者尝试把“超级IP”作为一种理论来解释2015年以来的各种网红现象.读完全书后,我的感觉是这个理论不怎么样: 1:作者完全不提现有的市场营销理论.我的问题是:现有的理论 ...
- [转]jQuery: how to get which button was clicked upon form submission?
本文转自:http://stackoverflow.com/questions/5721724/jquery-how-to-get-which-button-was-clicked-upon-form ...