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

Commit

Permalink
fix: avoid adding render function when no template in an SFC (#387)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
elevatebart authored Oct 20, 2020
1 parent 3222451 commit 6960203
Show file tree
Hide file tree
Showing 8 changed files with 1,152 additions and 21 deletions.
12 changes: 12 additions & 0 deletions examples/no-template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "simple",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "rollup -c"
},
"dependencies": {
"rollup": "^2.10.9",
"rollup-plugin-vue": "link:../.."
}
}
14 changes: 14 additions & 0 deletions examples/no-template/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import VuePlugin from 'rollup-plugin-vue'

export default [
{
input: 'src/HelloWorld.vue',
output: {
file: 'dist/HelloWorld.js',
format: 'esm',
sourcemap: 'inline',
},
plugins: [VuePlugin()],
external: ['vue'],
},
]
10 changes: 10 additions & 0 deletions examples/no-template/src/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { h } from 'vue'
export default {
props: ['name'],
render() {
return h('div', 'my render')
},
}
</script>
4 changes: 2 additions & 2 deletions examples/simple/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import VuePlugin from 'rollup-plugin-vue'

export default [
{
input: 'src/App.vue',
input: 'src/HelloWorld.vue',
output: {
file: 'dist/app.js',
file: 'dist/HelloWorld.js',
format: 'esm',
sourcemap: 'inline',
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@vue/compiler-sfc": "*"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^9.0.0",
"@types/debug": "^4.1.5",
"@types/jest": "^25.2.3",
"@types/node": "^13.13.2",
Expand All @@ -35,6 +36,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"rollup": "^2.7.2",
"rollup-plugin-postcss": "^3.1.8",
"ts-jest": "^26.0.0",
"typescript": "^3.9.3"
},
Expand Down
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,17 @@ function transformVueSFC(
const id = hash(isProduction ? shortFilePath + '\n' + code : shortFilePath)
// feature information
const hasScoped = descriptor.styles.some((s) => s.scoped)
const templateImport = getTemplateCode(
descriptor,
resourcePath,
id,
hasScoped,
isServer
)

const templateImport = !descriptor.template
? ''
: getTemplateCode(descriptor, resourcePath, id, hasScoped, isServer)

const renderReplace = !descriptor.template
? ''
: isServer
? `script.ssrRender = ssrRender`
: `script.render = render`

const scriptImport = getScriptCode(descriptor, resourcePath)
const stylesCode = getStyleCode(
descriptor,
Expand All @@ -441,7 +445,7 @@ function transformVueSFC(
templateImport,
stylesCode,
customBlocksCode,
isServer ? `script.ssrRender = ssrRender` : `script.render = render`,
renderReplace,
]
if (hasScoped) {
output.push(`script.__scopeId = ${_(`data-v-${id}`)}`)
Expand Down
14 changes: 14 additions & 0 deletions test/core.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ describe('simple', () => {
})
})

describe('no-template', () => {
let result!: RollupOutput

beforeAll(async () => {
result = await roll('no-template')
})

it('should leave the render function alone when no template is in the SFC', () => {
expect(result.output[0].code).not.toEqual(
expect.stringContaining('.render =')
)
})
})

describe('custom-block', () => {
let result!: RollupOutput

Expand Down
Loading

0 comments on commit 6960203

Please sign in to comment.