-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler-sfc): introduce
defineRender
- Loading branch information
Showing
6 changed files
with
155 additions
and
5 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineRender.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`defineRender() > JSX Element 1`] = ` | ||
"import { defineComponent as _defineComponent } from 'vue' | ||
export default /*#__PURE__*/_defineComponent({ | ||
setup(__props, { expose: __expose }) { | ||
__expose(); | ||
return () => <div /> | ||
} | ||
})" | ||
`; | ||
|
||
exports[`defineRender() > function 1`] = ` | ||
"import { defineComponent as _defineComponent } from 'vue' | ||
export default /*#__PURE__*/_defineComponent({ | ||
setup(__props, { expose: __expose }) { | ||
__expose(); | ||
return () => <div /> | ||
} | ||
})" | ||
`; | ||
|
||
exports[`defineRender() > identifier 1`] = ` | ||
"import { defineComponent as _defineComponent } from 'vue' | ||
import { renderFn } from './ctx' | ||
export default /*#__PURE__*/_defineComponent({ | ||
setup(__props, { expose: __expose }) { | ||
__expose(); | ||
return renderFn | ||
} | ||
})" | ||
`; |
46 changes: 46 additions & 0 deletions
46
packages/compiler-sfc/__tests__/compileScript/defineRender.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { compileSFCScript as compile, assertCode } from '../utils' | ||
|
||
describe('defineRender()', () => { | ||
test('JSX Element', () => { | ||
const { content } = compile( | ||
` | ||
<script setup lang="tsx"> | ||
defineRender(<div />) | ||
</script> | ||
`, | ||
{ defineRender: true } | ||
) | ||
assertCode(content) | ||
expect(content).toMatch(`return () => <div />`) | ||
expect(content).not.toMatch('defineRender') | ||
}) | ||
|
||
test('function', () => { | ||
const { content } = compile( | ||
` | ||
<script setup lang="tsx"> | ||
defineRender(() => <div />) | ||
</script> | ||
`, | ||
{ defineRender: true } | ||
) | ||
assertCode(content) | ||
expect(content).toMatch(`return () => <div />`) | ||
expect(content).not.toMatch('defineRender') | ||
}) | ||
|
||
test('identifier', () => { | ||
const { content } = compile( | ||
` | ||
<script setup lang="ts"> | ||
import { renderFn } from './ctx' | ||
defineRender(renderFn) | ||
</script> | ||
`, | ||
{ defineRender: true } | ||
) | ||
assertCode(content) | ||
expect(content).toMatch(`return renderFn`) | ||
expect(content).not.toMatch('defineRender') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Node } from '@babel/types' | ||
import { isCallOf } from './utils' | ||
import { ScriptCompileContext } from './context' | ||
import { warnOnce } from '../warn' | ||
|
||
export const DEFINE_RENDER = 'defineRender' | ||
|
||
export function processDefineRender( | ||
ctx: ScriptCompileContext, | ||
node: Node | ||
): boolean { | ||
if (!isCallOf(node, DEFINE_RENDER)) { | ||
return false | ||
} | ||
|
||
if (!ctx.options.defineRender) { | ||
warnOnce( | ||
`${DEFINE_RENDER}() is an experimental feature and disabled by default.\n` + | ||
`To enable it, follow the RFC at https://github.com/vuejs/rfcs/discussions/TODO.` | ||
) | ||
return false | ||
} | ||
|
||
if (ctx.hasDefineRenderCall) { | ||
ctx.error(`duplicate ${DEFINE_RENDER}() call`, node) | ||
} | ||
|
||
ctx.hasDefineRenderCall = true | ||
if (node.arguments.length > 0) { | ||
ctx.renderFunction = node.arguments[0] | ||
} | ||
|
||
return true | ||
} |