Skip to content

Commit dd1c936

Browse files
committed
docs for vue-template-compiler
1 parent 589e644 commit dd1c936

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed
Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
# vue-template-compiler
22

3-
> This package is auto-generated. For pull requests please see `src/entries/web-compiler.js`
3+
> This package is auto-generated. For pull requests please see [src/entries/web-compiler.js](https://github.com/vuejs/vue/blob/next/src/entries/web-compiler.js).
44
5-
Documentation coming soon.
5+
This package can be used to pre-compile Vue 2.0 templates into render functions to avoid runtime-compilation overhead and CSP restrictions. You will only need it if you are writing build tools with very specific needs. In most cases you should be using [vue-loader](https://github.com/vuejs/vue-loader) or [vueify](https://github.com/vuejs/vueify) instead, both of which use this package internally.
6+
7+
## Installation
8+
9+
``` bash
10+
npm install vue-template-compiler
11+
```
12+
13+
``` js
14+
var compiler = require('vue-template-compiler')
15+
```
16+
17+
## API
18+
19+
### compiler.compile(template, [options])
20+
21+
Compiles a template string and returns compiled JavaScript code. The returned result is an object of the following format:
22+
23+
``` js
24+
{
25+
render: string, // main render function code
26+
staticRenderFns: Array<string>, // render code for static sub trees, if any
27+
errors: Array<string> // template syntax errors, if any
28+
}
29+
```
30+
31+
Note the returned function code uses `with` and thus cannot be used in strict mode code.
32+
33+
**Options**
34+
35+
The optional `options` object can contain the following:
36+
37+
- `modules`
38+
39+
An array of compiler modules. For details on compiler modules, refer to its [type definition](https://github.com/vuejs/vue/blob/next/flow/compiler.js#L31) and the [built-in modules](https://github.com/vuejs/vue/tree/next/src/platforms/web/compiler/modules).
40+
41+
- `directives`
42+
43+
An object where the key is the directive name and the value is a function that transforms an template AST node. For example:
44+
45+
``` js
46+
compiler.compile('<div v-test></div>', {
47+
directives: {
48+
test (node, directiveMeta) {
49+
// transform node based on directiveMeta
50+
}
51+
})
52+
```
53+
54+
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.
55+
56+
Refer to the implementation of some [built-in compile-time directives](https://github.com/vuejs/vue/tree/next/src/platforms/web/compiler/directives).
57+
58+
---
59+
60+
### compiler.compileToFunctions(template)
61+
62+
Similar to `compiler.compile`, but directly returns instantiated functions:
63+
64+
``` js
65+
{
66+
render: Function,
67+
staticRenderFns: Array<Function>
68+
}
69+
```
70+
71+
This 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.
72+
73+
---
74+
75+
### compiler.parseComponent(file, [options])
76+
77+
Parse a SFC (single-file component, or `*.vue` file) into a [descriptor](https://github.com/vuejs/vue/blob/next/flow/compiler.js#L131). This is used in SFC build tools like `vue-loader` and `vueify`.
78+
79+
**Options**
80+
81+
- `pad`: with `{ pad: true }`, the extracted content for each block will be padded with newlines to ensure that the line numbers align with the original file. This is useful when you are piping the extracted content into other pre-processors, as you will get correct line numbers if there are any syntax errors.

0 commit comments

Comments
 (0)