diff --git a/README.md b/README.md index dbed0b5..8246a88 100644 --- a/README.md +++ b/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'))); - - var assets = require('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/', + note: www_static/[js/css]/lib are version ignored. + +### example-assets.json + + { + "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', - ], - }; - - // 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, - }); - } + "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" + ] } } + +### 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,37 +67,107 @@ 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/'), + 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) { - buildAssets(server,function(err) { - if (err) { - throw err; + function renderTemplatePath(viewPath) { + return function (req, res) { + res.locals.query = req.query; + var qi = req.url.indexOf('?'); + if (qi === -1) { + qi = req.url.length; } - console.log('Server init done.'); - // init other stuff... - }); + 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.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 + + + + + <%= pageTitle %> + + + + + <% if (pageCssFiles.length) { %><% pageCssFiles.forEach(function (cssFile) { %><% }) %><% } %> + + +
+
+
+
+
+
+
+
+
+ <% if (pageJsFiles.length) { %><% pageJsFiles.forEach(function (jsFile) { %><% }) %><% } %> + + + ## Config options @@ -133,16 +187,16 @@ A Node.js library providing automatic site assets aggregation. assets: { js: { - configCreate: null, - configFill: null, + configCreate: null, + configFill: null, assemblerCreate: null, - assemblerFill: null, + assemblerFill: null, }, css: { - configCreate: null, - configFill: null, + configCreate: null, + configFill: null, assemblerCreate: null, - assemblerFill: null, + assemblerFill: null, }, }, }; @@ -165,14 +219,14 @@ A Node.js library providing automatic site assets aggregation. }, linkTargetSingleResult: true, - linkTarget: null, - linkSources: [], - linkMapping: [], + linkTarget: null, + linkSources: [], + linkMapping: [], - assetType: null, - assetHeader: '\n/* '+name+': begin */\n\n', - assetFooter: '\n/* '+name+': end */\n\n', - assetSeperator: '\n/* '+name+': next */\n', + assetType: null, + 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 diff --git a/example/example-assets.json b/example/example-assets.json new file mode 100644 index 0000000..e4d3138 --- /dev/null +++ b/example/example-assets.json @@ -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" + ] + } +} diff --git a/example/example.js b/example/example.js index f0b4053..aa6564f 100644 --- a/example/example.js +++ b/example/example.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/'), + 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)); }; } @@ -123,14 +76,13 @@ buildAssets(server,function(err) { 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.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/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.'); diff --git a/lib/asset-assembler-config.js b/lib/asset-assembler-config.js index 159010b..c853a38 100644 --- a/lib/asset-assembler-config.js +++ b/lib/asset-assembler-config.js @@ -14,14 +14,14 @@ var template = { }, linkTargetSingleResult: true, - linkTarget: null, - linkSources: [], - linkMapping: [], + linkTarget: null, + linkSources: [], + linkMapping: [], - assetType: null, - assetHeader: '\n/* '+name+': begin */\n\n', - assetFooter: '\n/* '+name+': end */\n\n', - assetSeperator: '\n/* '+name+': next */\n', + assetType: null, + assetHeader: '/* '+name+': Auto generated. */\n', + assetFooter: '/* '+name+': end */\n', + assetSeperator: '\n/* '+name+': <%= file %> */\n\n', }; function checkConfig(config,callback) { diff --git a/lib/asset-assembler.js b/lib/asset-assembler.js index 1f1f9b3..118729c 100644 --- a/lib/asset-assembler.js +++ b/lib/asset-assembler.js @@ -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 { diff --git a/lib/assets-builder-config.js b/lib/assets-builder-config.js index d156f87..f305e74 100644 --- a/lib/assets-builder-config.js +++ b/lib/assets-builder-config.js @@ -12,16 +12,16 @@ var template = { assets: { js: { - configCreate: null, - configFill: null, + configCreate: null, + configFill: null, assemblerCreate: null, - assemblerFill: null, + assemblerFill: null, }, css: { - configCreate: null, - configFill: null, + configCreate: null, + configFill: null, assemblerCreate: null, - assemblerFill: null, + assemblerFill: null, }, }, }; diff --git a/lib/factory.js b/lib/factory.js index 0a3d369..e3564c6 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -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 }, diff --git a/test/test-asset-assembler.js b/test/test-asset-assembler.js index 9db29a5..6d4063e 100644 --- a/test/test-asset-assembler.js +++ b/test/test-asset-assembler.js @@ -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); + } + }); + }); + }); });