Skip to content

Commit 84dfd3d

Browse files
committed
✨ Build imports and types automatically
1 parent 92c7be0 commit 84dfd3d

18 files changed

+60
-51
lines changed

scripts/build.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import fs from 'fs'
2+
import buildImports from './buildImports.js'
3+
import buildTypes from './buildTypes.js'
24

35
const folders = {
46
'src/icons': 'dist/icons',
@@ -7,12 +9,6 @@ const folders = {
79
}
810

911
const files = {
10-
'src/astro.d.ts': 'dist/astro.d.ts',
11-
'src/astro.js': 'dist/astro.js',
12-
'src/svelte.d.ts': 'dist/svelte.d.ts',
13-
'src/svelte.js': 'dist/svelte.js',
14-
'src/react.d.ts': 'dist/react.d.ts',
15-
'src/react.js': 'dist/react.js',
1612
'README.md': 'dist/README.md',
1713
'LICENSE': 'dist/LICENSE',
1814
'package.json': 'dist/package.json'
@@ -40,4 +36,12 @@ Object.keys(files).forEach(key => {
4036
})
4137
})
4238

39+
fs.writeFileSync('dist/astro.js', buildImports('astro'))
40+
fs.writeFileSync('dist/svelte.js', buildImports('svelte'))
41+
fs.writeFileSync('dist/react.js', buildImports('tsx'))
42+
43+
fs.writeFileSync('dist/astro.d.ts', buildTypes('astro'))
44+
fs.writeFileSync('dist/svelte.d.ts', buildTypes('svelte'))
45+
fs.writeFileSync('dist/react.d.ts', buildTypes('react'))
46+
4347
console.log('✅ Package built')

scripts/buildImports.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from 'fs'
2+
3+
const buildImports = extension => {
4+
const components = fs.readdirSync('src/components')
5+
6+
return components.map(component => {
7+
return `import ${component}Component from './components/${component}/${component}.${extension}'`
8+
}).join('\n')
9+
+ `\n\n`
10+
+ components.map(component => `export const ${component} = ${component}Component`).join('\n')
11+
}
12+
13+
export default buildImports

scripts/buildTypes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fs from 'fs'
2+
3+
const buildTypes = type => {
4+
const components = fs.readdirSync('src/components')
5+
6+
return `
7+
declare module 'webcoreui/${type}' {
8+
${components.map(component => {
9+
return `import type { ${component}Props } from './components/${component}/${component.toLowerCase()}'`
10+
}).join('\n\t')}
11+
12+
${components.map(component => {
13+
return `export function ${component}(_props: ${component}Props): any`
14+
}).join('\n\t')}
15+
}
16+
`
17+
}
18+
19+
export default buildTypes

src/astro.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/astro.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/components/Accordion/Accordion.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AccordionProps } from './accordion'
33
import ArrowDown from '../../icons/arrow-down.svg?raw'
44
import './accordion.scss'
55

6-
export const Accordion = ({ items }: AccordionProps) => {
6+
const Accordion = ({ items }: AccordionProps) => {
77
const [state, setState] = useState(Array(items.length).fill(false))
88

99
const toggle = (index: number) => {
@@ -32,3 +32,5 @@ export const Accordion = ({ items }: AccordionProps) => {
3232
</ul>
3333
)
3434
}
35+
36+
export default Accordion

src/components/Button/Button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import type { ButtonProps } from './button'
33
import './button.scss'
44

5-
export const Button = ({
5+
const Button = ({
66
theme,
77
bold,
88
href,
@@ -30,3 +30,5 @@ export const Button = ({
3030
</button>
3131
)
3232
}
33+
34+
export default Button

src/components/Card/Card.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
const Card = () => <div />
4+
5+
export default Card

src/components/Card/card.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type CardProps = {}

src/components/Icon/Icon.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script lang="ts">
22
import type { IconProps } from './icon'
33
44
import Github from '../../icons/github.svg?raw'

src/components/Icon/Icon.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const iconMap = {
99
'arrow-down': ArrowDown
1010
}
1111

12-
export const Icon = ({
12+
const Icon = ({
1313
type,
1414
size = 24,
1515
color
@@ -22,3 +22,5 @@ export const Icon = ({
2222

2323
return <div dangerouslySetInnerHTML={{ __html: icon }} />
2424
}
25+
26+
export default Icon

src/env.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/pages/accordion.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ComponentWrapper from '@static/ComponentWrapper.astro'
44
55
import AstroAccordion from '@components/Accordion/Accordion.astro'
66
import SvelteAccorion from '@components/Accordion/Accordion.svelte'
7-
import { Accordion as ReactAccordion } from '@components/Accordion/Accordion.tsx'
7+
import ReactAccordion from '@components/Accordion/Accordion.tsx'
88
99
const accordionItems = [{
1010
title: 'Do you offer support?',

src/pages/button.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ComponentWrapper from '@static/ComponentWrapper.astro'
55
import Icon from '@components/Icon/Icon.astro'
66
import AstroButton from '@components/Button/Button.astro'
77
import SvelteButton from '@components/Button/Button.svelte'
8-
import { Button as ReactButton } from '@components/Button/Button.tsx'
8+
import ReactButton from '@components/Button/Button.tsx'
99
---
1010

1111
<Layout>

src/react.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/react.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/svelte.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/svelte.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)