-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Vue.js source-map technique for Svelte source code #22
Comments
Would you like a PR? |
Thanks for raising this and digging into it. I'm a little confused by some parts of it — bear with me:
Sorry if this sounds unhelpful — it's possible I've misunderstood something. |
Hi @Rich-Harris , Yeah, I know this is a bit weird. There seems two be reasons why it doesn't work out of the box:
However, given that this issue hasn't surfaced in Angular 1 code, Angular "new" code or React code before in my experience, I would conclude that the problem seems related to the source map produce by Svelte. Additionally, there seemed to be no harm caused (in my test case) by removing those prototype functions. Perhaps this behaviour could be configured by a loader option?
The https://github.com/gotwarlost/istanbul/blob/master/lib/report/html.js#L410:
writeDetailPage: function (writer, node, fileCoverage) {
var opts = this.opts,
sourceStore = opts.sourceStore,
templateData = opts.templateData,
// Next line is the key line ---->
sourceText = fileCoverage.code && Array.isArray(fileCoverage.code) ?
fileCoverage.code.join('\n') + '\n' : sourceStore.get(fileCoverage.path),
code = sourceText.split(/(?:\r?\n)|\r/),
count = 0,
structured = code.map(function (str) { count += 1; return { line: count, covered: null, text: new InsertionText(str, true) }; }),
context Does that make more sense? If there is an easier way to get this working, I'd love to know! :) |
@Rich-Harris would you like a PR which adds an option - something like "useGeneratedSourceForMap" - to the loader? I'm really keen to get this solved so that I can take this to the rest of my company and clients. |
I just don't think the fixes are appropriate, given that they're so specific to this particular setup. The The sourcemap is generated by magic-string — you can see the class here. In the case of this loader we could generate a new POJO with the same properties, but I'd be curious to see if Istanbul has a valid case against accepting these objects. What about forking this project so that you can work around these issues, is that an option? |
Ok, so I've done some further research on how languages like CoffeeScript and Vue.js are working: CoffeeScriptKarma-coverage has CoffeeScript examples. These examples are using an instrumenter called ibrik to do the CoffeeScript source code coverage. This line is using Istanbul's Vue.jsVue.js appears to use a similar setup to what I'm trying to get working. I followed these instructions for setting up a basic Vue.js project. As I investigated further, I found some differences between Vue and Svelte:
https://github.com/vuejs/vue-loader/blob/master/lib/selector.js#L18 :
... where
This must preserve the mapping between the line numbers in the sourceMap and the source code. Does that give you more to work with, @Rich-Harris ? |
Svelte has added much better source map support recently though there's some work left to do in |
This issue relates to sveltejs/svelte#286, and having done some detective work, I think there is a simple solution.
Current Behaviour
When using Webpack 2 + svelte + svelte-loader + babel, everything is fine. Here's the relevant Webpack config that I'm using:
However, when it comes to generating code coverage and instrumentation (using Karma and karma-coverage (which uses Istanbul)), I keep running into this error:
The root cause seems to be here: https://github.com/sveltejs/svelte-loader/blob/master/index.js#L30
Babel(-plugin-istanbul) is parsing the
map
object, which it expects is a "plain object" (according to Lodash' definition). In this case,map
has a 2 prototype methods:toString()
andtoUrl()
.When I replace the above code with the following code, the error disappears:
There are two additional changes which are necessary to support Istanbul with HTML reports:
code
property is an array of the lines of generated-by-svelte code. This property can be used within the karma.config.js file (see below) to cause Istanbul to correctly show code coverage for the generated code. Istanbul CANNOT use the original source code with HTML elements in it.sourcesContent
property is changed to be the generated-by-svelte code. Again, this is because Istanbul is expecting JS as the source code, not a mixture of HTML and JS.Combined with the following karma-coverage config in
karma.config.js
, we now get accurate source maps for the generated code!The text was updated successfully, but these errors were encountered: