sorcery.js
Sourcemaps are great - if you have a JavaScript file, and you minify it, your minifier can generate a map that lets you debug as though you were looking at the original uncompressed code.
But if you have more than one transformation - say you want to transpile your JavaScript, concatenate several files into one, and minify the result - it gets a little trickier. Each intermediate step needs to be able to both ingest a sourcemap and generate one, all the time pointing back to the original source.
Most compilers don't do that. (UglifyJS is an honourable exception.) So when you fire up devtools, instead of looking at the original source you find yourself looking at the final intermediate step in the chain of transformations.
Sorcery aims to fix that. Given an file at the end of a transformation chain (e.g., your minified JavaScript), it will follow the entire chain back to the original source, and generate a new sourcemap that describes the whole process. How? Magic.
This is a work-in-progress - suitable for playing around with, but don't rely on it to debug air traffic control software or medical equipment. Other than that, it can't do much harm.
Usage
As a node module
Install sorcery locally:
npm install sorcery
var sorcery = require( 'sorcery' );
sorcery.load( 'some/generated/code.min.js' ).then( function ( chain ) {
var map = chain.apply();
map.toString();
map.toUrl();
chain.write( 'output.js' );
chain.write( 'output.js', { absolutePath: true });
chain.write( 'output.js', { inline: true });
chain.write();
chain.write({ inline: true });
var loc = chain.trace( x, y );
});
var chain = sorcery.loadSync( 'some/generated/code.min.js' );
var map = chain.apply();
var loc = chain.trace( x, y );
chain.writeSync();
Advanced options
You can pass an optional second argument to sorcery.load()
and sorcery.loadSync()
, with zero or more of the following properties:
content
- a map of filename: contents
pairs. filename
will be resolved against the current working directory if needs besourcemaps
- a map of filename: sourcemap
pairs, where filename
is the name of the file the sourcemap is related to. This will override any sourceMappingURL
comments in the file itself.
For example:
sorcery.load( 'some/generated/code.min.js', {
content: {
'some/minified/code.min.js': '...',
'some/transpiled/code.js': '...',
'some/original/code.js': '...'
},
sourcemaps: {
'some/minified/code.min.js': {...},
'some/transpiled/code.js': {...}
}
}).then( chain => {
});
Any files not found will be read from the filesystem as normal.
On the command line
First, install sorcery globally:
npm install -g sorcery
Usage:
sorcery [options]
Options:
-h, --help Show help message
-v, --version Show version
-i, --input <file> Input file
-o, --output <file> Output file (if absent, will overwrite input)
-d, --datauri Append map as a data URI, rather than separate file
-x, --excludeContent Don't populate the sourcesContent array
Examples:
sorcery -i some/generated/code.min.js
sorcery -d -i some/generated/code.min.js
sorcery -i some/generated/code.min.js -o newfile.js
License
MIT