KeJunMao/unplugin-preprocessor-directives

Sourcemaps always report position 0

smcenlly opened this issue · 4 comments

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.

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'
}

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

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