Gruntjs: grunt-usemin使用心得
2014-03-10 00:00
573 查看
grunt-usemin:
Replaces references to non-optimized scripts or stylesheets into a set of HTML files
Usually,
——app/
——js/
——tpl/
源html结构
打包后的html
感谢gruntjs,yeoman
https://github.com/yeoman/grunt-usemin
Replaces references to non-optimized scripts or stylesheets into a set of HTML files
useminexports 2 different tasks:
useminPrepareprepares the configuration to transform specific construction (blocks) in the scrutinized file into a single line, targeting an optimized version of the files
useminreplaces 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,
useminPrepareis launched first, then the steps of the transformation flow (for example,
concat,
uglify, and
cssmin), and then, in the end
useminis 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
相关文章推荐
- JavaScript学习笔记——函数
- js中的关键字 var
- jsp页面中script代码放置的位置知识整理
- 各种js校验
- Javascript 基础知识
- js设计模式
- 使用匿名函数在回调函数中正确访问JS循环变量
- 使用匿名函数在回调函数中正确访问JS循环变量
- C#中读写JSON风格的配置信息
- JavaScript学习笔记——变量
- JSP页面的五种跳转方法
- javascript 不间断向左滚动图片
- Javascript模块化编程(三):require.js的用法
- javascript 单行向上滚动文字
- javascript tab onclick
- 润乾报表超链接调用js函数
- Javascript模块化编程(二):AMD规范
- js 求时间差
- IE8"开发人员工具"使用详解下(浏览器模式、文本模式、JavaScript调试、探查器)
- Javascript模块化编程(一):模块的写法