一个简单的 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 插件的更多相关文章

  1. 快速开发Grunt插件----压缩js模板

    前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...

  2. node.js安装及grunt插件,如何进行脚本压缩

    http://gruntjs.com/pluginshttp://gruntjs.com/getting-startedhttp://gruntjs.com/configuring-tasks#glo ...

  3. Grunt 插件使用汇总

    最近使用了很多 Grunt 插件,这里把使用 Grunt 中涉及的从开发.代码检查.单元测试.E2E 测试,直到发布所涉及的插件,做一个比较完全的汇总. 环境搭建 1. 创建 Web 前端开发环境 2 ...

  4. 如何开发 Grunt 插件

    创建 grunt 插件 准备工作:(node/npm/git 安装,在此不做赘述) yeoman generator 可以自动生成一个插件模板. 安装 yo npm install -g yo 安装 ...

  5. 详解Grunt插件之LiveReload实现页面自动刷新(两种方案)

    http://www.jb51.net/article/70415.htm    含Grunt系列教程 这篇文章主要通过两种方案详解Grunt插件之LiveReload实现页面自动刷新,需要的朋友可以 ...

  6. chrome下的Grunt插件断点调试——基于node-inspector

    之前调试grunt插件时,都是通过人肉打log来调试.不仅效率低,而且会产生一堆无用的代码.于是简单google了下node断点调试的方法,总结了下. 借助node-inspector,我们可以通过C ...

  7. [转载]Grunt插件之LiveReload 实现页面自动刷新,所见即所得编辑

    配置文件下载  http://vdisk.weibo.com/s/DOlfks4wpIj LiveReload安装前的准备工作: 安装Node.js和Grunt,如果第一次接触,可以参考:Window ...

  8. 使用Grunt 插件打包Electron Windows应用

    最近利用Electron来创建跨桌面应用的趋势似乎很火.看了几个用Electron开发的应用,这些应用在windows下面的安装方式,都是类似一个绿色软件的安装方法,下载.zip->解压到相应目 ...

  9. grunt插件[font-spider] : 转码,压缩字体 @font-face

    字蛛插件:压缩与转码静态页面中的 WebFont 需要注意的是,目前只支持 grunt@0.4.1 package.json { "name": "fontS" ...

随机推荐

  1. C# 解压缩工具类GZip

    using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...

  2. SQLAlchemy 使用(二)表关联

    前言 在上一章中我们介绍了 SQLAlchemy 建立基本表,但是一般情况下,表之间是有关联的,比如 一对一/一对多/多对多,当然 SQLAlchemy 是支持建立model时指定关系的 正文 多对一 ...

  3. Java 基本语法,标识符,修饰符,关键字

    基本语法 编写 Java 程序时,应注意以下几点: 大小写敏感:Java 是大小写敏感的,这就意味着标识符 Hello 与 hello 是不同的. 类名:对于所有的类来说,类名的首字母应该大写.如果类 ...

  4. 链表详解(C语言)

    链表是一种常见的基础数据结构,结构体指针在这里得到了充分的利用. 链表可以动态的进行存储分配,也就是说,链表是一个功能极为强大的数组,他可以在节点中定义多种数据类型,还可以根据需要随意增添,删除,插入 ...

  5. Windows Internals 笔记——线程优先级

    1.每个线程都被赋予0(最低)~31(最高)的优先级数.当系统确定给哪个线程分配CPU时,它会首先查看优先级为31的线程,并以循环的方式进行调度.如果有优先级为31的线程可供调度,那么系统就会将CPU ...

  6. C#学习-接口与抽象类

    接口与抽象类的区别 1.抽象类中可以包含虚方法.非抽象方法和静态成员: 当接口中不能包含虚方法和任何静态成员,并且接口中只能定义方法,不能有具体事项,方法的具体实现由实现类完成. 2.抽象类不能实现多 ...

  7. hadoop部署

    [root@xiong ~]# hostnamectl set-hostname hadoop001 [root@xiong ~]# vim /etc/hostnamehadoop001 vim /e ...

  8. CF1082

    D 乱搞题..发现只有a[i]=1是特殊的 瞎搞一下 E 发现一段的贡献是出现次数最多的-为c个数 然后考虑分别对每种颜色做一下 然后每次只有这种颜色和他们之间是有用的 然后做个最大区间和就好了 F ...

  9. Scrapyd 改进第二步: Web Interface 添加 STOP 和 START 超链接, 一键调用 Scrapyd API

    0.提出问题 Scrapyd 提供的开始和结束项目的API如下,参考 Scrapyd 改进第一步: Web Interface 添加 charset=UTF-8, 避免查看 log 出现中文乱码,准备 ...

  10. 广搜迷之RE及迷之WA

    最近做广搜的时候天天迷之RE,经过dalao@口昭寿指点,我把string数组换成了char二维数组就AC了,(然而我并不知道为什么) 传送门  <——以这个题为例 #include <b ...