Skip to content

Commit 9a78f23

Browse files
committed
Fix Export issue
1 parent 5b43897 commit 9a78f23

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/code-impl.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,13 @@ export function registerCodegen(ctx: typeof figma) {
176176
console.info(`[benchmark] devup-ui end ${Date.now() - time}ms`)
177177

178178
// Check if node itself is SECTION or has a parent SECTION
179-
const sectionNode = ResponsiveCodegen.canGenerateResponsive(node)
179+
const isNodeSection = ResponsiveCodegen.canGenerateResponsive(node)
180+
const parentSection = ResponsiveCodegen.hasParentSection(node)
181+
const sectionNode = isNodeSection
180182
? (node as SectionNode)
181-
: ResponsiveCodegen.hasParentSection(node)
183+
: parentSection
184+
// When parent is Section (not node itself), use Page postfix and export default
185+
const isParentSection = !isNodeSection && parentSection !== null
182186
let responsiveResult: {
183187
title: string
184188
language: 'TYPESCRIPT' | 'BASH'
@@ -190,10 +194,14 @@ export function registerCodegen(ctx: typeof figma) {
190194
const responsiveCodegen = new ResponsiveCodegen(sectionNode)
191195
const responsiveCode =
192196
await responsiveCodegen.generateResponsiveCode()
193-
const sectionComponentName = toPascal(sectionNode.name)
197+
const baseName = toPascal(sectionNode.name)
198+
const sectionComponentName = isParentSection
199+
? `${baseName}Page`
200+
: baseName
194201
const wrappedCode = wrapComponent(
195202
sectionComponentName,
196203
responsiveCode,
204+
{ exportDefault: isParentSection },
197205
)
198206
const sectionCodes: ReadonlyArray<readonly [string, string]> = [
199207
[sectionComponentName, wrappedCode],
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { paddingLeftMultiline } from './padding-left-multiline'
22

3-
export function wrapComponent(name: string, code: string) {
3+
interface WrapComponentOptions {
4+
exportDefault?: boolean
5+
}
6+
7+
export function wrapComponent(
8+
name: string,
9+
code: string,
10+
options: WrapComponentOptions = {},
11+
) {
12+
const { exportDefault = false } = options
413
const hasNewLine = code.includes('\n')
5-
return `export function ${name}() {
14+
const exportKeyword = exportDefault ? 'export default' : 'export'
15+
return `${exportKeyword} function ${name}() {
616
return ${hasNewLine ? `(\n${paddingLeftMultiline(code, 2)}\n )` : code}
717
}`
818
}

0 commit comments

Comments
 (0)