Skip to content

Commit

Permalink
- Recompilation is much faster! Integrate watchify modules for reco…
Browse files Browse the repository at this point in the history
…mpile.

- Improvements and bug fixing.
  • Loading branch information
vash15 authored and Michele Belluco committed Aug 4, 2016
1 parent ae042f0 commit d8f63ff
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 145 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ Identify the language of the source code. Possible values: `js`, `es6` (default

Check the version of eve required by the project.

### eve-watchify

The configuration for watchify. For more information see [watchify](https://github.com/substack/watchify).
Defaults:

```
{
delay: 500,
ignoreWatch: [ 'bundle.js', 'styles.css', 'package.json', '**/styles/**', '**/node_modules/**' ],
poll: false
}
```

### eve-configs-developement

Punt into object the variables for developement mode.
Expand All @@ -123,7 +136,6 @@ Punt into object the variables for production mode.

The object is generated based on the compilation mode (`development` or `production`). Into your app require this object for read the variables.


## Tips & Tricks

### File limit
Expand Down
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

# Release notes

## 2.2.0

- Recompilation is much faster! Integrate `watchify` modules for recompile.
- Improvements and bug fixing.

## 2.1.0

- Added support to ES6 for bower components
Expand Down
104 changes: 70 additions & 34 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

var _ = require('underscore');
var colors = require('colors');
var exorcist = require('exorcist')
var browserify = require('browserify');
var watchify = require('watchify');
var fs = require('fs');
var path = require('path');
var UglifyJS = require('uglify-js');
Expand All @@ -18,14 +20,13 @@ if ( fs.existsSync( bowerrcFile ) ){
}catch(e){}
}

module.exports = function(type, unit, options, done) {
module.exports = function(type, unit, watcher, options, done) {

if ( _.isFunction(options) ){
done = options;
options = {};
}

var time = process.hrtime();
var mode = options.mode || 'mobile';
var packageJSONFile = path.resolve('package.json')
var pkg = require(packageJSONFile);
Expand All @@ -34,14 +35,21 @@ module.exports = function(type, unit, options, done) {
pkg['eve-configs'] = {};

type = type || 'dev';
var namePkg = pkg['name'];
var configs = pkg['eve-configs'];
var language = pkg['eve-language'] || 'js';
var isForPublish = false;
var entryJsFile = path.join('lib', 'app.js');
var outputJsFile = path.join('build', 'assets', 'js', 'bundle.js');
var now = new Date();
var buildDate = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
var namePkg = pkg['name'];
var configs = pkg['eve-configs'];
var language = pkg['eve-language'] || 'js';
var watchifyConfig = pkg['eve-watchify'] || {};
var isForPublish = false;
var entryJsFile = path.join('lib', 'app.js');
var outputJsFile = path.join('build', 'assets', 'js', 'bundle.js');
var now = new Date();
var buildDate = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();

_.defaults(watchifyConfig, {
delay: 500,
ignoreWatch: [ 'bundle.js', 'styles.css', 'package.json', '**/styles/**', '**/node_modules/**' ],
poll: false
});

var mapFile;
var debug;
Expand Down Expand Up @@ -81,7 +89,16 @@ module.exports = function(type, unit, options, done) {

mapFile = outputJsFile + '.map';

var b = browserify({ debug: debug });
var b = browserify({
debug: debug,
cache: {},
packageCache: {}
});

if ( watcher )
b.plugin( watchify, watchifyConfig );


// If language is ECMAScript 2015 package adding lib directory to compile width babel
if (language === 'es6') {
babelifyDir.push('lib');
Expand Down Expand Up @@ -110,34 +127,53 @@ module.exports = function(type, unit, options, done) {

b.require(path.resolve(entryJsFile), { entry: true });

b.bundle(function (err, buf) {
if (err) return done(err);
done(null, beautifyTime(process.hrtime(time)), type);
})
.on("end", function() {
if ( !debug ) {
setTimeout(function() {
var src = fs.readFileSync( outputJsFile, { encoding: 'utf8'} );
var srcMinify = UglifyJS.minify( src, { fromString: true });

if ( srcMinify ) {
fs.writeFileSync(outputJsFile, srcMinify.code);

if ( isForPublish )
publisher(outputJsFile, mode, done);
// b.on('log', function (msg) {
// console.log(msg.gray);
// });

} else {
done(new Error('Minify error'));
}
b.on('update', bundle);
bundle();

if ( fs.existsSync(mapFile) )
fs.unlinkSync( mapFile );
function bundle(files) {

}, 2000);
if ( _.isArray(files) ){
files.forEach(function (file) {
console.log(" file change: %s".gray, path.relative( process.cwd(), file ) );
});
}
})
.pipe(exorcist(mapFile))
.pipe(fs.createWriteStream(outputJsFile, 'utf8'));

var time = process.hrtime();
b.bundle(function (err, buf) {
if (err) return done(err);
done(null, beautifyTime( process.hrtime(time) ), type);
})
.on("end", function() {
if ( !debug ) {
setTimeout(function() {
var src = fs.readFileSync( outputJsFile, { encoding: 'utf8'} );
var srcMinify = UglifyJS.minify( src, { fromString: true });

if ( srcMinify ) {
fs.writeFileSync(outputJsFile, srcMinify.code);

if ( isForPublish )
publisher(outputJsFile, mode, done);

} else {
done(new Error('Minify error'));
}

if ( fs.existsSync(mapFile) )
fs.unlinkSync( mapFile );

}, 2000);
}
})
.pipe(exorcist(mapFile))
.pipe(fs.createWriteStream(outputJsFile, 'utf8'));

};


function beautifyTime(diff) {
Expand Down
73 changes: 28 additions & 45 deletions lib/eve.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ var logo = '' +
console.log(logo.cyan);
console.log('In case of EMFILE error use this command: ulimit -n 2048'.gray);

process.env.building = null;

program
.command('init [name]')
.description('Crea il template di una app')
.description('Create an app from template.')
.action(function (name, options) {
var init = require('./init');
init(name, function (err) {
Expand All @@ -41,23 +43,22 @@ program
var build = require('./build');
var scss = require('./scss');

build(type, unit, options, function (err, time, type) {
build(type, unit, false, options, function (err, time, type) {
if (err) return handleError(err, 'JS');
console.log('JS builded with type '.gray + type.cyan + '...'.gray + time.green + '\u0007');
});
scss(type, unit, function (err, time) {
scss(type, unit, false, function (err, time) {
if (err) return handleError(err, 'SCSS');
console.log('SCSS builded...'.gray + time.green + '\u0007');
});
});

program
.command('start')
.description('Esegue l\'applicazione')
.description('Start a web server and watchify for developer\'s directory for build the application.')
.option('-p --port <port>', 'Port')
.option('-a --app [platform]', 'App platform')
.action(function (options) {
var watch = require('./watch');
var build = require('./build');
var scss = require('./scss');
var express = require('./express');
Expand All @@ -66,42 +67,24 @@ program
console.log('Server started at port '.grey + (port + '').cyan);
});

watch(function (isJs, isCss) {
var loading;
if (isJs) {
build('dev', null, function (err, time, type) {
if (err) return handleError(err, 'JS');
prepareApp(function(err, platform) {
clearInterval(loading);
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (err) return handleError(err, 'CORDOVA');
console.log('JS builded...'.gray + time.green + '\u0007');
if (platform)
console.log(' Cordova prepared...'.gray + platform.green);
});
});
}
if (isCss) {
scss('dev', null, function (err, time) {
if (err) return handleError(err, 'SCSS');
prepareApp(function(err, platform) {
clearInterval(loading);
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (err) return handleError(err, 'CORDOVA');
console.log('SCSS builded...'.gray + time.green + '\u0007');
if (platform)
console.log(' Cordova prepared...'.gray + platform.green);
});
});
}
if (isJs || isCss) {
process.stdout.write('Building.');
loading = setInterval(function() {
process.stdout.write('.');
}, 1000);
}
build('dev', null, true, function (err, time, type) {
if (err) return handleError(err, 'JS');
prepareApp(function(err, platform) {
if (err) return handleError(err, 'CORDOVA');
console.log('JS builded...'.gray + time.green + '\u0007');
if (platform)
console.log(' Cordova prepared...'.gray + platform.green);
});
});

scss('dev', null, true, function (err, time) {
if (err) return handleError(err, 'SCSS');
prepareApp(function(err, platform) {
if (err) return handleError(err, 'CORDOVA');
console.log('SCSS builded...'.gray + time.green + '\u0007');
if (platform)
console.log(' Cordova prepared...'.gray + platform.green);
});
});

function prepareApp(done) {
Expand All @@ -120,7 +103,7 @@ program

program
.command('test <unit>')
.description('Testa l\'applicazione')
.description('Testing mode')
.option('-p --port <port>', 'Port', 5001)
.action(function (unit, options) {
var watch = require('./watch');
Expand All @@ -136,7 +119,7 @@ program
watch(function (isJs, isCss) {
var loading;
if (isJs) {
build('test', unit, function (err, time, type) {
build('test', unit, true, function (err, time, type) {
clearInterval(loading);
process.stdout.clearLine();
process.stdout.cursorTo(0);
Expand All @@ -145,7 +128,7 @@ program
});
}
if (isCss) {
scss('test', unit, function (err, time) {
scss('test', unit, true, function (err, time) {
clearInterval(loading);
process.stdout.clearLine();
process.stdout.cursorTo(0);
Expand All @@ -164,7 +147,7 @@ program

program
.command('extract')
.description('Estrae le stringhe dall\'applicazione e le restituisce in un file per la traduzione')
.description('Extract strings from the application and returns them in a file for translation')
.option('-d, --dir <directory, directory, ...>', 'Directories', split)
.option('-o, --out <file>', 'Output file')
.action(function (options) {
Expand Down
Loading

0 comments on commit d8f63ff

Please sign in to comment.