-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
165 lines (148 loc) · 3.73 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
const { dest, parallel, series, src, watch } = require('gulp');
const autoprefixer = require('autoprefixer');
const browsersync = require('browser-sync').create();
const combinemq = require('postcss-combine-media-query');
const cssnano = require('cssnano');
const imagemin = require('gulp-imagemin');
const postcss = require('gulp-postcss');
const replace = require('gulp-replace');
const sass = require('gulp-sass')(require('sass'));
const svgo = require('gulp-svgo');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const tsify = require('tsify');
const sourcemaps = require('gulp-sourcemaps');
const buffer = require('vinyl-buffer');
// html task
function htmlTask_PROD() {
return src('index.html')
.pipe(replace(/\/output/g, ''))
.pipe(dest('dist'));
}
// assets
function assetsTask() {
return src(['src/assets/**', '!src/assets/images/**']).pipe(
dest('dist/src/assets')
);
}
// data
// function dataTask() {
// return src('src/data/**').pipe(dest('dist/src/data'));
// }
// images
function imageminTask() {
return src('src/assets/images/*.{png,jpg,jpeg}')
.pipe(imagemin({ verbose: true }))
.pipe(dest('dist/src/assets/images'));
}
// svgs
function svgoTask() {
return src('src/assets/images/*.svg')
.pipe(svgo())
.pipe(dest('dist/src/assets/images'));
}
// sass
function scssTask() {
return src('src/scss/Main.scss', { sourcemaps: true })
.pipe(sass().on('error', sass.logError))
.pipe(postcss([autoprefixer()]))
.pipe(dest('src/css', { sourcemaps: '.' }));
}
function scssTask_PROD() {
return src('src/scss/Main.scss', { sourcemaps: true })
.pipe(sass().on('error', sass.logError))
.pipe(postcss([autoprefixer(), combinemq(), cssnano()]))
.pipe(dest('dist/src/css', { sourcemaps: '.' }));
}
// ts
function typescriptTask() {
return browserify({
basedir: '.',
debug: true,
entries: ['src/ts/main.ts'],
cache: {},
packageCache: {},
})
.plugin(tsify)
.transform('babelify', {
presets: ['es2015'],
extensions: ['.ts'],
})
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(dest('src/js'));
}
function typescriptTask_PROD() {
return browserify({
basedir: '.',
debug: true,
entries: ['src/ts/main.ts'],
cache: {},
packageCache: {},
})
.plugin(tsify)
.transform('babelify', {
presets: ['es2015'],
extensions: ['.ts'],
})
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(dest('dist/src/js'));
}
// cache-busting
// note, don't forget to append within the index.html, ?cb=123 to any link and scripts
function cacheBustTask() {
let cbNumber = new Date().getTime();
return src('index.html')
.pipe(replace(/cb=\d+/g, 'cb=' + cbNumber))
.pipe(dest('.'));
}
// browsersync
function browserSyncServe(cb) {
browsersync.init({
server: {
baseDir: '.',
},
});
cb();
}
function browserSyncReload(cb) {
browsersync.reload();
cb();
}
// watch
function watchTask() {
watch('index.html', browserSyncReload);
watch(
['src/ts/**/*.ts', '!src/ts/tests/*.ts'],
series(typescriptTask, cacheBustTask, browserSyncReload)
);
watch(
['src/scss/**/*.scss'],
series(scssTask, cacheBustTask, browserSyncReload)
);
watch('src/assets/images/*', series(imageminTask, browserSyncReload));
}
exports.default = series(
scssTask,
typescriptTask,
cacheBustTask,
browserSyncServe,
watchTask
);
exports.prod = series(
htmlTask_PROD,
assetsTask,
// dataTask,
imageminTask,
svgoTask,
scssTask_PROD,
typescriptTask_PROD,
cacheBustTask
);