-
Notifications
You must be signed in to change notification settings - Fork 149
/
Gruntfile.js
241 lines (222 loc) · 8.61 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* jshint node:true */
"use strict";
var umdName = "MobileDragDrop";
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
// various cli execution configs
exec: {
rollup: {
cmd: "rollup -c"
},
ts: {
cmd: "tsc -p tsconfig.json"
},
tslint: {
cmd: "tslint -p tsconfig.json"
}
},
// append UMD declaration to d.ts files
append: {
umdDeclaration: {
append: "export as namespace " + umdName + ";",
files: {
"src/index.d.ts": "src/index.d.ts",
"src/scroll-behaviour.d.ts": "src/scroll-behaviour.d.ts"
}
}
},
// bump version, commit, tag
bump: {
options: {
files: ["package.json"],
updateConfigs: ["pkg"],
commit: true,
commitMessage: "Release v%VERSION%",
commitFiles: ["package.json", "package-lock.json", "CHANGELOG.md", "release"],
createTag: true,
tagName: "v%VERSION%",
tagMessage: "Version %VERSION%",
push: true,
pushTo: "origin",
gitDescribeOptions: "--tags --always --abbrev=1 --dirty=-d",
globalReplace: false,
prereleaseName: "rc",
metadata: "",
regExp: false
}
},
clean: {
release: ["release"]
},
copy: {
// copy files from src to release folder
release: {
files: [
// includes files within path
{
expand: true,
cwd: "src",
src: ["*.css", "**/*.d.ts", "*.js", "*.map"],
dest: "release/",
filter: "isFile",
flatten: false
},
{
src: ["package.json", "README.md", "LICENSE"],
dest: "release/"
}
]
}
},
// http server config for development and demo page
connect: {
// starts a server that will serve the development sources
// instead of the release sources.
dev: {
options: {
port: 8000,
open: "http://localhost:8000/demo/",
livereload: 35731,
middleware: function (connect, options, middlewares) {
// inject a custom middleware into the array of default middlewares
middlewares.unshift(function (req, res, next) {
// regex matching release file urls
var redirectFrom = /\/release\//;
// src url fragment
var redirectTo = "/src/";
if (redirectFrom.test(req.url)) {
// modify url to point to src files
var srcUrl = req.url.replace(redirectFrom, redirectTo);
// unminified sources
req.url = srcUrl.replace(".min", "");
}
next();
});
return middlewares;
}
}
},
// starts a server that will serve the demo page with release sources
release: {
options: {
port: 8000,
open: "http://localhost:8000/demo/"
}
}
},
// js minification
uglify: {
options: {
// mangle var names
mangle: {
reserved: [
umdName
],
properties: {
regex: /^_/ // this will mangle all properties starting with an underscore
}
},
reserveDOMProperties: true, // do not mangle browser props
compress: {
drop_console: true, // remove console log statements
drop_debugger: true, // remove debugger statements
dead_code: true, // removes unreachable code
unused: true, // remove unused code
sequences: true,
if_return: true,
join_vars: true,
keep_fargs: true,
conditionals: true,
evaluate: true,
passes: 3
},
sourceMap: true,
report: "gzip"
},
main: {
options: {
banner: "/*! <%= pkg.name %> <%= pkg.version %> | Copyright (c) <%= grunt.template.today('yyyy') %> Tim Ruffles | MIT License */",
sourceMapIn: "src/index.js.map"
},
src: "src/index.js",
dest: "src/index.min.js"
},
scroll: {
options: {
sourceMapIn: "src/scroll-behaviour.js.map"
},
src: "src/scroll-behaviour.js",
dest: "src/scroll-behaviour.min.js"
}
},
// automatically recompile on changes, demo page livereload
watch: {
ts: {
files: ["src/**/*.ts", "!src/**/*.d.ts"],
tasks: ["compile"],
options: {
debounceDelay: 250,
atBegin: true,
livereload: 35731
}
},
resources: {
files: ["src/**/*.css", "demo/**/*"],
options: {
debounceDelay: 500,
livereload: 35731
}
}
},
});
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-connect");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-npm");
grunt.loadNpmTasks("grunt-bump");
grunt.loadNpmTasks("grunt-exec");
grunt.registerMultiTask("append", function () {
var appendTxt = this.data.append || "";
this.files.forEach(function (file) {
file.src
.filter(function (filepath) {
// Remove nonexistent files (it's up to you to filter or warn here).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
})
.map(function (filepath) {
// Read and return the file's source.
return grunt.file.read(filepath) + appendTxt;
})
.forEach(function (content) {
// Write joined contents to destination filepath.
grunt.file.write(file.dest, content);
// Print a success message.
grunt.log.writeln('Appended "' + appendTxt + '" saved to "' + file.dest + '".');
});
});
});
grunt.registerTask("compile", ["exec:ts", "exec:rollup", "append:umdDeclaration"]);
grunt.registerTask("build-release", ["compile", "exec:tslint", "uglify", "clean", "copy"]);
// compile, lint, minify, clean copy to release folder
grunt.registerTask("prepare-release", "Prepare a release by building release files and bumping version", function (bump) {
if (!bump) {
grunt.log.error("You must specify the version bump! See https://github.com/vojtajina/grunt-bump/tree/v0.7.0");
return;
}
grunt.task.run("build-release", "bump-only:" + bump, "copy");
});
// serve release files
grunt.registerTask("serve-release", "serve release files for checking that release files have no issues", ["connect:release", "watch:resources"]);
// publish a prepared release
grunt.registerTask("publish-release", ["bump-commit"]);
// default task for developers to start coding
grunt.registerTask("default", ["connect:dev", "watch"]);
};