1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/gulpfile.js

167 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
2015-06-08 09:53:14 +02:00
require("harmonize")();
var gulp = require('gulp');
var gulpif = require('gulp-if');
var sourcemaps = require('gulp-sourcemaps');
var util = require('gulp-util');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var watchify = require('watchify');
var browserify = require('browserify');
var browserSync = require('browser-sync');
var babelify = require('babelify');
var notify = require('gulp-notify');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var _ = require('lodash');
2015-06-05 09:17:41 +02:00
var eslint = require('gulp-eslint');
2015-06-08 09:53:14 +02:00
var jest = require('jest-cli');
2015-06-08 15:24:58 +02:00
var argv = require('yargs').argv;
var server = require('./server.js').app;
2015-06-08 15:42:28 +02:00
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
2015-06-08 15:24:58 +02:00
2015-05-29 13:38:59 +02:00
var config = {
2015-06-08 09:53:14 +02:00
bootstrapDir: './node_modules/bootstrap-sass',
jestOptions: {
rootDir: 'js',
scriptPreprocessor: '../node_modules/babel-jest',
testFileExtensions: [
'es6',
'js'
],
unmockedModulePathPatterns: [
'<rootDir>/node_modules/react',
'<rootDir>/node_modules/react-tools'
],
moduleFileExtensions: [
'js',
'json',
'react',
'es6'
]
}
2015-05-29 13:38:59 +02:00
};
2015-06-08 15:42:28 +02:00
gulp.task('build', ['js:build', 'sass:build', 'copy'], function() {
});
gulp.task('js:build', function() {
bundle(false);
});
2015-06-08 15:24:58 +02:00
2015-06-08 15:42:28 +02:00
gulp.task('serve', ['browser-sync', 'run-server', 'lint:watch', 'sass:build', 'sass:watch', 'copy'], function() {
bundle(true);
});
2015-06-08 09:53:14 +02:00
gulp.task('jest', function(done) {
jest.runCLI({ config : config.jestOptions }, ".", function() {
done();
});
});
gulp.task('jest:watch', function(done) {
gulp.watch([ config.jestOptions.rootDir + "/**/*.js" ], [ 'jest' ]);
});
2015-06-08 15:24:58 +02:00
gulp.task('run-server', function() {
server.listen(4000);
});
gulp.task('browser-sync', function() {
browserSync({
2015-06-08 15:24:58 +02:00
files: ['build/css/*.css', 'build/js/*.js'],
proxy: 'http://localhost:4000',
port: 3000
});
});
2015-06-08 15:42:28 +02:00
gulp.task('sass:build', function () {
gulp.src('./sass/**/main.scss')
2015-06-08 15:42:28 +02:00
.pipe(gulpif(!argv.production, sourcemaps.init()))
2015-05-29 13:38:59 +02:00
.pipe(sass({
includePaths: [
config.bootstrapDir + '/assets/stylesheets'
]
}).on('error', sass.logError))
2015-06-08 15:42:28 +02:00
.pipe(gulpif(!argv.production, sourcemaps.write('./maps')))
.pipe(gulpif(argv.production, minifyCss()))
.pipe(gulp.dest('./build/css'))
2015-06-08 15:24:58 +02:00
.pipe(browserSync.stream());
2015-05-29 12:08:26 +02:00
});
gulp.task('sass:watch', function () {
2015-05-29 13:38:59 +02:00
gulp.watch('./sass/**/*.scss', ['sass']);
});
2015-05-29 11:57:24 +02:00
gulp.task('copy', function () {
2015-06-08 15:24:58 +02:00
var staticAssets = [
2015-05-29 11:57:24 +02:00
'./fonts/**/*',
2015-05-29 13:38:59 +02:00
'./img/**/*'
2015-05-29 11:57:24 +02:00
];
2015-06-08 15:24:58 +02:00
gulp.src(staticAssets, {base: './'})
.pipe(gulp.dest('./build'));
2015-05-29 13:38:59 +02:00
gulp.src(config.bootstrapDir + '/assets/fonts/**/*')
.pipe(gulp.dest('./build/fonts'));
2015-05-29 11:57:24 +02:00
});
2015-06-05 09:17:41 +02:00
gulp.task('lint', function () {
return gulp.src(['js/**/*.js'])
// eslint() attaches the lint output to the eslint property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
2015-06-08 09:53:14 +02:00
.pipe(eslint.format());
2015-06-05 09:17:41 +02:00
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failOnError last.
});
gulp.task('lint:watch', function () {
gulp.watch('js/**/*.js', ['lint']);
});
function bundle(watch) {
var bro;
2015-06-08 15:24:58 +02:00
if (watch) {
bro = watchify(browserify('./js/app.js',
// Assigning debug to have sourcemaps
_.assign(watchify.args, {
debug: true
})));
bro.on('update', function() {
rebundle(bro, true);
});
} else {
bro = browserify('./js/app.js', {
debug: true
});
}
bro.transform(babelify.configure({
compact: false
}));
function rebundle(bundler, watch) {
return bundler.bundle()
.on('error', notify.onError('Error: <%= error.message %>'))
.pipe(source('app.js'))
.pipe(buffer())
2015-06-08 15:42:28 +02:00
.pipe(gulpif(!argv.production, sourcemaps.init({
loadMaps: true
2015-06-08 15:42:28 +02:00
}))) // loads map from browserify file
.pipe(gulpif(!argv.production, sourcemaps.write())) // writes .map file
2015-06-08 17:20:08 +02:00
.pipe(gulpif(argv.production, uglify({mangle: false})))
2015-06-08 15:24:58 +02:00
.pipe(gulp.dest('./build/js'))
.pipe(browserSync.stream());
}
return rebundle(bro);
2015-06-08 15:24:58 +02:00
}