Skip to content

File

File

tsx
import React from "react"
import { createRoot, File } from '@kubb/react'

const root = createRoot()

const Component = () => {
  return (
    <File baseName="test.ts" path="path">
      <File.Import name={'React'} path="react" print />
    </File>
  )
}

root.render(<Component />)
root.output
//   ^?
typescript
export function test() {
  return true
}

API

WARNING

baseName or path can be used, you can not use them together!

tsx
import React from "react"
import { File } from '@kubb/react'

type Props = React.ComponentProps<typeof File>
PropertyDescriptionTypeDefault
baseNameName to be used to dynamicly create the baseName(based on input.path).string-
pathPath will be full qualified path to a specified file.string-
idUnique identifier to reuse later.string | undefinedcrypto.randomUUID()
envThis will override process.env[key] inside the source, see getFileSource.NodeJS.ProcessEnv | undefined-
overrideThis will call fileManager.add instead of fileManager.addOrAppend, adding the source when the files already exists.boolean | undefinedfalse
metaAdd extra meta to a file.object | undefined-
childrenKubbNode | undefined-

File.Import

tsx
import React from "react"
import { createRoot, File } from '@kubb/react'

const root = createRoot()

const Component = () => {
  return <File.Import name={'React'} path="react" print />
}

root.render(<Component />)
root.output
//   ^?
tsx
import React from "react"
import { createRoot, File } from '@kubb/react'

const root = createRoot()

const Component = () => {
  return <File.Import name={'React'} path="react" isTypeOnly print />
}

root.render(<Component />)
root.output
//   ^?
typescript
import React from 'react'
typescript
import type React from 'react'

API

tsx
import React from "react"
import { File } from '@kubb/react'

type Props = React.ComponentProps<typeof File.Import>
PropertyDescriptionTypeDefault
nameImport name to be used.
Examples: ["useState"], "React"
string | Array<string>-
pathPath for the import.
Examples: "@kubb/core"
string-
isTypeOnlyAdd type prefix to the import, this will result in: import type { Type } from './path'.boolean | undefined-
printWhen true, it will return the generated import. When false, it will add the import to a KubbFile instance(see fileManager).boolean | undefined-
rootWhen root is set it will get the path with relative getRelativePath(root, path).string | undefined-

File.Export

tsx
import React from "react"
import { createRoot, File } from '@kubb/react'

const root = createRoot()

const Component = () => {
  return <File.Export path="kubb" print />
}

root.render(<Component />)
root.output
//   ^?
typescript
export * from 'kubb'

API

tsx
import React from "react"
import { File } from '@kubb/react'

type Props = React.ComponentProps<typeof File.Export>
PropertyDescriptionTypeDefault
nameImport name to be used.
Examples: ["useState"], "React"
string | Array<string> | undefined-
pathPath for the import.
Examples: "@kubb/core"
string-
isTypeOnlyAdd type prefix to the import, this will result in: import type { Type } from './path'.boolean | undefined-
asAliasMake it possible to override the name, this will result in: export * as aliasName from './path'boolean | undefined-
printWhen true, it will return the generated import. When false, it will add the import to a KubbFile instance(see fileManager).boolean | undefined-

File.Source

tsx
import React from "react"
import { createRoot, File } from '@kubb/react'

const root = createRoot()

const Component = () => {
  return (
    <File baseName="test.ts" path="path">
      <File.Source>test</File.Source>
    </File>
  )
}

root.render(<Component />)
root.output
//   ^?
tsx
import React from "react"
import { createRoot, File } from '@kubb/react'

const root = createRoot()

const Component = () => {
  return (
    <File baseName="test.ts" path="path">
      <File.Source path={path.resolve(__dirname, './test.ts')} print></File.Source>
    </File>
  )
}

root.render(<Component />)
root.output
//   ^?
typescript
test
typescript
export const resultFromTestDotTs = "hello world";

API

WARNING

path or children can be used, you can not use them together!

tsx
import React from "react"
import { File } from '@kubb/react'

type Props = React.ComponentProps<typeof File.Source>
PropertyDescriptionTypeDefault
pathWhen path is set it will copy-paste that file as a string inside the component.string-
printWhen true, it will return the generated import. When false, it will add the import to a KubbFile instance(see fileManager).boolean-
removeCommentsRemoves comments.boolean-
noEmitHelpersWhen set it can override the print of the TypeScript compiler.boolean-
childrenKubbNode-

Released under the MIT License.