Rich-Harris / sorcery

Resolve a chain of sourcemaps back to the original source, like magic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to skip first directories

zba opened this issue · comments

I have the following compile flow:

babel->browserify->exorciise->sorcery

so I would to apply magic to browserify sourcemaps , but since browserify has no cwd option, it will generate paths relative to project root, so, if generated .js file not in project root path sorcery will not find original map... one possible workaround is symlink, but it would be better to have option like in 'patch' --strip=num (-pnum)

example project tree:

| App
|_Build
   |   |_babeljs
   |      |_some.js
   |      |_sone.js.map
   |_index.js // made from some.js
   |_index.js.map

~/dev/projec/$ sorcery -i index.js
Error: ENOENT, open /usr/dev/project/Build/Build/babeljs/some.js.map

Eek. I've faced a similar problem in the past - in the gobble plugin that wraps browserify, I had to explicitly make the paths absolute otherwise I'd end up in the same situation.

It seems that this is something that should probably be fixed within browserify. It doesn't have a concept of a 'destination' for the file, since it just creates a stream, but perhaps it should have a sourceMapFile option that it uses as a base to generate the relative paths from. I've raised an issue with browserify: browserify/browserify#1303

Incidentally you shouldn't need to use exorcist if you're using sorcery - sorcery will read the inline data URI just as happily as the separate .map file exorcist generates.

@zba FYI there was some discussion the browserify issue tracker, and a second issue has been raised - browserify/browserify#1304. Unfortunately I'm not sure that this is best solved within sorcery in the meantime, since it only addresses the first sourcemaps encountered - any transpilation step could have similar problems, and I don't think there's a particularly good way to address them via configuration options.

I think the best interim solution would be to modify the sourcemap between browserify and sorcery. mold-source-map might be useful here - I haven't used it, but I think it's suitable for this sort of task. Alternatively...

var json = fs.readFileSync('bundle.js.map', 'utf-8');
var map = JSON.parse(json);
map.sourceRoot = '../';
json = JSON.stringify(map);
fs.writeFileSync('bundle.js.map', json);