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

Chrome: SourceMap links not working #289

Open
spyro2000 opened this issue Feb 1, 2022 · 7 comments
Open

Chrome: SourceMap links not working #289

spyro2000 opened this issue Feb 1, 2022 · 7 comments
Labels

Comments

@spyro2000
Copy link

spyro2000 commented Feb 1, 2022

Hi there,

loving this library, using it every day with pure joy! :)

One of the coolest features might be the generated links to jump directly to the source. For some reason, that doesn't work for me. Clicking on one of the links just opens a new tab to about:blank instead:

image

(after clicking on the link)

image

Manually, I can open the file in the DevTools just fine:

image

Angular version 13.0.2:
ngx-logger version: 5.0.7
Chrome version: 97.0.4692.99 (running in debug mode without any plugins enabled)

Configuration (in app.module.ts):

imports: [
...
HttpClientModule,
LoggerModule.forRoot({
  level: environment.production ? NgxLoggerLevel.INFO : NgxLoggerLevel.DEBUG,
  serverLogLevel: NgxLoggerLevel.OFF,
  timestampFormat: 'HH:mm:ss.SSS',
  enableSourceMaps:true
})
]
  • I started angular dev environment with ng serve

Build config from angular.json:

    {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/styles"
              ]
            },
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss"
            ],
            "scripts": [],
            "vendorChunk": true,
            "extractLicenses": false,
            "buildOptimizer": false,
            "sourceMap": true,
            "optimization": false,
            "namedChunks": true
          }

Did I miss something?

Thank you!

@sylvain-gaborit
Copy link

hello,

I have the same problem.

Angular version 13.2.0:
ngx-logger version: 5.0.7
Chrome version: 98.0.4758.102

@spyro2000
Copy link
Author

Is this lib still maintained...?

@bmtheo
Copy link
Collaborator

bmtheo commented Feb 17, 2022

Yes it is but I am the only active maintainer for now

I am open to PR if you have time

About this issue, is the location of the log line correct ? (In your example "./src/app/core/components/.../grid-settings-store.service.ts:46:0")

@bmtheo bmtheo added the bug label Feb 17, 2022
@spyro2000
Copy link
Author

Hello and thank you. Yes, the position is correct only the link itself is not working.

@BenniG82
Copy link

BenniG82 commented Mar 26, 2022

This works for me:

Extend the NGXLoggerWriterService and replace prepareMetaString with your own implementation that removes the leading "." and adds 'webpack://'.

import { Injectable } from '@angular/core';
import { INGXLoggerConfig, INGXLoggerMetadata, NgxLoggerLevel, NGXLoggerWriterService } from 'ngx-logger';

@Injectable({
  providedIn: 'root'
})
export class MyLoggerWriterService extends NGXLoggerWriterService {
 protected prepareMetaString(metadata: INGXLoggerMetadata, config: INGXLoggerConfig): string {
    const fileName = metadata.fileName?.charAt(0) === '.' ? 'webpack://' + metadata.fileName?.substring(1) : metadata.fileName;
    return super.prepareMetaString({ ...metadata, fileName }, config);
  }
}

Configure ngx-logger to use it:

LoggerModule.forRoot({/* your log-config here */}, {
      writerProvider: { provide: TOKEN_LOGGER_WRITER_SERVICE, useClass: MyLoggerWriterService }
    }),

Maybe there is a better way, but this works (at least on my machine :D )
How I came up with webpack://? Thats what I saw, when hovering over the source of console.log statements:
image

Sample output:

2022-03-26T13:22:56.331Z TRACE [webpack:///src/app/core/services/auth/authentication.service.ts:77:0] Token is still valid

@spyro2000
Copy link
Author

This works!! So many thanks!

So together with the fix for the missing linebreak before the actual message the writer should look like this:

protected override prepareMetaString(metadata: INGXLoggerMetadata, config: INGXLoggerConfig): string {
    // Fix link to the source file in the console
    const fileName = metadata.fileName?.charAt(0) === '.'
      ? 'webpack://' + metadata.fileName?.substring(1)
      : metadata.fileName

    // We want to start the actual log message on a new line (after level + source link)
    return super.prepareMetaString({ ...metadata, fileName }, config) + '\n'
  }

(For some reason there is still an additional space added before the start of the actual message but it's already way more readable).

@ThisIsIvan
Copy link

Above method does not work for Angular 16.
To correctly parse the SourceMap I overwrote NGXLoggerMapperService::getMapping to use source-map-js.

  private getMapping(sourceMap: SourceMapPayload, position: INGXLoggerLogPosition): INGXLoggerLogPosition {
    try {
      const rawSourceMap: RawSourceMap = {
        file: sourceMap.file,
        mappings: sourceMap.mappings,
        names: sourceMap.names,
        sourceRoot: sourceMap.sourceRoot,
        sources: sourceMap.sources,
        sourcesContent: sourceMap.sourcesContent as string[],
        version: `${sourceMap.version}`,
      };
      const sourceMapConsumer = new SourceMapConsumer(rawSourceMap);
      const mappedPosition: MappedPosition = sourceMapConsumer.originalPositionFor({
        line: Number(position.lineNumber || 1),
        column: Number(position.columnNumber || 1),
      });
      return { fileName: mappedPosition.source, lineNumber: mappedPosition.line, columnNumber: mappedPosition.column };
    } catch (e) {
      return { fileName: 'unknown', lineNumber: 0, columnNumber: 0 };
    }
  }

If anyone wants to create a PR go ahead.

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

No branches or pull requests

5 participants