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

Sourcemaps always report position 0 #8

Open
smcenlly opened this issue Apr 4, 2024 · 4 comments
Open

Sourcemaps always report position 0 #8

smcenlly opened this issue Apr 4, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@smcenlly
Copy link

smcenlly commented Apr 4, 2024

In this function the sourcemap positions are always returned as position 0. This breaks mapping back to the original source and coverage reporting.

transformWithMap(code: string, _id: string) {

  transformWithMap(code: string, _id: string) {
    const generated = this.transform(code, _id)
    if (generated) {
      const ms = new MagicString(code, { filename: _id })
      ms.overwrite(0, code.length, generated)
      return {
        code: ms.toString(),
        map: ms.generateMap({ hires: true }),
      }
    }
  }

A simpler reproducible example (effectively the same code):

import MagicString from 'magic-string';

const code = `const answer = 42;\n\nconsole.log("The answer is", answer);`;
const generated = code;

const ms = new MagicString(code, { filename: 'test.ts' })
ms.overwrite(0, code.length, generated)
console.log(ms.generateMap({ hires: true }));

Returns:

SourceMap {
  version: 3,
  file: undefined,
  sources: [ '' ],
  sourcesContent: undefined,
  names: [],
  mappings: 'AAAA;AAAA'
}

AAAA is position 0 for both statements.

@KeJunMao KeJunMao added bug Something isn't working good first issue Good for newcomers labels Apr 4, 2024
@cookabc
Copy link

cookabc commented May 29, 2024

In this function the sourcemap positions are always returned as position 0. This breaks mapping back to the original source and coverage reporting.

transformWithMap(code: string, _id: string) {

  transformWithMap(code: string, _id: string) {
    const generated = this.transform(code, _id)
    if (generated) {
      const ms = new MagicString(code, { filename: _id })
      ms.overwrite(0, code.length, generated)
      return {
        code: ms.toString(),
        map: ms.generateMap({ hires: true }),
      }
    }
  }

A simpler reproducible example (effectively the same code):

import MagicString from 'magic-string';

const code = `const answer = 42;\n\nconsole.log("The answer is", answer);`;
const generated = code;

const ms = new MagicString(code, { filename: 'test.ts' })
ms.overwrite(0, code.length, generated)
console.log(ms.generateMap({ hires: true }));

Returns:

SourceMap {
  version: 3,
  file: undefined,
  sources: [ '' ],
  sourcesContent: undefined,
  names: [],
  mappings: 'AAAA;AAAA'
}

AAAA is position 0 for both statements.

It seems adding if condition can fix this:

import MagicString from 'magic-string';

const code = `const answer = 42;\n\nconsole.log("The answer is", answer);`;
const generated = code;

const ms = new MagicString(code, { filename: 'test.ts' })
if (code !== generated) {
    ms.overwrite(0, code.length, generated);
}
console.log(ms.generateMap({ hires: true }));

Returns:

SourceMap {
    version: 3,
    file: undefined,
    sources: [ '' ],
    sourcesContent: undefined,
    names: [],
    mappings: 'AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC'
}

@smcenlly
Copy link
Author

@cookabc - it's not really a fix... it's a fix for the case where the transformed code is the same as the original. I expect in this case, the unplugin processor hasn't done the work it may have needed to do.

Regardless, it's probably a good interim fix for when unplugin doesn't modify the code but it doesn't address the issue for when unplugin does modify the code.

@cookabc
Copy link

cookabc commented May 29, 2024

@cookabc - it's not really a fix... it's a fix for the case where the transformed code is the same as the original. I expect in this case, the unplugin processor hasn't done the work it may have needed to do.

Regardless, it's probably a good interim fix for when unplugin doesn't modify the code but it doesn't address the issue for when unplugin does modify the code.

After some investigation, I believe this might be a rather difficult one than a good first issue

@KeJunMao KeJunMao removed the good first issue Good for newcomers label Aug 21, 2024
@tluchowski
Copy link

Is there any plan to fix this soon? Interestingly enough, a second plugin I found that provides similar functionality, has this problem too: LZS911/vite-plugin-conditional-compile#5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants