KeJunMao / unplugin-preprocessor-directives

preprocessor directives for jsx,tsx,js,ts,html,css,vue and more
MIT License
63 stars 8 forks source link

Sourcemaps always report position 0 #8

Open smcenlly opened 5 months ago

smcenlly commented 5 months ago

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

https://github.com/KeJunMao/unplugin-preprocessor-directives/blob/667aca04e39db449f4fe533e10f22d1dc7054735/src/core/context/index.ts#L88

  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.

cookabc commented 3 months ago

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

https://github.com/KeJunMao/unplugin-preprocessor-directives/blob/667aca04e39db449f4fe533e10f22d1dc7054735/src/core/context/index.ts#L88

  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 commented 3 months ago

@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 commented 3 months ago

@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