1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00
epub.js/gulpfile.js

99 lines
2.4 KiB
JavaScript

var gulp = require('gulp');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var plumber = require('gulp-plumber');
var onError = function (err) {
gutil.log(gutil.colors.green(err));
};
var server = require("./tools/serve.js");
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var sourcemaps = require('gulp-sourcemaps');
// https://github.com/mishoo/UglifyJS2/pull/265
// uglify.AST_Node.warn_function = function() {};
// Lint JS
gulp.task('lint', function() {
return gulp.src('src/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// set up the browserify instance on a task basis
gulp.task('bundle', function () {
return bundle('epub.js');
});
// Minify JS
gulp.task('minify', ['bundle'], function(){
var uglifyOptions = {
mangle: true,
preserveComments : "license"
};
return gulp.src('dist/epub.js')
.pipe(plumber({ errorHandler: onError }))
.pipe(rename('epub.min.js'))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify(uglifyOptions))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'));
});
// Watch Our Files
gulp.task('watch', function(cb) {
// gulp.watch('src/*.js', ['watchify']);
bundle('epub.js', cb);
});
gulp.task('serve', ["watch"], function() {
server();
});
// Default
gulp.task('default', ['lint', 'build']);
// gulp.task('default', function() {
// // place code for your default task here
// });
function bundle(file, watch) {
var opt = {
entries: ['src/'+file],
standalone: 'ePub',
debug : true
};
var b = browserify(opt);
b.require('./src/'+file, {expose: 'epub'});
b.external('jszip');
// watchify() if watch requested, otherwise run browserify() once
var bundler = watch ? watchify(b) : b;
function rebundle() {
var stream = bundler.bundle();
return stream
.on('error', gutil.log)
.pipe(source(file))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'));
}
// listen for an update and run rebundle
bundler.on('update', function() {
rebundle();
gutil.log('Rebundle...');
});
// run it once the first time buildScript is called
return rebundle();
}