vue-typescript-debugger icon indicating copy to clipboard operation
vue-typescript-debugger copied to clipboard

Issue with getIndexOfScriptTag

Open starrin opened this issue 3 years ago • 1 comments

Thank you very much with your blogpost! I followed your steps and it works like a charm with my project, mostly. But not all files got accurate sourcemap, some files got a deviation of one line. After some debugging I found

function getIndexOfScriptTag(sourceFile) {
  const lines = sourceFile.match(/.+/g);

This match will ignore all empty lines in the file, which will cause trouble. And the offset length isn't accurate, the result should minus 1 before return. Your example file got a empty line between template section and script section, which accidently give you an accurate result. But if you create a .vue file with no empty line between template section and script section, or even with some empty lines in template section, the generated source map will got a deviation.

I think this function should be modified like this:

function getIndexOfScriptTag(sourceFile) {
    const lines = sourceFile.split('\n');
    let indexOfScriptTag = 0;

    for (const line of lines) {
        ++indexOfScriptTag;
        if (/<script/.test(line)) break;
    }

    return indexOfScriptTag - 1;
}

starrin avatar Mar 02 '22 07:03 starrin

Hi @starrin, thanks a lot for bringing this to my attention and for going so far as to investigate what is causing the problem.

I'll see if I can come up with a solution that works both if an empty line is present between the two sections and if there isn't one at all.

Thanks again!

fearnycompknowhow avatar Mar 03 '22 15:03 fearnycompknowhow