Skip to content
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

feat: add source maps to libraries bundle #144

Closed
wawyed opened this issue Jun 30, 2017 · 34 comments
Closed

feat: add source maps to libraries bundle #144

wawyed opened this issue Jun 30, 2017 · 34 comments
Milestone

Comments

@wawyed
Copy link
Contributor

wawyed commented Jun 30, 2017

It would be nice if the karma-typescript-bundle was mapped back to the original files. so when debugging in unit tests is easier to know where the errors come from.

@erikbarke
Copy link
Collaborator

Hey @wawyed, awesome idea! This shouldn't be too hard to implement, all the pieces are already there.

@erikbarke erikbarke added this to the 3.0.5 milestone Jul 1, 2017
@erikbarke erikbarke modified the milestones: 3.0.6, 3.0.5 Aug 1, 2017
erikbarke added a commit that referenced this issue Aug 12, 2017
@erikbarke
Copy link
Collaborator

There's basic support for source maps for the bundled packages now:

karmaTypescriptConfig: {
    bundlerOptions: {
        sourceMap: true
    }
}

To try it out you can install from the dev branch, npm i github:monounity/karma-typescript#next ☺️

@wawyed
Copy link
Contributor Author

wawyed commented Aug 13, 2017

You sir are on fire. I'll give it a try ^^

@wawyed
Copy link
Contributor Author

wawyed commented Aug 14, 2017

I just tried it, it works but it doesn't seem to keep the sourcemap comment for the original js. So if it had a map to typescript it doesn't show the original typescript code.

erikbarke added a commit that referenced this issue Aug 16, 2017
@erikbarke
Copy link
Collaborator

Source maps for the original code are now loaded (only first level) and it works for packages like es6-promise, rxjs, zone.js, I can step through the original source code. Packages like @angular and diff however, do not work, their source maps seem to be broken beyond belief 😠

@erikbarke
Copy link
Collaborator

@wawyed, have you tested the last commit, can I close this?

@wawyed
Copy link
Contributor Author

wawyed commented Aug 22, 2017

I'm trying to test it but Im getting this when trying to npm install the branch

npm ERR! code 128
npm ERR! Command failed: C:\Program Files\Git\cmd\git.EXE submodule update -q --init --recursive
npm ERR! fatal: No url found for submodule path 'examples/karma-ts-test-by-dts-file' in .gitmodules
npm ERR!

erikbarke added a commit that referenced this issue Aug 23, 2017
erikbarke added a commit that referenced this issue Aug 23, 2017
@erikbarke
Copy link
Collaborator

Oh great, I broke the git repo... I've cleaned up now and the build passes on the ci servers and I'm able to npm i the branch locally, does it work for you too now?

@wawyed
Copy link
Contributor Author

wawyed commented Aug 23, 2017

Yes, I got it working, unfortunately even though is mapping from the bundle to the original TS the line that it goes to when I'm setting a breaking point doesn't match the correct one.

@erikbarke
Copy link
Collaborator

Which package isn't working when you debug, a specific one or all? I banged my head on my desk for days before realizing that some packages simply have incorrect source maps, I verified by loading a few packages directly in Chrome:

<!DOCTYPE html>
<html>
<body>
    <script src="node_modules/@angular/core/bundles/core.umd.js"></script>
    <script src="node_modules/diff/dist/diff.js"></script>
    <script src="node_modules/es6-promise/dist/es6-promise.js"></script>
    <script>
        console.log("try setting a break point");
    </script>
</body>
</html>

Put this html file in the root of, for instance, the angular2 example project and open the html file in Chrome. The angular file will crash and you might have to hit reload before the source maps load but it's still possible to set break points.

Now add import "es6-promise"; somewhere in the exampe project and run the test project and try setting break points.

This is the result I get with Chrome on OSX:

  • angular v4.3.4, break points are off in both the simple test and in karma-typescript, weird packages paths
  • diff v3.3.0, seems completely broken in both the simple test and in karma-typescript (src/convert/xml.js??)
  • es6-promise v4.1.1, works in both the simple test and in karma-typescript

The pattern I see is if it works when loading a source map directly in Chrome, it will work in karma-typescript as well.

It seems some vendors have a broken build chain where they transpile with source maps first and then they add umd wrappers without updating the source map, which throws the line numbers off.

@wawyed
Copy link
Contributor Author

wawyed commented Aug 24, 2017

The package I used as a test is one of our private npm package and I can validate that the sourcemaps are correctly generated as they work fine when I run the app.

@erikbarke
Copy link
Collaborator

New approach: I've modified one of the typescript example projects to add the package karma-typescript-es6-transform (written in Typescript, transpiled with source maps) to the bundle.

Debugging this project works as expected, I can set a breakpoint in src/hello.component.ts and step through the code with correct line mappings.

Does it work if you install from the sourcemap branch, npm i github:monounity/karma-typescript#sourcemaps and debug the typescript-latest project or are the line numbers off? Is this an os/environment issue?

In your project, are the line numbers off by one or by a lot, or does the discrepancy grow as the line numbers grow?

@erikbarke
Copy link
Collaborator

@trwolfe13 @Salasar @patrickarlt, I would love some testing outside my environment for this, could you guys help me out please?

@wawyed
Copy link
Contributor Author

wawyed commented Sep 5, 2017

Hi, sorry about the delay in the reply. I have tried the branch and I'm getting mixed results. Some ts files are getting remapped but others just show an empty file while trying to load the ts file.

@erikbarke
Copy link
Collaborator

Ok, you should get the same results for your project with the next and the sourcemaps branches, they both contain the same source map logic.

Does the typescript-latest example project on the sourcemaps branch work, are you able to set a breakpoint in src/hello.component.ts and step into the code for karma-typescript-es6-transform? Are the line mappings correct for the (one and only) Typescript file in the karma-typescript-es6-transform package?

@wawyed
Copy link
Contributor Author

wawyed commented Sep 5, 2017

Yes, that seems to work correctly. One difference that I didn't mention, my compiled code uses inlineSources in the sourceMap so it doesn't point to a different file, contains the original code inside the sourcemap file.

@erikbarke
Copy link
Collaborator

Great, so it's not an os/environment issue then... Which leaves us with what? If you look at source-map.ts, there's nothing special going on in my code; file content is fed to combine-source-map which performs the actual magic.

I have yet to find a correct source map that doesn't work in karma-typescript, apart from your source maps, which I can't test since it's a private package.

I'm out of ideas here 😕

@wawyed
Copy link
Contributor Author

wawyed commented Sep 7, 2017

I'm going to try to reproduce it in your example.

@wawyed
Copy link
Contributor Author

wawyed commented Sep 11, 2017

Okay so I have created an example that you can reproduce :)

You can find it here:

https://github.com/wawyed/karma-typescript/tree/sourcemaps

Hope it helps!

If you look at MhrWindowService you will see that the original ts file is shown as an empty file.

@erikbarke
Copy link
Collaborator

Awesome!! Your source maps have sources that look like this:

[
    'webpack:///webpack/bootstrap a5a1de700b3a78264108',
    'webpack:///./src/services/index.ts',
    'webpack:///external "@angular/core"',
    'webpack:///./index.ts',
    'webpack:///./src/index.ts',
    'webpack:///./src/services/MhrWindow/MhrWindow.service.ts',
    'webpack:///./src/mhrCommon.module.ts',
    'webpack:///external "@angular/common"'
]

The webpack:///prefix apparently makes combine-source-map create non-working source maps, so I created a fix that simply removes the prefix, you can try it out on the nextbranch, npm i github:monounity/karma-typescript#next 🚀

@wawyed
Copy link
Contributor Author

wawyed commented Sep 12, 2017

Hello, good news is that the files are not blank anymore :D. Bad news... the lines seem offset by a few lines.

@wawyed
Copy link
Contributor Author

wawyed commented Sep 12, 2017

I found this tool to check sourcemaps visually. Maybe it can help you find out what's wrong:

http://sokra.github.io/source-map-visualization/

Seems to have a limitation on the number of lines it can show

sokra/source-map-visualization#21

so might not be that useful.

@erikbarke
Copy link
Collaborator

Darn.

I loaded @ mhrwebux/common/index.js and its source map from your tar ball into the visualization tool you linked and inspected row 127-129 of index.js:

function _window() {
    return window;
}

Row 128 (return window;) points back to row 4 in webpack:///src/services/MhrWindow/MhrWindow.service.ts [4->4:2] [10->4:8] [11->4:9] [17->4:15] [18->4:16].

I then launched the typescript-latest project from your fork, with the fix for empty files, and located the function _window() { part in the bundle (around line 22000 in my case). If I set a breakpoint on the return window; row in Chrome, the mapped file MhrWindow.service.ts opens with a breakpoint on row 4 which contains return window;.

Row 162 of index.js, var core_1 = __webpack_require__(1); points back to row 2 in webpack:///src/mhrCommon.module.ts [0->2:0]. If I locate that row in the bundle and set a breakpoint, the mapped file mhrCommon.module.ts opens with a breakpoint on row 2 which contains import { NgModule } from '@angular/core';.

This seems pretty accurate and consistent to me, could you give me an example of a file and a row in your fork where the source mapping is off please?

I suspect there is more webpack magic hidden in your source maps 😕

@wawyed
Copy link
Contributor Author

wawyed commented Sep 12, 2017

If I try to put a breakpoint on the getter function it adds it to the class definition, have a look at this:

image

@erikbarke
Copy link
Collaborator

Aah, windows line breaks, the gift from the dot-matrix printers era that keeps on giving ☺️

If I hardcode \n as line breaks in the bundler instead of os.EOL (The Right Way™) the source maps seem to work correctly also on windows. This is clearly a CRLF issue, I'll see if I can come up something less hacky.

@wawyed
Copy link
Contributor Author

wawyed commented Sep 13, 2017

I was going to say it might be something related to line endings since you didn't seem to have issues.

Could you do something like?

var nl = (process.platform === 'win32' ? '\r\n' : '\n')

@erikbarke
Copy link
Collaborator

This is exactly what os.EOL does, it returns the correct newline for each platform, ie \r\n on windows. What I did was using a hardcoded \n in the bundler regardless of platform, which made the source maps work correctly, go figure 😕

@wawyed
Copy link
Contributor Author

wawyed commented Sep 13, 2017

hmm, from what I can see the sourcemap gets converted into a base64 string that gets read by chrome, so maybe chrome wants linux endings regardless?

@erikbarke
Copy link
Collaborator

I'm using the fix the hardcoded unix line breaks, it doesn't seem to affect anything else but the source maps, the fix is on the next branch, npm i github:monounity/karma-typescript#next!

@wawyed
Copy link
Contributor Author

wawyed commented Sep 14, 2017

So, that fixes the issue :)... unfortunately, it seems that there is some issue with the sourcemaps of the src or test files. I don't seem to be able to set a breakpoint in some statements, specially if I have new lines in between.

The crossed lines are the ones I'm not able to add a breakpoint.

image

But if i remove the line lines..

image

Works fine.

Don't know if it's a chrome thing or what.

@erikbarke
Copy link
Collaborator

This must be a Chrome issue, I get the same behavior on windows with and without the latest line break fix. Also, the new source maps logic only affects the main bundle, the project ts files still use the inline source maps generated by Typescript.

Anyway, I managed to set breakpoints in Chrome on windows by setting them in the compiled file and the breakpoints end on the correct lines, so it's a Chrome issue I guess:

screen shot 2017-09-14 at 16 33 59

@erikbarke
Copy link
Collaborator

Ok, case closed then ☺️

@wawyed
Copy link
Contributor Author

wawyed commented Sep 18, 2017

Releasing the new version soon? :)

@erikbarke
Copy link
Collaborator

3.0.6 is on npm now 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants