This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgulpfile.js
220 lines (198 loc) · 6.85 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
/// <binding BeforeBuild='default' Clean='clean' ProjectOpened='watch' />
var gulp = require("gulp"),
inject = require("gulp-inject"),
filter = require("gulp-filter"),
concat = require("gulp-concat"),
replace = require("gulp-replace"),
rename = require("gulp-rename"),
clean = require("gulp-clean"),
del = require("del"),
mergestream = require("merge-stream"),
sequence = require("run-sequence"),
util = require("gulp-util"), // preserve to be able output custom messages in future
uglify = require("gulp-uglify"),
bourbon = require("node-bourbon"),
autoprefixer = require("autoprefixer"),
mainBowerFiles = require("main-bower-files"),
cssnano = require("cssnano"),
postcss = require("gulp-postcss"),
sass = require("gulp-sass"),
htmlmin = require("gulp-htmlmin"),
imagemin = require("gulp-image"),
sourcemaps = require("gulp-sourcemaps"),
eslint = require("gulp-eslint"),
zip = require("gulp-zip"),
gitignore = require("gulp-exclude-gitignore"),
bowerMain = require("bower-main"),
merge2 = require("merge2");
var bowerMainJavaScriptFiles = bowerMain("js", "min.js");
var regex = {
css: /\.css$/,
html: /\.(html|htm)$/,
js: /\.js$/,
ext: /\.([^\.]+)$/
};
function getPackage() {
delete require.cache[require.resolve("./package.json")];
return require("./package.json");
}
function getBundleConfig() {
delete require.cache[require.resolve("./bundleconfig.json")];
return require("./bundleconfig.json");
}
function merge(streams) {
return streams.length ? mergestream(streams) : mergestream().end();
}
gulp.task("min", ["min:js", "min:css", "min:html"]);
function mapSources() {
return sourcemaps.mapSources(function(sourcePath, file) {
var sourceRootPathEndIndex = sourcePath.indexOf("assets");
var sourceRootPath = sourcePath.substring(0, sourceRootPathEndIndex);
// ../../../ for assets/static/bundle + ../ count of parent folders in real path
var relativeRootPath =
"../".repeat(
(sourceRootPath.match(new RegExp("/", "g")) || []).length + 3
) + sourcePath.substring(sourceRootPathEndIndex);
return relativeRootPath;
});
}
gulp.task("min:js", function() {
var tasks = getBundles(regex.js).map(function(bundle) {
return gulp
.src(bundle.inputFiles, { base: "." })
.pipe(sourcemaps.init())
.pipe(mapSources())
.pipe(concat(bundle.outputFileName))
//.pipe(uglify({ mangle: false }))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("."));
});
return merge(tasks);
});
gulp.task("packJavaScript", function() {
return merge2(
gulp.src(bowerMainJavaScriptFiles.minified),
gulp.src(bowerMainJavaScriptFiles.minifiedNotFound)
)
.pipe(concat("scripts_dependencies.js"))
.pipe(gulp.dest("assets/static/bundle"));
});
gulp.task("min:css", function() {
var tasks = getBundles(regex.css).map(function(bundle) {
return gulp
.src(bundle.inputFiles, { base: "." })
.pipe(sourcemaps.init())
.pipe(mapSources())
.pipe(concat(bundle.outputFileName))
.pipe(
postcss([
autoprefixer({
browsers: [
"Explorer >= 10",
"Edge >= 12",
"Firefox >= 19",
"Chrome >= 20",
"Safari >= 8",
"Opera >= 15",
"iOS >= 8",
"Android >= 4.4",
"ExplorerMobile >= 10",
"last 2 versions"
]
}),
cssnano()
])
)
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("."));
});
return merge(tasks);
});
gulp.task("min:html", function() {
var tasks = getBundles(regex.html).map(function(bundle) {
return gulp
.src(bundle.inputFiles, { base: "." })
.pipe(concat(bundle.outputFileName))
.pipe(
htmlmin({
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true
})
)
.pipe(gulp.dest("."));
});
return merge(tasks);
});
gulp.task("clean", function() {
var files = [].concat.apply(
[],
getBundleConfig().map(function(bundle) {
var fileName = bundle.outputFileName;
return [fileName, fileName.replace(regex.ext, ".$1.map")];
})
);
return del(files);
});
gulp.task("watch", function() {
gulp.watch("./bundleconfig.json", ["min"]);
getBundles(regex.js).forEach(function(bundle) {
gulp.watch(bundle.inputFiles, ["min:js"]);
});
getBundles(regex.css).forEach(function(bundle) {
gulp.watch(bundle.inputFiles, ["min:css"]);
});
getBundles(regex.html).forEach(function(bundle) {
gulp.watch(bundle.inputFiles, ["min:html"]);
});
});
function getBundles(regexPattern) {
return getBundleConfig().filter(function(bundle) {
return regexPattern.test(bundle.outputFileName);
});
}
gulp.task("lint", function() {
var tasks = getBundles(regex.js)
.filter(function(bundle) {
return !bundle.disableLint || bundle.disableLint === undefined;
})
.map(function(bundle) {
return gulp
.src(bundle.inputFiles, { base: "." })
.pipe(eslint("./.eslintrc.json"))
.pipe(eslint.format());
});
return merge(tasks);
});
gulp.task("compress", ["min", "packJavaScript"], function() {
var package = getPackage();
return merge2(
gulp.src(
[].concat(
["./*/**", '!./node_modules/**'],
[].concat.apply(
[],
getBundleConfig().map(function(bundle) {
return bundle.inputFiles.map(function(inputFile) {
return "!" + inputFile;
});
})
)
)
)
.pipe(gitignore()),
// Need to add them manually because otherwise all bundles will be skipped as they are in .gitignore
gulp.src("assets/static/bundle/**", {base: '.'})
)
.pipe(
rename(function(path) {
path.dirname = "default/" + path.dirname;
})
)
.pipe(zip(package.name + "-" + package.version + ".zip"))
.pipe(gulp.dest("artifacts"));
});
// DEFAULT Tasks
gulp.task("default", function(callback) {
sequence("lint", ["min"], callback);
});