-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
156 lines (126 loc) · 5.14 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
"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();
// Source code directory
var srcPath = "client/src";
// Build directory
var buildPath = "client/build";
// Vendor files
var vendorBuildPath = buildPath + "/vendor";
// Web application directory
var distPath = "public/client";
// Vendor packages
var bowerPath = "bower_components";
var cfg = {
// 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"]},
// CSS source
bootstrap_sass: { src: bowerPath + "/bootstrap-sass/assets/stylesheets/" },
// Fonts source
bootstrap_fonts: { src: bowerPath + "/bootstrap-sass/assets/fonts/**/*" },
// JS source
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" },
// Build locations
vendor_js : { bld: vendorBuildPath + "/javascripts" },
vendor_css : { bld: vendorBuildPath + "/stylesheets" },
vendor_fonts : { bld: vendorBuildPath + "/stylesheets/fonts" },
apiUrl: { dev: "http://localhost:3000", prd: "" },
};
// 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 in a build directory
gulp.task("clean:build", function() { return del(buildPath); });
// Remove all files in a dist directory
gulp.task("clean:dist", function() { return del(distPath); });
// Remove all files
gulp.task("clean", ["clean:build", "clean:dist"]);
// Vendor CSS files in a build directory
gulp.task("vendor_css", function() {
return gulp.src([ /*cfg.bootstrap_css.src,*/ ])
.pipe(gulp.dest(cfg.vendor_css.bld));
});
// Vendor JS files in a build directory
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));
});
// Vendor Font files in a build directory
gulp.task('vendor_fonts', function() {
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());
});
gulp.task("build", sync.sync(["clean:build", ["vendor_css", "vendor_js", "vendor_fonts", "css"]]));
// Method to launch server and to watch for changes with Browser-sync
function browserSyncInit(baseDir, watchFiles) {
browserSync.instance = browserSync.init(watchFiles, {
server: { baseDir: baseDir },
port: 8080,
ui: { port: 8090 }
});
};
// Watch files that being edited
gulp.task("browserSync", ["build"], function() {
browserSyncInit(devResourcePath, [ cfg.root_html.src, cfg.css.bld + "/**/*.css",
cfg.js.src, cfg.html.src ]);
});
// Launch server and watch for changes
gulp.task("run", ["build", "browserSync"], function () {
gulp.watch(cfg.css.src, ["css"]);
});
// 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)))
.pipe(gulpif(["**/*.js"], uglify()))
.pipe(gulpif(["**/*.css"], cssMin()))
.pipe(gulp.dest(distPath)).pipe(debug());
});
// Build and copy font files into a dist directory
gulp.task("dist:fonts", function() {
return gulp.src(cfg.vendor_fonts.bld + "/**/*", {base: cfg.vendor_css.bld})
.pipe(gulp.dest(distPath));
});
// Build and copy HTML files into a dist directory
gulp.task("dist:html", function() {
return gulp.src(cfg.html.src, {base: srcPath + "/javascripts"}).pipe(debug())
.pipe(htmlMin({collapseWhitespace: true}))
.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"]));
gulp.task("dist:run", ["dist"], function() {
browserSyncInit(distPath);
});
// Default task - Build and open the browser
gulp.task("default", ["run"]);