Skip to content

Commit 81be0ca

Browse files
znckhaoqunjiang
authored andcommitted
fix: generate correct source-map when content is not padded (#52)
1 parent 7aeafec commit 81be0ca

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

lib/parse.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SourceMapGenerator } from 'source-map'
12
import {
23
RawSourceMap,
34
VueTemplateCompiler,
@@ -6,7 +7,6 @@ import {
67

78
const hash = require('hash-sum')
89
const cache = require('lru-cache')(100)
9-
const { SourceMapGenerator } = require('source-map')
1010

1111
const splitRE = /\r?\n/g
1212
const emptyRE = /^(?:\/\/)?\s*$/
@@ -48,7 +48,7 @@ export function parse(options: ParseOptions): SFCDescriptor {
4848
source,
4949
filename = '',
5050
compiler,
51-
compilerParseOptions = { pad: 'line' },
51+
compilerParseOptions = { pad: 'line' } as VueTemplateCompilerParseOptions,
5252
sourceRoot = '',
5353
needMap = true
5454
} = options
@@ -62,7 +62,8 @@ export function parse(options: ParseOptions): SFCDescriptor {
6262
filename,
6363
source,
6464
output.script.content,
65-
sourceRoot
65+
sourceRoot,
66+
compilerParseOptions.pad
6667
)
6768
}
6869
if (output.styles) {
@@ -72,7 +73,8 @@ export function parse(options: ParseOptions): SFCDescriptor {
7273
filename,
7374
source,
7475
style.content,
75-
sourceRoot
76+
sourceRoot,
77+
compilerParseOptions.pad
7678
)
7779
}
7880
})
@@ -86,19 +88,28 @@ function generateSourceMap(
8688
filename: string,
8789
source: string,
8890
generated: string,
89-
sourceRoot: string
91+
sourceRoot: string,
92+
pad?: 'line' | 'space'
9093
): RawSourceMap {
9194
const map = new SourceMapGenerator({
9295
file: filename.replace(/\\/g, '/'),
9396
sourceRoot: sourceRoot.replace(/\\/g, '/')
9497
})
98+
let offset = 0
99+
if (!pad) {
100+
offset =
101+
source
102+
.split(generated)
103+
.shift()!
104+
.split(splitRE).length - 1
105+
}
95106
map.setSourceContent(filename, source)
96107
generated.split(splitRE).forEach((line, index) => {
97108
if (!emptyRE.test(line)) {
98109
map.addMapping({
99110
source: filename,
100111
original: {
101-
line: index + 1,
112+
line: index + 1 + offset,
102113
column: 0
103114
},
104115
generated: {
@@ -108,5 +119,5 @@ function generateSourceMap(
108119
})
109120
}
110121
})
111-
return map.toJSON()
122+
return JSON.parse(map.toString())
112123
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"postcss": "^7.0.7",
6060
"postcss-selector-parser": "^5.0.0",
6161
"prettier": "1.16.0",
62-
"source-map": "^0.7.3",
62+
"source-map": "~0.6.1",
6363
"vue-template-es2015-compiler": "^1.6.0"
6464
}
6565
}

yarn.lock

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4725,6 +4725,11 @@ source-map@0.1.x:
47254725
dependencies:
47264726
amdefine ">=0.0.4"
47274727

4728+
source-map@0.6.1, source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1:
4729+
version "0.6.1"
4730+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
4731+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
4732+
47284733
source-map@^0.4.2:
47294734
version "0.4.4"
47304735
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"
@@ -4737,16 +4742,6 @@ source-map@^0.5.3, source-map@^0.5.6, source-map@^0.5.7, source-map@~0.5.1:
47374742
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
47384743
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
47394744

4740-
source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1:
4741-
version "0.6.1"
4742-
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
4743-
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
4744-
4745-
source-map@^0.7.3:
4746-
version "0.7.3"
4747-
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
4748-
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
4749-
47504745
spdx-correct@^3.0.0:
47514746
version "3.1.0"
47524747
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.0.tgz#fb83e504445268f154b074e218c87c003cd31df4"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy