-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
108 lines (100 loc) · 2.73 KB
/
gulpfile.babel.js
File metadata and controls
108 lines (100 loc) · 2.73 KB
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
// Dependencies
import glob from 'glob';
import gulp from 'gulp';
import gutil from 'gulp-util';
import babel from 'gulp-babel';
import plumber from 'gulp-plumber';
import less from 'gulp-less';
import prefix from 'gulp-autoprefixer';
import minify from 'gulp-cssnano';
import uglify from 'uglifyify';
import concat from 'gulp-concat';
import browserify from 'browserify';
import babelify from 'babelify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import eventStream from 'event-stream';
// Paths
const MODULES_PATH = './node_modules';
const SRC_PATH = 'app/assets';
const DIST_PATH = 'public/assets';
// Configs
const config = {
paths: {
less: {
src: [`${SRC_PATH}/stylesheets/*.less`],
dist: `${DIST_PATH}/stylesheets/`
},
img: {
src: [`${SRC_PATH}/images/*.{jpg,png,svg}`],
dist: `${DIST_PATH}/images/`
},
js: {
src: `${SRC_PATH}/javascripts/*.js`,
dist: `${DIST_PATH}/javascripts/`
}
},
options: {
babel: {
presets: [
'es2015',
'stage-0',
'react'
]
},
prefix: {
cascade: false,
browsers: [
'last 3 versions'
]
}
}
};
// Images
gulp.task('images', () => {
gulp.src(config.paths.img.src)
.pipe(gulp.dest(config.paths.img.dist));
});
// Stylesheets
gulp.task('stylesheets', () => {
let production = gutil.env.type === 'production';
gulp.src(config.paths.less.src)
.pipe(plumber())
.pipe(less())
.pipe(prefix(config.options.prefix))
.pipe(production ? minify() : gutil.noop())
.pipe(plumber.stop())
.pipe(gulp.dest(config.paths.less.dist));
});
// JavaScripts
gulp.task('javascripts', () => {
let sourceDistPath;
let browserifyInstance;
let production = gutil.env.type === 'production';
let sourcesFilesPaths = glob.sync(config.paths.js.src);
let sourceStreams = sourcesFilesPaths.map((sourceFile) => {
sourceDistPath = sourceFile.replace(`${SRC_PATH}/javascripts/`, '');
browserifyInstance = browserify({ entries: [sourceFile] }).transform(babelify);
if(production) {
browserifyInstance.transform(uglify);
}
return browserifyInstance
.bundle()
.on('error', console.error.bind(console))
.pipe(plumber())
.pipe(source(sourceDistPath))
.pipe(buffer())
.pipe(plumber.stop())
.pipe(gulp.dest(config.paths.js.dist))
});
return eventStream.merge.apply(null, sourceStreams);
});
// Development
gulp.task('watch', () => {
gulp.watch(`${SRC_PATH}/stylesheets/**/*.less`, ['stylesheets', 'images']);
gulp.watch(`${SRC_PATH}/javascripts/**/*.js`, ['javascripts']);
});
// Bundle
gulp.task('bundle', [ 'images', 'stylesheets', 'javascripts' ]);
// Default task
gulp.task('default', [ 'watch', 'build' ]);