From 7c6ffdab3eb2ebb357d9ad31ccfb6e7bec4d7715 Mon Sep 17 00:00:00 2001 From: Ivar Prudnikovas Date: Fri, 11 Jul 2014 12:56:27 +0100 Subject: [PATCH] added ability to resolve multiple build scripts from various html files 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' --- lib/configwriter.js | 44 +++++++++++++++++++++++++++++++- tasks/usemin.js | 5 +++- test/test-config-writer.js | 51 ++++++++++++++++++++------------------ test/test-usemin.js | 30 +++++++++++----------- 4 files changed, 89 insertions(+), 41 deletions(-) diff --git a/lib/configwriter.js b/lib/configwriter.js index 987b151..929d5de 100644 --- a/lib/configwriter.js +++ b/lib/configwriter.js @@ -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 = {}; @@ -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 || {}; diff --git a/tasks/usemin.js b/tasks/usemin.js index 13f2190..849944a 100644 --- a/tasks/usemin.js +++ b/tasks/usemin.js @@ -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') @@ -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 = []; @@ -182,6 +184,7 @@ module.exports = function (grunt) { }); this.filesSrc.forEach(function (filepath) { + var config; try { config = c.process(filepath, grunt.config()); diff --git a/test/test-config-writer.js b/test/test-config-writer.js index 8473599..080d7e0 100644 --- a/test/test-config-writer.js +++ b/test/test-config-writer.js @@ -61,7 +61,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'] }] } @@ -69,8 +69,8 @@ describe('ConfigWriter', function () { 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'] }] } } @@ -97,7 +97,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'] }] } @@ -105,8 +105,8 @@ 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'] }] } } @@ -133,7 +133,7 @@ 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'] }] } @@ -141,8 +141,8 @@ describe('ConfigWriter', function () { 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'] }] } } @@ -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'] }] } @@ -196,13 +196,13 @@ 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'] }] } @@ -210,8 +210,8 @@ describe('ConfigWriter', function () { 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'] }] } } @@ -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'] }] }, @@ -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 () { @@ -284,7 +287,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'] }] } @@ -292,8 +295,8 @@ describe('ConfigWriter', function () { 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'] }] } } @@ -319,7 +322,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'] }] } @@ -327,8 +330,8 @@ describe('ConfigWriter', function () { 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'] }] } } diff --git a/test/test-usemin.js b/test/test-usemin.js index cf6afe6..e9dcf41 100644 --- a/test/test-usemin.js +++ b/test/test-usemin.js @@ -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 () { @@ -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')]); }); @@ -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 () { @@ -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')); }); }); @@ -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 () { @@ -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')]); }); @@ -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')]); });