-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
feat(compiler-sfc): introduce defineRender
macro
#9400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sxzz
wants to merge
8
commits into
minor
Choose a base branch
from
feat/return-setup
base: minor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−61
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d046962
feat(compiler-sfc): introduce `defineRender`
sxzz 3ba54c9
refactor: unwrap if...else
sxzz cee3fd3
refactor: throw an error using with <template>
sxzz 6862bd4
test: add empty arg
sxzz 03139fc
chore: update rfc link
sxzz 2f82c43
feat: export defineRender runtime function
sxzz 59dcb2b
fix: types
sxzz e4c1b23
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineRender.spec.ts.snap
This file contains hidden or 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,62 @@ | ||
// 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() > empty argument 1`] = ` | ||
"const foo = 'bar' | ||
|
||
export default { | ||
setup(__props, { expose: __expose }) { | ||
__expose(); | ||
|
||
|
||
|
||
return { foo } | ||
} | ||
|
||
}" | ||
`; | ||
|
||
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 | ||
} | ||
|
||
})" | ||
`; |
79 changes: 79 additions & 0 deletions
79
packages/compiler-sfc/__tests__/compileScript/defineRender.spec.ts
This file contains hidden or 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,79 @@ | ||
import { assertCode, compileSFCScript as compile } 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') | ||
}) | ||
|
||
test('empty argument', () => { | ||
const { content } = compile( | ||
` | ||
<script setup> | ||
const foo = 'bar' | ||
defineRender() | ||
</script> | ||
`, | ||
{ defineRender: true }, | ||
) | ||
assertCode(content) | ||
expect(content).toMatch(`return { foo }`) | ||
expect(content).not.toMatch('defineRender') | ||
}) | ||
|
||
describe('errors', () => { | ||
test('w/ <template>', () => { | ||
expect(() => | ||
compile( | ||
` | ||
<script setup lang="tsx"> | ||
defineRender(<div />) | ||
</script> | ||
<template> | ||
<span>hello</span> | ||
</template> | ||
`, | ||
{ defineRender: true }, | ||
), | ||
).toThrow(`defineRender() cannot be used with <template>.`) | ||
}) | ||
}) | ||
}) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 type { Node } from '@babel/types' | ||
import { isCallOf } from './utils' | ||
import type { 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/585.`, | ||
) | ||
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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.