plasticine / inject-loader

💉📦 A Webpack loader for injecting code into modules via their dependencies.
https://www.npmjs.com/package/inject-loader
MIT License
482 stars 47 forks source link

Source maps break #17

Closed jnicholl closed 7 years ago

jnicholl commented 7 years ago

So far working with inject-loader has been great, but I found that source maps don't produce the right line numbers for injected modules. I'm running ES6 code through babel and using inject-loader to mock out dependencies for testing. I was able to get something working by joining lines with spaces instead of \n and just passing the provided source map (second argument) back in this.callback. This isn't really right - the source map really needs a transform applied?

module.exports = function inject(src, sourceMap) {
  this.cacheable && this.cacheable();
  var regex = createRequireStringRegex(loaderUtils.parseQuery(this.query));

  var lines = [
    'module.exports = function inject(injections) {',
    'var module = {exports: {}};',
    'var exports = module.exports;',
    src.replace(regex, "(injections[$1] || $&)"),
    'return module.exports;',
    '}'
  ];

  if (sourceMap) {
    this.callback(null, lines.join(" "), sourceMap);
  } else {
    return lines.join("\n");
  }
}
plasticine commented 7 years ago

Hey @jnicholl Thanks for the report. This is the same issue as #10, so if you don’t mind I’ll close this in favour of that earlier issue.

jnicholl commented 7 years ago

Works for me. Sorry, I read through that issue but didn't quite realize it was the same.