Webpack loader for Phaser.io asset files
Your current implementation doesn't work!

Look at this snippet, where I extracted the standalone function:



function exports(source) {
  var s = source;
  var re = /"url":\s*\".*,/g;
  var m;
  do {
    m = re.exec(source);
    if (!!m) {
      var path = m[0].split(':')[1].trim().slice(0, -1);
      s = s.replace(path, 'require(' + path + ')');
   } 
  } while (m);
  return s;
}
var source = {
      "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
};
document.write(exports(JSON.stringify(source)).replace(/overwrite/g, '&lt;span style="color:red;"&gt;$&amp;&lt;/span&gt;'));




The result is that the overwrite key following the url one is part of the require(...).
It comes from the way you expressed the regexp:

/"url":\s*\".*,/


Since the regexp is greedy the ending .*, consumes almost the whole string, up to the very last ,. Then when you apply split(':') on it, you keep the overwrite attached at the end of your path.

To fix this issue, you'd better to use this regexp:

/"url":\s*\"[^"]*",/


Here is the same snippet, now with this new regexp:



function exports(source) {
  var s = source;
  var re = /"url":\s*"[^"]*",/g;
  var m;
  do {
    m = re.exec(source);
    if (!!m) {
      var path = m[0].split(':')[1].trim().slice(0, -1);
      s = s.replace(path, 'require(' + path + ')');
   } 
  } while (m);
  return s;
}
var source = {
      "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
};
document.write(exports(JSON.stringify(source)).replace(/overwrite/g, '&lt;span style="color:red;"&gt;$&amp;&lt;/span&gt;'));




Well, as expected the overwrite is not included in the require(...).
But... now the require(...) itself is duplicated on the 2nd url occurrence, and omitted on the 3d one!

I din't try to look deeper on this second issue, because with the newly expressed regexp we clearly can do the job much more simply, like this:

function exports(source) {
  return source.replace(/"url"\s*:\s*"([^"]*)"/g, function(match, path) {
    return '"url":require("' + path + '")';
  });
}


Here is this version working:



function exports(source) {
  return source.replace(/"url"\s*:\s*"([^"]*)"/g, function(match, path) {
    return '"url":require("' + path + '")';
  });
}
var source = {
      "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
};
document.write(exports(JSON.stringify(source)).replace(/overwrite/g, '&lt;span style="color:red;"&gt;$&amp;&lt;/span&gt;'));