added test and updated readme prep next release
This commit is contained in:
parent
144854400f
commit
95478f85c3
234
README.md
234
README.md
|
@ -13,68 +13,52 @@ A Node.js library providing automatic site assets aggregation.
|
|||
|
||||
## Usage
|
||||
|
||||
// note1: www_static/[js/css]/lib is version ignored.
|
||||
// note2: server routes;
|
||||
// app.use('/static', express.static(path.join(__dirname,'www_static')));
|
||||
// app.use('/static/module/flot', express.static(path.join(__dirname,'node_modules/flot/dist')));
|
||||
// app.use('/static/module/bootstrap', express.static(path.join(__dirname,'node_modules/bootstrap/dist')));
|
||||
note: www_static/[js/css]/lib are version ignored.
|
||||
|
||||
var assets = require('node-ff-assets');
|
||||
### example-assets.json
|
||||
|
||||
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/',
|
||||
{
|
||||
"linkMapping" : {
|
||||
"/static/module/bootstrap/": "node_modules/bootstrap/dist/",
|
||||
"/static/module/flot/": "node_modules/flot/",
|
||||
"/static/": "www_static/"
|
||||
},
|
||||
// Site sources in html page.
|
||||
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',
|
||||
],
|
||||
};
|
||||
"css": {
|
||||
"linkTarget": "/static/css/lib/assets.css",
|
||||
"linkSources": [
|
||||
"/static/module/bootstrap/css/bootstrap.css"
|
||||
]
|
||||
},
|
||||
"js": {
|
||||
"linkTarget": "/static/js/lib/assets.js",
|
||||
"linkSources": [
|
||||
"/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) {
|
||||
// Override rendered result from command line
|
||||
var singleResult = 'false' != process.env.DEV_ASSETS_SINGLE_RESULT;
|
||||
if (type == 'css') {
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
### example.js
|
||||
|
||||
var express = require('express');
|
||||
var async = require('async');
|
||||
var path = require('path');
|
||||
var assets = require('../lib/node-ff-assets');
|
||||
|
||||
function buildAssets(server,callbackDone) {
|
||||
var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT;
|
||||
var assetsConfig = require('./example-assets.json');
|
||||
assets.build({
|
||||
assets: {
|
||||
js: {
|
||||
configCreate: createBuildConfig('js'),
|
||||
configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'js'),
|
||||
configFill: function (config, callback) {
|
||||
async.series([
|
||||
assets.factory.lib.async.pushLinkSources(config, '/static/js/', 'www_static/js/'),
|
||||
|
@ -83,36 +67,106 @@ A Node.js library providing automatic site assets aggregation.
|
|||
},
|
||||
},
|
||||
css: {
|
||||
configCreate: createBuildConfig('css'),
|
||||
configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'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()));
|
||||
},
|
||||
},
|
||||
},
|
||||
assemblerCreate: assets.factory.builder.assemblerCreate.readFileRegex(),
|
||||
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));
|
||||
assembler.config.linkTargetSingleResult = singleResult;
|
||||
callback();
|
||||
},
|
||||
},callbackDone);
|
||||
}
|
||||
|
||||
exports.build = function(server) {
|
||||
function renderTemplatePath(viewPath) {
|
||||
return function (req, res) {
|
||||
res.locals.query = req.query;
|
||||
var qi = req.url.indexOf('?');
|
||||
if (qi === -1) {
|
||||
qi = req.url.length;
|
||||
}
|
||||
res.render(viewPath + req.url.substring(req.route.path.length-1, qi));
|
||||
};
|
||||
}
|
||||
|
||||
function renderPage(server) {
|
||||
return function (req, res) {
|
||||
res.render('index', {
|
||||
pageTitle: 'node-ff-assets example',
|
||||
pageKeywords: 'node,ff,assets,example,ui',
|
||||
pageCssFiles: server.get('ff_assets_css'),
|
||||
pageJsFiles: server.get('ff_assets_js'),
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function sendRedirect() {
|
||||
return function (req, res) {
|
||||
res.redirect('/example-ui');
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
var server = express();
|
||||
buildAssets(server,function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log('Server init done.');
|
||||
// init other stuff...
|
||||
console.info('Server assets done.');
|
||||
|
||||
server.set('view engine', 'ejs');
|
||||
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('/', sendRedirect());
|
||||
server.get('/example-ui/thtml/*', renderTemplatePath('thtml/'));
|
||||
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'
|
||||
};
|
||||
|
||||
### index.ejs
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" ng-app="exampleUI">
|
||||
<head>
|
||||
<title><%= pageTitle %></title>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="keywords" content="<%= pageKeywords %>">
|
||||
<% if (pageCssFiles.length) { %><% pageCssFiles.forEach(function (cssFile) { %><link rel="stylesheet" href="<%= cssFile %>"><% }) %><% } %>
|
||||
</head>
|
||||
<body>
|
||||
<div id="wrapper">
|
||||
<div ng-include="'/example-ui/thtml/layout/header'"></div>
|
||||
<div id="page-wrapper">
|
||||
<div id="container-fluid">
|
||||
<div ng-view></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div ng-include="'/example-ui/thtml/layout/footer'"></div>
|
||||
<% if (pageJsFiles.length) { %><% pageJsFiles.forEach(function (jsFile) { %><script src="<%= jsFile %>"></script><% }) %><% } %>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
## Config options
|
||||
|
@ -170,9 +224,9 @@ A Node.js library providing automatic site assets aggregation.
|
|||
linkMapping: [],
|
||||
|
||||
assetType: null,
|
||||
assetHeader: '\n/* '+name+': begin */\n\n',
|
||||
assetFooter: '\n/* '+name+': end */\n\n',
|
||||
assetSeperator: '\n/* '+name+': next */\n',
|
||||
assetHeader: '/* '+name+': Auto generated. */\n',
|
||||
assetFooter: '/* '+name+': end */\n',
|
||||
assetSeperator: '\n/* '+name+': <%= file %> */\n\n',
|
||||
};
|
||||
|
||||
### Assembler Download Force
|
||||
|
@ -182,7 +236,7 @@ A Node.js library providing automatic site assets aggregation.
|
|||
|
||||
linkSources: ['/static/js/lib/dynamic-generated.js@@http://localhost:8080/example-ui/include-js/generateModels'];
|
||||
|
||||
note: if using localhost to download use the downloadStartDelay option so server is really up.
|
||||
note: if using localhost to download use the downloadStartDelay option and start server before building.
|
||||
|
||||
The downloadForce option will redownload all resources of asset group.
|
||||
|
||||
|
@ -255,6 +309,10 @@ A Node.js library providing automatic site assets aggregation.
|
|||
// Same as default, plain inclusion of content.
|
||||
var assetAssemblerB = new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFile());
|
||||
|
||||
// Filter content based on regexes
|
||||
var assetAssemblerF = new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileRegex());
|
||||
var assetAssemblerH = new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileRegex(regexData));
|
||||
|
||||
// Minify content, with optional Minify config options.
|
||||
var assetAssemblerC = new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileMinify());
|
||||
var assetAssemblerD = new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileMinify(minifyOptions));
|
||||
|
@ -285,44 +343,6 @@ A Node.js library providing automatic site assets aggregation.
|
|||
//done
|
||||
});
|
||||
|
||||
## Rendering
|
||||
|
||||
In these examples I use the server (app/express) object to store the results in for displaying.
|
||||
Which the index page renderer will get and inject in page context.
|
||||
|
||||
function renderIndex(app) {
|
||||
return function (req, res, next) {
|
||||
res.render('index', {
|
||||
includeCssFiles: server.get('ff_assets_css'),
|
||||
includeJsFiles: server.get('ff_assets_js'),
|
||||
});
|
||||
}
|
||||
};
|
||||
app.get('/',renderIndex(app));
|
||||
|
||||
// Template
|
||||
index.jade
|
||||
doctype html
|
||||
html(lang='en',ng-app='ffUI')
|
||||
head
|
||||
base(href='/ui')
|
||||
title='Index'
|
||||
meta(charset='UTF-8')
|
||||
meta(http-equiv='X-UA-Compatible' content='IE=edge')
|
||||
meta(name='viewport' content='width=device-width, initial-scale=1')
|
||||
meta(name='keywords' content='keywords')
|
||||
each cssFile in includeCssFiles
|
||||
link(rel='stylesheet' href='#{cssFile}')
|
||||
body
|
||||
div(id='wrapper')
|
||||
div(ng-include='\'/ui/include/html/layout/header\'')
|
||||
div(id='page-wrapper')
|
||||
div(id='container-fluid')
|
||||
div(ng-view)
|
||||
div(ng-include='\'/ui/include/html/layout/footer\'')
|
||||
each jsFile in includeJsFiles
|
||||
script(src='#{jsFile}')
|
||||
|
||||
## Example log output
|
||||
|
||||
// Result First run
|
||||
|
@ -425,6 +445,12 @@ Add unit tests for any new or changed functionality. Lint and test your code.
|
|||
|
||||
## Release History
|
||||
|
||||
### 0.2.3
|
||||
* Added json config reader.
|
||||
* Added readFileRegex data filter.
|
||||
* Added filename template to seperator.
|
||||
* Converted example to ejs.
|
||||
|
||||
### 0.2.2
|
||||
* Fixed error to callback
|
||||
* Fixed 404 error
|
||||
|
|
27
example/example-assets.json
Normal file
27
example/example-assets.json
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"linkMapping" : {
|
||||
"/static/module/bootstrap/": "node_modules/bootstrap/dist/",
|
||||
"/static/module/flot/": "node_modules/flot/",
|
||||
"/static/": "www_static/"
|
||||
},
|
||||
"css": {
|
||||
"linkTarget": "/static/css/lib/assets.css",
|
||||
"linkSources": [
|
||||
"/static/module/bootstrap/css/bootstrap.css"
|
||||
]
|
||||
},
|
||||
"js": {
|
||||
"linkTarget": "/static/js/lib/assets.js",
|
||||
"linkSources": [
|
||||
"/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"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -5,59 +5,13 @@ 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, {
|
||||
linkTargetSingleResult: singleResult,
|
||||
linkMapping: assetsConfig.linkMapping,
|
||||
linkTarget: '/static/js/lib/assets.js',
|
||||
linkSources: assetsConfig.linkSourcesJs,
|
||||
assetType: type,
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function buildAssets(server,callbackDone) {
|
||||
var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT;
|
||||
var assetsConfig = require('./example-assets.json');
|
||||
assets.build({
|
||||
assets: {
|
||||
js: {
|
||||
configCreate: createBuildConfig('js'),
|
||||
configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'js'),
|
||||
configFill: function (config, callback) {
|
||||
async.series([
|
||||
assets.factory.lib.async.pushLinkSources(config, '/static/js/', 'www_static/js/'),
|
||||
|
@ -66,34 +20,33 @@ function buildAssets(server,callbackDone) {
|
|||
},
|
||||
},
|
||||
css: {
|
||||
configCreate: createBuildConfig('css'),
|
||||
configCreate: assets.factory.builder.configCreate.fromJSON(assetsConfig,'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()));
|
||||
},
|
||||
},
|
||||
},
|
||||
assemblerCreate: assets.factory.builder.assemblerCreate.readFileRegex(),
|
||||
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));
|
||||
assembler.config.linkTargetSingleResult = singleResult;
|
||||
callback();
|
||||
},
|
||||
},callbackDone);
|
||||
}
|
||||
|
||||
function renderTemplate() {
|
||||
function renderTemplatePath(viewPath) {
|
||||
return function (req, res) {
|
||||
if (req.params.sub) {
|
||||
res.render('thtml/' + req.params.page + '/' + req.params.sub);
|
||||
} else {
|
||||
res.render('thtml/' + req.params.page);
|
||||
res.locals.query = req.query;
|
||||
var qi = req.url.indexOf('?');
|
||||
if (qi === -1) {
|
||||
qi = req.url.length;
|
||||
}
|
||||
res.render(viewPath + req.url.substring(req.route.path.length-1, qi));
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -128,9 +81,8 @@ buildAssets(server,function(err) {
|
|||
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/thtml/:page', renderTemplate());
|
||||
server.get('/example-ui/thtml/:page/:sub', renderTemplate());
|
||||
server.get('/', sendRedirect());
|
||||
server.get('/example-ui/thtml/*', renderTemplatePath('thtml/'));
|
||||
server.get('/example-ui', renderPage(server));
|
||||
server.get('/example-ui/*', renderPage(server)); // must be last; for HTML5 history
|
||||
console.info('Server config done.');
|
||||
|
|
|
@ -19,9 +19,9 @@ var template = {
|
|||
linkMapping: [],
|
||||
|
||||
assetType: null,
|
||||
assetHeader: '\n/* '+name+': begin */\n\n',
|
||||
assetFooter: '\n/* '+name+': end */\n\n',
|
||||
assetSeperator: '\n/* '+name+': next */\n',
|
||||
assetHeader: '/* '+name+': Auto generated. */\n',
|
||||
assetFooter: '/* '+name+': end */\n',
|
||||
assetSeperator: '\n/* '+name+': <%= file %> */\n\n',
|
||||
};
|
||||
|
||||
function checkConfig(config,callback) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
var u = require('underscore');
|
||||
var events = require('events');
|
||||
var fetch = require('fetch');
|
||||
var fs = require('fs-extra');
|
||||
|
@ -8,7 +9,11 @@ var config = require('./asset-assembler-config');
|
|||
|
||||
|
||||
function buildEnd(assembler, targetFile, resultUriList, callback) {
|
||||
fs.appendFile(targetFile, assembler.config.assetFooter, function (err) {
|
||||
var footer = '';
|
||||
if (assembler.config.assetFooter !== null) {
|
||||
footer = assembler.config.assetFooter;
|
||||
}
|
||||
fs.appendFile(targetFile, footer, function (err) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
} else {
|
||||
|
@ -97,10 +102,11 @@ function aggregateFileList(assembler, targetFile, aggregateList, readFile, callb
|
|||
} else {
|
||||
assembler.emit('file-read-post',aggregateFile);
|
||||
assembler.emit('log','debug','readFile: '+aggregateFile+' size: '+data.length);
|
||||
if (aggregateList.length > 0) {
|
||||
data = data + assembler.config.assetSeperator;
|
||||
if (assembler.config.assetSeperator !== null && assembler.config.assetSeperator.length > 0) {
|
||||
var seperatorTemplate = u.template(assembler.config.assetSeperator);
|
||||
data = seperatorTemplate({file: aggregateFile}) + data;
|
||||
}
|
||||
fs.appendFile(targetFile, data, function (err) {
|
||||
fs.appendFile(targetFile, data + '\n', function (err) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
} else {
|
||||
|
@ -205,7 +211,11 @@ function buildStart(assembler, readFile, callback) {
|
|||
callback(err);
|
||||
} else {
|
||||
assembler.emit('file-write-pre',targetFile);
|
||||
fs.writeFile(targetFile, assembler.config.assetHeader, function(err) {
|
||||
var header = '';
|
||||
if (assembler.config.assetHeader !== null) {
|
||||
header = assembler.config.assetHeader;
|
||||
}
|
||||
fs.writeFile(targetFile, header, function(err) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
} else {
|
||||
|
|
|
@ -55,7 +55,7 @@ var assemblerConstructors = {
|
|||
for (var i = 0; i < regexList.length; i++) {
|
||||
var regex = new RegExp(regexList[i],'gm');
|
||||
var regexReplace = regexData[regexList[i]];
|
||||
//console.log('regex: \''+regex+'\' replace: \''+regexReplace+'\'');
|
||||
console.log('regex: \''+regex+'\' replace: \''+regexReplace+'\'');
|
||||
data = data.replace(regex,regexReplace);
|
||||
}
|
||||
callback(null, data);
|
||||
|
@ -149,6 +149,12 @@ var builderAssemblerCreate = {
|
|||
callback(null,new AS(assemblerConfig,assemblerConstructors.readFileMinify(options)));
|
||||
};
|
||||
},
|
||||
readFileRegex: function(options) {
|
||||
return function (assemblerConfig, callback) {
|
||||
var AS = require('./asset-assembler');
|
||||
callback(null,new AS(assemblerConfig,assemblerConstructors.readFileRegex(options)));
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
var builderEmptyCallback = function () {
|
||||
|
@ -159,6 +165,21 @@ var builderEmptyCallback = function () {
|
|||
};
|
||||
};
|
||||
|
||||
var builderConfigCreate = {
|
||||
nop: builderEmptyCallback,
|
||||
fromJSON: function (configJSON,type) {
|
||||
return function(callback) {
|
||||
callback(null,{
|
||||
// TODO: add merging
|
||||
linkMapping: configJSON.linkMapping,
|
||||
linkTarget: configJSON[type].linkTarget,
|
||||
linkSources: configJSON[type].linkSources,
|
||||
assetType: type,
|
||||
});
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
assembler: {
|
||||
constructor : assemblerConstructors,
|
||||
|
@ -168,7 +189,7 @@ module.exports = {
|
|||
},
|
||||
},
|
||||
builder: {
|
||||
configCreate: { nop: builderEmptyCallback },
|
||||
configCreate: builderConfigCreate,
|
||||
configFill: { nop: builderEmptyCallback },
|
||||
assemblerCreate: builderAssemblerCreate,
|
||||
assemblerFill: { nop: builderEmptyCallback },
|
||||
|
|
|
@ -5,7 +5,7 @@ var fs = require('fs-extra');
|
|||
var assert = require("assert");
|
||||
var assets = require("../lib/node-ff-assets");
|
||||
|
||||
function createTestAssembler(suite) {
|
||||
function createTestAssembler(suite, readFile) {
|
||||
var assetAssembler = new assets.AssetAssembler({
|
||||
linkMapping: {'/static-test/': 'test/data/'+suite.title+'/'},
|
||||
linkTarget: '/static-test/output.css',
|
||||
|
@ -13,7 +13,7 @@ function createTestAssembler(suite) {
|
|||
'/static-test/input0.css',
|
||||
'/static-test/input1.css',
|
||||
],
|
||||
});
|
||||
},readFile);
|
||||
assetAssembler.on ('log',function (logLevel, logMessage) {
|
||||
var level = (logLevel+' ').substring(0,6);
|
||||
console.log(' '+ level + ' ' + suite.title + ' ' +logMessage);
|
||||
|
@ -21,15 +21,18 @@ function createTestAssembler(suite) {
|
|||
return assetAssembler;
|
||||
}
|
||||
|
||||
function createTestData(suite) {
|
||||
function createTestData(suite,extraData0) {
|
||||
var testName = suite.title;
|
||||
var outputFile = 'test/data/'+testName+'/output.css';
|
||||
var dataCss0 = 'test/data/'+testName+'/input0.css';
|
||||
var dataCss1 = 'test/data/'+testName+'/input1.css';
|
||||
fs.ensureFileSync(dataCss0);
|
||||
fs.appendFile(dataCss0,'input__0');
|
||||
fs.appendFileSync(dataCss0,'input__0');
|
||||
if (extraData0 !== undefined) {
|
||||
fs.appendFileSync(dataCss0,extraData0);
|
||||
}
|
||||
fs.ensureFileSync(dataCss1);
|
||||
fs.appendFile(dataCss1,'input__1');
|
||||
fs.appendFileSync(dataCss1,'input__1');
|
||||
return outputFile;
|
||||
}
|
||||
|
||||
|
@ -158,5 +161,60 @@ describe('lib/asset-assembler.js', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('aggregate-regex', function() {
|
||||
this.timeout(10000);
|
||||
var outputFile = createTestData(this,'\n// RM_THIS_COMMENT_LINE\n/* RM_THIS_COMMENT_BLOCK */\n');
|
||||
var assetAssembler = createTestAssembler(this,assets.factory.assembler.constructor.readFileRegex());
|
||||
it('run builder and check output', function (done) {
|
||||
assetAssembler.run(function (err) {
|
||||
try {
|
||||
assert.ok(true);
|
||||
assert.equal(true, fs.existsSync(outputFile));
|
||||
|
||||
var outputData = ''+fs.readFileSync(outputFile);
|
||||
assert.equal(true, outputData.indexOf('input__0') > 0);
|
||||
assert.equal(true, outputData.indexOf('input__1') > 0);
|
||||
|
||||
assert.equal(false, outputData.indexOf('RM_THIS_COMMENT_LINE') > 0);
|
||||
assert.equal(false, outputData.indexOf('RM_THIS_COMMENT_BLOCK') > 0);
|
||||
|
||||
done();
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('aggregate-no-comments', function() {
|
||||
this.timeout(10000);
|
||||
var outputFile = createTestData(this);
|
||||
var assetAssembler = createTestAssembler(this);
|
||||
assetAssembler.config.assetHeader = null;
|
||||
assetAssembler.config.assetFooter = null;
|
||||
assetAssembler.config.assetSeperator = null;
|
||||
it('run builder and check output', function (done) {
|
||||
assetAssembler.run(function (err) {
|
||||
try {
|
||||
assert.ok(true);
|
||||
assert.equal(true, fs.existsSync(outputFile));
|
||||
|
||||
var outputData = ''+fs.readFileSync(outputFile);
|
||||
assert.equal(true, outputData.indexOf('input__0') >= 0);
|
||||
assert.equal(true, outputData.indexOf('input__1') >= 0);
|
||||
|
||||
assert.equal(false, outputData.indexOf('node-ff-assets') > 0);
|
||||
assert.equal(false, outputData.indexOf('begin') > 0);
|
||||
assert.equal(false, outputData.indexOf('end') > 0);
|
||||
assert.equal(false, outputData.indexOf('input0.css') > 0);
|
||||
assert.equal(false, outputData.indexOf('input1.css') > 0);
|
||||
|
||||
done();
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue