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