-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·187 lines (156 loc) · 6.21 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
"use strict";
var gulp = require('gulp');
//gulp flow control
var gulpif = require('gulp-if');
var sync = require('gulp-sync')(gulp);
//build tools
var del = require('del');
var debug = require('gulp-debug');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var replace = require('gulp-replace');
//dist minification
var useref = require('gulp-useref');
var uglify = require('gulp-uglify');
var cssMin = require('gulp-clean-css');
var htmlMin = require('gulp-htmlmin');
//runtime tools
var browserSync = require('browser-sync').create();
//where we place out source code
var srcPath = "client/src";
//where any processed code or vendor files gets placed for use in development
var buildPath = "client/build";
//location to place vendor files for use in development
var vendorBuildPath = buildPath + "/vendor";
//where the final web application is placed
var distPath = "public/client";
//location of our vendor packages
var bowerPath = "bower_components";
var cfg={
//our client application source code src globs and build paths
root_html : { src: srcPath + "/index.html", bld: buildPath },
css : { src: srcPath + "/stylesheets/**/*.css", bld: buildPath + "/stylesheets" },
js : { src: srcPath + "/javascripts/**/*.js" },
html : { src: [srcPath + "/**/*.html", "!"+srcPath + "/*.html"]},
//vendor css src globs
bootstrap_sass: { src: bowerPath + "/bootstrap-sass/assets/stylesheets/" },
//vendor fonts src globs
bootstrap_fonts: { src: bowerPath + "/bootstrap-sass/assets/fonts/**/*" },
//vendor js src globs
jquery: { src: bowerPath + "/jquery2/jquery.js" },
bootstrap_js: { src: bowerPath + "/bootstrap-sass/assets/javascripts/bootstrap.js" },
angular: { src: bowerPath + "/angular/angular.js" },
angular_ui_router: { src: bowerPath + "/angular-ui-router/release/angular-ui-router.js" },
angular_resource: { src: bowerPath + "/angular-resource/angular-resource.js" },
//vendor build locations
vendor_js : { bld: vendorBuildPath + "/javascripts" },
vendor_css : { bld: vendorBuildPath + "/stylesheets" },
vendor_fonts : { bld: vendorBuildPath + "/stylesheets/fonts" },
apiUrl: { dev: "http://localhost:3000",
prd: "https://glacial-earth-69618.herokuapp.com"},
};
//files within these paths will be served as root-level resources in this priority order
var devResourcePath = [
cfg.vendor_js.bld,
cfg.vendor_css.bld,
buildPath+"/javascripts",
buildPath+"/stylesheets",
srcPath,
srcPath+"/javascripts",
srcPath+"/stylesheets",
];
//remove all files below the build area
gulp.task("clean:build", function() {
return del(buildPath);
});
//remove all files below the dist area
gulp.task("clean:dist", function() {
return del(distPath);
});
//remove all files below both the build and dist area
gulp.task("clean", ["clean:build", "clean:dist"]);
//place vendor css files in build area
gulp.task("vendor_css", function(){
return gulp.src([
//cfg.bootstrap_css.src,
])
.pipe(gulp.dest(cfg.vendor_css.bld));
});
//place vendor js files in build area
gulp.task("vendor_js", function(){
return gulp.src([
cfg.jquery.src,
cfg.bootstrap_js.src,
cfg.angular.src,
cfg.angular_ui_router.src,
cfg.angular_resource.src,
])
.pipe(gulp.dest(cfg.vendor_js.bld));
});
//place vendor font files in build area
gulp.task('vendor_fonts', function() {
//access the following font files
return gulp.src([
cfg.bootstrap_fonts.src,
])
.pipe(gulp.dest(cfg.vendor_fonts.bld));
});
gulp.task('css', function() {
return gulp.src(cfg.css.src).pipe(debug())
.pipe(sourcemaps.init())
.pipe(sass({ includePaths: [cfg.bootstrap_sass.src] }))
.pipe(sourcemaps.write("./maps"))
.pipe(gulp.dest(cfg.css.bld)).pipe(debug());
});
//prepare the development area
gulp.task("build", sync.sync(["clean:build", ["vendor_css", "vendor_js", "vendor_fonts", "css"]]));
//helper method to launch server and to watch for changes
function browserSyncInit(baseDir, watchFiles) {
browserSync.instance = browserSync.init(watchFiles, {
server: { baseDir: baseDir },
port: 8080,
ui: { port: 8090 }
});
};
//run the browser against the development/build area and watch files being edited
gulp.task("browserSync", ["build"], function() {
browserSyncInit(devResourcePath,[
cfg.root_html.src,
cfg.css.bld + "/**/*.css",
cfg.js.src,
cfg.html.src,
]);
});
//prepare the development environment, launch server, and watch for changes
gulp.task("run", ["build", "browserSync"], function (){
//extensions to watch() within even if we need to pre-process source code
gulp.watch(cfg.css.src, ["css"]);
});
//build assets referenced from root-level HTML file and create refs in HTML file
gulp.task("dist:assets", ["build"], function(){
return gulp.src(cfg.root_html.src).pipe(debug())
.pipe(useref({ searchPath: devResourcePath }))
.pipe(gulpif(["**/*.js"], replace(cfg.apiUrl.dev,cfg.apiUrl.prd))) //change URLs
.pipe(gulpif(["**/*.js"], uglify())) //minify JS
.pipe(gulpif(["**/*.css"], cssMin())) //minify CSS
.pipe(gulp.dest(distPath)).pipe(debug());
});
//build/copy over font resources into dist tree
gulp.task("dist:fonts", function(){
return gulp.src(cfg.vendor_fonts.bld + "/**/*", {base: cfg.vendor_css.bld})
.pipe(gulp.dest(distPath));
});
//build/copy over HTML resources into dist tree
gulp.task("dist:html", function(){
return gulp.src(cfg.html.src, {base: srcPath+"/javascripts"}).pipe(debug())
.pipe(htmlMin({collapseWhitespace: true})) //minify HTML
.pipe(gulp.dest(distPath)).pipe(debug());
});
//build all dist artifacts ready for deployment
gulp.task("dist", sync.sync(["clean:dist","build", "dist:assets", "dist:fonts", "dist:html"]));
//execute the dist webapp in a web server
gulp.task("dist:run", ["dist"], function() {
browserSyncInit(distPath);
});
//default task will build the development area and launch the browser
gulp.task("default", ["run"]);