题记:写这篇博客要主是加深自己对项目工具的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢。
Grunt 是一个基于务任的 JavaScript 项目令命行构建工具,运行于 Node.js 平台。Grunt 够能从板模倏地创立项目,合并、压缩和校验 CSS & JS 文件,运行单元测试以及动启静态服务器。

安装 Grunt
荐推 Windows 户用用使 Git Shell 来停止令命行作操。安装 Windows 桌面版 GitHub 的时候会自动安装 Git Shell。
GitHub for Windows 下载址地:http://windows.github.com
Grunt 运行于 Node.js 环境,这里假设你经已安装了 Node.js 和 NPM。
npm install grunt
npm install -g grunt
创立项目框架
安装好 Grunt 后以可就开始创立项目了,Grunt 内置面下四种基本的项目板模:
gruntfile,创立令命:
grunt init:gruntfile
grunt init:commonjs
grunt init:jquery
grunt init:node

如果须要盖覆,这个时候须要用使 --forece 参数:
grunt init:jquery --force

OK,到这里项目就创立胜利了!面下是项目的目录结构:

并且 README.md 文件的内容和格式也生成好了:

然后以可就编写插件代码了。Grunt 供提的 jQuery 插件代码框架如下:
/*
* GruntDemo
* https://github.com/bluesky/grunt-demo
*
* Copyright (c) 2013 BlueSky
* Licensed under the MIT license.
*/
(function($) {
// Collection method.
$.fn.awesome = function() {
return this.each(function() {
$(this).html('awesome');
});
};
// Static method.
$.awesome = function() {
return 'awesome';
};
// Custom selector.
$.expr[':'].awesome = function(elem) {
return elem.textContent.indexOf('awesome') >= 0;
};
}(jQuery));
/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false*/
/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/
/*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/
(function($) {
module('jQuery#awesome', {
setup: function() {
this.elems = $('#qunit-fixture').children();
}
});
test('is chainable', 1, function() {
// Not a bad test to run on collection methods.
strictEqual(this.elems.awesome(), this.elems, 'should be chaninable');
});
test('is awesome', 1, function() {
strictEqual(this.elems.awesome().text(), 'awesomeawesomeawesome', 'should be thoroughly awesome');
});
module('jQuery.awesome');
test('is awesome', 1, function() {
strictEqual($.awesome(), 'awesome', 'should be thoroughly awesome');
});
module(':awesome selector', {
setup: function() {
this.elems = $('#qunit-fixture').children();
}
});
test('is awesome', 1, function() {
// Use deepEqual & .get() when comparing jQuery objects.
deepEqual(this.elems.filter(':awesome').get(), this.elems.last().get(), 'knows awesome when it sees it');
});
}(jQuery));
下篇预报:《JavaScript 项目构建工具 Grunt 实际:务任和指令》,敬请期待……
您可能感兴致的相干文章
本文接链:JavaScript 项目构建工具 Grunt 实际:简介和安装
译编源来:妄想空天 ◆ 注关前端发开技巧 ◆ 分享网页设计资源
文章结束给大家分享下程序员的一些笑话语录: 一边用着越狱的ip,一边拜乔帮主的果粉自以为是果粉,其实在乔帮主的眼里是不折不扣的叛徒。
本文介绍Grunt作为JavaScript项目构建工具的基础使用方法,包括安装过程、项目框架创建及示例代码。

被折叠的 条评论
为什么被折叠?



