0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/docs/publishing.md

95 lines
3.1 KiB
Markdown
Raw Normal View History

2021-06-07 09:39:57 -05:00
# Publishing Astro components
Astro is able to use components from most popular frameworks such as React, Vue, Svelte and Preact out of the box. You can also write components in the `.astro` format.
Every framework recommends different methods for publishing components, this document talks about how we suggest you publish Astro components to [npm](https://www.npmjs.com/).
2021-06-15 14:29:01 -05:00
## Astro component use cases
2021-06-07 09:39:57 -05:00
Astro components are server-only and provide a lightweight HTML-like syntax.
This makes Astro components a good match for anything that doesn't need to be interactive in the client. Astro comes with a few built-in components such as the [Prism](https://prismjs.com/) component which you can use like so:
```jsx
---
import { Prism } from 'astro/components';
---
<Prism code={`const foo = 'bar';`} />
```
This component provides syntax highlighting for code blocks. Since this never changes in the client it makes sense to use an Astro component (it's equally reasonable to use a framework component for this kind of thing; Astro is server-only by default for all frameworks!).
## Publishing components
Some frameworks, such as [React](https://reactjs.org/) recommend pre-compiling components to JavaScript and publishing the artifacts. Astro currently doesn't have a way to pre-compile components, so we recommend publishing the `.astro` files directly.
Here's an example project with a couple of components.
```
/my-components/
├── package.json
├── index.js
├── capitalize.astro
└── bold.astro
```
2021-06-07 09:41:07 -05:00
Where **index.js** looks like this:
2021-06-07 09:39:57 -05:00
```js
export { default as Capitalize } from './capitalize.astro';
export { default as Bold } from './bold.astro';
```
2021-06-07 09:41:07 -05:00
In your **package.json** define an [exports entry](https://nodejs.org/api/packages.html) like so:
2021-06-07 09:39:57 -05:00
```json
{
"name": "@example/my-components",
"version": "1.0.0",
"exports": "./index.js"
}
```
This will allow consumers to import your components like so:
```svelte
---
import { Bold, Capitalize } from '@example/my-components';
---
<Capitalize phrase={`Hello world`} />
```
### Importing astro components directly
2021-06-07 09:41:07 -05:00
Above we created an index file that re-exports our components, which gives us the ability to publish several components in a single package. Since Astro components are server only we don't need to worry about tree-shaking concerns.
2021-06-07 09:39:57 -05:00
However you can also import published `.astro` files directly, in the same manner that you import `.astro` files in your own project.
2021-06-07 09:41:07 -05:00
Change the above **package.json** to this:
2021-06-07 09:39:57 -05:00
```json
{
"name": "@example/my-components",
"version": "1.0.0",
"exports": {
".": "./index.js",
"./bold.astro": "./bold.astro",
"./capitalize.astro": "./capitalize.astro"
}
}
```
2021-06-07 09:41:07 -05:00
The `"."` is used to signify the package's main module. We set it to the **index.js** file to allow the import method shown above.
2021-06-07 09:39:57 -05:00
Adding `"./bold.astro"` and `"./capitalize.astro"` to the exports field also allows consumers to import the components directly, by file name, like so:
```svelte
---
import Capitalize from '@example/my-components/capitalize.astro';
---
<Capitalize phrase={`Hello world`} />
2021-06-07 09:41:07 -05:00
```