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

Commit 6960203

Browse files
authored
fix: avoid adding render function when no template in an SFC (#387)
* test: make simple test more precise * test: add the no-template example for test * fix: when an SFC has no template leave render * test: fix import example * fix: missed template point * tests: add missing dependencies necessary for test
1 parent 3222451 commit 6960203

File tree

8 files changed

+1152
-21
lines changed

8 files changed

+1152
-21
lines changed

examples/no-template/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "simple",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"build": "rollup -c"
7+
},
8+
"dependencies": {
9+
"rollup": "^2.10.9",
10+
"rollup-plugin-vue": "link:../.."
11+
}
12+
}

examples/no-template/rollup.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import VuePlugin from 'rollup-plugin-vue'
2+
3+
export default [
4+
{
5+
input: 'src/HelloWorld.vue',
6+
output: {
7+
file: 'dist/HelloWorld.js',
8+
format: 'esm',
9+
sourcemap: 'inline',
10+
},
11+
plugins: [VuePlugin()],
12+
external: ['vue'],
13+
},
14+
]
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import { h } from 'vue'
3+
4+
export default {
5+
props: ['name'],
6+
render() {
7+
return h('div', 'my render')
8+
},
9+
}
10+
</script>

examples/simple/rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import VuePlugin from 'rollup-plugin-vue'
22

33
export default [
44
{
5-
input: 'src/App.vue',
5+
input: 'src/HelloWorld.vue',
66
output: {
7-
file: 'dist/app.js',
7+
file: 'dist/HelloWorld.js',
88
format: 'esm',
99
sourcemap: 'inline',
1010
},

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@vue/compiler-sfc": "*"
2626
},
2727
"devDependencies": {
28+
"@rollup/plugin-node-resolve": "^9.0.0",
2829
"@types/debug": "^4.1.5",
2930
"@types/jest": "^25.2.3",
3031
"@types/node": "^13.13.2",
@@ -35,6 +36,7 @@
3536
"npm-run-all": "^4.1.5",
3637
"prettier": "^2.0.5",
3738
"rollup": "^2.7.2",
39+
"rollup-plugin-postcss": "^3.1.8",
3840
"ts-jest": "^26.0.0",
3941
"typescript": "^3.9.3"
4042
},

src/index.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,17 @@ function transformVueSFC(
417417
const id = hash(isProduction ? shortFilePath + '\n' + code : shortFilePath)
418418
// feature information
419419
const hasScoped = descriptor.styles.some((s) => s.scoped)
420-
const templateImport = getTemplateCode(
421-
descriptor,
422-
resourcePath,
423-
id,
424-
hasScoped,
425-
isServer
426-
)
420+
421+
const templateImport = !descriptor.template
422+
? ''
423+
: getTemplateCode(descriptor, resourcePath, id, hasScoped, isServer)
424+
425+
const renderReplace = !descriptor.template
426+
? ''
427+
: isServer
428+
? `script.ssrRender = ssrRender`
429+
: `script.render = render`
430+
427431
const scriptImport = getScriptCode(descriptor, resourcePath)
428432
const stylesCode = getStyleCode(
429433
descriptor,
@@ -441,7 +445,7 @@ function transformVueSFC(
441445
templateImport,
442446
stylesCode,
443447
customBlocksCode,
444-
isServer ? `script.ssrRender = ssrRender` : `script.render = render`,
448+
renderReplace,
445449
]
446450
if (hasScoped) {
447451
output.push(`script.__scopeId = ${_(`data-v-${id}`)}`)

test/core.e2e.ts

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ describe('simple', () => {
1212
})
1313
})
1414

15+
describe('no-template', () => {
16+
let result!: RollupOutput
17+
18+
beforeAll(async () => {
19+
result = await roll('no-template')
20+
})
21+
22+
it('should leave the render function alone when no template is in the SFC', () => {
23+
expect(result.output[0].code).not.toEqual(
24+
expect.stringContaining('.render =')
25+
)
26+
})
27+
})
28+
1529
describe('custom-block', () => {
1630
let result!: RollupOutput
1731

0 commit comments

Comments
 (0)