Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 423b955

Browse files
committed
test: add template comments test
1 parent a15bd01 commit 423b955

15 files changed

+943
-680
lines changed

package-lock.json

Lines changed: 809 additions & 584 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"babel-preset-env": "^1.6.1",
3131
"eslint": "^3.19.0",
3232
"eslint-plugin-vue-libs": "^1.2.0",
33-
"jest": "^21.1.0",
33+
"jest": "^22.1.1",
3434
"puppeteer": "^0.13.0",
3535
"rollup": "^0.53.4",
3636
"rollup-plugin-babel": "^3.0.3",
@@ -46,6 +46,7 @@
4646
"dependencies": {
4747
"hash-sum": "^1.0.2",
4848
"js-beautify": "^1.7.3",
49+
"lodash.defaultsdeep": "^4.6.0",
4950
"lru-cache": "^4.1.1",
5051
"postcss": "^6.0.12",
5152
"postcss-modules-sync": "^1.0.0",

src/assemble.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { struct } = require('superstruct')
2+
const defaultsdeep = require('lodash.defaultsdeep')
23
const _s = require('./utils/stringify')
34
const importStatement = require('./utils/import-statement')
45
const assertType = require('./utils/assert-type')
@@ -46,7 +47,7 @@ const Source = struct({
4647
descriptor: struct.union(['object', 'null'])
4748
}])
4849
})
49-
const Config = struct({
50+
const Config = any => defaultsdeep(struct({
5051
esModule: 'boolean?',
5152
require: 'object?',
5253
scopeId: 'string?',
@@ -55,7 +56,7 @@ const Config = struct({
5556
isProduction: 'boolean?',
5657
hasStyleInjectFn: 'boolean?',
5758
onWarn: 'function?'
58-
}, {
59+
})(any), {
5960
esModule: true,
6061
require: {
6162
normalizeComponent: 'vue-component-compiler/src/runtime/normalize-component',

src/style-compiler/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const postcss = require('postcss')
22
const { default: cssModules } = require('postcss-modules-sync')
33
const { struct } = require('superstruct')
4+
const defaultsdeep = require('lodash.defaultsdeep')
45

56
const trim = require('./plugins/trim')
67
const scopeId = require('./plugins/scope-id')
@@ -12,14 +13,14 @@ const Style = struct({
1213
descriptor: 'object'
1314
})
1415

15-
const Config = struct({
16+
const Config = any => defaultsdeep(struct({
1617
async: 'boolean?',
1718
needMap: 'boolean?',
1819
onWarn: 'function?',
1920
options: 'object?',
2021
plugins: 'array?',
2122
scopeId: 'string'
22-
}, {
23+
})(any), {
2324
async: false,
2425
needMap: true,
2526
onWarn: () => message => console.warn(message),

src/template-compiler/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const compiler = require('vue-template-compiler/build.js')
22
const transpile = require('vue-template-es2015-compiler')
33
const prettier = require('prettier')
44
const { struct } = require('superstruct')
5+
const defaultsdeep = require('lodash.defaultsdeep')
56

67
const transformRequire = require('./modules/transform-require')
78
const transformSrcset = require('./modules/transform-srcset')
@@ -13,7 +14,7 @@ const Template = struct({
1314
descriptor: 'object'
1415
})
1516

16-
const Config = struct({
17+
const Config = any => defaultsdeep(struct({
1718
scopeId: 'string',
1819
isServer: 'boolean?',
1920
isProduction: 'boolean?',
@@ -23,7 +24,7 @@ const Config = struct({
2324
options: 'object?',
2425
transformRequire: 'object?',
2526
plugins: 'array?'
26-
}, {
27+
})(any), {
2728
isServer: false,
2829
esModule: true,
2930
isProduction: true,
@@ -36,7 +37,9 @@ const Config = struct({
3637
},
3738
options: {
3839
preserveWhitespace: true,
39-
modules: []
40+
comments: true,
41+
modules: [],
42+
directives: []
4043
},
4144
plugins: []
4245
})
@@ -48,9 +51,13 @@ module.exports = function compileTemplate (template, filename, config) {
4851

4952
const options = config.options
5053

51-
options.modules = options.modules.concat(config.plugins)
52-
options.modules.push(transformRequire(config.transformToRequire))
53-
options.modules.push(transformSrcset())
54+
options.scopeId = config.scopeId
55+
options.modules = [].concat(
56+
options.modules || [],
57+
config.plugins || [], [
58+
transformRequire(config.transformToRequire),
59+
transformSrcset()
60+
])
5461

5562
const compile = (config.isServer && config.optimizeSSR !== false && compiler.ssrCompile) ? compiler.ssrCompile : compiler.compile
5663
const compiled = compile(template.code, options)
@@ -60,15 +67,15 @@ module.exports = function compileTemplate (template, filename, config) {
6067
}
6168

6269
if (output.errors && output.errors.length) {
63-
output.code = `function render () {}\nvar staticRenderFns = []`
70+
output.code = `function render () {}\nvar staticRenderFns = []\n`
6471
} else {
6572
const stripWithFunctional = template.descriptor.attrs && template.descriptor.attrs.functional
6673

6774
config.buble.transforms.stripWithFunctional = stripWithFunctional
6875

6976
output.code = transpile(
7077
'var render = ' + toFunction(compiled.render, stripWithFunctional) + '\n' +
71-
'var staticRenderFns = [' + compiled.staticRenderFns.map(it => toFunction(it, stripWithFunctional)).join(',') + ']',
78+
'var staticRenderFns = [' + compiled.staticRenderFns.map(it => toFunction(it, stripWithFunctional)).join(',') + ']\n',
7279
config.buble
7380
)
7481

test/assemble.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { mount } from '@vue/test-utils'
22

33
// Fixtures
4-
import Transform from './fixtures/transform.vue'
5-
import Functional from './fixtures/functional.vue'
6-
import FunctionalRoot from './fixtures/functional-root.vue'
4+
import Transform from './fixtures/with-template-require-transform.vue'
5+
import Functional from './fixtures/with-template-functional.vue'
6+
import FunctionalRoot from './fixtures/with-template-functional-root.vue'
7+
import TemplateComment from './fixtures/with-template-comment.vue'
78

89
test('transfom to require', () => {
910
const wrapper = mount(Transform)
@@ -54,3 +55,14 @@ test('functional template', () => {
5455
wrapper.find('.clickable').trigger('click')
5556
expect(fn).toHaveBeenCalled()
5657
})
58+
59+
test('template comments', () => {
60+
const wrapper = mount(TemplateComment)
61+
62+
expect(wrapper.is('div')).toBeTruthy()
63+
expect(wrapper.find('h2')).toBeTruthy()
64+
expect(wrapper.find('h2.red')).toBeTruthy()
65+
expect(wrapper.find('h2#test')).toBeTruthy()
66+
expect(wrapper.vnode.children[1].isComment).toEqual(true)
67+
expect(wrapper.vnode.children[1].text).toEqual(expect.stringContaining('comment here'))
68+
})

test/fixtures/scoped-css.vue

Lines changed: 0 additions & 57 deletions
This file was deleted.

test/fixtures/with-style-scoped.vue

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,57 @@
1-
<template>
2-
<h1 id="test" class="title">Hello {{ name }}!</h1>
3-
</template>
4-
5-
<script>
6-
export default {
7-
data () {
8-
return { name: 'John Doe' }
9-
}
10-
}
11-
</script>
12-
131
<style scoped>
14-
.title {
2+
.test {
3+
color: red;
4+
}
5+
.test:after {
6+
content: 'bye!';
7+
}
8+
h1 {
9+
color: green;
10+
}
11+
.anim {
12+
animation: color 5s infinite, other 5s;
13+
}
14+
.anim-2 {
15+
animation-name: color;
16+
animation-duration: 5s;
17+
}
18+
.anim-3 {
19+
animation: 5s color infinite, 5s other;
20+
}
21+
.anim-multiple {
22+
animation: color 5s infinite, opacity 2s;
23+
}
24+
.anim-multiple-2 {
25+
animation-name: color, opacity;
26+
animation-duration: 5s, 2s;
27+
}
28+
29+
@keyframes color {
30+
from { color: red; }
31+
to { color: green; }
32+
}
33+
@-webkit-keyframes color {
34+
from { color: red; }
35+
to { color: green; }
36+
}
37+
@keyframes opacity {
38+
from { opacity: 0; }
39+
to { opacity: 1; }
40+
}
41+
@-webkit-keyframes opacity {
42+
from { opacity: 0; }
43+
to { opacity: 1; }
44+
}
45+
.foo p >>> .bar {
1546
color: red;
1647
}
1748
</style>
49+
50+
<template>
51+
<div>
52+
<div><h1>hi</h1></div>
53+
<p class="abc def">hi</p>
54+
<template v-if="!ok"><p class="test" id="test">Hello</p></template>
55+
<svg><template><p></p></template></svg>
56+
</div>
57+
</template>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template comments>
2+
<div>
3+
<h2 class="red" id="test">{{msg}}</h2><!-- comment here -->
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
comments: true,
10+
data () {
11+
return {
12+
msg: 'Hello from Component A!'
13+
}
14+
}
15+
}
16+
</script>
17+
18+
<style>
19+
.red {
20+
color: red;
21+
}
22+
</style>

test/fixtures/functional-root.vue renamed to test/fixtures/with-template-functional-root.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</template>
1010

1111
<script>
12-
import Functional from './functional.vue'
12+
import Functional from './with-template-functional.vue'
1313
export default {
1414
components: {
1515
Functional

test/setup/utils.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nodeResolve = require('rollup-plugin-node-resolve')
55
const image = require('rollup-plugin-image')
66
const { readFileSync } = require('fs')
77
const { join, resolve } = require('path')
8+
const defaultsdeep = require('lodash.defaultsdeep')
89
const compiler = require('../..')
910

1011
module.exports = { compile, build, open, pack }
@@ -39,15 +40,15 @@ function load (ext, handle) {
3940
}
4041
}
4142

42-
function compile (filename, source) {
43+
function compile (filename, source, options = {}) {
4344
source = source || readFileSync(filename).toString()
4445
const descriptor = compiler.parse(source, filename, { needMap: true })
4546
const scopeId = compiler.generateScopeId(filename, source)
4647
const render = descriptor.template ? compiler.compileTemplate(
47-
{ code: descriptor.template.content, descriptor: descriptor.template }, filename, { scopeId }
48+
{ code: descriptor.template.content, descriptor: descriptor.template }, filename, defaultsdeep({ scopeId }, options.template)
4849
) : null
4950
const styles = descriptor.styles.map(it => compiler.compileStyle(
50-
{ code: it.content, descriptor: it }, filename, { scopeId }
51+
{ code: it.content, descriptor: it }, filename, defaultsdeep({ scopeId }, options.style)
5152
)).map((style, i) => ({ descriptor: descriptor.styles[i], code: style.code, map: style.map, modules: style.modules }))
5253
return compiler.assemble({
5354
styles,
@@ -60,14 +61,14 @@ function compile (filename, source) {
6061
descriptor: descriptor.script
6162
},
6263
customBlocks: []
63-
}, filename, {
64+
}, filename, defaultsdeep({
6465
scopeId,
6566
require: {
6667
normalizeComponent: resolve(__dirname, '../../src/runtime/normalize-component.js'),
6768
injectStyleClient: resolve(__dirname, '../../src/runtime/inject-style-client.js'),
6869
injectStyleServer: resolve(__dirname, '../../src/runtime/inject-style-server.js')
6970
}
70-
})
71+
}), options.assemble)
7172
}
7273

7374
const babelit = babel({
@@ -99,10 +100,10 @@ async function pack (filename, source) {
99100
}
100101

101102
const cache = {}
102-
async function build (filename) {
103+
async function build (filename, source) {
103104
if (filename in cache) return cache[filename]
104105

105-
const source = compile(filename)
106+
source = source || compile(filename)
106107
const component = filename + '__.js'
107108
const input = filename + '__app.js'
108109

0 commit comments

Comments
 (0)