Skip to content

Instantly share code, notes, and snippets.

@andrewschaaf
Created January 7, 2011 18:05
Show Gist options
  • Save andrewschaaf/769845 to your computer and use it in GitHub Desktop.
Save andrewschaaf/769845 to your computer and use it in GitHub Desktop.

CHCMF[H]: (coord to coord)-mapping format[, with hashes]

  • CCMF: (Closure's format)
  • CCMFH: (Closure's format) + (backward-compatible extras)

CCMF

Let json_encode be a JSON encoder such that no linebreaks occur in its output.

Let json_encode_min be a JSON encoder with no extra whitespace of any kind.

The following can be read as pseudocode, but it's also valid CoffeeScript.

write_ccmf = (info, linemaps, fileinfos, mappings) ->
    
    write "/** Begin line maps. **/" + json_encode(info) + "\n"
    for linemap in linemaps
        write json_encode_min(linemap) + "\n"
    
    write "/** Begin file information. **/\n"
    for fileinfo in fileinfos
        write "[]\n" # Yes, that's not a typo.
    
    write "/** Begin mapping definitions. **/\n"
    for mapping in mappings
        write json_encode_min(mapping) + "\n"

#
#var x = 1;
#
#var y = (
#    x +
#    2);
#
#console.log(y);
#
source = "\nvar x = 1;\n\nvar y = (\n    x +\n    2);\n\nconsole.log(y);\n"
compiled = "var x=1,y=x+2;console.log(y);"

fileinfos = [
    {}
]
info = {
    # REQUIRED
    count: fileinfos.length
    
    # OPTIONAL: any additional info
    # Closure includes the relative path to this mapping file
    # at the time of creation, which seems kind of silly:
    file: "./map.ccmf"
}
mappings = [
    # lineno: number of lines before this one
    # colno:  number of columns before this one
    # [filename, lineno, colno[, label]]
    ["foo.js",2,0]           # 0
    ["foo.js",2,4,"x"]       # 1
    ["foo.js",2,8]           # 2
    ["foo.js",4,4,"y"]       # 3
    ["foo.js",5,4,"x"]       # 4
    ["foo.js",6,4]           # 5
    ["foo.js",8,11]          # 6
    ["foo.js",8,0,"log"]     # 7
    ["foo.js",8,0,"console"] # 8
    ["foo.js",8,12,"y"]      # 9
]
linemaps = [
    # Letting _ stand for a space,
    #v a r _ x = 1 , y = x + 2 ; c o n s o l e . l o g ( y )
    [0,0,0,0,1,1,2,0,3,3,4,3,5,8,8,8,8,8,8,8,8,7,7,7,7,6,9,6]
]

CCMFH

Same as CCMF, except:

    ...
    for fileinfo in fileinfos
        write "[" + json_encode(fileinfo) + "]\n"
    ...

fileinfos = [
    {
        # REQUIRED
        sha1: "6027ead5042b2f9fe458dfd330dc0dfa92d4a899"
        
        # OPTIONAL
        name: "foo.js"
        path: "[...]/foo.js"
    }
]
info = {
    ...
    # REQUIRED
    result_sha1: "c03c3fd4e498d57b4a9e39a7c6d4d238c0ec91ce"
}
mappings = [
    # OPTIONAL: make the filename strings here something very small
    # e.g. "1", "2", ...
    ["1",2,0]
    ...
]


Closure Compiler: minify and create CCMF

mkdir temp; cd temp;

echo $'\nvar x = 1;\n\nvar y = (\n    x +\n    2);\n\nconsole.log(y);\n' > foo.js

java -jar $CLOSURE_JAR \
    --js foo.js \
    --create_source_map ./map.ccmf \
    --js_output_file result.js
var x = 1;
var y = (
x +
2);
console.log(y);
/** Begin line maps. **/{ "file" : "./map.ccmf", "count": 1 }
[0,0,0,0,1,1,2,0,3,3,4,3,5,8,8,8,8,8,8,8,8,7,7,7,7,6,9,6]
/** Begin file information. **/
[]
/** Begin mapping definitions. **/
["foo.js",2,0]
["foo.js",2,4,"x"]
["foo.js",2,8]
["foo.js",4,4,"y"]
["foo.js",5,4,"x"]
["foo.js",6,4]
["foo.js",8,11]
["foo.js",8,0,"log"]
["foo.js",8,0,"console"]
["foo.js",8,12,"y"]
var x=1,y=x+2;console.log(y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment