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 ...
随机推荐
- Dex Loader] Unable to execute dex: Multiple dex files define
在打包的过程中可能会出现这样的问题,原因是有重复的.jar被引用,可以查看你的build path或Java build path,尤其是Android Dependencies等相关android包 ...
- 【转】OpenStack和Docker、ServerLess能不能决定云计算胜负吗?
还记得在十多年前,SaaS鼻祖SalesForce喊出的口号『No Software』吗?SalesForce在这个口号声中开创了SaaS行业,并成为当今市值460亿美元的SaaS之王.今天谈谈『No ...
- spring的定时任务配置
本文来源于:http://myspace1916.iteye.com/blog/1570707 也可参考:http://www.oschina.net/question/8676_9032 (个人只是 ...
- Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)
上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...
- SQL Server连接SQL Server、SQL Server连接ORACLE 链接服务器
夸数据库访问有很多种方式,其中部分用作接口访问,这里要介绍的是MSSQL访问另一台MSSQL,MSSQL访问ORACLE,其它暂不介绍. 1.MSSQL访问另一台MSSQL: a.展开服务器对象--& ...
- C#语言基础——特殊集合
特殊集合一.stack集合栈:stack,先进后出,一个一个赋值,一个一个取值,按顺序,且没有索引属性和方法:.count 取集合内元素的个数.push() 将元素一个一个推入集合中.pop() 将元 ...
- MySQL创建和操作数据库表demo
[1]建立员工档案表要求字段:员工员工编号,员工姓名,性别,工资,email,入职时间,部门. [2]合理选择数据类型及字段修饰符,要求有NOT NULL,auto_increment, primar ...
- Linux磁盘管理之创建磁盘分区05
一.磁盘基础知识 磁盘安装在计算机上后,在系统读取到硬盘后并不能直接使用,必须经过分区.格式化才能够正确使用.这一次主要是针对磁盘分区进行简单总结,存储设备类型:U盘.光盘.软盘.硬盘.磁带. 硬盘接 ...
- Linux小技巧总结
1.fdisk创建磁盘分区不重启系统partprobe 使用fdisk工具只是将分区信息写到磁盘,如果需要mkfs磁盘分区则需要重启系统才能够读取到/dev/sda*,而使用partprobe则可以使 ...
- ASP.NET 跨域
#region 支持跨域请求 //Response.ClearHeaders(); string origin = Request.Headers["Origin"]; Respo ...