-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
164 lines (149 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
// Load plugins
const gulp = require('gulp');
const babel = require('gulp-babel');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const babelify = require('babelify');
const connect = require('gulp-connect');
const source = require('vinyl-source-stream');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const cleanCss = require('gulp-clean-css');
const eslint = require('gulp-eslint');
const uglify = require('gulp-uglify');
const imagemin = require('gulp-imagemin');
const rename = require('gulp-rename');
const clean = require('gulp-clean');
const cache = require('gulp-cache');
const concat = require('gulp-concat');
const runSequence = require('run-sequence');
const open = require('open');
// Lint javascript
gulp.task('lint', () => {
return gulp.src([
'src/**/*.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// Public assets
gulp.task('public', () => {
return gulp.src([
'public/**/*'
])
.pipe(connect.reload())
.pipe(gulp.dest('dist/'));
});
// Styles
gulp.task('styles', () => {
return gulp.src([
'src/styles/index.scss'
])
.pipe(sass().on('error', sass.logError))
.pipe(rename({
basename: 'app'
}))
.pipe(autoprefixer({
browsers: ['last 10 versions'],
cascade: false
}))
.pipe(cleanCss({
compatibility: 'ie8'
}))
.pipe(gulp.dest('dist/styles'))
.pipe(rename({
suffix: '.min'
}))
.pipe(connect.reload())
.pipe(gulp.dest('dist/styles'))
});
// Vendor
gulp.task('vendor', () => {
return gulp.src([
'node_modules/requirejs/require.js'
])
.pipe(concat('vendor.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(rename({ suffix: '.min' }))
.pipe(uglify())
.pipe(connect.reload())
.pipe(gulp.dest('dist/scripts'));
});
// Scripts
gulp.task('scripts', ['lint'], () => {
return browserify({
entries: './src/index.js',
extensions: ['.js'],
debug: true,
paths: ['./src/', './node_modules']
})
.transform(babelify, {
presets: ['env', 'react']
})
.bundle()
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulp.dest('dist/scripts'))
.pipe(rename({
suffix: '.min'
}))
.pipe(uglify())
.pipe(connect.reload())
.pipe(gulp.dest('dist/scripts'));
});
// Images
gulp.task('images', () => {
return gulp.src('src/images/**/*')
.pipe(cache(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
})))
.pipe(connect.reload())
.pipe(gulp.dest('dist/images'));
});
// Clean
gulp.task('clean', () => {
return gulp.src(['dist'], { read: false })
.pipe(clean());
});
// Build App
gulp.task('build', ['clean'], (callback) => {
runSequence(
['public', 'vendor', 'images'],
'styles',
'scripts',
callback
);
});
// Default task
gulp.task('default', ['build']);
// Watch
gulp.task('server', ['build'], () => {
// Watch .scss files
gulp.watch('src/styles/**/*.scss', (event) => {
console.log(`File ${event.path} was ${event.type}, running tasks...`);
gulp.run('styles');
});
// Watch .js files
gulp.watch('src/**/*.js', (event) => {
console.log(`File ${event.path} was ${event.type}, running tasks...`);
gulp.run('scripts');
});
// Watch image files
gulp.watch('src/images/**/*', (event) => {
console.log(`File ${event.path} was ${event.type}, running tasks...`);
gulp.run('images');
});
// Watch public assets
gulp.watch('public/**/*', (event) => {
console.log(`File ${event.path} was ${event.type}, running tasks...`);
gulp.run('public');
});
connect.server({
root: 'dist',
livereload: true
});
open('http://localhost:8080');
});