Skip to content

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

Closed

Description

@aminanan

馃悶 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?

Activity

  1. alan-agius4 commented on Oct 11, 2019

    @alan-agius4
    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
  2. aminanan commented on Oct 11, 2019

    @aminanan
    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.

  3. 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[/+] on Oct 11, 2019
  4. aminanan commented on Oct 31, 2019

    @aminanan
    Author

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

  5. aminanan commented on Nov 5, 2019

    @aminanan
    Author

    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.

  6. aminanan commented on Nov 24, 2019

    @aminanan
    Author

    Hi @alan-agius4. Can you please tell me if there is any progress on the issue ? I鈥檓 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.

  7. added
    needs: investigationRequires some digging to determine if action is needed
    and removed on Nov 26, 2019
  8. alan-agius4 commented on Nov 26, 2019

    @alan-agius4
    Collaborator

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

  9. modified the milestone: Backlog on Dec 2, 2019
  10. alan-agius4 commented on Dec 2, 2019

    @alan-agius4
    Collaborator

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


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

  11. ceefour commented on Apr 6, 2020

    @ceefour

    @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".

  12. angular-automatic-lock-bot commented on Dec 6, 2020

    @angular-automatic-lock-bot

    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.

  13. locked and limited conversation to collaborators on Dec 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions