mitmproxy/web/gulpfile.js

225 lines
6.2 KiB
JavaScript
Raw Normal View History

var path = require('path');
2014-09-13 15:00:05 +00:00
2015-03-05 00:52:47 +00:00
var packagejs = require('./package.json');
var conf = require('./conf.js');
// Sorted alphabetically!
2015-10-08 10:43:55 +00:00
var babelify = require('babelify');
var browserify = require('browserify');
2015-03-05 00:52:47 +00:00
var gulp = require("gulp");
2014-09-13 15:00:05 +00:00
var concat = require('gulp-concat');
var connect = require('gulp-connect');
2015-10-08 10:43:55 +00:00
var eslint = require('gulp-eslint');
2014-09-13 15:00:05 +00:00
var less = require("gulp-less");
var livereload = require("gulp-livereload");
var minifyCSS = require('gulp-minify-css');
2014-09-13 23:21:13 +00:00
var notify = require("gulp-notify");
2015-03-05 00:52:47 +00:00
var peg = require("gulp-peg");
2014-09-13 21:05:12 +00:00
var plumber = require("gulp-plumber");
2014-09-13 15:00:05 +00:00
var react = require("gulp-react");
var rename = require("gulp-rename");
var replace = require('gulp-replace');
2014-09-13 15:00:05 +00:00
var sourcemaps = require('gulp-sourcemaps');
2015-10-08 10:43:55 +00:00
var gutil = require("gulp-util");
2015-03-05 00:52:47 +00:00
var _ = require('lodash');
var map = require("map-stream");
var reactify = require('reactify');
2015-10-08 10:43:55 +00:00
var uglifyify = require('uglifyify');
2015-03-05 00:52:47 +00:00
var buffer = require('vinyl-buffer');
var source = require('vinyl-source-stream');
var transform = require('vinyl-transform');
2015-10-08 10:43:55 +00:00
var watchify = require('watchify');
2014-09-13 23:21:13 +00:00
var vendor_packages = _.difference(
_.union(
_.keys(packagejs.dependencies),
conf.js.vendor_includes
),
conf.js.vendor_excludes
);
2015-10-08 10:43:55 +00:00
var handleError = {errorHandler: notify.onError("Error: <%= error.message %>")};
2015-03-11 12:11:41 +00:00
/*
* Sourcemaps are a wonderful way to develop directly from the chrome devtools.
* However, generating correct sourcemaps is a huge PITA, especially on Windows.
* Fixing this upstream is tedious as apparently nobody really cares and
* a single misbehaving transform breaks everything.
* Thus, we just manually fix all paths.
*/
2015-10-08 10:43:55 +00:00
function fixSourceMaps(file) {
2015-03-11 12:11:41 +00:00
file.sourceMap.sources = file.sourceMap.sources.map(function (x) {
2015-10-08 10:43:55 +00:00
return path.relative(".", x).split(path.sep).join('/');
2015-03-11 12:11:41 +00:00
});
return "/";
}
// Browserify fails for paths starting with "..".
2015-10-08 10:43:55 +00:00
function fixBrowserifySourceMaps(file) {
file.sourceMap.sources = file.sourceMap.sources.map((x) => {
return x.replace("src/js/node_modules", "node_modules");
2015-03-11 12:11:41 +00:00
});
2015-10-08 10:43:55 +00:00
return fixSourceMaps(file);
}
function fixLessSourceMaps(file) {
file.sourceMap.sources = file.sourceMap.sources.map((x) => {
if(!x.startsWith("..")){
return "../src/css/" + x;
}
return x.replace("src/js/node_modules", "node_modules");
});
return fixSourceMaps(file);
2015-03-11 12:11:41 +00:00
}
2015-10-08 10:43:55 +00:00
function styles(files, dev){
return gulp.src(files)
.pipe(dev ? plumber(handleError) : gutil.noop())
.pipe(sourcemaps.init())
.pipe(less())
2015-10-08 10:43:55 +00:00
.pipe(dev ? gutil.noop() : minifyCSS())
.pipe(sourcemaps.write(".", {sourceRoot: fixLessSourceMaps}))
.pipe(gulp.dest(conf.static))
2015-10-08 10:43:55 +00:00
.pipe(livereload({auto: false}));
}
2015-10-08 10:43:55 +00:00
gulp.task("styles-app-dev", function () {
styles(conf.css.app, true);
});
2015-10-08 10:43:55 +00:00
gulp.task("styles-vendor-dev", function () {
styles(conf.css.vendor, true);
});
2015-10-08 10:43:55 +00:00
gulp.task("styles-app-prod", function () {
styles(conf.css.app, false);
});
2015-10-08 10:43:55 +00:00
gulp.task("styles-vendor-prod", function () {
styles(conf.css.app, false);
});
2014-09-13 15:00:05 +00:00
2015-10-08 10:43:55 +00:00
function buildScript(bundler, filename, dev) {
if (dev) {
bundler = watchify(bundler);
} else {
bundler = bundler.transform({global: true}, uglifyify);
}
function rebundle() {
return bundler.bundle()
.pipe(dev ? plumber(handleError) : gutil.noop())
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(rename(filename))
.pipe(sourcemaps.write('.', {sourceRoot: fixBrowserifySourceMaps}))
.pipe(gulp.dest(conf.static))
.pipe(livereload({auto: false}));
}
// listen for an update and run rebundle
bundler.on('update', rebundle);
bundler.on('log', gutil.log);
// run it once the first time buildScript is called
return rebundle();
}
function vendor_stream(dev) {
var bundler = browserify({
entries: [],
debug: true,
cache: {}, // required for watchify
packageCache: {} // required for watchify
});
2015-10-08 10:43:55 +00:00
for (var vp of vendor_packages) {
bundler.require(vp);
}
return buildScript(bundler, "vendor.js", dev);
2014-09-13 15:00:05 +00:00
}
2015-10-08 10:43:55 +00:00
gulp.task("scripts-vendor-dev", function () {
return vendor_stream(true);
});
2015-10-08 10:43:55 +00:00
gulp.task("scripts-vendor-prod", function () {
return vendor_stream(false);
});
2015-10-08 10:43:55 +00:00
function app_stream(dev) {
var bundler = browserify({
entries: [conf.js.app],
debug: true,
cache: {}, // required for watchify
packageCache: {} // required for watchify
});
2015-10-08 10:43:55 +00:00
for (var vp of vendor_packages) {
bundler.external(vp);
}
bundler = bundler.transform(babelify).transform(reactify);
return buildScript(bundler, "app.js", dev);
}
gulp.task('scripts-app-dev', function () {
2015-10-08 10:43:55 +00:00
return app_stream(true);
});
gulp.task('scripts-app-prod', function () {
2015-10-08 10:43:55 +00:00
return app_stream(false);
});
2014-09-13 15:00:05 +00:00
2015-10-08 10:43:55 +00:00
gulp.task("eslint", function () {
return gulp.src(conf.js.eslint)
.pipe(plumber(handleError))
.pipe(eslint())
.pipe(eslint.format())
2014-12-12 16:49:10 +00:00
});
2015-10-08 10:43:55 +00:00
gulp.task("copy", function () {
2015-03-11 12:11:41 +00:00
return gulp.src(conf.copy, {base: conf.src})
2015-03-05 00:52:47 +00:00
.pipe(gulp.dest(conf.static));
});
2015-10-08 10:43:55 +00:00
gulp.task('templates', function(){
2015-03-11 12:11:41 +00:00
return gulp.src(conf.templates, {base: conf.src})
.pipe(gulp.dest(conf.dist));
2015-10-08 10:43:55 +00:00
});
2014-09-13 15:00:05 +00:00
gulp.task("peg", function () {
2015-03-11 12:11:41 +00:00
return gulp.src(conf.peg, {base: conf.src})
2015-10-08 10:43:55 +00:00
.pipe(plumber(handleError))
.pipe(peg())
.pipe(gulp.dest("src/"));
});
gulp.task(
"dev",
[
"copy",
"styles-vendor-dev",
"styles-app-dev",
"scripts-vendor-dev",
"peg",
"scripts-app-dev",
2015-10-08 10:43:55 +00:00
"templates"
]
);
gulp.task(
"prod",
[
"copy",
"styles-vendor-prod",
"styles-app-prod",
"scripts-vendor-prod",
"peg",
"scripts-app-prod",
2015-10-08 10:43:55 +00:00
"templates"
]
);
2015-10-08 10:43:55 +00:00
gulp.task("default", ["dev"], function () {
2014-09-13 15:00:05 +00:00
livereload.listen({auto: true});
gulp.watch(["src/css/vendor*"], ["styles-vendor-dev"]);
gulp.watch(["src/css/**"], ["styles-app-dev"]);
2015-10-08 10:43:55 +00:00
gulp.watch(conf.templates, ["templates"]);
2015-10-08 10:43:55 +00:00
gulp.watch(conf.peg, ["peg"]);
gulp.watch(["src/js/**"], ["eslint"]);
// other JS is handled by watchify.
gulp.watch(conf.copy, ["copy"]);
2014-09-15 16:08:26 +00:00
});