Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit ee8e121

Browse files
committed
Added test & additional check for pr bezoerb#13
1 parent 7f886c2 commit ee8e121

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

Gruntfile.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,15 @@ module.exports = function (grunt) {
170170
options: {
171171
base: 'test/fixture',
172172
minify: true,
173+
css: [
174+
'test/fixture/styles/main.css',
175+
'test/fixture/styles/bootstrap.css'
176+
],
173177
width: 1300,
174-
height: 900,
175-
css: [
176-
'test/fixture/styles/main.css',
177-
'test/fixture/styles/bootstrap.css'
178-
],
178+
height: 900
179179
},
180-
files: [
181-
{expand: true, cwd: 'test/fixture/multiple', src: ['*.html'], dest: 'test/generated/multiple-files-folder'}
182-
]
180+
src: 'test/fixture/multiple/index{1,2,3}.html',
181+
dest: 'test/generated/multiple-files-folder/'
183182
}
184183
}
185184
});
@@ -188,7 +187,7 @@ module.exports = function (grunt) {
188187
grunt.loadTasks('tasks');
189188

190189

191-
grunt.registerTask('test', ['complexity', 'jshint', 'critical', 'simplemocha', 'watch']);
192-
grunt.registerTask('ci', ['complexity', 'jshint', 'critical', 'simplemocha']);
190+
grunt.registerTask('test', [ 'jshint', 'critical', 'simplemocha', 'watch']);
191+
grunt.registerTask('ci', [ 'jshint', 'critical', 'simplemocha']);
193192
grunt.registerTask('default', ['test']);
194193
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
},
2828
"dependencies": {
2929
"async": "^1.2.0",
30-
"critical": "^0.5.6"
30+
"critical": "^0.5.6",
31+
"fs-extra": "^0.18.4"
3132
},
3233
"devDependencies": {
3334
"chai": "^3.0.0",

tasks/critical.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ module.exports = function (grunt) {
1313
var path = require('path');
1414
var async = require('async');
1515
var extend = require('util')._extend;
16+
var fs = require('fs-extra');
17+
18+
19+
function isDir(p) {
20+
return typeof p === 'string' && (grunt.file.isDir(p) || /\/$/.test(p));
21+
}
1622

1723
grunt.registerMultiTask('critical', 'Extract & inline critical-path CSS from HTML', function () {
1824

@@ -49,28 +55,29 @@ module.exports = function (grunt) {
4955
return;
5056
}
5157

58+
if (srcFiles.length > 1 && !isDir(f.dest)) {
59+
grunt.log.warn('Destination needs to be a directory for multiple src files');
60+
return;
61+
}
62+
5263
// choose wether to create raw css or complete html
5364
var command = (/\.(css|scss|less|styl)/.test(path.extname(f.dest))) ? 'generate' : 'generateInline';
5465

5566
async.eachSeries(srcFiles,function(src,cb){
5667
var opts = extend({},options);
5768
opts.src = path.resolve(src).replace(basereplace,'');
5869

59-
// check if the destination is a folder and not a file
60-
var destination;
61-
if (grunt.file.isDir(f.dest)) {
62-
63-
destination = path.join(f.dest, src);
64-
} else {
65-
destination = f.dest;
70+
var destination = f.dest;
71+
if (isDir(f.dest)) {
72+
destination = path.join(f.dest, opts.src);
6673
}
6774

6875
try {
6976
critical[command](opts, function (err, output){
7077
if (err) {
7178
return cb(err);
7279
}
73-
grunt.file.write(destination, output);
80+
fs.outputFileSync(destination, output);
7481
// Print a success message.
7582
grunt.log.ok('File "' + destination + '" created.');
7683

test/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@ describe('critical',function(){
7676
expect(exists('fixture/styles/bootstrap.1956cc2d.css')).to.equal(true);
7777
});
7878

79+
it('should write files to folder when folder is specified as dest', function(){
80+
var output,expected = read('expected/index-multiple-minified.html');
81+
82+
for (var i=1; i<=3; i++) {
83+
expect(exists('generated/multiple-files-folder/multiple/index'+i+'.html')).to.equal(true);
84+
output = read('generated/multiple-files-folder/multiple/index'+i+'.html');
85+
expect(output).to.equal(expected.replace('<title>pagex</title>','<title>page' + i + '</title>'));
86+
}
87+
});
88+
7989
});

0 commit comments

Comments
 (0)