-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
197 lines (172 loc) · 5.08 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
let tasks = [
{
source: 'src',
dest: 'dist',
folders: ['home_page', ]
},
];
var mapsIncludeContent = true;
const gulp = require('gulp'),
// sass = require('gulp-sass'),
// autoprefixer = require('gulp-autoprefixer'),
// minify = require('gulp-minify'),
// notify = require('gulp-notify'),
concat = require('gulp-concat'),
sourcemaps = require('gulp-sourcemaps');
//#region /// for compiling sass files.
function stylesCompile(source, dest, options = {}) {
let sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer');
//#region preparations
if (typeof (source) === 'string') {
source = gulp.src(source);
}
let defaultOptions = {
fileName: 'style',
outputStyle: 'expanded'
};
options = { ...defaultOptions, ...options };
let { fileName, outputStyle } = options;
if (!Array.isArray(outputStyle)) {
outputStyle = [outputStyle];
}
//#endregion
let last;
for (let outStyle of outputStyle) {
let filename = fileName + '.css';
last = source
.pipe(sourcemaps.init({ largeFile: true, loadMaps: false }))
.pipe(sass({ outputStyle: outStyle }))
.pipe(autoprefixer('last 2 versions'))
.pipe(sourcemaps.write('.', { includeContent: mapsIncludeContent }))
.pipe(gulp.dest(dest));
}
return last;
}
function stylesDoAll() {
let a;
for (let t of tasks) {
for (let folder of t.folders) {
a = stylesCompile(t.source + '/styles/' + folder + '/**/*.scss', t.dest + '/styles', { fileName: folder, outputStyle: ['compressed'] });
}
}
return a;
}
gulp.task('sass', stylesDoAll);
//#endregion
//#region /// for js files.
function jsCompile(source, dest = './dist/js', options = {}) {
//#region preparations
if (typeof (source) === 'string') {
source = gulp.src(source);
}
const defaultOptions = {
fileName: 'all',
outputStyle: 'expanded'
};
options = { ...defaultOptions, ...options };
let { fileName, outputStyle } = options;
if (!Array.isArray(outputStyle)) {
outputStyle = [outputStyle];
}
//#endregion
const babel = require('gulp-babel');
let babelOptions = {
presets: ['@babel/env']
};
const terser = require('gulp-terser');
let last;
for (let outStyle of outputStyle) {
if (outStyle === 'compressed') {
let filename = fileName + '.js';
last = source
.pipe(sourcemaps.init())
.pipe(babel(babelOptions))
.pipe(terser())
.pipe(concat(filename))
.pipe(sourcemaps.write('.', { includeContent: mapsIncludeContent }))
.pipe(gulp.dest(dest));
} else if (outStyle === 'expanded') {
let filename = fileName + '.js';
last = source
.pipe(sourcemaps.init())
.pipe(babel(babelOptions))
.pipe(terser())
.pipe(sourcemaps.write('.', { includeContent: mapsIncludeContent }))
.pipe(gulp.dest(dest));
}
}
return last;
}
function jsDoAll() {
let a;
for (let t of tasks) {
for (let folder of t.folders) {
a = jsCompile(t.source + '/js/' + folder + '/**/*.scss', t.dest + '/js', { fileName: folder, outputStyle: ['compressed'] });
}
}
return a;
}
gulp.task('js', jsDoAll);
//#endregion
gulp.task('all', () => {
stylesDoAll();
return jsDoAll();
});
gulp.task('all-styles', () => {
return stylesDoAll();
});
gulp.task('all-js', () => {
return jsDoAll();
});
//#region watching
function _watch(sourceFolder, destDir, folders) {
let last;
console.log('=================');
console.log('to:>>>>> ', destDir);
for (let folder of folders) {
/// for scss
{
let sourceDir, func;
sourceDir = sourceFolder + '/styles/' + folder + '/**/*.scss';
console.log('watching: ', sourceDir);
func = () => {
console.log('styles');
console.log('from:', sourceDir);
console.log('to:', destDir + '/styles');
return stylesCompile(sourceDir, destDir + '/styles', { fileName: folder, outputStyle: ['compressed'] });
}
gulp.watch([sourceDir], func);
}
/// for js
{
let sourceDir, func;
sourceDir = sourceFolder + '/js/' + folder + '/**/*.js';
console.log('watching: ', sourceDir);
func = () => {
console.log('scripts');
console.log('from:', sourceDir);
console.log('to:', destDir + '/js');
return jsCompile(sourceDir, destDir + '/js', { fileName: folder, outputStyle: ['compressed'] });
};
last = gulp.watch([sourceDir], func);
}
}
return last;
}
function watchHome() {
return _watch(homePageSourceDir, homePageDestDir, home_folders);
}
function watchCalculator() {
return _watch(calculatorSourceDir, calculatorDestDir, calc_folders);
}
function watchAll() {
let a;
for (let t of tasks) {
a = _watch(t.source, t.dest, t.folders);
}
return a;
}
gulp.task('watch', watchAll);
gulp.task('default', watchAll);
//#endregion