From 7be6d11dba6096ece5c28b419e3ee9bc71c7c605 Mon Sep 17 00:00:00 2001 From: Buddhike de Silva Date: Thu, 13 Feb 2014 23:03:54 +1100 Subject: [PATCH] Output path respects the hierarchy of the input file. #297 --- lib/configwriter.js | 7 ++++++- test/fixtures/relative_path_parent.html | 12 +++++++++++ test/test-usemin.js | 28 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/relative_path_parent.html diff --git a/lib/configwriter.js b/lib/configwriter.js index 987b151..731446b 100644 --- a/lib/configwriter.js +++ b/lib/configwriter.js @@ -145,7 +145,10 @@ ConfigWriter.prototype.process = function (file, config) { lfile = new File(file); } + var relativePath = self.root ? '' : lfile.dir.split('/').splice(1).join('/'); + lfile.blocks.forEach(function (block) { + // FIXME: support several searchPath... var context = { inDir: self.root || lfile.searchPath[0], @@ -165,7 +168,9 @@ ConfigWriter.prototype.process = function (file, config) { // 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 ? path.join(self.dest, relativePath) : + path.join(self.staging, writer.name, relativePath); + context.last = last; config[writer.name] = config[writer.name] || {}; config[writer.name].generated = config[writer.name].generated || {}; diff --git a/test/fixtures/relative_path_parent.html b/test/fixtures/relative_path_parent.html new file mode 100644 index 0000000..8be1605 --- /dev/null +++ b/test/fixtures/relative_path_parent.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/test-usemin.js b/test/test-usemin.js index cf6afe6..c6e8ba4 100644 --- a/test/test-usemin.js +++ b/test/test-usemin.js @@ -460,6 +460,34 @@ describe('useminPrepare', function () { }); }); + describe('relative path', function () { + it('should have the correct root for generated path', function () { + grunt.log.muted = true; + grunt.config.init(); + grunt.config('useminPrepare', { html: 'app/foo/index.html' }); + grunt.file.mkdir('app/foo'); + grunt.file.copy(path.join(__dirname, 'fixtures/relative_path_parent.html'), 'app/foo/index.html'); + + grunt.task.run('useminPrepare'); + grunt.task.start(); + + var concat = grunt.config('concat'); + assert.ok(concat); + assert.ok(concat.generated); + assert.equal(concat.generated.files.length, 1); + var files = concat.generated.files[0]; + + assert.equal(files.dest, '.tmp/concat/scripts/thisthat.js'); + assert.equal(files.src.length, 2); + assert.equal(files.src[0], 'app/scripts/this.js'); + + var uglify = grunt.config('uglify'); + assert.equal(uglify.generated.files.length, 1); + files = uglify.generated.files[0]; + assert.equal(files.dest, 'dist/scripts/thisthat.js'); + }); + }); + it('should take dest option into consideration', function () { grunt.log.muted = true; grunt.config.init();