2
0
Fork 0
tcrud/lib/plugin/server/config/config-resources-web.js
2017-06-10 16:53:29 +02:00

145 lines
4 KiB
JavaScript

var debug = require('debug')('ff:tcrud:plugin:server:config:resouces:web');
var configRegistry = require('./../../../config-registry');
var configUtil = require('./../../../config-util');
var fetch = require('fetch');
// FIXME: use type as key
var assetCss = '';
var assetCssUrl = '';
var assetCssHash = '';
var assetCssData = '';
var assetCssDataUrl = '';
var assetCssDataHash = '';
var assetJs = '';
var assetJsUrl = '';
var assetJsHash = '';
var plugin = (function () {
var webAssets = [];
var fetchResource = function(fetchEntry,cb) {
fetch.fetchUrl(fetchEntry.url,function(err, meta, data) {
if (err) {
throw err;
}
debug('fetched url: '+fetchEntry.url+' length: '+meta.responseHeaders['content-length']);
//debug('meta: '+JSON.stringify(meta));
if (fetchEntry.type === 'css') {
assetCss += '\n/* FILE: '+fetchEntry.url+' */\n';
assetCss += data;
} else if (fetchEntry.type === 'js') {
assetJs += '\n/* FILE: '+fetchEntry.url+' */\n';
assetJs += data;
} else if (fetchEntry.type === 'dss') {
assetCssData += '\n/* FILE: '+fetchEntry.url+' */\n';
assetCssData += data;
}
cb();
});
};
var fetchResources = function(fetchList, cb) {
var startTime = new Date().getTime();
debug('createResources: '+fetchList.length);
var resourceStack = fetchList;
var resourceLoader = function() {
resourceStack = resourceStack.slice(1);
if (resourceStack.length === 0) {
debug('fetchResources done in '+(new Date().getTime()-startTime)+' ms.');
cb();
} else {
fetchResource(resourceStack[0],resourceLoader);
}
};
fetchResource(resourceStack[0],resourceLoader);
};
return function ServerConfigResourcesWebPlugin() {
this.configPlugin = function (ctx) {
ctx.key = 'serverConfigResourcesWeb';
ctx.description = 'Exports client resources web.';
ctx.localDir = __dirname;
ctx.localConfigTemplate = 'config-resources-web.json';
};
this.configServer = function(ctx) {
ctx.server.get(ctx.createSlugApiServerBase(),ctx.renderFunctionJSON(function () {
return {
resources: webAssets
}
}));
};
this.configPostBoot = function(ctx) {
fetchResources(configRegistry.createClientResourceFetchList(), function() {
assetJsHash = configUtil.stringHash(assetJs);
assetCssHash = configUtil.stringHash(assetCss);
assetCssDataHash = configUtil.stringHash(assetCssData);
debug('Total size assets.js: '+assetJs.length+' hash: '+assetJsHash);
debug('Total size assets.css: '+assetCss.length+' hash: '+assetCssHash);
debug('Total size assets-data.css: '+assetCssData.length+' hash: '+assetCssDataHash);
// note: js first to inject href asap by loader
webAssets.push({
url: assetJsUrl,
type: 'js',
hash: assetJsHash
});
webAssets.push({
url: assetCssUrl,
type: 'css',
hash: assetCssHash
});
webAssets.push({
url: assetCssDataUrl,
type: 'dss',
hash: assetCssDataHash
});
});
}
};
})();
var pluginDownload = (function () {
return function ServerConfigResourcesWebAssetPlugin() {
this.configPlugin = function (ctx) {
ctx.key = 'serverConfigResourcesWebAsset';
ctx.description = 'Exports downloaded client web resources.';
ctx.dependencies.push('serverConfigResourcesWeb');
};
this.configServer = function(ctx) {
assetCssDataUrl = ctx.createSlugApiPluginBase('assets-data.css');
ctx.server.get(assetCssDataUrl,function (req, res, next) {
res.set('Content-Type','text/css');
res.write(assetCssData);
res.end();
});
assetCssUrl = ctx.createSlugApiPluginBase('assets.css');
ctx.server.get(assetCssUrl,function (req, res, next) {
res.set('Content-Type','text/css');
res.write(assetCss);
res.end();
});
assetJsUrl = ctx.createSlugApiPluginBase('assets.js');
ctx.server.get(assetJsUrl,function (req, res, next) {
res.set('Content-Type','application/javascript');
res.write(assetJs);
res.end();
});
};
};
})();
module.exports = {
web: plugin,
webAssets: pluginDownload
};