forked from jamesward/force-rest-cors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
220 lines (170 loc) · 5.8 KB
/
gulpfile.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
var gulp = require("gulp"),
path = require("path"),
gutil = require("gulp-util"),
rename = require("gulp-rename"),
pkg = require("./package.json");
var SRC = "app";
var SRC_LESS_BASE = path.join(SRC, "less");
var SRC_JAVASCRIPT_BASE = path.join(SRC, "js");
var SRC_IMAGES_BASE = path.join(SRC, "img");
var SRC_ALL = path.join(SRC, "**");
var SRC_HTML = path.join(SRC, "html", "**", "*.html");
var SRC_LESS_ALL = path.join(SRC_LESS_BASE, "**", "*.less");
var SRC_JAVASCRIPT_ALL = path.join(SRC_JAVASCRIPT_BASE, "**", "*.js");
var SRC_IMAGES_ALL = path.join(SRC_IMAGES_BASE, "**", "*");
var DIST = "dist";
var DIST_LIB = path.join(DIST, "lib");
var DIST_ALL = path.join(DIST, "**");
var DIST_LESS = path.join(DIST, "css");
var DIST_JAVASCRIPT = path.join(DIST, "js");
var DIST_IMAGES = path.join(DIST, "img");
var MAIN_SCRIPT = "index.js";
// LESS
// Compile app/less sources in CSS and auto-prefix the CSS
function compileLess() {
return gulp.src(SRC_LESS_ALL)
.pipe(require("gulp-less")({ paths: [ path.join(SRC_LESS_BASE, "includes") ] }))
.pipe(require("gulp-autoprefixer")("last 2 version", "safari 5", "ie 8", "ie 9", "opera 12.1", "ios 6", "android 4"))
.pipe(gulp.dest(DIST_LESS));
}
gulp.task("compile:less", ["update"], function() {
return compileLess();
});
// Minify the CSS
gulp.task("dist:less", ["update"], function() {
return compileLess()
.pipe(rename({ suffix: ".min" }))
.pipe(require('gulp-minify-css')())
.pipe(gulp.dest(DIST_LESS));
});
// JavaScripts
// Run JSHint on all of the app/js files and concatenate everything together
function compileJavaScript() {
var jshint = require("gulp-jshint");
return gulp.src(SRC_JAVASCRIPT_ALL)
.pipe(jshint())
.pipe(jshint.reporter(require('jshint-stylish')))
.pipe(require("gulp-concat")(MAIN_SCRIPT))
.pipe(gulp.dest(DIST_JAVASCRIPT));
}
gulp.task("compile:javascript", ["update"], function() {
return compileJavaScript();
});
// Uglify the JS
gulp.task("dist:javascript", ["update"], function() {
return compileJavaScript()
.pipe(rename({ suffix: ".min" }))
.pipe(require('gulp-ngmin')()) // ngmin makes angular injection syntax compatible with uglify
.pipe(require("gulp-uglify")())
.pipe(gulp.dest(DIST_JAVASCRIPT));
});
// Images
gulp.task("compile:images", ["update"], function() {
return gulp.src(SRC_IMAGES_ALL)
.pipe(gulp.dest(DIST_IMAGES));
});
// Compress the images
gulp.task("dist:images", ["update"], function() {
return gulp.src(SRC_IMAGES_ALL)
.pipe(require("gulp-imagemin")({ optimizationLevel: 3, progressive: true, interlaced: true }))
.pipe(gulp.dest(DIST_IMAGES));
});
// Copy the html assets without modification
gulp.task("compile:html", ["update"], function() {
return gulp.src(SRC_HTML)
.pipe(gulp.dest(DIST));
});
// Replace the non-minified paths in html assets with the minified paths
// Todo: This brute force hacky way of doing this is error prone
gulp.task("dist:html", ["update"], function() {
var replace = require('gulp-replace');
return gulp.src(SRC_HTML)
.pipe(replace(/\.js/g, ".min.js"))
.pipe(replace(/\.css/g, ".min.css"))
.pipe(gulp.dest(DIST));
});
// Copy Bower assets
gulp.task("copy-bower", ["update"], function() {
return gulp.src("bower_components/**")
.pipe(gulp.dest(DIST_LIB));
});
// Compile everything
gulp.task("compile", ["copy-bower", "compile:html", "compile:less", "compile:javascript", "compile:images"]);
// Dist everything
gulp.task("dist", ["copy-bower", "dist:html", "dist:less", "dist:javascript", "dist:images"]);
// Clean the DIST dir
gulp.task("clean", function() {
return gulp.src([DIST, ".build"], {read: false})
.pipe(require("gulp-clean")());
});
// Updates the Bower dependencies based on the bower.json file
gulp.task("update", function(next) {
var needsUpdate = false;
gulp.src("bower.json")
.pipe(require("gulp-newer")(".build"))
.pipe(gulp.dest(".build")) // todo: don't do this if the bower install fails
.on("close", function() {
if (!needsUpdate) {
next();
}
})
.on("error", function(error) {
if (!needsUpdate) {
next(error);
}
})
.on("data", function() {
// updated bower.json
needsUpdate = true;
gutil.log("Updating Bower Dependencies");
require("bower").commands.install([], {}, { interactive: false })
.on("end", function () {
gutil.log("Bower Dependencies Updated");
next();
})
.on("log", function (log) {
if (log.level == "action" && log.id == "install") {
gutil.log("Added Bower Dependency: " + log.message);
}
})
.on("error", function (error) {
gutil.error("Bower Error:", error);
next(error);
});
})
});
// Server that serves static content from DIST
gulp.task("server", ["compile"], function(next) {
var port = process.env.PORT || 5000;
var connect = require("connect");
var server = connect();
server.use(connect.static(DIST)).listen(port, next);
gutil.log("Server up and running: http://localhost:" + port);
});
// Auto-Reloading Development Server
gulp.task("dev", ["server"], function() {
gulp.watch(SRC_ALL, ["compile"]);
gulp.watch("bower.json", ["copy-bower"]);
var lrserver = require("gulp-livereload")();
gulp.watch(DIST_ALL).on("change", function(file) {
lrserver.changed(file.path);
});
});
// Prod Server
gulp.task("prod", ["server", "dist"]);
// Help
gulp.task("help", function(next) {
gutil.log("--- " + pkg.name + " ---");
gutil.log("");
gutil.log("See all of the available tasks:");
gutil.log("$ gulp -T");
gutil.log("");
gutil.log("Run a dev mode server:");
gutil.log("$ gulp dev");
gutil.log("");
gutil.log("Run a prod mode server:");
gutil.log("$ gulp prod");
next();
});
// Default
gulp.task("default", ["help"]);