|
1 | 1 | /* @flow */ |
2 | 2 |
|
| 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 | + |
3 | 8 | import { parseHTML } from './html-parser' |
4 | 9 | import { makeMap } from 'shared/util' |
5 | | -import deindent from 'de-indent' |
6 | 10 |
|
| 11 | +const splitRE = /\r?\n/g |
7 | 12 | const isSpecialTag = makeMap('script,style,template', true) |
8 | 13 |
|
| 14 | +type Attribute = { |
| 15 | + name: string, |
| 16 | + value: string |
| 17 | +} |
| 18 | + |
9 | 19 | /** |
10 | 20 | * Parse a single-file component (*.vue) file into an SFC Descriptor Object. |
11 | 21 | */ |
12 | | -export function parseComponent (content: string): SFCDescriptor { |
| 22 | +export function parseComponent ( |
| 23 | + content: string, |
| 24 | + options?: Object |
| 25 | + ): SFCDescriptor { |
13 | 26 | const sfc: SFCDescriptor = { |
14 | 27 | template: null, |
15 | 28 | script: null, |
16 | 29 | styles: [] |
17 | 30 | } |
18 | 31 | let depth = 0 |
19 | | - let currentBlock |
| 32 | + let currentBlock: ?SFCBlock = null |
20 | 33 |
|
21 | | - function start (tag, attrs) { |
| 34 | + function start (tag: string, attrs: Array<Attribute>) { |
22 | 35 | depth++ |
23 | 36 | if (depth > 1) { |
24 | 37 | return |
25 | 38 | } |
26 | 39 | if (isSpecialTag(tag)) { |
27 | | - const block: SFCBlock = currentBlock = { |
| 40 | + currentBlock = { |
28 | 41 | type: tag, |
29 | 42 | content: '' |
30 | 43 | } |
31 | | - for (let i = 0; i < attrs.length; i++) { |
32 | | - const attr = attrs[i] |
33 | | - if (attr.name === 'lang') { |
34 | | - block.lang = attr.value |
35 | | - } |
36 | | - if (attr.name === 'scoped') { |
37 | | - block.scoped = true |
38 | | - } |
39 | | - if (attr.name === 'src') { |
40 | | - block.src = attr.value |
41 | | - } |
42 | | - } |
| 44 | + checkAttrs(currentBlock, attrs) |
43 | 45 | if (tag === 'style') { |
44 | | - sfc.styles.push(block) |
| 46 | + sfc.styles.push(currentBlock) |
45 | 47 | } else { |
46 | | - sfc[tag] = block |
| 48 | + sfc[tag] = currentBlock |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function checkAttrs (block: SFCBlock, attrs: Array<Attribute>) { |
| 54 | + for (let i = 0; i < attrs.length; i++) { |
| 55 | + const attr = attrs[i] |
| 56 | + if (attr.name === 'lang') { |
| 57 | + block.lang = attr.value |
| 58 | + } |
| 59 | + if (attr.name === 'scoped') { |
| 60 | + block.scoped = true |
| 61 | + } |
| 62 | + if (attr.name === 'src') { |
| 63 | + block.src = attr.value |
47 | 64 | } |
48 | 65 | } |
49 | 66 | } |
50 | 67 |
|
51 | 68 | function end () { |
52 | 69 | depth-- |
| 70 | + if (currentBlock && options && options.map) { |
| 71 | + addSourceMap(currentBlock) |
| 72 | + } |
53 | 73 | currentBlock = null |
54 | 74 | } |
55 | 75 |
|
56 | | - function chars (text) { |
| 76 | + function chars (text: string) { |
57 | 77 | if (currentBlock) { |
58 | | - currentBlock.content = deindent(text) |
| 78 | + currentBlock.start = content.indexOf(text) |
| 79 | + currentBlock.end = currentBlock.start + text.length |
| 80 | + text = deindent(text) |
| 81 | + // pad content so that linters and pre-processors can output correct |
| 82 | + // line numbers in errors and warnings |
| 83 | + if (currentBlock.type !== 'template' && options && options.pad) { |
| 84 | + text = padContent(currentBlock) + text |
| 85 | + } |
| 86 | + currentBlock.content = text |
59 | 87 | } |
60 | 88 | } |
61 | 89 |
|
| 90 | + function padContent (block: SFCBlock) { |
| 91 | + const leadingContent = content.slice(0, block.start) |
| 92 | + const padChar = block.type === 'script' && !block.lang |
| 93 | + ? '//\n' |
| 94 | + : '\n' |
| 95 | + return Array(leadingContent.split(splitRE).length).join(padChar) |
| 96 | + } |
| 97 | + |
| 98 | + function addSourceMap (block: SFCBlock) { |
| 99 | + |
| 100 | + } |
| 101 | + |
62 | 102 | parseHTML(content, { |
63 | 103 | isSpecialTag, |
64 | 104 | start, |
|
0 commit comments