一个简单的 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#解决浏览器跨域问题

    1.浏览器为什么不能跨域? 浏览器有一个基本的安全策略--同源策略.为保证用户的信息安全,它对不同源的文档或脚本对当前文档的读写操作做了限制.域名,子域名,端口号或协议不同都属于不同源,当脚本被认为是 ...

  2. 洛谷P4774 [NOI2018]屠龙勇士 [扩欧,中国剩余定理]

    传送门 思路 首先可以发现打每条龙的攻击值显然是可以提前算出来的,拿multiset模拟一下即可. 一般情况 可以搞出这么一些式子: \[ atk_i\times x=a_i(\text{mod}\ ...

  3. springboot 多模块打war 部署

    先展示一下自己项目结构 一共有5个模块 依赖关系:下面的模块依赖上面所有的模块 其中 rongke-web是我要部署的模块 最终要打war进行部署,其他模块均打jar 被rongke-web引用. 开 ...

  4. java实现一个简单的Web服务器

    注:本段内容来源于<JAVA 实现 简单的 HTTP服务器> 1. HTTP所有状态码 状态码 状态码英文名称 中文描述 100 Continue 继续.客户端应继续其请求 101 Swi ...

  5. 算法工程师<数学题/智力题>

    <数学题/智力题> 1.如果一个女生说,她集齐了十二个星座的前男友,我们应该如何估计她前男友的数量? https://blog.csdn.net/FnqTyr45/article/deta ...

  6. thymleaf th:text="|第${user.courseSort}课|" 对于不知道的真的是解渴了

    简单描述:最近再做一个课程管理,列表显示第几课,但是后台传递过来的只是数字0~9,意味着,我得自己拼了ヾ(◍°∇°◍)ノ゙  我最烦的就是这种拼,各种难调,果真和我想的一样,4.5遍了还没出来. 我的 ...

  7. springboot整合mybatis开发

    1创建项目,在启动类上添加映射扫描注解 2导入依赖,添加mybatis generator自动生成代码插件 <!-- mybatis generator 自动生成代码插件 --> < ...

  8. AWS S3服务使用

    AWS S3是亚马逊的一种文件存储服务使用方便. 一.配置服务 public static class AWS_S3ClientInfo { private static readonly strin ...

  9. Deepin 自动挂载win NTFS磁盘

    安装了双系统 WIN10+Deepin15.4,系统开机进入Linux,刚开始在/media/my-PC里面还能看到win系统上的C.E.F盘,系统提示更新后,再次打开/media 看不见NTFS分区 ...

  10. [转]CDH QuickStart VM基本使用

    https://blog.csdn.net/wiborgite/article/details/78731944 https://www.cnblogs.com/harrychinese/p/big_ ...