-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
182 lines (155 loc) · 4.75 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
const gulp = require('gulp');
// Load plugins
const terser = require('gulp-terser');
const sass = require('gulp-sass')(require('sass'));
const autoprefixer = require('gulp-autoprefixer');
const concat = require('gulp-concat');
const sourcemaps = require('gulp-sourcemaps');
const path = require('path');
const gulp_if = require('gulp-if');
const del = require('del');
let production = true;
let add_sourcemaps = true;
const root = path.resolve('./').replace(/\\/g, '/') + '/';
const lib = root + 'lib/';
//const node = root + 'node_modules/';
const tasks = {
// SCSS
scss_custom: {
type: 'scss',
watch: [
lib + 'src/scss/custom.scss',
],
src_scss: lib + 'src/scss/custom.scss',
dst_path: lib + 'dist/css',
},
scss_static: {
type: 'scss',
watch: [
lib + 'src/scss/static.scss',
lib + 'src/scss/scss/**/*.scss',
],
src_scss: lib + 'src/scss/static.scss',
dst_path: lib + 'dist/css',
},
// JS
js_custom: {
type: 'js',
watch: [
lib + 'src/js/custom.js',
],
dst_path: lib + 'dist/js',
dst_name: 'custom.js',
src_path: lib + 'src/js',
},
}
// ---------------- EXPORTS START ---------------- //
exports.watch_all = ()=>{watchTasks(tasks).forEach(item=>{gulp.watch(item.watch, gulp.task(item.task))})}
exports.run_all = runTasks(tasks);
function watchTasks(tasks) {
let returnTasks = [];
for (const [task_name, item] of Object.entries(tasks)) {
if (typeof item.watch !== 'undefined') {
if (item.type === 'scss') {
gulp.task(task_name, function(){
return scss(item.src_scss, item.dst_path);
});
} else if (item.type === 'js') {
gulp.task(task_name, function(){
return js(item.watch, item.dst_path, item.dst_name, item.src_path);
});
} else if (item.type === 'concat') {
gulp.task(task_name, function(){
return concat_files(item.watch, item.dst_path, item.dst_name);
});
} else if (item.type === 'copy') {
gulp.task(task_name, function(){
return copy(item.watch, item.dst_path);
});
}
returnTasks.push({
watch: item.watch,
task: task_name,
});
}
}
return returnTasks;
}
function runTasks(tasks) {
let runTasks = [];
for (const [task_name, item] of Object.entries(tasks)) {
if (item.type === 'scss') {
gulp.task(task_name, function(){
return scss(item.src_scss, item.dst_path);
});
} else if (item.type === 'js') {
gulp.task(task_name, function(){
return js(item.watch, item.dst_path, item.dst_name, item.src_path);
});
} else if (item.type === 'concat') {
gulp.task(task_name, function(){
return concat_files(item.watch, item.dst_path, item.dst_name);
});
} else if (item.type === 'copy') {
gulp.task(task_name, function(){
return copy(item.watch, item.dst_path);
});
}
runTasks.push(task_name);
}
return gulp.series(runTasks);
}
// ---------------- EXPORTS END ---------------- //
// ---------------- FUNCTIONS START ---------------- //
// JS function
function js(src_mix, dst_path, dst_name, src_path) {
const sourceMapsOptions = {
includeContent: false, // bunun false ile çalışabilmesi için alttaki sourceRoot kısmının doğru ayarlanması gerekir
sourceRoot: path.relative(dst_path, src_path) // Önemli. Genellikle Çıktı: "../../src/js"
};
if (!add_sourcemaps) {
const dst_map = dst_path.replace(/\/$/, '') + '/' + dst_name + '.map';
del(dst_map);
}
return gulp.src(src_mix)
.pipe(gulp_if(add_sourcemaps, sourcemaps.init()))
.pipe(concat(dst_name))
.pipe(gulp_if(production, terser({
output: {
comments: false,
},
})))
.pipe(gulp_if(add_sourcemaps, sourcemaps.write('.', sourceMapsOptions)))
.pipe(gulp.dest(dst_path));
}
// SCSS function
function scss(src_file, dst_path){
let sourceMapsOptions = {
includeContent: false, // bunun false ile çalışabilmesi için alttaki sourceRoot kısmının doğru ayarlanması gerekir
sourceRoot: path.relative(dst_path, src_file.replace(/\/[^\/]*$/, '')) // Önemli. Genellikle Çıktı: "../../src/scss"
}
if (!add_sourcemaps) {
const split = src_file.split('/');
const dst_map = dst_path.replace(/\/$/, '') + '/' + split[split.length - 1].replace(/\.s[ca]ss$/, '.css') + '.map';
del(dst_map);
}
return gulp.src(src_file)
.pipe(gulp_if(add_sourcemaps, sourcemaps.init()))
.pipe(sass({outputStyle: 'compressed'}, true).on('error', sass.logError))
.pipe(autoprefixer({
overrideBrowserslist: ['last 2 versions'],
cascade: false,
}))
.pipe(gulp_if(add_sourcemaps, sourcemaps.write('.', sourceMapsOptions)))
.pipe(gulp.dest(dst_path));
}
function concat_files(src_mix, dst_path, dst_name) {
return gulp.src(src_mix)
.pipe(concat(dst_name))
.pipe(gulp.dest(dst_path));
}
function copy(src_mix, dst_path) {
return gulp.src(src_mix)
.pipe(gulp.dest(dst_path));
}
// ---------------- FUNCTIONS END ---------------- //