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'), ...@@ -4,65 +4,33 @@ var gulp = require('gulp'),
settings = require('./settings'), settings = require('./settings'),
config = settings.getConfig(), config = settings.getConfig(),
path = require('path'), path = require('path'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
argv = require('yargs').argv, argv = require('yargs').argv,
rename = require('gulp-rename'),
babelify = require('babelify'),
glob = require('glob'), glob = require('glob'),
eventStream = require('event-stream'), compiler,
plugins = require('gulp-load-plugins')({ plugins = require('gulp-load-plugins')({
pattern: ['gulp-*', 'gulp.*', 'browserify', 'browser-sync'] pattern: ['gulp-*', 'gulp.*', 'browserify', 'browser-sync']
}); });
function processJs(files, done) { function setUpCompiler() {
if (!config.js.compiler) {
var tasks = files.map(function(file) { plugins.util.log(plugins.util.colors.red('No compiler specified!'));
var browserify = plugins.browserify({ plugins.util.log(plugins.util.colors.red('You need to specify which JavaScript compiler you want to use inside your .sgc-config file.'));
entries: [file], done();
paths: config.js.libraryPaths, return;
debug: true }
});
switch (config.js.compiler) {
if (argv.production) { case 'es6':
return browserify.transform(babelify.configure({ compiler = new require('./js-compilers/es6')();
presets: [require('babel-preset-es2015')] break;
})) case 'typescript':
.bundle() compiler = new require('./js-compilers/typescript')();
.pipe(source(file)) break;
.pipe(buffer()) default:
.pipe(plugins.uglify()) plugins.util.log(plugins.util.colors.red('The specified JavaScript compiler "' + config.js.compiler + '" could not be found'));
.on('error', console.log) done();
.pipe(rename({ return;
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);
} }
gulp.task('js:qa', function() { gulp.task('js:qa', function() {
...@@ -72,14 +40,16 @@ gulp.task('js:qa', function() { ...@@ -72,14 +40,16 @@ gulp.task('js:qa', function() {
}); });
gulp.task('js:compile', function(done) { gulp.task('js:compile', function(done) {
glob(settings.getPath() + '/' + config.directories.javascriptSrc + '/*.js', function(err, files) { setUpCompiler(done);
return processJs(files, 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) { gulp.task('js:compileAndSync', function(done) {
glob(settings.getPath() + '/' + config.directories.javascriptSrc + '/*.js', function(err, files) { setUpCompiler(done);
return processJs(files, done) glob(`${settings.getPath()}/${config.directories.javascriptSrc}/*${compiler.getFileExtension()}`, function(err, files) {
return compiler.compile(files, done, argv.production)
.pipe(plugins.browserSync.stream()); .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