Skip to content
Browse files

use single PhantomJS instance (+200% speed)

  • Loading branch information...
1 parent faa97f5 commit c0e8b701d86df29461af7e5b43dc1c8e7966d3ce @dbushell committed Jun 12, 2013
Showing with 56 additions and 65 deletions.
  1. +1 −1 package.json
  2. +30 −19 tasks/lib/svg2png.js
  3. +25 −45 tasks/svg2png.js
View
2 package.json
@@ -3,7 +3,7 @@
"description": "Grunt plugin to rasterize SVG to PNG images using PhantomJS",
- "version": "0.1.2",
+ "version": "0.2.0",
"author": {
"name": "David Bushell",
View
49 tasks/lib/svg2png.js
@@ -6,32 +6,43 @@
* Licensed under The MIT License (MIT)
*/
-var fs = require('fs');
+var fs = require('fs'),
+ page = require('webpage').create(),
+ files = JSON.parse(phantom.args[0]),
+ total = files.length,
+ next = 0,
-var inputFile = phantom.args[0],
- outputFile = phantom.args[1],
+ file, svgdata, frag, svg, width, height;
- svgdata = fs.read(inputFile) || '',
+var nextFile = function()
+{
+ if (next >= total) {
+ phantom.exit(0);
+ return;
+ }
- frag = window.document.createElement('div'),
+ file = files[next++];
- page = require('webpage').create();
+ svgdata = fs.read(file.src) || '';
-frag.innerHTML = svgdata;
+ frag = window.document.createElement('div');
+ frag.innerHTML = svgdata;
-var svg = frag.querySelector('svg'),
- width = svg.getAttribute('width'),
+ svg = frag.querySelector('svg');
+ width = svg.getAttribute('width');
height = svg.getAttribute('height');
-page.viewportSize = {
- width: parseFloat(width),
- height: parseFloat(height)
+ page.viewportSize = {
+ width: parseFloat(width),
+ height: parseFloat(height)
+ };
+
+ page.open('data:image/svg+xml;utf8,' + svgdata, function(status)
+ {
+ page.render(file.dest);
+ console.log(JSON.stringify({ 'file': file, 'status': status }));
+ nextFile();
+ });
};
-// page.open(inputFile, function(status)
-page.open('data:image/svg+xml;utf8,' + svgdata, function(status)
-{
- page.render(outputFile);
- phantom.exit(false);
- process.exit(0);
-});
+nextFile();
View
70 tasks/svg2png.js
@@ -18,9 +18,7 @@ module.exports = function(grunt)
var done = this.async(),
start = new Date(),
- processed = 0,
completed = 0,
- maxspawns = 10,
files = [],
total = 0;
@@ -50,42 +48,6 @@ module.exports = function(grunt)
grunt.log.subhead('Rasterizing SVG to PNG (' + files.length + ' files)...');
- var onComplete = function()
- {
- if (++completed >= total) {
- update();
- grunt.log.write("\n");
- grunt.log.ok("Rasterization complete.");
- done();
- } else {
- update();
- if (processed < total) {
- next();
- }
- }
- };
-
- var next = function()
- {
- if (!files[processed]) {
- return;
- }
- grunt.util.spawn(
- {
- cmd: phantomjs.path,
- args: [
- path.resolve(__dirname, 'lib/svg2png.js'),
- files[processed].src,
- files[processed].dest
- ]
- },
- function(error, result, code) {
- onComplete();
- }
- );
- processed++;
- };
-
var styles = {
'bold' : ['\x1B[1m', '\x1B[22m'],
@@ -134,14 +96,32 @@ module.exports = function(grunt)
process.stdout.write(str);
};
- if (!total) {
- onComplete();
- } else {
- update();
- while(maxspawns--) {
- next();
+ var spawn = grunt.util.spawn({
+ cmd: phantomjs.path,
+ args: [
+ path.resolve(__dirname, 'lib/svg2png.js'),
+ JSON.stringify(files)
+ ]
+ },
+ function(err, result, code)
+ {
+ grunt.log.write("\n");
+ grunt.log.ok("Rasterization complete.");
+ done();
}
- }
+ );
+
+ spawn.stdout.on('data', function(buffer)
+ {
+ try {
+ var result = JSON.parse(buffer.toString());
+ if (result.status) {
+ completed++;
+ update();
+ }
+ } catch (e) { }
+ });
+ update();
});
};

0 comments on commit c0e8b70

Please sign in to comment.
Something went wrong with that request. Please try again.