mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 06:05:11 +00:00
feat(vue-renderer): support evaluation in templates (#6505)
This commit is contained in:
parent
9ada4e63a9
commit
bb9427ee74
13
examples/custom-template/app.html
Normal file
13
examples/custom-template/app.html
Normal file
@ -0,0 +1,13 @@
|
||||
<html>
|
||||
<head>
|
||||
{{ HEAD }}
|
||||
{% if (ENV.NODE_ENV === 'dev') { %}
|
||||
<!-- debug scripts -->
|
||||
{% } else if (ENV.NODE_ENV === 'production') { %}
|
||||
<!-- production scripts -->
|
||||
{% } %}
|
||||
</head>
|
||||
<body>
|
||||
{{ APP }}
|
||||
</body>
|
||||
</html>
|
5
examples/custom-template/nuxt.config.js
Normal file
5
examples/custom-template/nuxt.config.js
Normal file
@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
NODE_ENV: process.env.NODE_ENV
|
||||
}
|
||||
}
|
12
examples/custom-template/package.json
Normal file
12
examples/custom-template/package.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "example-custom-template",
|
||||
"dependencies": {
|
||||
"nuxt": "latest"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "nuxt",
|
||||
"build": "nuxt build",
|
||||
"start": "nuxt start",
|
||||
"post-update": "yarn upgrade --latest"
|
||||
}
|
||||
}
|
5
examples/custom-template/pages/index.vue
Normal file
5
examples/custom-template/pages/index.vue
Normal file
@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<div class="container">
|
||||
<h1>Welcome!</h1>
|
||||
</div>
|
||||
</template>
|
@ -354,7 +354,8 @@ export default class VueRenderer {
|
||||
|
||||
parseTemplate (templateStr) {
|
||||
return template(templateStr, {
|
||||
interpolate: /{{([\s\S]+?)}}/g
|
||||
interpolate: /{{([\s\S]+?)}}/g,
|
||||
evaluate: /{%([\s\S]+?)%}/g
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,6 @@
|
||||
export default {
|
||||
appTemplatePath: './test/mytemplate.html'
|
||||
appTemplatePath: './test/mytemplate.html',
|
||||
env: {
|
||||
tracker: 'ga'
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,11 @@
|
||||
<html {{ HTML_ATTRS }}>
|
||||
<head>
|
||||
{{ HEAD }}
|
||||
{% if (ENV.tracker === 'ga') { %}
|
||||
<!-- Google Analytics -->
|
||||
{% } else if (ENV.tracker === 'heap') { %}
|
||||
<!-- Heap Analytics -->
|
||||
{% } %}
|
||||
</head>
|
||||
<body {{ BODY_ATTRS }}>
|
||||
{{ APP }}
|
||||
|
@ -1,25 +1,57 @@
|
||||
import { getPort, loadFixture, Nuxt } from '../utils'
|
||||
|
||||
let port
|
||||
let nuxt = null
|
||||
let options
|
||||
|
||||
describe('custom-app-template', () => {
|
||||
beforeAll(async () => {
|
||||
const options = await loadFixture('custom-app-template')
|
||||
nuxt = new Nuxt(options)
|
||||
options = await loadFixture('custom-app-template')
|
||||
})
|
||||
|
||||
test('Home page with google analytics', async () => {
|
||||
const nuxt = new Nuxt(options)
|
||||
await nuxt.ready()
|
||||
|
||||
port = await getPort()
|
||||
const port = await getPort()
|
||||
await nuxt.server.listen(port, '0.0.0.0')
|
||||
})
|
||||
test('/', async () => {
|
||||
|
||||
const { html } = await nuxt.server.renderRoute('/')
|
||||
expect(html).toContain('<p>My Template</p>')
|
||||
expect(html).toContain('<h1>Custom!</h1>')
|
||||
expect(html).toContain('Google Analytics')
|
||||
|
||||
await nuxt.close()
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
afterAll(async () => {
|
||||
test('Home page with heap analytics', async () => {
|
||||
const nuxt = new Nuxt(options)
|
||||
options.env.tracker = 'heap'
|
||||
await nuxt.ready()
|
||||
|
||||
const port = await getPort()
|
||||
await nuxt.server.listen(port, '0.0.0.0')
|
||||
|
||||
const { html } = await nuxt.server.renderRoute('/')
|
||||
expect(html).toContain('<p>My Template</p>')
|
||||
expect(html).toContain('<h1>Custom!</h1>')
|
||||
expect(html).toContain('Heap Analytics')
|
||||
|
||||
await nuxt.close()
|
||||
})
|
||||
|
||||
test('Home page with no analytics', async () => {
|
||||
const nuxt = new Nuxt(options)
|
||||
options.env.tracker = '-'
|
||||
await nuxt.ready()
|
||||
|
||||
const port = await getPort()
|
||||
await nuxt.server.listen(port, '0.0.0.0')
|
||||
|
||||
const { html } = await nuxt.server.renderRoute('/')
|
||||
expect(html).toContain('<p>My Template</p>')
|
||||
expect(html).toContain('<h1>Custom!</h1>')
|
||||
expect(html).not.toContain('google Analytics')
|
||||
expect(html).not.toContain('Heap Analytics')
|
||||
|
||||
await nuxt.close()
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user