Skip to content

Commit

Permalink
added ability to resolve multiple build scripts from various html fil…
Browse files Browse the repository at this point in the history
…es even if script destination is the sameWhen files are put on .tmp directory they go into the same relative path as html is in omitting 'concat' or 'build' directory within '.tmp'. but it comes with price: html files expected to be in the same relative path in 'dist' after usemin process is over; but there is also 'srcStrip' optionadded for the user to be able to remove parts of path used when temporary storing concatenated files and when moving files to 'dist'
  • Loading branch information
Ivar Prudnikovas committed Jul 11, 2014
1 parent 191a4f4 commit 7c6ffda
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 41 deletions.
44 changes: 43 additions & 1 deletion lib/configwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var ConfigWriter = module.exports = function (flow, dirs) {
this.flow = flow;
// FIXME: check dest and staging are furnished
this.root = dirs.root;
this.srcStrip = dirs.srcStrip;
this.dest = dirs.dest;
this.staging = dirs.staging;
this.steps = {};
Expand Down Expand Up @@ -159,13 +160,54 @@ ConfigWriter.prototype.process = function (file, config) {
}

self.forEachStep(block.type, function (writer, last) {

var blockConfig;
var fileSet;
var dest;
var regexp;
var stageDir;
var destinationDir;
var strippedInDir = context.inDir;
var splitDirs = [];

// If this is the last writer of the pipe, we need to output
// in the destination directory
context.outDir = last ? self.dest : path.join(self.staging, writer.name);
//context.outDir = last ? self.dest : path.join(self.staging, writer.name);

/////////
// staging dir should reflect the file dir
// if file is in /bla/x/y/z and staging set to .tmp
// then actual staging dir will be /.tmp/bla/x/y/z
// this way there is no collision if 2 html files specify
// same destination path

// remove parts of srcStrip dir if specified in config
if (_.isString(self.srcStrip)){
if (context.inDir.indexOf(self.srcStrip + '/') === 0){
regexp = new RegExp('^' + self.srcStrip + '/');
strippedInDir = context.inDir.replace(regexp, '');
} else if (context.inDir.indexOf('/' + self.srcStrip + '/') > -1){
splitDirs = context.inDir.split('/' + self.srcStrip + '/');
strippedInDir = path.join.apply(this, splitDirs);
} else if (context.inDir.indexOf('/' + self.srcStrip) > -1){
regexp = new RegExp('/' + self.srcStrip + '$');
strippedInDir = context.inDir.replace(regexp,'');
}
}

// in last pipe remove staging dir
if (last && _.isString(self.staging) && strippedInDir.indexOf(self.staging) === 0){
// regexp finds either exact .tmp string or .tmp/ as start of string
regexp = new RegExp('(?:^' + self.staging + '$)|(?:^' + self.staging + '/)');
strippedInDir = strippedInDir.replace(regexp, '');
}

stageDir = path.join(self.staging, strippedInDir);
destinationDir = path.join(self.dest, strippedInDir);
context.outDir = last ? destinationDir : stageDir;

//////////

context.last = last;
config[writer.name] = config[writer.name] || {};
config[writer.name].generated = config[writer.name].generated || {};
Expand Down
5 changes: 4 additions & 1 deletion tasks/usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ module.exports = function (grunt) {
var dest = options.dest || 'dist';
var staging = options.staging || '.tmp';
var root = options.root;
var srcStrip = options.srcStrip;

grunt.log
.writeln('Going through ' + grunt.log.wordlist(this.filesSrc) + ' to update the config')
Expand All @@ -166,7 +167,8 @@ module.exports = function (grunt) {
var c = new ConfigWriter(flow, {
root: root,
dest: dest,
staging: staging
staging: staging,
srcStrip: srcStrip
});

var cfgNames = [];
Expand All @@ -182,6 +184,7 @@ module.exports = function (grunt) {
});

this.filesSrc.forEach(function (filepath) {

var config;
try {
config = c.process(filepath, grunt.config());
Expand Down
51 changes: 27 additions & 24 deletions test/test-config-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ describe('ConfigWriter', function () {
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
dest: '.tmp/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
dest: 'dist/app/scripts/site.js',
src: ['.tmp/app/scripts/site.js']
}]
}
}
Expand All @@ -97,16 +97,16 @@ describe('ConfigWriter', function () {
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
dest: '.tmp/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'destination/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
dest: 'destination/app/scripts/site.js',
src: ['.tmp/app/scripts/site.js']
}]
}
}
Expand All @@ -133,16 +133,16 @@ describe('ConfigWriter', function () {
concat: {
generated: {
files: [{
dest: 'staging/concat/scripts/site.js',
dest: 'staging/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['staging/concat/scripts/site.js']
dest: 'dist/app/scripts/site.js',
src: ['staging/app/scripts/site.js']
}]
}
}
Expand All @@ -169,7 +169,7 @@ describe('ConfigWriter', function () {
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
dest: 'dist/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
Expand All @@ -196,22 +196,22 @@ describe('ConfigWriter', function () {
uglify: {
generated: {
files: [{
dest: 'staging/uglify/foo.js',
dest: 'staging/app/foo.js',
src: ['app/foo.js']
}, {
dest: 'staging/uglify/bar.js',
dest: 'staging/app/bar.js',
src: ['app/bar.js']
}, {
dest: 'staging/uglify/baz.js',
dest: 'staging/app/baz.js',
src: ['app/baz.js']
}]
}
},
concat: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['staging/uglify/foo.js', 'staging/uglify/bar.js', 'staging/uglify/baz.js']
dest: 'dist/app/scripts/site.js',
src: ['staging/app/foo.js', 'staging/app/bar.js', 'staging/app/baz.js']
}]
}
}
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('ConfigWriter', function () {
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
dest: '.tmp/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
},
Expand All @@ -254,16 +254,19 @@ describe('ConfigWriter', function () {
uglify: {
generated: {
files: [{
dest: 'destination/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
dest: 'destination/app/scripts/site.js',
src: ['.tmp/app/scripts/site.js']
}]
}
}
});
assert.deepEqual(config, expected);
});

it('should allow for a flow per block type');

it('should allow for an empty flow');

it('should allow for a filename as input');

it('should deduplicate blocks', function () {
Expand All @@ -284,16 +287,16 @@ describe('ConfigWriter', function () {
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
dest: '.tmp/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
dest: 'dist/app/scripts/site.js',
src: ['.tmp/app/scripts/site.js']
}]
}
}
Expand All @@ -319,16 +322,16 @@ describe('ConfigWriter', function () {
concat: {
generated: {
files: [{
dest: '.tmp/concat/scripts/site.js',
dest: '.tmp/app/scripts/site.js',
src: ['app/foo.js', 'app/bar.js', 'app/baz.js']
}]
}
},
uglify: {
generated: {
files: [{
dest: 'dist/scripts/site.js',
src: ['.tmp/concat/scripts/site.js']
dest: 'dist/app/scripts/site.js',
src: ['.tmp/app/scripts/site.js']
}]
}
}
Expand Down
30 changes: 15 additions & 15 deletions test/test-usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,15 @@ describe('useminPrepare', function () {
assert.ok(concat.generated.files);
assert.equal(concat.generated.files.length, 2);

assert.equal(concat.generated.files[1].dest, path.normalize('.tmp/concat/scripts/plugins.js'));
assert.equal(concat.generated.files[1].dest, path.normalize('.tmp/scripts/plugins.js'));
assert.equal(concat.generated.files[1].src.length, 13);
assert.equal(concat.generated.files[0].dest, path.normalize('.tmp/concat/styles/main.min.css'));
assert.equal(concat.generated.files[0].dest, path.normalize('.tmp/styles/main.min.css'));
assert.equal(concat.generated.files[0].src.length, 1);

var uglify = grunt.config('uglify');

assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/scripts/plugins.js')]);
assert.equal(uglify.generated.files[0].dest, path.normalize('dist/scripts/plugins.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/concat/scripts/plugins.js')]);
});

it('should use alternate search dir if asked to', function () {
Expand All @@ -394,12 +394,12 @@ describe('useminPrepare', function () {

var concat = grunt.config('concat');
assert.ok(concat);
assert.equal(concat.generated.files[0].dest, path.normalize('.tmp/concat/scripts/foo.js'));
assert.equal(concat.generated.files[0].dest, path.normalize('.tmp/build/scripts/foo.js'));
assert.deepEqual(concat.generated.files[0].src, [path.normalize('build/scripts/bar.js'), path.normalize('build/scripts/baz.js')]);

var uglify = grunt.config('uglify');
assert.equal(uglify.generated.files[0].dest, path.normalize('dist/scripts/foo.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/concat/scripts/foo.js')]);
assert.equal(uglify.generated.files[0].dest, path.normalize('dist/build/scripts/foo.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/build/scripts/foo.js')]);
});


Expand All @@ -416,12 +416,12 @@ describe('useminPrepare', function () {

var concat = grunt.config('concat');
assert.ok(concat);
assert.equal(concat.generated.files[0].dest, path.normalize('.tmp/concat/scripts/foo.js'));
assert.equal(concat.generated.files[0].dest, path.normalize('.tmp/build/scripts/foo.js'));
assert.equal(concat.generated.files[0].src.length, 2);

var uglify = grunt.config('uglify');
assert.equal(uglify.generated.files[0].dest, path.normalize('dist/scripts/foo.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/concat/scripts/foo.js')]);
assert.equal(uglify.generated.files[0].dest, path.normalize('dist/build/scripts/foo.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/build/scripts/foo.js')]);
});

describe('absolute path', function () {
Expand Down Expand Up @@ -449,14 +449,14 @@ describe('useminPrepare', function () {
assert.equal(concat.generated.files.length, 1);
var files = concat.generated.files[0];

assert.equal(files.dest, path.normalize('.tmp/concat/scripts/foo.js'));
assert.equal(files.dest, path.normalize('.tmp/foo/scripts/foo.js'));
assert.equal(files.src.length, 2);
assert.equal(files.src[0], path.normalize('foo/scripts/bar.js'));

var uglify = grunt.config('uglify');
assert.equal(uglify.generated.files.length, 1);
files = uglify.generated.files[0];
assert.equal(files.dest, path.normalize('dist/scripts/foo.js'));
assert.equal(files.dest, path.normalize('dist/foo/scripts/foo.js'));
});
});

Expand All @@ -474,8 +474,8 @@ describe('useminPrepare', function () {
grunt.task.start();

var uglify = grunt.config('uglify');
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/scripts/plugins.js')]);
assert.equal(uglify.generated.files[0].dest, path.normalize('foo/scripts/plugins.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('.tmp/concat/scripts/plugins.js')]);

});
it('should take staging option into consideration', function () {
Expand All @@ -493,8 +493,8 @@ describe('useminPrepare', function () {

var concat = grunt.config('concat');
var uglify = grunt.config('uglify');
assert.equal(concat.generated.files[0].dest, path.normalize('foo/concat/styles/main.min.css'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('foo/concat/scripts/plugins.js')]);
assert.equal(concat.generated.files[0].dest, path.normalize('foo/styles/main.min.css'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('foo/scripts/plugins.js')]);

});

Expand All @@ -513,7 +513,7 @@ describe('useminPrepare', function () {

var uglify = grunt.config('uglify');
assert.equal(uglify.generated.files[0].dest, path.normalize('dist/scripts/plugins.js'));
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('foo/concat/scripts/plugins.js')]);
assert.deepEqual(uglify.generated.files[0].src, [path.normalize('foo/scripts/plugins.js')]);

});

Expand Down

0 comments on commit 7c6ffda

Please sign in to comment.