Skip to content
This repository has been 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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/no-template/package.json
@@ -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
@@ -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
@@ -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
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
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
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
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