-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgulpfile.js
117 lines (99 loc) · 3.01 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
require('dotenv').config();
const MODE = process.env.MODE;
//Modules
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const sass = require('gulp-sass');
const cleanCss = require('gulp-clean-css');
const rename = require('gulp-rename');
const gulpWebpack = require('webpack-stream');
const webpack = require('webpack');
const runSequence = require('gulp-run-sequence');
const del = require('del');
const inject = require('gulp-inject-string');
//Webpack Configs
const webpackDevelopConfig = require('./webpack.development.js');
const webpackProductionConfig = require('./webpack.production.js');
var catalog;
switch(MODE) {
case 'development':
catalog = 'build';
break;
case 'production':
catalog = 'dist';
break;
default:
catalog = 'build';
break;
}
gulp.task('serve', () => {
browserSync.init({
server: catalog
});
gulp.watch('src/*.html', ['html']);
gulp.watch('src/scss/*.scss', ['scss']);
gulp.watch('src/**/*.js', ['js']);
gulp.watch('src/**/*.jsx', ['js']);
});
gulp.task('html', () => {
if(MODE === 'production') {
return gulp.src('src/index.html')
.pipe(inject.after('<!-- CSS -->', '\n\t<link rel="stylesheet" href="css/style.min.css" />'))
.pipe(inject.after('<!-- JS -->', '\n\t<script src="js/bundle.min.js"><\/script>'))
.pipe(gulp.dest(`${catalog}`));
}
if(MODE === 'development') {
return gulp.src('src/index.html')
.pipe(inject.after('<!-- CSS -->', '\n\t<link rel="stylesheet" href="css/style.css" />'))
.pipe(inject.after('<!-- JS -->', '\n\t<script src="js/bundle.js"><\/script>'))
.pipe(gulp.dest(`${catalog}`))
.pipe(browserSync.stream());
}
});
gulp.task('scss', () => {
if(MODE === 'development') {
return gulp.src('src/scss/*.scss')
.pipe(sass())
.pipe(rename('style.css'))
.pipe(gulp.dest(`${catalog}/css`))
.pipe(browserSync.stream());
}
if(MODE === 'production') {
return gulp.src('src/scss/*.scss')
.pipe(sass())
.pipe(cleanCss())
.pipe(rename('style.min.css'))
.pipe(gulp.dest(`${catalog}/css`));
}
});
gulp.task('js', () => {
if(MODE === 'development') {
return gulp.src('src/main.jsx')
.pipe(gulpWebpack(webpackDevelopConfig)).on('error', function handleError() {
this.emit('end'); // Recover from errors
})
.pipe(gulp.dest(`${catalog}/js`))
.pipe(browserSync.stream());
}
if(MODE === 'production') {
return gulp.src('src/main.jsx')
.pipe(gulpWebpack(webpackProductionConfig)).on('error', function handleError() {
this.emit('end'); // Recover from errors
})
.pipe(gulp.dest(`${catalog}/js`));
}
});
gulp.task('clean', () => {
if(MODE === 'development') {
return del(['build']);
}
if(MODE === 'production') {
return del(['dist']);
}
});
gulp.task('development', function(cb) {
runSequence('clean', ['html', 'scss', 'js'], 'serve', cb);
});
gulp.task('production', function(cb) {
runSequence('clean', ['html', 'scss', 'js'], cb);
});