1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-11 23:55:06 +01:00
onion/gulpfile.js

207 lines
6.1 KiB
JavaScript
Raw Normal View History

'use strict';
2015-06-08 09:53:14 +02:00
require("harmonize")();
var gulp = require('gulp');
2015-06-09 17:53:44 +02:00
var template = require('gulp-template');
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');
var opn = require('opn');
2015-05-29 13:38:59 +02:00
2015-06-09 17:53:44 +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'
]
},
filesToWatch: [
'build/css/*.css',
'build/js/*.js'
]
2015-05-29 13:38:59 +02:00
};
2015-08-13 16:50:28 +02:00
var SERVER_URL = process.env.ONION_SERVER_URL || 'https://staging.ascribe.io/';
2015-08-13 17:06:27 +02:00
2015-06-10 17:28:36 +02:00
var constants = {
BASE_URL: (function () { var baseUrl = process.env.ONION_BASE_URL || '/'; return baseUrl + (baseUrl.match(/\/$/) ? '' : '/'); })(),
2015-06-24 18:02:32 +02:00
SERVER_URL: SERVER_URL,
2015-08-13 16:50:28 +02:00
API_ENDPOINT: SERVER_URL + 'api/' || 'https://staging.ascribe.io/api/',
2015-06-10 17:28:36 +02:00
DEBUG: !argv.production,
CREDENTIALS: 'ZGltaUBtYWlsaW5hdG9yLmNvbTowMDAwMDAwMDAw' // dimi@mailinator:0000000000
};
2015-06-09 17:53:44 +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
gulp.task('serve', ['browser-sync', 'run-server', 'sass:build', 'sass:watch', 'copy'], function() {
bundle(true);
// opens the browser window with the correct url, which is localhost.com
opn('http://www.localhost.com:3000');
});
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({
files: config.filesToWatch,
2015-06-08 15:24:58 +02:00
proxy: 'http://localhost:4000',
port: 3000,
open: false, // does not open the browser-window anymore (handled manually)
ghostMode: false
});
});
2015-06-08 15:42:28 +02:00
gulp.task('sass:build', function () {
gulp.src('./sass/**/main.scss')
2015-06-10 17:28:36 +02:00
.pipe(template(constants))
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')))
// We need to set `advanced` to false, as it merges
// some of the styles wrongly
.pipe(gulpif(argv.production, minifyCss({
advanced: false
})))
.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-06-12 13:36:55 +02:00
gulp.watch('./sass/**/*.scss', ['sass:build']);
});
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-06-09 17:53:44 +02:00
gulp.src('./index.html')
2015-06-10 17:28:36 +02:00
.pipe(template(constants))
2015-06-09 17:53:44 +02:00
.pipe(gulp.dest('./build'));
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'))
.on('error', notify.onError('Error: <%= error.message %>'))
.pipe(buffer())
.on('error', notify.onError('Error: <%= error.message %>'))
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
.on('error', notify.onError('Error: <%= error.message %>'))
2015-06-08 15:42:28 +02:00
.pipe(gulpif(!argv.production, sourcemaps.write())) // writes .map file
.on('error', notify.onError('Error: <%= error.message %>'))
2015-07-20 11:26:06 +02:00
.pipe(gulpif(argv.production, uglify({
2015-09-04 18:04:03 +02:00
mangle: true
2015-07-20 11:26:06 +02:00
})))
2015-07-14 10:52:44 +02:00
.on('error', notify.onError('Error: <%= error.message %>'))
2015-06-08 15:24:58 +02:00
.pipe(gulp.dest('./build/js'))
.on('error', notify.onError('Error: <%= error.message %>'))
.pipe(browserSync.stream())
.on('error', notify.onError('Error: <%= error.message %>'));
}
return rebundle(bro);
2015-06-08 15:24:58 +02:00
}