Source Maps Utilities Php Library

Public Announcement of a new php library for working with source maps: koala-framework/sourcemaps

Source maps help with debugging minified javascript or css files, see the html5rocks article for a short introduction.

This library originated from koala framework where we support source maps created by sass and uglifyjs.

Features

  • creating mappings
  • reading mappings
  • source map aware string replace for existing mapping
  • concat maps (optimized for best performance)

Use cases

Concat into a single file

A common pattern is the creating of a single javascript and css file out of many source files for better performance (as the browser has to make less requests).This library allows concating multiple source maps and keeps the mappings intact.

During development performance is key, so we implemented this concat as performant as possible: we don't parse and write the whole mapping information, instead we only work with the strings and put them together - with an offset correction injected - so the maps stay intact.

String replacements

When different versions of a file need to be generated (eg. translation or replacing a base path) it makes sense to cache the uglifyjs compiled file and do required string replacements on that.

Example usage

//read
$map = new Kwf_SourceMaps_SourceMap($mapContents, $minimiedFileContents);
$map->getMappings()
 
//create new map
$map = Kwf_SourceMaps_SourceMap::createEmptyMap($minimiedFileContents);
$map->addMapping(2, 3, 10, 12, 'foo.js'); //$generatedLine, $generatedColumn, $originalLine, $originalColumn, $originalSource, $originalName = null)
$map->getMapContents();
 
//merge two maps
$map1->concat($map2);
 
//perform string replacement
$map->stringReplace('foo', 'bar');

Found any issue, bug or have a question? Create an issue on github.

This entry was posted in Koala Framework, Web Development by Niko Sams. Bookmark the permalink.

Leave a Reply

Your email address will not be published.