2
0
Fork 0
node-ff-assets/example/example.js

143 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-02-27 14:20:01 +00:00
var express = require('express');
var async = require('async');
var path = require('path');
var assets = require('../lib/node-ff-assets');
var assetsConfig = {
// Map from uri paths to file systems paths.
linkMapping : {
'/static/module/bootstrap/': 'node_modules/bootstrap/dist/',
'/static/module/flot/': 'node_modules/flot/',
'/static/': 'www_static/',
},
linkSourcesCss : [
'/static/module/bootstrap/css/bootstrap.css',
],
linkSourcesJs : [
'/static/js/lib/jquery-2.1.3/jquery.js@http://code.jquery.com/jquery-2.1.3.js',
'/static/module/bootstrap/js/bootstrap.js',
'/static/module/flot/jquery.flot.js',
'/static/module/flot/jquery.flot.resize.js',
'/static/module/flot/jquery.flot.pie.js',
'/static/js/lib/angularjs-1.4.0-b4/angular.js@https://code.angularjs.org/1.4.0-beta.4/angular.js',
'/static/js/lib/angularjs-1.4.0-b4/angular-route.js@https://code.angularjs.org/1.4.0-beta.4/angular-route.js',
'/static/js/lib/angularjs-1.4.0-b4/angular-resource.js@https://code.angularjs.org/1.4.0-beta.4/angular-resource.js',
'/static/js/lib/angularjs-1.4.0-b4/angular-touch.js@https://code.angularjs.org/1.4.0-beta.4/angular-touch.js',
],
}
//callback helper to return an assembler config per asset type
function createBuildConfig(type) {
return function(callback) {
var singleResult = 'false' != process.env.DEV_ASSETS_SINGLE_RESULT;
console.info('Asset single page result: '+singleResult+' for: '+type);
if (type == 'css') {
callback(null,{
linkMapping: assetsConfig.linkMapping,
linkTargetSingleResult: singleResult,
linkTarget: '/static/css/lib/assets.css',
linkSources: assetsConfig.linkSourcesCss,
assetType: type,
});
} else {
callback(null, {
downloadStartDelay: 200,
linkTargetSingleResult: singleResult,
linkMapping: assetsConfig.linkMapping,
linkTarget: '/static/js/lib/assets.js',
linkSources: assetsConfig.linkSourcesJs,
assetType: type,
});
}
}
}
function buildAssets(server,callbackDone) {
assets.build({
assets: {
js: {
configCreate: createBuildConfig('js'),
configFill: function (config, callback) {
async.series([
assets.factory.lib.async.pushLinkSources(config, '/static/js/', 'www_static/js/'),
assets.factory.lib.async.pushLinkSources(config, '/static/js/controller/', 'www_static/js/controller/'),
],callback);
},
},
css: {
configCreate: createBuildConfig('css'),
configFill: function (config, callback) {
async.series([
assets.factory.lib.async.pushLinkSources(config, '/static/css/', 'www_static/css/'),
],callback);
},
assemblerCreate: function (assemblerConfig, callback) {
callback(null,new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileMinify()));
},
},
},
assemblerFill: function (assembler, callback) {
var serverResultKey = 'ff_assets_'+assembler.config.assetType;
assembler.on ('log', assets.factory.assembler.event.log.console(assembler.config.assetType));
assembler.on ('result', assets.factory.assembler.event.result.objectSet(server,serverResultKey));
callback();
},
},callbackDone);
}
function renderTemplate(server) {
return function (req, res, next) {
if (req.params.sub) {
res.render('include/' + req.params.page + '/' + req.params.sub);
} else {
res.render('include/' + req.params.page);
}
}
}
function renderPage(server) {
return function (req, res, next) {
res.render('index', {
includeCssFiles: server.get('ff_assets_css'),
includeJsFiles: server.get('ff_assets_js'),
});
}
}
function sendRedirect() {
return function (req, res, next) {
res.redirect('/example-ui');
}
}
var server = express();
buildAssets(server,function(err) {
if (err) {
throw err;
}
console.info('Server assets done.');
server.set('view engine', 'jade');
server.set('views', path.join(__dirname, 'www_views'));
server.use('/static',express.static(path.join(__dirname,'www_static')));
server.use('/static/module/bootstrap',express.static(path.join(__dirname,'node_modules/bootstrap/dist')));
server.use('/static/module/flot',express.static(path.join(__dirname,'node_modules/flot')));
server.get('/example-ui/include/:page', renderTemplate());
server.get('/example-ui/include/:page/:sub', renderTemplate());
server.get('/', sendRedirect());
server.get('/example-ui', renderPage(server));
server.get('/example-ui/*', renderPage(server)); // must be last; for HTML5 history
console.info('Server config done.');
server.listen(8080);
console.info('Server started on port 8080');
});
module.exports = {
version: 'example'
};