• Text Using node.js for a build script

    Lately we‘ve been amazed by how node allows you to use plain JavaScript for all your scripting needs. My coworkers still rely on Python or Ruby for those stuff, but since I’m a frontend guy, this comes in really handy for me. (Don’t get me wrong, Python and Ruby are really great languages, but I’m just too lazy to learn, when I can achieve the same thing with the language I’m using day to day).

    Amazed and inspired by the many approaches the OpenSource community has towards using node for build script stuff I decided to hack my own build script for a few of my recent projects. Again, I’m being pretty lazy here. I could have just learned my way into one of the existing build scripts out there, but I wanted full flexibility and a syntax I could easily hack for different project structures. So let’s get started.

    If you’re coming from the frontend like me, you’ll know that the essential part of frontend building is file concatenation and minification of assets. So that’s where I started. Concatenating files is pretty straightforward in using nodes built in filesystem-module. So my basic script workflow looks like this: Find all the files, concatenate them into a temporary file, minify the contents of the temporary file to the build target and delete that temporary file. (Lot’s of temporary going on there…)

    Let’s say you have an array of files you want to concatenate that looks like this:

    var paths = ['js/script1.js', 'js/script2.js', 'js/script3.js']; 

    You can then simply make one file from these three files using map and node’s very own filesystem module:

    var _fs = require('fs'); // map the contents of all files to the array "out" var out = paths.map(function(path) { return _fs.readFileSync(path, 'utf8'); }); // join "out" with a linebreak and save that stuff to a temporary file _fs.writeFileSync('tempFile', out.join('\n'), 'utf8'); 

    It’s that simple! Next step is to minify that stuff. No need to reinvent the wheel here. Before rolling my own script I’ve been pretty happy using Google’s Closure Compiler for Javascript and YUI Compressor for CSS. Nice thing about node: It also has a child_process module, that let’s you run command line stuff from inside your scripts. So simply take the syntax you’re using on the command line and drop it inside your script. (You’ll obviously have to place the .jar files of YUI Compressor and GCC in the same folder as your node script…)

    var exec = require('child_process').exec; // ClosureCompiler exec('java -jar google_closure_compiler-r1810.jar --js=tempFile --js_output_file=script.min.js', function (err, stdout, stderr) { // Put some callback stuff here }); // YUI Compressor exec('java -jar -Xss2048k /yuicompressor-2.4.7.jar tempFile -o style.min.css', function(err, stdout, stderr) { // Again, callback should go here }); 

    That’s all there is to using node for build scripts. You’ve just learned how to concatenate and minify assets with node.js. This is all you need to know to get started hacking a build script for your own project.

    Rodolphe Stoclin has written a nice node module called “node-minify” to provide some great abstraction over those minification called. You can find it on github: https://github.com/srod/node-minify

    At the time of writing this, “node-minify” has no ability to automatically concatenate files. So I’ve forked the module, added that stuff and sent a pull request to Rodolphe. I’ll keep you updated. In the meantime you can check out my implementation at https://github.com/lnolte/node-minify

    So in the end file concatenation and minification in node becomes as simple as this:

    var compressor = require('node-minify'); // This will concatenate your script files and minify them using Google Closure Compiler (gcc) new compressor.minify({ type: 'gcc', fileIn: ['public/js/plugins.js', 'public/js/scripts.js'], fileOut: 'public/js/script.min.js', callback: function(err){ console.log(err); } }); 

    From now on I’ll be using node a lot more for streamlining my frontend build processes. I’m still trying to figure out a nice way to distribute the whole process, but I feel like I’m getting there.

    What are your experiences with build proccesses? Please let me know. Thanks for reading!

    #node #build script #javascript 
  • Link OneClick Wordpress

    OneClick Wordpress let’s you easily set up a nice search engine optimized WP instance, let’s you fully configure your blog and then export all your stuff to static HTML. What’s the advantage you ask? Flat HTML files are freaaaaaaaking fast! Be sure to check it out – highly recommend it ;)

  • Text Magento – Fatal error: Call to a member function toHtml() resolved

    When setting up Magento for a client I decided to start with the pretty neat Magento Blank Theme. I didn’t have that much experience with Magento so I thought it’d be best to install a blank theme and then rewrite it step by step to match my client’s need. After installing the blank theme I ran into this error: Fatal error: Call to a member function toHtml()

    Just wanted to share my solution with you. Inspired by this post in the Magento forums I opened the blank theme’s page.xml and changed

    <block type="core/profiler" output="toHtml"/>

    (it’s in line 87) to:

    <block type="core/profiler" output="toHtml" name="core_profiler"/>

    Shazam! It works!

  • Text Working with YQL in CodeIgniter

    YQL is a pretty neat tool for web developers. Consider it an API for APIs. It really takes the pain out of mashing together several webservices by providing a unified interface to access these services. Go check it out, if you haven’t already.

    When working with YQL to build RIAs you’d probably want to go with JavaScript using libraries such as YUI or jQuery. Recently, however, we had the urgent need to use YQL from CodeIgniter for collecting information from several webservices and cache them on our own servers. This is important to us, because in the app we’re currently building, we’ll need to compare different versions of information and query results. Therefore we decided to go server-side. Since we’re building on CodeIgniter anyway, we decided to write a little library. It’s nothing special, but still we wanted to share it with you. Hope you find it useful. If you have any improvement ideas or feature ideas, let us know!

    _set_latest_query($query);
    		
    		$url = "http://query.yahooapis.com/v1/public/yql?q=";
    		$url .= urlencode($query);
    		$url .= "&format=json&env=store://datatables.org/alltableswithkeys";
    		
    		$response = $this->_query($url);
    		
    		return $this->_decode_response($response);
    	}
    	
    	private function _set_latest_query($value) {
    		$this->_latest_query = $value;
    	}
    	
        function get_latest_query() {
    		return $this->_latest_query;
    	}
    	
    	private function _query($url) {
    		return file_get_contents($url);
    	}
    	
    	private function _decode_response($response) {
    		$this->_dec_response = json_decode($response);
    		return $this->_dec_response->query->results;
    	}
    }

  • Photo
  • Photo Waaaaaay to cold today

    Waaaaaay to cold today

  • Photo slippery when icy

    slippery when icy

  • Photo 2
    Notes Probably no training outdoors today

    Probably no training outdoors today

  • Photo 91miles&#8217; first training plan.

    91miles’ first training plan.

  • Photo 1
    Notes Chamber of pain :)

    Chamber of pain :)

For Tumblr
By Peter Vidani
Theme: Papercut