Skip to content

Commit

Permalink
feat(task): adds configurable filenames to BundleJsTask
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAuth committed Nov 7, 2016
1 parent be1f0f8 commit d972e38
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
26 changes: 21 additions & 5 deletions source/task/BundleJsTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const difference = require('lodash.difference');
const co = require('co');
const PATH_SEPERATOR = require('path').sep;
const fs = require('co-fs-extra');
const templateString = require('es6-template-strings');


/**
Expand Down Expand Up @@ -65,6 +66,19 @@ class BundleJsTask extends BaseTask
}


/**
* @protected
* @returns {Promise<Array>}
*/
prepareParameters(buildConfiguration, parameters)
{
const result = super.prepareParameters(buildConfiguration, parameters);
result.query = result.query || '*';
result.filenameTemplate = result.filenameTemplate || '${site.name.urlify()}/${group.urlify()}.js';
return result;
}


/**
* @protected
* @returns {Promise<Array>}
Expand All @@ -75,16 +89,16 @@ class BundleJsTask extends BaseTask
const promise = co(function *()
{
// Prepare
const query = parameters ? parameters.query || '*' : '*';
const params = scope.prepareParameters(buildConfiguration, parameters);

// Start
const work = scope._cliLogger.section('Generating bundle configuration for <' + query + '>');
const work = scope._cliLogger.section('Generating bundle configuration for <' + params.query + '>');

// Get Sites
let sites = [];
if (query !== '*')
if (params.query !== '*')
{
const site = yield scope._sitesRepository.findBy(Site.ANY, query);
const site = yield scope._sitesRepository.findBy(Site.ANY, params.query);
sites.push(site);
}
else
Expand Down Expand Up @@ -117,10 +131,11 @@ class BundleJsTask extends BaseTask
const bundles = {};
for (const group in sourceFiles)
{
const filename = templateString(params.filenameTemplate, { site: site, group: group });
const groupWork = scope._cliLogger.work('Genrating bundle config for <' + site.name + '> / <' + group + '>');
const bundle =
{
filename : site.name.toLowerCase() + PATH_SEPERATOR + group.toLowerCase() + '.js',
filename : filename,
prepend: [],
append: [],
include: [],
Expand Down Expand Up @@ -282,6 +297,7 @@ class BundleJsTask extends BaseTask
{
const siteBundles = yield scope.generateConfiguration(buildConfiguration, parameters);
const work = scope._cliLogger.section('Bundling js files');
scope._cliLogger.options(scope.prepareParameters(buildConfiguration, parameters));
for (const siteBundle of siteBundles)
{
const siteFiles = yield scope.compileBundles(siteBundle, buildConfiguration, parameters);
Expand Down
30 changes: 25 additions & 5 deletions test/task/BundleJsTaskSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe(BundleJsTask.className, function()
return promise;
});

it('should resolve to an array containing bundle configs for each site', function()
it('should resolve to an array containing bundle configs for each configured site', function()
{
const promise = co(function *()
{
Expand All @@ -100,10 +100,30 @@ describe(BundleJsTask.className, function()
const bundles = yield testee.generateConfiguration();
const baseBundle = bundles[0];
const extendedBundle = bundles[1];
expect(baseBundle).to.have.contain.key('common');
expect(baseBundle).to.have.contain.key('core');
expect(extendedBundle).to.have.contain.key('common');
expect(extendedBundle).to.have.contain.key('extended');
expect(baseBundle).to.contain.key('common');
expect(baseBundle.common.filename).to.be.equal(pathes.normalizePathSeperators('base/common.js'));
expect(baseBundle).to.contain.key('core');
expect(baseBundle.core.filename).to.be.equal(pathes.normalizePathSeperators('base/core.js'));
expect(extendedBundle).to.contain.key('common');
expect(extendedBundle.common.filename).to.be.equal(pathes.normalizePathSeperators('extended/common.js'));
expect(extendedBundle).to.contain.key('extended');
expect(extendedBundle.extended.filename).to.be.equal(pathes.normalizePathSeperators('extended/extended.js'));
});
return promise;
});

it('should allow to customize bundle file pathes', function()
{
const promise = co(function *()
{
const testee = createTestee();
const bundles = yield testee.generateConfiguration(undefined, { filenameTemplate: '${group.urlify()}.js'});
const baseBundle = bundles[0];
const extendedBundle = bundles[1];
expect(baseBundle.common.filename).to.be.equal(pathes.normalizePathSeperators('common.js'));
expect(baseBundle.core.filename).to.be.equal(pathes.normalizePathSeperators('core.js'));
expect(extendedBundle.common.filename).to.be.equal(pathes.normalizePathSeperators('common.js'));
expect(extendedBundle.extended.filename).to.be.equal(pathes.normalizePathSeperators('extended.js'));
});
return promise;
});
Expand Down
6 changes: 3 additions & 3 deletions test/task/CompileSassTaskSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe(CompileSassTask.className, function()
return promise;
});

it('should should generate a file for each group of each configured site', function()
it('should generate a file for each group of each configured site', function()
{
const promise = co(function *()
{
Expand All @@ -107,7 +107,7 @@ describe(CompileSassTask.className, function()
expect(files.find(item => item.path == normalize('base/css/common.scss'))).to.be.ok;
expect(files.find(item => item.path == normalize('base/css/core.scss'))).to.be.ok;
expect(files.find(item => item.path == normalize('extended/css/common.scss'))).to.be.ok;
expect(files.find(item => item.path == normalize('extended/css/core.scss'))).to.be.ok;
expect(files.find(item => item.path == normalize('extended/css/core.scss'))).to.be.ok;
});
return promise;
});
Expand Down Expand Up @@ -268,7 +268,7 @@ describe(CompileSassTask.className, function()
{
expect(file.contents.toString()).to.not.contain('@import \'');
expect(file.path).to.be.oneOf([normalize('base/common.css'), normalize('base/core.css'),
normalize('extended/common.css'), normalize('extended/core.css')]);
normalize('extended/common.css'), normalize('extended/core.css')]);
}
});
return promise;
Expand Down

0 comments on commit d972e38

Please sign in to comment.