-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
189 lines (149 loc) · 4.07 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
const DEV_OUTPUT_DIR = 'dist/'; // eslint-disable-line no-unused-vars
const browserify = require('browserify');
const vinyl_source_stream = require('vinyl-source-stream');
const gulp = require('gulp');
const eslint = require('gulp-eslint');
const plumber = require('gulp-plumber');
const minify = require('gulp-minify');
const rename = require('gulp-rename');
const filelog = require('gulp-filelog');
const replace = require('gulp-replace');
const header = require('gulp-header');
const expect = require('gulp-expect-file');
const fs = require('fs');
const path = require('path');
const ip = require('ip');
const del = require('del');
const browserSync = require('browser-sync');
const PKG_INFO = require('./package.json');
// Build filenames.
const BUILDS = {
uncompressed: PKG_INFO.name + '.js',
compressed: PKG_INFO.name + '-' + PKG_INFO.version +'.min.js'
};
// gulp-header.
const BANNER = fs.readFileSync('banner.txt').toString();
const BANNER_OPTS = {
pkg: PKG_INFO,
currentYear: (new Date()).getFullYear()
};
// gulp-expect-file options.
const EXPECT_OPTS = {
silent: true,
errorOnFailure: true,
checkRealFile: true
};
const JS_FILES = ['gulpfile.js', 'lib/*.js'];
// gulp.task('dev-config', function() {
// return gulp.src('lib/config.production.js')
// .pipe(replace('janus.dot.cc','dotengine.dot.cc'))
// .pipe(replace('TestToken','TestToken?from=web'))
// .pipe(rename('config.js'))
// .pipe(gulp.dest('lib/'));
// });
// gulp.task('production-config', function(){
// return gulp.src('lib/config.production.js')
// .pipe(rename('config.js'))
// .pipe(gulp.dest('lib/'));
// });
gulp.task('clean', () => del(DEV_OUTPUT_DIR,{force:true}))
gulp.task('lint', function () {
return gulp.src(JS_FILES)
.pipe(plumber())
.pipe(eslint({
plugins: ['import'],
extends : [ 'eslint:recommended' ],
envs:
[
'browser',
'es6',
'node'
],
rules:
{
'no-console' : 0,
'no-undef' : 2,
'no-unused-vars' : [ 2, { vars: 'all', args: 'after-used' }],
'no-empty' : 0,
},
parserOptions: {
sourceType: 'module',
ecmaVersion: 2017
}
})).pipe(eslint.format());
});
gulp.task('browserify', function () {
return browserify({
entries : path.join(__dirname,PKG_INFO.main),
extensions: ['.js']
})
.transform('babelify',{
presets : [ 'es2015' ],
plugins : [ 'transform-runtime', 'transform-object-assign' ]
})
.bundle()
.pipe(vinyl_source_stream(PKG_INFO.name + '.js'))
.pipe(filelog('browserify'))
.pipe(header(BANNER, BANNER_OPTS))
.pipe(rename(BUILDS.uncompressed))
.pipe(gulp.dest(DEV_OUTPUT_DIR));
});
gulp.task('uglify', function () {
var src = DEV_OUTPUT_DIR + BUILDS.uncompressed;
return gulp.src(src)
.pipe(filelog('uglify'))
.pipe(expect(EXPECT_OPTS, src))
.pipe(minify())
.pipe(header(BANNER, BANNER_OPTS))
.pipe(rename(BUILDS.compressed))
.pipe(gulp.dest('dist/'));
});
gulp.task('livebrowser', (done) =>
{
let host = ip.address();
console.log('host: ', host);
browserSync(
{
open : 'local',
server :
{
baseDir : DEV_OUTPUT_DIR
},
https : false,
ghostMode : false,
files : path.join(DEV_OUTPUT_DIR, '**', '*')
});
done();
});
gulp.task('html', () =>
{
return gulp.src(['example/index.html', 'example/sub.html'])
.pipe(replace('=timestamp','='+ Math.random().toString(36).substring(7)))
.pipe(gulp.dest(DEV_OUTPUT_DIR));
});
gulp.task('watch', (done) =>
{
// Watch changes in HTML.
gulp.watch([ 'example/index.html', 'example/sub.html'], gulp.series(
'html'
));
// Watch changes in JS files.
gulp.watch([ 'gulpfile.js', 'lib/*.js' ], gulp.series(
'lint',
'browserify',
'html'
));
done();
});
gulp.task('live', gulp.series(
'clean',
'lint',
'browserify',
'html',
'watch',
'livebrowser'
));
gulp.task('dev', gulp.series('lint', 'browserify','html'));
gulp.task('dist', gulp.series('lint', 'browserify', 'uglify'));
gulp.task('default', gulp.series('live'));