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

[plugin-next] Fix Rollup when no template in an SFC #387

Merged
merged 6 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link

@jtommy jtommy Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h isnt' the default export, you should use 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
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right condition is !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