Skip to content

Commit 0c2d470

Browse files
committed
feat(compiler): add withFallback option to compile components to createComponentWithFallback
1 parent 4e38087 commit 0c2d470

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

packages/compiler/src/compile.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
transform,
77
type DirectiveTransform,
88
type NodeTransform,
9+
type TransformOptions,
910
} from './transform'
1011
import { transformChildren } from './transforms/transformChildren'
1112
import { transformElement } from './transforms/transformElement'
@@ -84,10 +85,8 @@ export function compile(
8485
return generate(ir, resolvedOptions)
8586
}
8687

87-
export type CompilerOptions = HackOptions<BaseCompilerOptions> & {
88-
source?: string
89-
templates?: string[]
90-
}
88+
export type CompilerOptions = HackOptions<BaseCompilerOptions> &
89+
TransformOptions
9190
export type TransformPreset = [
9291
NodeTransform[],
9392
Record<string, DirectiveTransform>,

packages/compiler/src/transform.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,16 @@ export type StructuralDirectiveTransform = (
6060
) => void | (() => void)
6161

6262
export type TransformOptions = HackOptions<BaseTransformOptions> & {
63+
source?: string
6364
templates?: string[]
65+
/**
66+
* Compile JSX Component to createComponentWithFallback.
67+
* @default false
68+
*/
69+
withFallback?: boolean
6470
}
6571
const defaultOptions = {
72+
source: '',
6673
filename: '',
6774
hoistStatic: false,
6875
hmr: false,
@@ -80,6 +87,7 @@ const defaultOptions = {
8087
inSSR: false,
8188
ssrCssVars: ``,
8289
isTS: false,
90+
withFallback: false,
8391
onError: defaultOnError,
8492
onWarn: defaultOnWarn,
8593
}

packages/compiler/src/transforms/transformElement.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,20 @@ function transformComponentElement(
8787
singleRoot: boolean,
8888
context: TransformContext<JSXElement>,
8989
) {
90-
let asset = true
90+
let asset = context.options.withFallback
9191

92-
const fromSetup = tag
93-
if (fromSetup) {
94-
tag = fromSetup
95-
asset = false
96-
}
9792
const dotIndex = tag.indexOf('.')
9893
if (dotIndex > 0) {
9994
const ns = tag.slice(0, dotIndex)
10095
if (ns) {
10196
tag = ns + tag.slice(dotIndex)
102-
asset = false
10397
}
10498
}
99+
100+
if (tag.includes('-')) {
101+
asset = true
102+
}
103+
105104
if (asset) {
106105
context.component.add(tag)
107106
}

packages/compiler/test/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ exports[`compiler: element transform > Fragment should not mark as single root 1
77
"
88
`;
99

10+
exports[`compiler: element transform > component with fallback 1`] = `
11+
"
12+
const _component_foo_bar = _resolveComponent("foo-bar")
13+
const n0 = _createComponentWithFallback(_component_foo_bar, null, null, true)
14+
return n0
15+
"
16+
`;
17+
1018
exports[`compiler: element transform > generate single root component 1`] = `
1119
"
1220
const n0 = _createComponent(Comp, null, null, true)

packages/compiler/test/transforms/transformElement.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ describe('compiler: element transform', () => {
8181
expect(code).toMatchSnapshot()
8282
expect(code).contains('_createComponent(Comp)')
8383
})
84+
85+
test('component with fallback', () => {
86+
const { code } = compileWithElementTransform(`<foo-bar />`)
87+
expect(code).toMatchSnapshot()
88+
expect(code).contains(
89+
'_createComponentWithFallback(_component_foo_bar, null, null, true)',
90+
)
91+
})
8492
})

0 commit comments

Comments
 (0)