UNPKG

6.52 kBMarkdownView Raw
1# vue-template-compiler
2
3> This package is auto-generated. For pull requests please see [src/platforms/web/entry-compiler.js](https://github.com/vuejs/vue/tree/dev/src/platforms/web/entry-compiler.js).
4
5This package can be used to pre-compile Vue 2.0 templates into render functions to avoid runtime-compilation overhead and CSP restrictions. In most cases you should be using it with [`vue-loader`](https://github.com/vuejs/vue-loader), you will only need it separately if you are writing build tools with very specific needs.
6
7## Installation
8
9``` bash
10npm install vue-template-compiler
11```
12
13``` js
14const compiler = require('vue-template-compiler')
15```
16
17## API
18
19### compiler.compile(template, [options])
20
21Compiles a template string and returns compiled JavaScript code. The returned result is an object of the following format:
22
23``` js
24{
25 ast: ?ASTElement, // parsed template elements to AST
26 render: string, // main render function code
27 staticRenderFns: Array<string>, // render code for static sub trees, if any
28 errors: Array<string> // template syntax errors, if any
29}
30```
31
32Note the returned function code uses `with` and thus cannot be used in strict mode code.
33
34#### Options
35
36- `outputSourceRange` *new in 2.6*
37 - Type: `boolean`
38 - Default: `false`
39
40 Set this to true will cause the `errors` returned in the compiled result become objects in the form of `{ msg, start, end }`. The `start` and `end` properties are numbers that mark the code range of the error source in the template. This can be passed on to the `compiler.generateCodeFrame` API to generate a code frame for the error.
41
42- `whitespace`
43 - Type: `string`
44 - Valid values: `'preserve' | 'condense'`
45 - Default: `'preserve'`
46
47 The default value `'preserve'` handles whitespaces as follows:
48
49 - A whitespace-only text node between element tags is condensed into a single space.
50 - All other whitespaces are preserved as-is.
51
52 If set to `'condense'`:
53
54 - A whitespace-only text node between element tags is removed if it contains new lines. Otherwise, it is condensed into a single space.
55 - Consecutive whitespaces inside a non-whitespace-only text node are condensed into a single space.
56
57 Using condense mode will result in smaller compiled code size and slightly improved performance. However, it will produce minor visual layout differences compared to plain HTML in certain cases.
58
59 **This option does not affect the `<pre>` tag.**
60
61 Example:
62
63 ``` html
64 <!-- source -->
65 <div>
66 <span>
67 foo
68 </span> <span>bar</span>
69 </div>
70
71 <!-- whitespace: 'preserve' -->
72 <div> <span>
73 foo
74 </span> <span>bar</span> </div>
75
76 <!-- whitespace: 'condense' -->
77 <div><span> foo </span> <span>bar</span></div>
78 ```
79
80- `modules`
81
82 It's possible to hook into the compilation process to support custom template features. **However, beware that by injecting custom compile-time modules, your templates will not work with other build tools built on standard built-in modules, e.g `vue-loader` and `vueify`.**
83
84 An array of compiler modules. For details on compiler modules, refer to the `ModuleOptions` type in [flow declarations](https://github.com/vuejs/vue/blob/dev/flow/compiler.js#L38-L45) and the [built-in modules](https://github.com/vuejs/vue/tree/dev/src/platforms/web/compiler/modules).
85
86- `directives`
87
88 An object where the key is the directive name and the value is a function that transforms an template AST node. For example:
89
90 ``` js
91 compiler.compile('<div v-test></div>', {
92 directives: {
93 test (node, directiveMeta) {
94 // transform node based on directiveMeta
95 }
96 }
97 })
98 ```
99
100 By default, a compile-time directive will extract the directive and the directive will not be present at runtime. If you want the directive to also be handled by a runtime definition, return `true` in the transform function.
101
102 Refer to the implementation of some [built-in compile-time directives](https://github.com/vuejs/vue/tree/dev/src/platforms/web/compiler/directives).
103
104- `preserveWhitespace` **Deprecated since 2.6**
105 - Type: `boolean`
106 - Default: `true`
107
108 By default, the compiled render function preserves all whitespace characters between HTML tags. If set to `false`, whitespace between tags will be ignored. This can result in slightly better performance but may affect layout for inline elements.
109
110---
111
112### compiler.compileToFunctions(template)
113
114Similar to `compiler.compile`, but directly returns instantiated functions:
115
116``` js
117{
118 render: Function,
119 staticRenderFns: Array<Function>
120}
121```
122
123This is only useful at runtime with pre-configured builds, so it doesn't accept any compile-time options. In addition, this method uses `new Function()` so it is not CSP-compliant.
124
125---
126
127### compiler.ssrCompile(template, [options])
128
129> 2.4.0+
130
131Same as `compiler.compile` but generates SSR-specific render function code by optimizing parts of the template into string concatenation in order to improve SSR performance.
132
133This is used by default in `vue-loader@>=12` and can be disabled using the [`optimizeSSR`](https://vue-loader.vuejs.org/en/options.html#optimizessr) option.
134
135---
136
137### compiler.ssrCompileToFunctions(template)
138
139> 2.4.0+
140
141Same as `compiler.compileToFunction` but generates SSR-specific render function code by optimizing parts of the template into string concatenation in order to improve SSR performance.
142
143---
144
145### compiler.parseComponent(file, [options])
146
147Parse a SFC (single-file component, or `*.vue` file) into a descriptor (refer to the `SFCDescriptor` type in [flow declarations](https://github.com/vuejs/vue/blob/dev/flow/compiler.js)). This is used in SFC build tools like `vue-loader` and `vueify`.
148
149---
150
151### compiler.generateCodeFrame(template, start, end)
152
153Generate a code frame that highlights the part in `template` defined by `start` and `end`. Useful for error reporting in higher-level tooling.
154
155#### Options
156
157#### `pad`
158
159`pad` is useful when you are piping the extracted content into other pre-processors, as you will get correct line numbers or character indices if there are any syntax errors.
160
161- with `{ pad: "line" }`, the extracted content for each block will be prefixed with one newline for each line in the leading content from the original file to ensure that the line numbers align with the original file.
162- with `{ pad: "space" }`, the extracted content for each block will be prefixed with one space for each character in the leading content from the original file to ensure that the character count remains the same as the original file.