-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathGruntfile.js
125 lines (114 loc) · 3.05 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* 自动化脚本定义
*/
module.exports = function (grunt) {
'use strict';
//load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
require('shelljs/global');
var path = require('path');
//define tasks
//启动Web服务器,监控变更,自动加载
grunt.registerTask('server', ['connect', 'open', 'watch:server']);
grunt.registerTask('dev', ['connect', 'open', 'watch:dev']);
grunt.registerTask('build', ['clean', 'concat', 'uglify']);
//读取配置
var pkg = grunt.file.readJSON('package.json');
var cfg = {
pkg: pkg,
src: './',
// Change 'localhost' to '0.0.0.0' to access the server from outside.
serverHost: '0.0.0.0',
serverPort: 9000,
serverUrl: 'http://localhost:9000/example/src/index.html',
livereload: 35729
};
//grunt config
grunt.initConfig({
//======== 配置相关 ========
cfg: cfg,
//======== 开发相关 ========
//开启服务
connect: {
options: {
port: cfg.serverPort,
hostname: cfg.serverHost,
middleware: function(connect, options) {
return [
require('connect-livereload')({
port: cfg.livereload
}),
connect.query(),
// Serve static files.
connect.static(options.base),
// Make empty directories browsable.
connect.directory(options.base),
];
}
},
server: {
options: {
base: cfg.src
}
}
},
//监控文件变化
watch: {
options: {
livereload: cfg.livereload,
},
server: {
files: [
cfg.src + '/**'
],
tasks: []
},
dev: {
files: [
cfg.src + '/**',
'!dist/**'
],
tasks: ['build']
}
},
//清理
clean: {
build: ['dist/*']
},
//复制
concat: {
options: {
banner: '/*! <%= cfg.pkg.name %> - v<%= cfg.pkg.version %> - <%= cfg.pkg.repository.url %> - <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
files: [
{expand: true, cwd:'src/', src: ['angular-lazyload.js'], dest: 'dist/'}
]
}
},
//压缩
uglify: {
options: {
banner: '/*! <%= cfg.pkg.name %> - v<%= cfg.pkg.version %> - <%= cfg.pkg.repository.url %> - <%= grunt.template.today("yyyy-mm-dd") %> */\n',
mangle: {
except: ['require','exports', 'module']
}
},
build: {
files: {
'dist/angular-lazyload.min.js' : ['dist/angular-lazyload.js']
}
}
}
});
/**
* 在跨域chrome中打开首页
*/
grunt.registerTask('open', function(crossdomain) {
var url = cfg.serverUrl || 'http://localhost:' + cfg.serverPort;
url = url && url.replace(/"/g, '\\\"') || 'about:blank';
var param = !crossdomain ? '' : ' --user-data-dir=C:/CHROME_DEV --disable-web-security --allow-file-access-from-files ';
var cmd = 'start chrome '+ url + ' ' + param;
exec(cmd);
});
};