-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
135 lines (116 loc) · 3.32 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
'use strict';
const watchify = require('watchify');
const browserify = require('browserify');
const gulp = require('gulp');
const debug = require('gulp-debug');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const gutil = require('gulp-util');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const autoprefixer = require('gulp-autoprefixer');
const gulpif = require('gulp-if');
const plumber = require('gulp-plumber');
const cssminify = require('gulp-minify-css');
const babel = require('gulp-babel');
const rename = require('gulp-rename');
const merge = require('merge-stream');
const runSequence = require('run-sequence');
const del = require('del');
const sass = require('gulp-sass');
const babelify = require('babelify');
var bundle;
function onError(err) {
gutil.log(gutil.colors.red('Error:'), err);
this.emit('end');
}
function onSuccess(msg) {
gutil.log(gutil.colors.green('Build:'), msg);
}
function getBrowserify(dev) {
if(!bundle) {
bundle = browserify({
debug: !dev,
entries: 'src/js/web-components.js',
cache: {},
packageCache: {},
extensions: ['.js', '.jsx'],
})
.transform(
babelify
);
if(dev) {
bundle.plugin(watchify);
bundle.on('update', function() {
return getJS(dev);
});
bundle.on('log', onSuccess);
bundle.on('error', onError);
}
}
return bundle;
}
function getJS(dev) {
return getBrowserify(dev).bundle()
.on('error', onError)
.pipe(source('web-components.js'))
.pipe(debug({title: 'web-components JS:'}))
.pipe(buffer())
.pipe(plumber({errorHandler: onError}))
.pipe(gulpif(dev, sourcemaps.init({loadMaps: true})))
.pipe(gulpif(!dev, gulp.dest('./build')))
.pipe(gulpif(!dev, uglify()))
.pipe(gulpif(!dev, rename({ extname: '.min.js' })))
.pipe(gulpif(dev, sourcemaps.write('./')))
.pipe(gulp.dest('./build'));
}
function getSass(dev) {
return gulp.src('src/scss/web-components.scss')
.on('error', onError)
.pipe(debug({title: 'web-components scss:'}))
.pipe(plumber({errorHandler: onError}))
.pipe(gulpif(dev, sourcemaps.init({loadMaps: true})))
.pipe(sass())
.pipe(autoprefixer({
browsers: ['last 2 versions', 'IE 10']
}))
.pipe(gulpif(!dev, gulp.dest('./build')))
.pipe(gulpif(!dev, cssminify()))
.pipe(gulpif(!dev, rename({ extname: '.min.css' })))
.pipe(gulpif(dev, sourcemaps.write('./')))
.pipe(gulp.dest('./build'));
}
gulp.task('clean:build', () => {
return del('./build');
});
gulp.task('clean:prepublish', () => {
return del(['./lib', './sass']);
});
gulp.task('sass', () => {
return getSass(true);
});
gulp.task('js', () => {
return getJS(true);
});
gulp.task('dev', ['clean:build'], () => {
gulp.watch('src/scss/**/*.scss', ['sass']);
return merge(getSass(true), getJS(true));
});
gulp.task('build', ['clean:build'], () => {
return merge(getSass(false), getJS(false));
});
gulp.task('prepublish:js', () => {
process.env.NODE_ENV='production';
return gulp.src('src/js/**/*.js')
.pipe(babel())
.pipe(gulp.dest('./lib/'));
});
gulp.task('prepublish:sass', () => {
return gulp.src('src/scss/*.scss')
.pipe(gulp.dest('./sass/'));
});
gulp.task('prepublish', ['clean:prepublish'], (done) => {
return runSequence('prepublish:js', 'prepublish:sass', 'build', done);
});
gulp.task('postpublish', ['clean:prepublish']);
gulp.task('default', ['dev']);