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

143 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-02-27 20:50:19 +00:00
'use strict';
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',
],
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
//callback helper to return an assembler config per asset type
function createBuildConfig(type) {
return function(callback) {
2015-02-27 20:50:19 +00:00
var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT;
2015-02-27 14:20:01 +00:00
console.info('Asset single page result: '+singleResult+' for: '+type);
2015-02-27 20:50:19 +00:00
if (type === 'css') {
2015-02-27 14:20:01 +00:00
callback(null,{
linkMapping: assetsConfig.linkMapping,
linkTargetSingleResult: singleResult,
linkTarget: '/static/css/lib/assets.css',
linkSources: assetsConfig.linkSourcesCss,
assetType: type,
});
} else {
callback(null, {
linkTargetSingleResult: singleResult,
linkMapping: assetsConfig.linkMapping,
linkTarget: '/static/js/lib/assets.js',
linkSources: assetsConfig.linkSourcesJs,
assetType: type,
});
}
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
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);
}
2015-02-27 20:50:19 +00:00
function renderTemplate() {
return function (req, res) {
2015-02-27 14:20:01 +00:00
if (req.params.sub) {
res.render('include/' + req.params.page + '/' + req.params.sub);
} else {
res.render('include/' + req.params.page);
}
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
function renderPage(server) {
2015-02-27 20:50:19 +00:00
return function (req, res) {
2015-02-27 14:20:01 +00:00
res.render('index', {
includeCssFiles: server.get('ff_assets_css'),
includeJsFiles: server.get('ff_assets_js'),
});
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
function sendRedirect() {
2015-02-27 20:50:19 +00:00
return function (req, res) {
2015-02-27 14:20:01 +00:00
res.redirect('/example-ui');
2015-02-27 20:50:19 +00:00
};
2015-02-27 14:20:01 +00:00
}
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'
};