plasticine / inject-loader

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

Maintain sourcemapping for injected modules. #10

Closed chengyin closed 7 years ago

chengyin commented 9 years ago

I am not 100% which side is the best to resolve this issue since there are multiple plugins involve. However, I do want to report this so the maintainer and other users are aware of this problem.

First, our setup is:

The istanbul-instrumenter-loader was used to hook each module to Istanbul.

Now, when test package requires both a module, and an injected version of the same module. I'm getting exceptions from Istanbul. On the surface, the issue is caused by a mismatch between statements and statementMap, two internal variables of Istanbul to record statements and the location of them in the file. The length of the two didn’t agree. One recorded more statements than the other for "the same file".

I did some debugging and I believe this is what's happening:

  1. inject-loader will add statements to the module for the injection
  2. istanbul-instrumenter-loader registers both the original file and the injected version to Istanbul understand the same file name

The two functions, under the same name, have different statement count, led to the issue.

I also provided this info to Istanbul under gotwarlost/istanbul#192.

nkbt commented 9 years ago

This is a problem of istanbul-instrumenter-loader https://github.com/deepsweet/istanbul-instrumenter-loader/pull/12

plasticine commented 9 years ago

Thanks for reporting. This is an issue I'm aware of. I think to work up a fix I will need to reprocess the source maps if they are supplied.

I'm on vacation for the next week or so, i plan to do a bunch of work on this stuff when I get back to work. :v:

dallonf commented 9 years ago

Just ran into exactly this problem. Anything I can do to help?

nkbt commented 9 years ago

Not to include same file with inject loader and without in the same build.

This is usually an indication, some of your unit tests include too much.

Though I made a dirty fix for istanbul-loader to address this (not accepted)

austinknight commented 8 years ago

Did anyone find a workaround for this? I've got inject-loader working just fine, the coverage report still just reports tested lines as untested still.

plasticine commented 8 years ago

To be honest I’m pretty out of my depth here — I know very little about the implementation details of sourcemaps.

Anyone have any useful pointers on how to resolve this? I’m making some improvements to inject-loader and would kinda like to be able to fix this while I’m here...

ricardolpd commented 7 years ago

Hi guys, Did you guys ever looked into this. I am not using instanbul, and i am getting the same issue, as soon as a test injects uses this framework breakpoints are broken. Did you guys ever created a solution for this?

plasticine commented 7 years ago

@ricardolpd Looked into it, yes. But no solution—still an open issue.

vladimir-tikhonov commented 7 years ago

I've added sourcemaps support in #36. Could someone give it a try?

vladimir-tikhonov commented 7 years ago

Fixed by #36

ricardolpd commented 7 years ago

@vladimir-tikhonov thanks for adding support for sourcemaps. I just done a couple tests with my existent config: It seems that breakpoints tests in my test folder are working now, however in my production code folder they only seem to work (not reliably though), if i add the breakpoint in the webpack output file and in same place in the original file.

I am using webpack 2.x + mocha, in vs code with node v7.5.0. the sourcemaps i am using in webpack is devtool: source-map.

I would love to have this working so i am happy to keep debuging as much as possible, to see if this is a config issue on my side or something else, that could probably help down the line.

vladimir-tikhonov commented 7 years ago

@ricardolpd are you sure that this issues are caused by inject-loader? Can you try to remove it and check if this solves the issue? Webpack itself has a lot of problems with sourcemaps (especially when combined with UglifyJSPlugin), check this link for details - https://github.com/webpack/webpack/issues/2145

ricardolpd commented 7 years ago

@vladimir-tikhonov, yes i agree with you, sourcemaps and webpack are not amazing:(. But yeah i think i am pretty sure its caused by inject-loader. I have had split test with folders with test scripts where i use inject-loader, and where i dont. And if the tests dont have a inject-loader import i dont see the issues i mention above. Also i am not using uglifyJSPlugin. I really would like to see this working but my knowledge with sourcemaps and webpack is not amazing (far from it). So if there is anything u need to help i am happy to oblige.

Also i am using node debugger with vscode, the only sourcemaps that work with vscode and webstorm (tools used at work, is the 'source-map', nothing else).