UNPKG

2.77 kBMarkdownView Raw
1# require-extension-hooks-vue
2Simple parser for vue files
3
4Using require-extension-hooks you can load *.vue* files in node, extremely helpful for browserless unit testing.
5
6## Installation
7`npm install require-extension-hooks require-extension-hooks-vue --save-dev`
8
9## Usage
10```javascript
11const hooks = require('require-extension-hooks');
12hooks('vue').plugin('vue');
13
14let component = require('./components/app.vue');
15```
16
17*rehv* will convert `<template>` blocks into render functions for you.
18
19You can load external templates and scripts:
20```html
21<template src="./tpl.html"/>
22<script src="./script.js"/>
23```
24
25You can also transpile templates in other languages:
26```html
27<template lang="pug">...</template>
28```
29Just install the relevant library as you would for `vue-loader`:
30```
31npm install pug --save-dev
32```
33and *rehv* will pick it up.
34
35For scripts in other languages:
36```html
37<script lang="ts">...</script>
38```
39You will need to register a hook for that extension name:
40```javascript
41hooks('ts').push(function({content}){
42/* transpile your script code here */
43});
44```
45*There will likely be additional hook libraries for script languages available soon*
46
47## Custom Blocks
48If you have custom blocks in your `.vue` files, you can parse then using `require-extension-hooks`.
49Blocks are available by hooking to the file type `vue-block-(block name)`. The hook should return
50JavaScript code, in a string, which will be appended to the compiled `.vue` file.
51
52A helper named `COMPONENT_OPTIONS` is available in on the plugin export to allow you to modify
53the exported component options object from the `.vue` file.
54
55For example, for a `<json></json>` block to have its data available via `this.json` in the
56component, you could use a [mixin](https://vuejs.org/v2/guide/mixins.html):
57
58```javascript
59const { COMPONENT_OPTIONS } = require('require-extension-hooks-vue');
60hooks('vue-block-json').push(({ content }) =>
61 `${COMPONENT_OPTIONS}.mixins = (${COMPONENT_OPTIONS}.mixins || []).concat({
62 data: () => ({
63 json: ${JSON.parse(content)}
64 })
65 });`
66);
67```
68
69## Register
70You can automatically register the vue hook using the register file:
71```js
72require('require-extension-hooks-vue/register');
73```
74Which means you can register the module from cli tools:
75```
76mocha --require require-extension-hooks-vue/register
77```
78
79## Configuration
80Set configuration options using the `configure` method:
81```js
82const plugin = require('require-extension-hooks-vue');
83plugin.configure({ transpileTemplates: false });
84```
85
86### transpileTemplates
87`true`
88whether or not to automatically transpile templates that have a `lang` attribute
89
90### sourceMaps
91`true`
92whether or not to set up source map support. This utilises the `source-map-support` library.