Commit 5794fc02 authored by Philipp Nowinski's avatar Philipp Nowinski
Browse files

[FEATURE][WIP] restructure js compilation to allow multiple compilers

parent 10d4c80f
......@@ -4,65 +4,33 @@ var gulp = require('gulp'),
settings = require('./settings'),
config = settings.getConfig(),
path = require('path'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
argv = require('yargs').argv,
rename = require('gulp-rename'),
babelify = require('babelify'),
glob = require('glob'),
eventStream = require('event-stream'),
compiler,
plugins = require('gulp-load-plugins')({
pattern: ['gulp-*', 'gulp.*', 'browserify', 'browser-sync']
});
function processJs(files, done) {
var tasks = files.map(function(file) {
var browserify = plugins.browserify({
entries: [file],
paths: config.js.libraryPaths,
debug: true
});
if (argv.production) {
return browserify.transform(babelify.configure({
presets: [require('babel-preset-es2015')]
}))
.bundle()
.pipe(source(file))
.pipe(buffer())
.pipe(plugins.uglify())
.on('error', console.log)
.pipe(rename({
extname: '.bundled.min.js',
dirname: ''
}))
.pipe(plugins.checkFilesize({
enableGzip: true
}))
.pipe(gulp.dest(settings.getPath() + '/' + config.directories.javascriptDest));
} else {
return browserify.transform(babelify.configure({
presets: [require('babel-preset-es2015')]
}))
.bundle()
.pipe(source(file))
.pipe(buffer())
.pipe(plugins.sourcemaps.init({loadMaps: true}))
.pipe(plugins.uglify())
.on('error', console.log)
.pipe(rename({
extname: '.bundled.min.js',
dirname: ''
}))
.pipe(plugins.checkFilesize({
enableGzip: true
}))
.pipe(plugins.sourcemaps.write('../SourceMaps'))
.pipe(gulp.dest(settings.getPath() + '/' + config.directories.javascriptDest));
}
});
return eventStream.merge(tasks).on('end', done);
function setUpCompiler() {
if (!config.js.compiler) {
plugins.util.log(plugins.util.colors.red('No compiler specified!'));
plugins.util.log(plugins.util.colors.red('You need to specify which JavaScript compiler you want to use inside your .sgc-config file.'));
done();
return;
}
switch (config.js.compiler) {
case 'es6':
compiler = new require('./js-compilers/es6')();
break;
case 'typescript':
compiler = new require('./js-compilers/typescript')();
break;
default:
plugins.util.log(plugins.util.colors.red('The specified JavaScript compiler "' + config.js.compiler + '" could not be found'));
done();
return;
}
}
gulp.task('js:qa', function() {
......@@ -72,14 +40,16 @@ gulp.task('js:qa', function() {
});
gulp.task('js:compile', function(done) {
glob(settings.getPath() + '/' + config.directories.javascriptSrc + '/*.js', function(err, files) {
return processJs(files, done);
setUpCompiler(done);
glob(`${settings.getPath()}/${config.directories.javascriptSrc}/*${compiler.getFileExtension()}`, function(err, files) {
return compiler.compile(files, done, argv.production);
});
});
gulp.task('js:compileAndSync', function(done) {
glob(settings.getPath() + '/' + config.directories.javascriptSrc + '/*.js', function(err, files) {
return processJs(files, done)
setUpCompiler(done);
glob(`${settings.getPath()}/${config.directories.javascriptSrc}/*${compiler.getFileExtension()}`, function(err, files) {
return compiler.compile(files, done, argv.production)
.pipe(plugins.browserSync.stream());
});
});
......
module.exports = function() {
'use strict';
var es6 = {},
gulp = require('gulp'),
settings = require('../settings'),
config = settings.getConfig(),
eventStream = require('event-stream'),
babelify = require('babelify'),
rename = require('gulp-rename'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
plugins = require('gulp-load-plugins')({
pattern: ['gulp-*', 'gulp.*', 'browserify', 'browser-sync']
});
es6.compile = function(files, done, _productionMode) {
var tasks = files.map(function(file) {
var browserify = plugins.browserify({
entries: [file],
paths: config.js.libraryPaths,
debug: true
});
if (_productionMode) {
return browserify.transform(babelify.configure({
presets: [require('babel-preset-es2015')]
}))
.bundle()
.pipe(source(file))
.pipe(buffer())
.pipe(plugins.uglify())
.on('error', console.log)
.pipe(rename({
extname: '.bundled.min.js',
dirname: ''
}))
.pipe(plugins.checkFilesize({
enableGzip: true
}))
.pipe(gulp.dest(settings.getPath() + '/' + config.directories.javascriptDest));
} else {
return browserify.transform(babelify.configure({
presets: [require('babel-preset-es2015')]
}))
.bundle()
.pipe(source(file))
.pipe(buffer())
.pipe(plugins.sourcemaps.init({loadMaps: true}))
.pipe(plugins.uglify())
.on('error', console.log)
.pipe(rename({
extname: '.bundled.min.js',
dirname: ''
}))
.pipe(plugins.checkFilesize({
enableGzip: true
}))
.pipe(plugins.sourcemaps.write('../SourceMaps'))
.pipe(gulp.dest(settings.getPath() + '/' + config.directories.javascriptDest));
}
});
return eventStream.merge(tasks).on('end', done);
};
es6.getFileExtension = function() {
return '.js';
};
return es6;
};
module.exports = function() {
'use strict';
var typescript = {},
gulp = require('gulp'),
plugins = require('gulp-load-plugins')({
pattern: ['gulp-*', 'gulp.*', 'browser-sync']
});
typescript.compile = function(files, done, _productionMode) {
};
typescript.getFileExtension = function() {
return '.ts';
};
return typescript;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment