Skip to content

Javascript files are mapped to TypeScript compiled code instead of the original code #15804

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

Closed
aminanan opened this issue Oct 10, 2019 · 9 comments · Fixed by #19114
Closed

Comments

@aminanan
Copy link

🐞 Bug report

Command (mark with an x)

- [ ] new
- [ ] build
- [x] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Description

Javascript files are mapped to transpiled code instead of the original code.

🔬 Minimal Reproduction

Create new app


ng new testsourcemap
cd testsourcemap

Add '"allowJs": true' in compilerOptions section of tsconfig.json


{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "allowJs": true,
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

Create file testjs.js in src/app


function add(a,b){
  return a+b;
}

export {
  add
};

Change app.component.ts to


import { Component } from '@angular/core';
import * as test from './testjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'testsourcemap';
  
  constructor() {
    let add = test.add;
  }
}

🔥 Exception or Error

The mapped file 'webpack:///./src/app/testjs.js' under Chrome shows the following code




import * as tslib_1 from "tslib";
function add(a, b) {
    return a + b;
}
export { add };
//# sourceMappingURL=testjs.js.map

🌍 Your Environment




Angular CLI: 8.3.8
Node: 10.15.3
OS: win32 x64
Angular: 8.2.10
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.8
@angular-devkit/build-angular     0.803.8
@angular-devkit/build-optimizer   0.803.8
@angular-devkit/build-webpack     0.803.8
@angular-devkit/core              8.3.8
@angular-devkit/schematics        8.3.8
@angular/cli                      8.3.8
@ngtools/webpack                  8.3.8
@schematics/angular               8.3.8
@schematics/update                0.803.8
rxjs                              6.4.0
typescript                        3.5.3
webpack                           4.39.2

Anything else relevant?

@alan-agius4
Copy link
Collaborator

Hi, I tried this and I cannot reproduce. I am not sure from where that import from tslib came from however, in your sourcemap, I can see the export which does mean that it's pointing to the original code.

Here what I got by following your reproduction steps

Sourcemap contents

function add(a, b) {
    return a + b;
}
export { add };
//# sourceMappingURL=test.js.map

Actual transpiled JS contents

/*! exports provided: add */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "add", function() { return add; });
function add(a, b) {
    return a + b;
}
//# sourceMappingURL=test.js.map

@alan-agius4 alan-agius4 added the needs: more info Reporter must clarify the issue label Oct 11, 2019
@aminanan
Copy link
Author

Hi, by transpiled code I mean the code compiled by Typescript and not the final code generated in main.js. If the Sourcemap content above was the original code, why there is the added code below in the last line.
//# sourceMappingURL=test.js.map
Also, the empty line before the export is removed compared to the original code.

@aminanan aminanan changed the title Javascript files are mapped to transpiled code instead of the original code Javascript files are mapped to TypeScript compiled code instead of the original code Oct 11, 2019
@aminanan
Copy link
Author

Hi, did you need more info, or the issue is clear now?

@aminanan
Copy link
Author

aminanan commented Nov 5, 2019

To better show the difference between the original file and the TypeScript compiled file, I added async to the add function in the testjs.js file above, as follows:

async function add(a,b){
  return a+b;
}

export {
  add
}

The sourcemap contents becomes

import * as tslib_1 from "tslib";
function add(a, b) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        return a + b;
    });
}
export { add };
//# sourceMappingURL=testjs.js.map

Here we can see that the sourcemap is mapped to the TypeScript compiled file and not the original file. If we change the file name extension to testjs.ts, instead of testjs.js, we get exactlly the content of the original file.

@aminanan
Copy link
Author

Hi @alan-agius4. Can you please tell me if there is any progress on the issue ? I’m integration the PDF.js viewer (written in JavaScript) in an Angular project. When debugging with Chrome DevTools I can see the TypeScript compiled code and not the original files and there is a big difference between the two versions compared to the simple examples above. The problem is when debugging with Visual Studio 2017. In this case, it is impossible to step over the code since Visual studio shows the original code but steps over the Typescript compiled code (i.e. breakpoints cannot be set or sometimes can be set over comments).

Thanks.

@alan-agius4 alan-agius4 added needs: investigation Requires some digging to determine if action is needed and removed needs: more info Reporter must clarify the issue labels Nov 26, 2019
@alan-agius4
Copy link
Collaborator

Hi @Hakimon, unfortunately we did have time yet to investigate the issue.

@alan-agius4 alan-agius4 added area: @angular-devkit/build-angular freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix and removed needs: investigation Requires some digging to determine if action is needed labels Dec 2, 2019
@ngbot ngbot bot modified the milestone: Backlog Dec 2, 2019
@alan-agius4
Copy link
Collaborator

The issue seems to be caused by these 2 test patterns, which doesn't cover js files.


module: { rules: [{ test: /\.tsx?$/, loader: NgToolsLoader }] },

@ceefour
Copy link

ceefour commented Apr 6, 2020

@aminanan I also get this issue, but in Create-React-App with TypeScript.

I have tried "allowJs": false but problem persists:

image

(note: my case may or may not be relevant to this issue, but I hope it helps to know that this problem is real)

One noticeable thing is the file is buildQuery.ts but it's clearly transpiled code, not original TypeScript. Additionally it said "source mapped from 0.chunk.js".

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants