Skip to content

Commit 16bd212

Browse files
committed
Add option to minify templates
1 parent 231d47c commit 16bd212

File tree

4 files changed

+326
-110
lines changed

4 files changed

+326
-110
lines changed

README.md

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
<template>
1212
<div class="page">{{msg}}</div>
1313
<!-- Inline partials -->
14-
{{> 'foo'}}
15-
{{> 'bar'}}
14+
{{> 'foo'}} {{> 'bar'}}
1615
<!-- External partials -->
1716
{{> 'external'}}
1817
</template>
@@ -26,16 +25,18 @@
2625
</template-partial>
2726

2827
<script>
29-
export default {
30-
data () {
31-
return {
32-
msg: 'Hello world!'
33-
}
34-
}
35-
}
28+
export default {
29+
data() {
30+
return {
31+
msg: 'Hello world!',
32+
};
33+
},
34+
};
3635
</script>
3736
```
37+
3838
#### External partial templates example (see config for location)
39+
3940
```html
4041
<!-- external.f7p.html -->
4142
<template>
@@ -64,9 +65,14 @@ module.exports = {
6465
{
6566
loader: 'framework7-component-loader',
6667
options: {
68+
// path to file that exports array of Template7 helpers names
6769
helpersPath: './src/template7-helpers-list.js',
70+
// path where to look for Template7 partials
6871
partialsPath: './src/pages/',
69-
partialsExt: '.f7p.html'
72+
// Template7 partials file extension
73+
partialsExt: '.f7p.html',
74+
// When enabled it will minify templates HTML content
75+
minifyTemplate: true,
7076
}
7177
}
7278
],
@@ -79,6 +85,36 @@ module.exports = {
7985
}
8086
```
8187

82-
## Framework7 Webpack Template
88+
## Template7 Helpers
8389

84-
There is already ready to use [Framework7 Webpack Template](https://github.com/framework7io/framework7-template-webpack) pre-configured with `framework7-component-loader`
90+
To use Template7 helpers, we need to specify helpers names in separate file and specify path to file in `helpersPath` loader parameter. It is required because template is compiled on server side which doesn't know about helpers registered during app runtime.
91+
92+
So, if we use helpers named `foo` and `bar` in our templates, we need to register their names in file:
93+
94+
```js
95+
/* src/template7-helpers-list.js */
96+
module.exports = ['foo', 'bar'];
97+
```
98+
99+
And specify this file in loader options:
100+
101+
```js
102+
rules: [
103+
...
104+
{
105+
test: /\.f7.html$/,
106+
use: [
107+
'babel-loader',
108+
{
109+
loader: 'framework7-component-loader',
110+
options: {
111+
// path to file that exports array of Template7 helpers names
112+
helpersPath: './src/template7-helpers-list.js',
113+
// ...
114+
}
115+
}
116+
],
117+
},
118+
...
119+
]
120+
```

lib/get-template.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
module.exports = ({ source }) => {
1+
const minify = require('html-minifier').minify;
2+
3+
module.exports = ({ source, options }) => {
24
let template = null;
35
const hasTemplate = source.match(/<template([ ]?)([a-z0-9-]*)>/);
46
const templateType = hasTemplate[2] || 't7';
7+
const minifyTemplate = !options || options.minifyTemplate !== false;
58
if (hasTemplate) {
69
template = source
710
.split(/<template[ ]?[a-z0-9-]*>/)
@@ -14,6 +17,17 @@ module.exports = ({ source }) => {
1417
.replace(/\/template>([ \n]*){{\/raw}}/g, '/template>{{/raw}}')
1518
.replace(/([ \n])<template/g, '$1{{#raw}}<template')
1619
.replace(/\/template>([ \n])/g, '/template>{{/raw}}$1');
20+
const originalTemplate = template;
21+
if (minifyTemplate) {
22+
try {
23+
template = minify(template, {
24+
removeAttributeQuotes: true,
25+
collapseWhitespace: true,
26+
});
27+
} catch (err) {
28+
template = originalTemplate;
29+
}
30+
}
1731
}
1832
return {
1933
template,

0 commit comments

Comments
 (0)