grunt 插件
一个简单的 grunt 插件, 作用是 把 css 文件中的 /images/ 替换成指定的 url path, 以实现 图片 cdn 路劲改造
插件项目文件结构
grunt-contrib-staticpath
|--tasks
|----staticpath.js
|--package.json
package.json
{
"name": "grunt-contrib-staticpath",
"version": "1.0.2",
"description": "A grunt plugin to help front engineer replacing public static path of css.",
"main": "tasks/staticpath.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@192.168.1.22:node/grunt-contrib-staticpath.git"
},
"keywords": [
"grunt-contrib-staticpath"
],
"author": "ecalf",
"license": "ISC"
}
staticpath.js
var
path = require('path'); module.exports = function (grunt) {
"use strict"; grunt.registerMultiTask('publicPath', 'Replace publicPath of image and update the CSS file.', function () {
var done = this.async(); var options = this.options({
imagepath:"/images/",
imagepathPublic:"//node-img.b0.upaiyun.com/wmzy-pc/images/"
}); //console.log("options>>>",options);
//console.log("this.files>>>",this.files) function replaceCSSImagesPath(cssData){
var oldPath = options.imagepath;
var newPath = options.imagepathPublic; var reg = new RegExp('url\\(\\s*([\\\'\\\"])?'+oldPath,'gi');
//console.log("todo reg:",reg)
cssData = cssData.replace(reg,'url($1'+newPath);
//console.log("done cssData.replace");
//console.log('indexOf ',newPath,': ',cssData.indexOf(newPath));
return cssData
} function donePathReplace(cssData, destCSS){
grunt.file.write(destCSS, cssData);
grunt.log.writelns(('Done! [Replace publicPath of image, Created] -> ' + destCSS));
} function staticPathIterator(file, callback){
var src = file.src[0];
var fileName = path.basename(src, '.css'); var destCSS = file.dest;
var cssData = grunt.file.read(src);
var newCssData = replaceCSSImagesPath(cssData); donePathReplace(newCssData, destCSS);
callback(null);
} grunt.util.async.forEachSeries(this.files, staticPathIterator, function(success){
done(success);
});
});
};
使用时在 Gruntfile.js 中的配置
publicPath:{
options:{
imagepath:"/images/",
imagepathPublic:"//node-img.b0.upaiyun.com/wmzy-pc/images/"
},
autoPublicPath:{
files: [
{
expand: true,
cwd: "public/src/css/",
src: "**/*.css",
dest: "public/src/css/"
}
]
}
},
用法:
grunt.loadNpmTasks('grunt-contrib-staticpath');
//test task
grunt.registerTask('test',["publicPath"]);
grunt 插件的更多相关文章
- 快速开发Grunt插件----压缩js模板
前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...
- node.js安装及grunt插件,如何进行脚本压缩
http://gruntjs.com/pluginshttp://gruntjs.com/getting-startedhttp://gruntjs.com/configuring-tasks#glo ...
- Grunt 插件使用汇总
最近使用了很多 Grunt 插件,这里把使用 Grunt 中涉及的从开发.代码检查.单元测试.E2E 测试,直到发布所涉及的插件,做一个比较完全的汇总. 环境搭建 1. 创建 Web 前端开发环境 2 ...
- 如何开发 Grunt 插件
创建 grunt 插件 准备工作:(node/npm/git 安装,在此不做赘述) yeoman generator 可以自动生成一个插件模板. 安装 yo npm install -g yo 安装 ...
- 详解Grunt插件之LiveReload实现页面自动刷新(两种方案)
http://www.jb51.net/article/70415.htm 含Grunt系列教程 这篇文章主要通过两种方案详解Grunt插件之LiveReload实现页面自动刷新,需要的朋友可以 ...
- chrome下的Grunt插件断点调试——基于node-inspector
之前调试grunt插件时,都是通过人肉打log来调试.不仅效率低,而且会产生一堆无用的代码.于是简单google了下node断点调试的方法,总结了下. 借助node-inspector,我们可以通过C ...
- [转载]Grunt插件之LiveReload 实现页面自动刷新,所见即所得编辑
配置文件下载 http://vdisk.weibo.com/s/DOlfks4wpIj LiveReload安装前的准备工作: 安装Node.js和Grunt,如果第一次接触,可以参考:Window ...
- 使用Grunt 插件打包Electron Windows应用
最近利用Electron来创建跨桌面应用的趋势似乎很火.看了几个用Electron开发的应用,这些应用在windows下面的安装方式,都是类似一个绿色软件的安装方法,下载.zip->解压到相应目 ...
- grunt插件[font-spider] : 转码,压缩字体 @font-face
字蛛插件:压缩与转码静态页面中的 WebFont 需要注意的是,目前只支持 grunt@0.4.1 package.json { "name": "fontS" ...
随机推荐
- mysql常用
查询表占用大小 select sum(DATA_LENGTH)+sum(INDEX_LENGTH) from information_schema.tables where table_schema= ...
- 【原创】大数据基础之Impala(3)部分调优
1)将coordinator和executor角色分离 By default, each host in the cluster that runs the impalad daemon can ac ...
- IE兼容事件绑定V1.0
想要兼容IE678,少用原型,因为它们没有完全实现ECMA-262规范 (function(window){ //兼容IE678时少用原型,因为它没有完全遵循ECMA-262规范 //衬垫代码:isA ...
- oracle wallet使用与维护
oracle wallet使用与维护2015年05月26日 17:58:55 SilenceRiver 阅读数:1614oracle wallet使用与维护---oracle无密码登录分类: Orac ...
- 解决springboot启动失败问题:Unable to start embedded container;
将一个springboot项目导入到eclipse后,启动时报错Unable to start embedded container,以下时全部错误信息: Application startup fa ...
- 关于PCB走线能不能走锐角的讨论
(此文参考吴川斌的博客) 很多PCB工程师都知道Layout走线时忌走直角,那么锐角能走吗? 回答当然是否定的!为什么呢? 这里先不说锐角对高速信号走线会不会造成负面影响,单从PCB DFM(可制造性 ...
- 一个spring mvc 需要用到到文件
一. 类 org.springframework.stereotype.Controller; org.springframework.web.bind.annotation.RequestMappi ...
- vertx的HttpServer模块
Start HttpServer /** * 启动 HttpServer * multi instances 采用 synchronized防止线程安全问题 * addHandlers 方法是acto ...
- 马拉车算法——求回文串起点hdu3294
#include<bits/stdc++.h> using namespace std; #define maxn 500005 int p[maxn]; ]; int start; in ...
- 小米Note 2简单卡刷开发版启用root超级权限的步骤
小米的机器不同手机型号一般MIUI官方论坛都提供两个不同版本,分别为稳定版和开发版,稳定版没有提供Root超级权限管理,开发版中就开启了Root超级权限,较多时候我们需要使用的一些功能强大的软件,都需 ...