Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

[dev] Add type support #542

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions tasks/usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ module.exports = function (grunt) {
});
var blockReplacements = options.blockReplacements || {};

debug('Looking at %s target', this.target);

var type = options.type;
var patterns = [];
var type = this.target;

debug('Looking at %s target', type);

// Check if we have a user defined pattern
if (options.patterns && options.patterns[this.target]) {
debug('Adding user defined patterns for %s', this.target);
patterns = options.patterns[this.target];
if (options.patterns && options.patterns[type]) {
debug('Adding user defined patterns for %s', type);
patterns = options.patterns[type];
}

// var locator = options.revmap ? grunt.file.readJSON(options.revmap) : function (p) { return grunt.file.expand({filter: 'isFile'}, p); };
Expand Down Expand Up @@ -158,11 +158,14 @@ module.exports = function (grunt) {
});

grunt.registerMultiTask('useminPrepare', 'Using HTML markup as the primary source of information', function () {
var options = this.options();
var options = this.options({
type: this.target
});
// collect files
var dest = options.dest || 'dist';
var staging = options.staging || '.tmp';
var root = options.root;
var type = options.type;

if (!this.filesSrc.length) {
grunt.fail.warn('No source file found.');
Expand Down
63 changes: 63 additions & 0 deletions test/test-usemin-prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,67 @@ describe('useminPrepare', function () {
assert.equal(options.foo, 'bar');

});

describe('multiple target support', function () {
before(helpers.directory('temp'));

it('should honor type option', function () {
grunt.log.muted = true;
grunt.config.init();
grunt.config('useminPrepare', {
foohtml: {
files: {
src: ['index.html']
},
options: {
type: 'html'
}
}
});

grunt.file.copy(path.join(__dirname, 'fixtures/usemin.html'), 'index.html');
grunt.file.copy(path.join(__dirname, 'fixtures/style.css'), 'styles/main.css');
grunt.file.mkdir('scripts');
grunt.file.write('scripts/bar.js', 'bar');
grunt.file.write('scripts/baz.js', 'baz');
grunt.file.mkdir('scripts/vendor');
grunt.file.mkdir('scripts/vendor/bootstrap');
grunt.file.write('scripts/vendor/bootstrap/bootstrap-affix.js', 'bootstrap-affix');
grunt.file.write('scripts/vendor/bootstrap/bootstrap-alert.js', 'bootstrap-alert');

grunt.task.run('useminPrepare');
grunt.task.start();

// we only test that default flow has been applied
var concat = grunt.config('concat');
assert.equal(concat.generated.files.length, 2);

var uglify = grunt.config('uglify');
assert.equal(uglify.generated.files.length, 1);

var cssmin = grunt.config('cssmin');
assert.equal(cssmin.generated.files.length, 1);
});

it('should not warn if no type option provided and target not known', function () {
grunt.log.muted = true;
grunt.config.init();
grunt.config('useminPrepare', {
foohtml: 'index.html'
});

grunt.task.run('useminPrepare');
grunt.task.start();

// we only test that default flow has been applied
var concat = grunt.config('concat');
assert.equal(concat.generated.files.length, 2);

var uglify = grunt.config('uglify');
assert.equal(uglify.generated.files.length, 1);

var cssmin = grunt.config('cssmin');
assert.equal(cssmin.generated.files.length, 1);
});
});
});
31 changes: 31 additions & 0 deletions test/test-usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,35 @@ describe('usemin', function () {
// Check replace has performed its duty
assert.ok(changed.match('<link rel="stylesheet" href="styles/main.css">'));
});

it('should honor options.type', function () {
grunt.file.mkdir('build');
grunt.file.mkdir('build/images');
grunt.file.mkdir('build/images/misc');
grunt.file.write('build/images/test.23012.png', 'foo');
grunt.file.write('build/images/bar.23012.png', 'foo');
grunt.file.write('build/images/misc/test.2a436.png', 'foo');
grunt.file.copy(path.join(__dirname, 'fixtures/htmlprocessor_absolute.html'), 'build/index.html');

grunt.log.muted = true;
grunt.config.init();
grunt.config('usemin', {
foohtml: {
files: {
src: ['build/index.html']
},
options: {
type: 'html'
}
}
});
grunt.task.run('usemin');
grunt.task.start();

var changed = grunt.file.read('build/index.html');

assert.ok(changed.match(/<img src="\/images\/test\.23012\.png">/));
assert.ok(changed.match(/<img src="\/\/images\/bar\.23012\.png">/));
assert.ok(changed.match(/<img src="\/images\/misc\/test\.2a436\.png">/));
});
});