Skip to content

Commit 2af9f68

Browse files
committed
remove source-map generation from sfc parser
1 parent cf1a697 commit 2af9f68

File tree

10 files changed

+56
-114
lines changed

10 files changed

+56
-114
lines changed

.flowconfig

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ module.name_mapper='^shared/\(.*\)$' -> '<PROJECT_ROOT>/src/shared/\1'
1717
module.name_mapper='^web/\(.*\)$' -> '<PROJECT_ROOT>/src/platforms/web/\1'
1818
module.name_mapper='^server/\(.*\)$' -> '<PROJECT_ROOT>/src/server/\1'
1919
module.name_mapper='^entries/\(.*\)$' -> '<PROJECT_ROOT>/src/entries/\1'
20+
module.name_mapper='^sfc/\(.*\)$' -> '<PROJECT_ROOT>/src/sfc/\1'

build/alias.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ module.exports = {
77
shared: path.resolve(__dirname, '../src/shared'),
88
web: path.resolve(__dirname, '../src/platforms/web'),
99
server: path.resolve(__dirname, '../src/server'),
10-
entries: path.resolve(__dirname, '../src/entries')
10+
entries: path.resolve(__dirname, '../src/entries'),
11+
sfc: path.resolve(__dirname, '../src/sfc')
1112
}

build/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var builds = [
6565
{
6666
entry: 'src/entries/web-compiler.js',
6767
format: 'cjs',
68-
external: ['entities', 'de-indent', 'source-map'],
68+
external: ['entities'],
6969
out: 'packages/vue-template-compiler/index.js'
7070
},
7171
// Web server renderer (CommonJS).

flow/compiler.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,5 @@ declare type SFCBlock = {
140140
end?: number,
141141
lang?: string,
142142
src?: string,
143-
scoped?: boolean,
144-
map?: Object
143+
scoped?: boolean
145144
}

flow/modules.js

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ declare module 'entities' {
33
declare function decodeHTML(html: string): string;
44
}
55

6-
declare module 'de-indent' {
7-
declare var exports: {
8-
(str: string): string;
9-
}
10-
}
11-
126
declare module 'source-map' {
137
declare class SourceMapGenerator {
148
setSourceContent(filename: string, content: string): void;

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"chromedriver": "^2.21.2",
5353
"codecov.io": "^0.1.6",
5454
"cross-spawn": "^4.0.0",
55-
"de-indent": "^1.0.2",
5655
"entities": "^1.1.1",
5756
"eslint": "^2.11.0",
5857
"eslint-config-vue": "^1.0.3",
@@ -81,7 +80,6 @@
8180
"rollup-plugin-babel": "^2.4.0",
8281
"rollup-plugin-replace": "^1.1.0",
8382
"selenium-server": "2.53.0",
84-
"source-map": "^0.5.6",
8583
"uglify-js": "^2.6.2",
8684
"webpack": "^1.13.1"
8785
}

src/entries/web-compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { extend } from 'shared/util'
22
import { compile as baseCompile, baseOptions } from 'web/compiler/index'
33
import { detectErrors } from 'compiler/error-detector'
44

5-
export { parseComponent } from 'compiler/parser/sfc-parser'
5+
export { parseComponent } from 'sfc/parser'
66
export { compileToFunctions } from 'web/compiler/index'
77

88
export function compile (

src/sfc/deindent.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* @flow */
2+
3+
const splitRE = /\r?\n/g
4+
const emptyRE = /^\s*$/
5+
const needFixRE = /^(\r?\n)*[\t\s]/
6+
7+
export default function deindent (str: string): string {
8+
if (!needFixRE.test(str)) {
9+
return str
10+
}
11+
const lines = str.split(splitRE)
12+
let min = Infinity
13+
let type, cur, c
14+
for (let i = 0; i < lines.length; i++) {
15+
var line = lines[i]
16+
if (!emptyRE.test(line)) {
17+
if (!type) {
18+
c = line.charAt(0)
19+
if (c === ' ' || c === '\t') {
20+
type = c
21+
cur = count(line, type)
22+
if (cur < min) {
23+
min = cur
24+
}
25+
} else {
26+
return str
27+
}
28+
} else {
29+
cur = count(line, type)
30+
if (cur < min) {
31+
min = cur
32+
}
33+
}
34+
}
35+
}
36+
return lines.map(line => line.slice(min)).join('\n')
37+
}
38+
39+
function count (line: string, type: string): number {
40+
var i = 0
41+
while (line.charAt(i) === type) {
42+
i++
43+
}
44+
return i
45+
}

src/compiler/parser/sfc-parser.js src/sfc/parser.js

+4-42
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
/* @flow */
22

3-
// this file is used in the vue-template-compiler npm package
4-
// and assumes its dependencies and a Node/CommonJS environment
5-
import deindent from 'de-indent'
6-
import { SourceMapGenerator } from 'source-map'
7-
8-
import { parseHTML } from './html-parser'
3+
import deindent from './deindent'
4+
import { parseHTML } from 'compiler/parser/html-parser'
95
import { makeMap } from 'shared/util'
106

117
const splitRE = /\r?\n/g
12-
const emptyRE = /^(?:\/\/)?\s*$/
138
const isSpecialTag = makeMap('script,style,template', true)
149

1510
type Attribute = {
@@ -82,50 +77,17 @@ export function parseComponent (
8277
text = padContent(currentBlock) + text
8378
}
8479
currentBlock.content = text
85-
if (options.map && !currentBlock.src) {
86-
addSourceMap(currentBlock)
87-
}
8880
currentBlock = null
8981
}
9082
depth--
9183
}
9284

9385
function padContent (block: SFCBlock) {
86+
const offset = content.slice(0, block.start).split(splitRE).length
9487
const padChar = block.type === 'script' && !block.lang
9588
? '//\n'
9689
: '\n'
97-
return Array(getPaddingOffset(block) + 1).join(padChar)
98-
}
99-
100-
function getPaddingOffset (block: SFCBlock) {
101-
return content.slice(0, block.start).split(splitRE).length - 1
102-
}
103-
104-
function addSourceMap (block: SFCBlock) {
105-
const filename = options.map.filename
106-
/* istanbul ignore if */
107-
if (!filename) {
108-
throw new Error('Should provide original filename in the map option.')
109-
}
110-
const offset = options.pad ? 0 : getPaddingOffset(block)
111-
const map = new SourceMapGenerator()
112-
map.setSourceContent(filename, content)
113-
block.content.split(splitRE).forEach((line, index) => {
114-
if (!emptyRE.test(line)) {
115-
map.addMapping({
116-
source: filename,
117-
original: {
118-
line: index + 1 + offset,
119-
column: 0
120-
},
121-
generated: {
122-
line: index + 1,
123-
column: 0
124-
}
125-
})
126-
}
127-
})
128-
block.map = JSON.parse(map.toString())
90+
return Array(offset).join(padChar)
12991
}
13092

13193
parseHTML(content, {
+1-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { parseComponent } from 'compiler/parser/sfc-parser'
2-
import { SourceMapConsumer } from 'source-map'
1+
import { parseComponent } from 'sfc/parser'
32

43
describe('Single File Component parser', () => {
54
it('should parse', () => {
@@ -64,61 +63,4 @@ describe('Single File Component parser', () => {
6463
expect(res.script.content).toBe(Array(3 + 1).join('//\n') + '\nexport default {}\n')
6564
expect(res.styles[0].content).toBe(Array(6 + 1).join('\n') + '\nh1 { color: red }\n')
6665
})
67-
68-
it('source map (without pad)', () => {
69-
const res = parseComponent(`
70-
<script>
71-
export default {}
72-
</script>
73-
<style>
74-
h1 { color: red }
75-
</style>
76-
`.trim(), {
77-
map: {
78-
filename: 'test.vue'
79-
}
80-
})
81-
const scriptConsumer = new SourceMapConsumer(res.script.map)
82-
const scriptPos = scriptConsumer.originalPositionFor({
83-
line: 2,
84-
column: 1
85-
})
86-
expect(scriptPos.line).toBe(2)
87-
88-
const styleConsumer = new SourceMapConsumer(res.styles[0].map)
89-
const stylePos = styleConsumer.originalPositionFor({
90-
line: 2,
91-
column: 1
92-
})
93-
expect(stylePos.line).toBe(5)
94-
})
95-
96-
it('source map (with pad)', () => {
97-
const res = parseComponent(`
98-
<script>
99-
export default {}
100-
</script>
101-
<style>
102-
h1 { color: red }
103-
</style>
104-
`.trim(), {
105-
pad: true,
106-
map: {
107-
filename: 'test.vue'
108-
}
109-
})
110-
const scriptConsumer = new SourceMapConsumer(res.script.map)
111-
const scriptPos = scriptConsumer.originalPositionFor({
112-
line: 2,
113-
column: 1
114-
})
115-
expect(scriptPos.line).toBe(2)
116-
117-
const styleConsumer = new SourceMapConsumer(res.styles[0].map)
118-
const stylePos = styleConsumer.originalPositionFor({
119-
line: 5,
120-
column: 1
121-
})
122-
expect(stylePos.line).toBe(5)
123-
})
12466
})

0 commit comments

Comments
 (0)