-
Notifications
You must be signed in to change notification settings - Fork 17
/
gulpfile.js
90 lines (81 loc) · 2.65 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
var gulp = require('gulp');
var gutil = require('gulp-util');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserSync = require('browser-sync');
var browserify = require('browserify');
var sassify = require('sassify');
var stringify = require('stringify');
var watchify = require('watchify');
var gulpif = require('gulp-if');
function setupBrowserify(watch) {
var bundleOptions = {
cache: {},
packageCache: {},
paths: ['./src'],
standalone: 'XPayStationWidget',
fullPaths: false,
debug: true
};
var bundler = browserify('./src/main.js', bundleOptions);
bundler.require('./src/main.js', {entry: true, expose: 'main'});
bundler.transform({
outputStyle: 'compressed',
base64Encode: false,
'auto-inject': true
}, sassify);
bundler.transform(stringify({
extensions: ['.svg'],
minify: true,
minifier: {
extensions: ['.svg'],
options: {
removeComments: true,
removeCommentsFromCDATA: true,
removeCDATASectionsFromCDATA: true,
collapseWhitespace: true
}
}
}));
if (watch) {
bundler = watchify(bundler);
bundler.on('update', function () { // on any dep update, runs the bundler
runBundle(bundler, watch);
});
}
runBundle(bundler, watch);
}
function runBundle(bundler, watch) {
return bundler.bundle()
// log errors if they happen
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('widget.js'))
.pipe(gulp.dest('./dist'))
.pipe(buffer())
.pipe(rename('widget.min.js'))
.pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
.pipe(uglify())
.pipe(sourcemaps.write('./', {includeContent: true, sourceRoot: '.', debug: false})) // writes .map file
.pipe(gulp.dest('./dist'))
.pipe(gulpif(watch, browserSync.reload({stream: true, once: true})));
}
gulp.task('build', function () {
setupBrowserify(false);
});
gulp.task('browser-sync', function () {
browserSync({
startPath: '/index.html',
server: {
baseDir: ['example', 'dist']
},
port: 3100,
ghostMode: false
});
});
gulp.task('serve', ['browser-sync'], function () {
setupBrowserify(true);
gulp.watch(['example/*.html', 'dist/*.js']).on('change', browserSync.reload); //all the other files are managed by watchify
});