Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created March 12, 2013 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmuellr/5143384 to your computer and use it in GitHub Desktop.
Save pmuellr/5143384 to your computer and use it in GitHub Desktop.
For browserify 2.5.1, this tool will split the sourcemap out of your generated file, place it in a separate .map file (after base64 decoding it), and replace the sourcemap line in the original with a URL to the new .map file. Also massages source file names to make them shorter.
fs = require "fs"
path = require "path"
iFileName = process.argv[2]
oFileName = "#{iFileName}.map"
iFile = fs.readFileSync iFileName, "utf8"
match = iFile.match /\/\/@ sourceMappingURL=data:application\/json;base64,(.*)\n/
if !match?
console.log "no sourcemap in file"
process.exit()
data64 = match[1]
data = new Buffer(data64, "base64").toString("ascii")
data = JSON.parse(data)
cwd = process.cwd()
sources = data.sources
newSources = []
for source in sources
source = path.relative(cwd, source)
match = source.match /node_modules\/browserify\/node_modules(.*)/
if match
source = match[1]
newSources.push source
data.sources = newSources
data = JSON.stringify data, null, 4
oFileBaseName = path.basename oFileName
iFile = iFile.replace /\/\/@ sourceMappingURL=data:application\/json;base64,(.*)\n/, "//@ sourceMappingURL=#{oFileBaseName}\n"
fs.writeFileSync iFileName, iFile
fs.writeFileSync oFileName, data
console.log "re-wrote data-url sourcemap in #{iFileName} to #{oFileName}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment