You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.4 KiB

4 years ago
  1. # vue-template-compiler
  2. > 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).
  3. This 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.
  4. ## Installation
  5. ``` bash
  6. npm install vue-template-compiler
  7. ```
  8. ``` js
  9. const compiler = require('vue-template-compiler')
  10. ```
  11. ## API
  12. ### compiler.compile(template, [options])
  13. Compiles a template string and returns compiled JavaScript code. The returned result is an object of the following format:
  14. ``` js
  15. {
  16. ast: ?ASTElement, // parsed template elements to AST
  17. render: string, // main render function code
  18. staticRenderFns: Array<string>, // render code for static sub trees, if any
  19. errors: Array<string> // template syntax errors, if any
  20. }
  21. ```
  22. Note the returned function code uses `with` and thus cannot be used in strict mode code.
  23. #### Options
  24. - `outputSourceRange` *new in 2.6*
  25. - Type: `boolean`
  26. - Default: `false`
  27. 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.
  28. - `whitespace`
  29. - Type: `string`
  30. - Valid values: `'preserve' | 'condense'`
  31. - Default: `'preserve'`
  32. The default value `'preserve'` handles whitespaces as follows:
  33. - A whitespace-only text node between element tags is condensed into a single space.
  34. - All other whitespaces are preserved as-is.
  35. If set to `'condense'`:
  36. - A whitespace-only text node between element tags is removed if it contains new lines. Otherwise, it is condensed into a single space.
  37. - Consecutive whitespaces inside a non-whitespace-only text node are condensed into a single space.
  38. 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.
  39. **This option does not affect the `<pre>` tag.**
  40. Example:
  41. ``` html
  42. <!-- source -->
  43. <div>
  44. <span>
  45. foo
  46. </span> <span>bar</span>
  47. </div>
  48. <!-- whitespace: 'preserve' -->
  49. <div> <span>
  50. foo
  51. </span> <span>bar</span> </div>
  52. <!-- whitespace: 'condense' -->
  53. <div><span> foo </span> <span>bar</span></div>
  54. ```
  55. - `modules`
  56. 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`.**
  57. 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).
  58. - `directives`
  59. An object where the key is the directive name and the value is a function that transforms an template AST node. For example:
  60. ``` js
  61. compiler.compile('<div v-test></div>', {
  62. directives: {
  63. test (node, directiveMeta) {
  64. // transform node based on directiveMeta
  65. }
  66. }
  67. })
  68. ```
  69. 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.
  70. Refer to the implementation of some [built-in compile-time directives](https://github.com/vuejs/vue/tree/dev/src/platforms/web/compiler/directives).
  71. - `preserveWhitespace` **Deprecated since 2.6**
  72. - Type: `boolean`
  73. - Default: `true`
  74. 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.
  75. ---
  76. ### compiler.compileToFunctions(template)
  77. Similar to `compiler.compile`, but directly returns instantiated functions:
  78. ``` js
  79. {
  80. render: Function,
  81. staticRenderFns: Array<Function>
  82. }
  83. ```
  84. 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.
  85. ---
  86. ### compiler.ssrCompile(template, [options])
  87. > 2.4.0+
  88. Same 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.
  89. This 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.
  90. ---
  91. ### compiler.ssrCompileToFunctions(template)
  92. > 2.4.0+
  93. Same 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.
  94. ---
  95. ### compiler.parseComponent(file, [options])
  96. Parse 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`.
  97. ---
  98. ### compiler.generateCodeFrame(template, start, end)
  99. Generate a code frame that highlights the part in `template` defined by `start` and `end`. Useful for error reporting in higher-level tooling.
  100. #### Options
  101. #### `pad`
  102. `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.
  103. - 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.
  104. - 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.