-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
162 lines (136 loc) · 4.18 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
var gulp = require("gulp");
var del = require("del");
var path = require("path");
var lazypipe = require("lazypipe");
var $ = require("gulp-load-plugins")({lazy: false});
var paths = {
src: "./src",
assets: {
src: "./assets",
target: "./build/sources/assets"
},
target: {
root: "./build",
js: "./build/js",
css: "./build/css"
},
scss: {
src: "./scss",
target: "./assets/css/scss/"
}
};
var _ = {
vars: {},
pathAny: function (dir, extension) {
return path.join(dir, "**/*." + (extension || "*"))
},
isProduction: function () {
return !!_.vars.prod;
},
ifProduction: function (doStream) {
return _.isProduction() ? doStream : gutil.noop();
},
isEnv: function (variable) {
return !($.util.env[variable] === undefined)
},
extendHtml: function() {
var context = {
environment: (_.isProduction()) ? "prod" : "dev"
};
return lazypipe()
.pipe($.htmlExtend, {annotations: false, verbose: true, root: paths.src})
.pipe($.preprocess, {context: context}); //should be after htmlExtend
}
};
gulp.task("clean", function (cb) {
del(paths.target.root, cb);
});
gulp.task("clean:assets", function (cb) {
del([paths.target.js, paths.target.css], cb);
});
gulp.task("scss", function () {
return gulp.src(_.pathAny(paths.scss.src))
.pipe($.sourcemaps.init())
.pipe($.sass())
.pipe($.autoprefixer('last 1 version', '> 1%', 'ie 8'))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(paths.scss.target));
});
gulp.task("useref", function () {
var options = {
searchPath: paths.assets.src
};
var sourceMapsWriteOptions = {
sourceRoot: "/sources/does-not-matter/",
includeContent: false
};
var cssFilter = $.filter("**/*.css");
var jsFilter = $.filter("**/*.js");
var assets = $.useref.assets(options, lazypipe().pipe($.sourcemaps.init, {loadMaps: true}));
var stream = gulp
.src(_.pathAny(paths.src, "html"))
.pipe(_.extendHtml()())
.pipe(assets);
var processJs = lazypipe()
.pipe($.ngAnnotate, {gulpWarnings: false})
.pipe($.uglify);
if (_.isEnv("minify")) {
stream = stream
.pipe($.if("*.js", processJs()))
.pipe($.if("*.css", $.minifyCss()))
.pipe($.rev())
.pipe($.if("*.js", $.sourcemaps.write(".", sourceMapsWriteOptions)));
}
return stream
.pipe(assets.restore())
.pipe($.useref())
.pipe($.revReplace())
.pipe(gulp.dest(paths.target.root))
});
gulp.task("assets", function() {
return gulp
.src(_.pathAny(paths.assets.src))
.pipe(gulp.dest(paths.target.root))
});
gulp.task("assets:copy-original", function() {
return gulp
.src(_.pathAny(paths.assets.src, "js"))
.pipe(gulp.dest(paths.assets.target))
});
gulp.task("htmlExtend", function () {
return gulp
.src(_.pathAny(paths.src, "html"))
.pipe(_.extendHtml()())
.pipe(gulp.dest(paths.target.root))
});
gulp.task("webserver:run", function () {
gulp.src(paths.target.root)
.pipe($.webserver({
host: "0.0.0.0"
}));
});
gulp.task("build.dev", gulp.series("clean", "scss", "htmlExtend", "assets"));
gulp.task("build.prod", function() {
_.vars.prod = true;
return gulp.series("clean", "scss", "useref", "assets:copy-original")();
});
gulp.task("webserver", gulp.series("build.dev", "webserver:run"));
(function watch() {
gulp.task("watch:scss", function() {
$.watch(_.pathAny(paths.scss.src), gulp.series("scss"))
});
gulp.task("watch:assets", function() {
$.watch(_.pathAny(paths.assets.src), gulp.series("clean:assets", "assets"))
});
gulp.task("watch:html", function() {
$.watch(_.pathAny(paths.src), gulp.series("build.dev"))
});
gulp.task("watch", gulp.series("build.dev", gulp.parallel("webserver:run", "watch:scss", "watch:assets", "watch:html")));
})();
(function() {
if (_.isProduction()) {
$.util.log("----PRODUCTION ENV----")
} else {
$.util.log("----DEV ENV----")
}
})();