GitHub地址:https://github.com/MrLeo/SeaJS

目录结构

目录结构说明

web存放HTML文件

static存放所有HTML需要用到静态资源文件(css、js、img…)

  • module存放HTML对应的业务模块
  • common存放与业务无关的模块

Get Start

准备工作

  1. 安装 Node.jsnpm

  2. 安装 grunt-cli (允许安装多版本grunt)

$ npm install -g grunt-cli

```

  1. 用命令行进入到项目所在目录

$ cd /d F:\WWW\SeaJS\static

```

  1. 安装 grunt 及 插件 到项目所在目录(--save-dev)

$ npm install grunt --save-dev //grunt

$ npm install grunt-cmd-transport --save-dev //提取模块ID

$ npm install grunt-cmd-concat --save-dev //合并文件

$ npm install grunt-contrib-uglify --save-dev //压缩文件

$ npm install grunt-contrib-clean --save-dev //清理临时目录

```

构建项目

  1. 创建package.json

此文件被npm用于存储项目的元数据,以便将此项目发布为npm模块。你可以在此文件中列出项目依赖的grunt和Grunt插件,放置于devDependencies配置段内。

```
{
"name": "SeaJS",
"version": "1.0.0",
"author": "Leo",
"spm": {
"alias": { //同 seajs.config 中设置的别名
"base": "module/base/base",
"jquery": "libs/jquery/jquery-1.8.3.min"
}
},
"devDependencies": { //grunt 构建用到的依赖包
"grunt": "*", //"*"代表最新版本;"~0.4.1"代表指定版本
"grunt-cmd-transport": "*",
"grunt-cmd-concat": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-clean": "*"
}
}
```
  1. 创建Gruntfile.js

此文件用来配置或定义任务(task)并加载Grunt插件的。Gruntfile.js 是有效的 JavaScript 文件,和package.json文件在同一目录层级。

```js
module.exports = function(grunt) {
require('time-grunt')(grunt);//Time how long tasks take
//require('load-grunt-tasks')(grunt);//Load grunt tasks automatically grunt.initConfig({
pkg: grunt.file.readJSON("package.json"), //引入package.json的JSON元数据
/**
* step 1:
* 创建一个临时目录
* 将需要合并的js文件转为具名函数,并保持独立地保存在这个临时目录
*/
transport: {//task任务
options: {
paths: ['.'], //模块的路径,'.'代表相对路径,默认的是 sea-modules
alias: '<%= pkg.spm.alias %>' //模板字符串语法来从package.json引入模块别名
},
common: {//target
options: {
idleading: 'common-dist/', //构建后的模块ID的前缀
},
files: [{
expand: true, //开启处理动态的src-dest文件映射
filter: 'isFile', //匹配过滤src文件路径
cwd: 'common', //所有src指定的匹配都将相对于此处指定的路径(但不包括此路径)
src: '**/*.js', //相对于cwd路径的匹配模式(**代表当前路径以及子路径)
dest: '.build/common' //目标文件路径前缀
}]
},
base: {
options: {
idleading: 'module/base-dist/',
},
files: [{
expand: true,
filter: 'isFile',
cwd: 'module/base',
src: '**/*.js',
dest: '.build/module/base'
}]
},
demo: {
options: {
idleading: 'module/page-dist/demo/',
},
files: [{
expand: true,
filter: 'isFile',
cwd: 'module/page/demo',
src: '**/*.js',
dest: '.build/module/page/demo'
}]
}
},
/**
* step 2:
* 将临时目录下独立的具名函数文件 合并为 1个 js 文件
* 将这个合并的 js 文件 拷贝到 我们的输出目录
*/
concat: {
options: {
separator: ';', // 定义一个用于插入合并输出文件之间的字符
include: 'relative' //relative(默认)只会合并相对标识的依赖;;all会合并所有依赖
},
common: {
files: [{
expand: true,
ext: '.js',
cwd: '.build/common/',
src: ['**/*.js'],
dest: 'common-dist/'
}]
},
demo: {
files: {
'module/page-dist/demo/index.js': ['.build/module/page/demo/index.js'],
'module/page-dist/demo/index-debug.js': ['.build/module/page/demo/index-debug.js']
}
}
},
/**
* step 3:
* 压缩 这个 合并后的 文件
*/
uglify: {
common: {
options: {
// 此处定义的banner注释将插入到输出文件的顶部
banner: '/*! <%= pkg.author %> @ <%= grunt.template.today("dd-mm-yyyy") %> */\n'
},
files: [{
expand: true,
ext: '.js',
cwd: 'common-dist/',
src: ['**/*.js', '!**/*-debug.js'],
dest: 'common-dist/'
}]
},
main: {
options: {
// 此处定义的banner注释将插入到输出文件的顶部
banner: '/*! <%= pkg.author %> @ <%= grunt.template.today("dd-mm-yyyy") %> */\n'
},
files: {
'module/page-dist/demo/index.js': ['module/page-dist/demo/index.js']
}
}
},
/**
* step 4:
* 将这个临时目录删除
*/
clean: {
spm: ['.build']
}
}); grunt.loadNpmTasks('grunt-cmd-transport');
grunt.loadNpmTasks('grunt-cmd-concat');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('build', ['transport', 'concat', 'uglify', 'clean']);
}; ```
  1. 用命令行进入到 Gruntfile.js 所在目录,执行 grunt
grunt build

参考

Grunt 构建SeaJS的更多相关文章

  1. 使用grunt构建seajs项目

    1.安装nodejs 2.安装grunt-cli npm install -g grunt-cli 3.进入到项目目录,同时准备好package.json和Gruntfile.js文件 //packa ...

  2. ☀【SeaJS】SeaJS Grunt构建

    如何使用Grunt构建一个中型项目?https://github.com/twinstony/seajs-grunt-build spmjshttp://docs.spmjs.org/doc/inde ...

  3. Grunt打包seajs项目

    在使用seajs时,常常将若干脚本分为多次require进来,这样开发中比较方便,但是,会增加http请求次数,在生产环境中需要进行打包合并.压缩等操作. 以Grunt构建工具为例,对一个seajs项 ...

  4. grunt与seajs结合应用

    9.seajs构建的问题 01.png和02.jpg 10.seajs与grunt如何结合开发.两个插件:grunt-cmd-transport grunt-cmd-contact ,去grunt官网 ...

  5. Grunt构建工具能做哪些事?

    Grunt到底有什么作用?一般用来干嘛? 很多前端的工作,包括Less编译.javascript压缩.Css压缩等零零碎碎的工作, 都可以让Grunt来做. 实际上在项目开发中,一般是前端代码 与 后 ...

  6. 前端工程化系列[04]-Grunt构建工具的使用进阶

    在前端工程化系列[02]-Grunt构建工具的基本使用和前端工程化系列[03]-Grunt构建工具的运转机制这两篇文章中,我们对Grunt以及Grunt插件的使用已经有了初步的认识,并探讨了Grunt ...

  7. 前端工程化系列[03]-Grunt构建工具的运转机制

    在前端工程化系列[02]-Grunt构建工具的基本使用这篇文章中,已经对Grunt做了简单的介绍,此外,我们还知道了该如何来安装Grunt环境,以及使用一些常见的插件了,这篇文章主要介绍Grunt的核 ...

  8. 使用grunt构建前端项目

    1. grunt构建工具是基于nodejs上的,所以在使用之前一定要先安装好nodejs 2. 安装好nodejs后,node -v查看node版本 npm-v 查看npm版本信息 3. 在需要用到的 ...

  9. 构建seajs业务模块之grunt VS spm build

    在最开始,我并不知道grunt可以构建CMD模块.(以下spm指代spm build) 当时正困惑于如何用spm方便的构建业务模块,后来看到@twinstony (感谢@twinstony的分享)使用 ...

随机推荐

  1. 重置kafka的offset

    如果你在使用Kafka来分发消息,在数据处理的过程中可能会出现处理程序出异常或者是其它的错误,会造成数据丢失或不一致.这个时候你也许会想要通过kafka把数据从新处理一遍,我们知道kafka默认会在磁 ...

  2. JavaScript随机数

    function random(start,end){ var total=start+end; return Manth.floor(Manth.random()+total-start); }

  3. iOS 10 推送必看(高阶1)

    来源:徐不同 链接:http://www.jianshu.com/p/3d602a60ca4f iOS10 推送必看(基础篇) 虽然这篇文章比较长,也不好理解,但是还是建议大家收藏,以后用到的时候,可 ...

  4. [Java] Servlet 3 —— 用Java生成GET/POST请求

    Servlet是SUN指定的Java服务器端编程规范,用以处理来自客户端的请求,处理并做出响应的一套基础API.Servlet是运行在 Servlet容器中的Java小程序,容器运行在服务器端,服务器 ...

  5. Android进阶笔记20:Android手机屏幕坐标系

    1. 手机屏幕坐标系: 整个坐标系是以手机屏幕左上角为原点(0,0),如下:

  6. iOS UILable的一些用法

    1.按钮上的文字添加下划线 问题:实现下图中右侧的按钮文字效果 代码(绿色为按钮文字加下划线方法): [MyTools createMyImageview:topEditView frame:CGRe ...

  7. iOS之block块

    Block块. 1.声明Block int (^myBlock)(int n) = ^(int num) 类型 (^名称)(需要传的参数)= ^(参数) 2 __block 变量 在block块中修改 ...

  8. Linux_netstat 详解

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  9. 05. 取SQL分组中的某几行数据

    对表中数据分组,有时只需要某列的聚合值:有时却需要返回整行数据,常用的方法有:子查询.ROW_NUMBER.APPLY,总体感觉还是ROW_NUMBER比较直观.测试数据: if OBJECT_ID( ...

  10. 剑指Offer40 和为s的连续正数序列

    /************************************************************************* > File Name: 40_Contin ...