This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gulpfile.babel.js
144 lines (116 loc) · 3.72 KB
/
Gulpfile.babel.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
import gulp from 'gulp';
import gutil from 'gulp-util';
import eslint from 'gulp-eslint';
import sourcemaps from 'gulp-sourcemaps';
import del from 'del';
import sass from 'gulp-ruby-sass';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import rename from 'gulp-rename';
import browserSync from 'browser-sync';
import uglify from 'gulp-uglify';
import babel from 'gulp-babel';
import babelify from 'babelify';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
const SRC = './src';
const EXAMPLE_SRC = './example';
const DIST = './dist';
const EXAMPLE_DIST = './distExample';
const SCSS = '/scss';
const JS = '/js';
const PROD_CONFIG = {
sass: { style: 'compressed' },
browserify: {},
};
const DEV_CONFIG = {
sass: { style: 'expanded', sourcemap: true },
browserify: { debug: true },
};
function styles(source, dist, isProd, isServer) {
const config = isProd ? PROD_CONFIG : DEV_CONFIG;
return sass(source, config.sass)
.pipe(postcss([
autoprefixer({
browsers: ['last 2 version'],
}),
]))
.pipe(isProd ? rename({ suffix: '.min' }) : gutil.noop())
.pipe(isServer ? sourcemaps.init({ loadMaps: true }) : gutil.noop())
.pipe(isServer ? sourcemaps.write('./') : gutil.noop())
.pipe(gulp.dest(dist))
.pipe(isServer ? browserSync.stream() : gutil.noop());
}
function lint(src, isProd) {
return gulp.src(`${src}/**/*.js`)
.pipe(eslint())
.pipe(eslint.format())
.pipe(isProd ? eslint.failOnError() : gutil.noop());
}
function scripts(isProd) {
return gulp.src(SRC + JS + '/index.js')
.pipe(babel())
.pipe(isProd ? uglify() : gutil.noop())
.pipe(isProd ? rename({ suffix: '.min' }) : gutil.noop())
.pipe(gulp.dest(DIST));
}
gulp.task('clean', (callback) => {
return del([DIST, EXAMPLE_DIST], callback);
});
gulp.task('styles', () => {
const source = `${SRC}${SCSS}/react-buttons.scss`;
styles(source, DIST, false, false);
return styles(source, DIST, true, false);
});
gulp.task('lint', () => {
return lint(SRC);
});
gulp.task('scripts', ['lint'], () => {
scripts(false);
return scripts(true);
});
gulp.task('dist', ['clean', 'styles', 'scripts']);
gulp.task('default', ['dist']);
function bundle(isProd) {
const config = isProd ? PROD_CONFIG : DEV_CONFIG;
const bundle = browserify(config.browserify);
bundle.add(EXAMPLE_SRC + JS + '/index.js');
bundle.transform(babelify);
return bundle.bundle()
.on('error', function(err) {
gutil.log(gutil.colors.red('ERROR'), err.message);
this.emit('end');
})
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(isProd ? uglify() : gutil.noop())
.pipe(isProd ? rename({ suffix: '.min' }) : gutil.noop())
.pipe(gulp.dest(EXAMPLE_DIST));
}
gulp.task('statics:example', () => {
gulp.src(`${EXAMPLE_SRC}/index.html`).pipe(gulp.dest(EXAMPLE_DIST));
});
gulp.task('styles:example', () => {
return styles(`${EXAMPLE_SRC}${SCSS}/main.scss`, EXAMPLE_DIST, false, true);
});
gulp.task('styles-watch:example', ['styles:example']);
gulp.task('lint:example', () => {
return lint(EXAMPLE_SRC);
});
gulp.task('scripts:example', ['lint:example'], () => {
return bundle(false);
});
gulp.task('scripts-watch:example', ['scripts:example'], browserSync.reload);
gulp.task('dist:example', ['styles:example', 'scripts:example', 'statics:example']);
gulp.task('serve', ['dist:example'], () => {
browserSync({
server: {
baseDir: EXAMPLE_DIST,
},
});
gulp.watch(['./src/**/*.js', './example/**/*.js'], ['scripts-watch:example']);
gulp.watch(['./src/**/*.scss', './example/**/*.scss'], ['styles-watch:example']);
});