A basic component library built with [vanilla-extract](https://vanilla-extract.style/) for use in my NextJS sites, namely [prawn.cadell.dev](https://prawn.cadell.dev/) and [emojis.cadell.dev](https://emojis.cadell.dev/).
npm install cadells-vanilla-componentsA basic component library built with vanilla-extract for use in my NextJS sites, namely prawn.cadell.dev and emojis.cadell.dev.
It uses tsup and np for building and publishing to NPM.
- npm run build - Build the package.
- npm run release - Publish a new version to NPM.
- npm run build-release - Build and Publish to NPM.
Cadell's NextJS Template uses this package.
1. Add cadells-vanilla-components.
```
npm install cadells-vanilla-components @fontsource/source-sans-pro
`
1. Add MDX dependencies.
`
npm install --save-dev @next/mdx
`
1. Add vanilla-extract dependencies.
`
npm install --save-dev @vanilla-extract/css @vanilla-extract/next-plugin
next.config.js
1. Setup .`
const withMDX = require("@next/mdx")({
extension: /\.mdx$/,
});
const { createVanillaExtractPlugin } = require("@vanilla-extract/next-plugin");
const withVanillaExtract = createVanillaExtractPlugin();
const nextConfig = {
pageExtensions: ["js", "jsx", "ts", "tsx", "md", "mdx"],
};
module.exports = withVanillaExtract(withMDX(nextConfig));
`_document.tsx
1. Configure inside /pages.`
import { InitialiseTheme } from "cadells-vanilla-components";
import Document, { Html, Head, Main, NextScript } from "next/document";
export default class MyDocument extends Document {
static async getInitialProps(ctx: any) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
render() {
return (
1. Configure _app.tsx inside /pages.
`
import { Container, ThemeToggle } from "cadells-vanilla-components";
import "cadells-vanilla-components/dist/index.css";
import "@fontsource/source-sans-pro/400.css";
import "@fontsource/source-sans-pro/600.css"; const App = ({ Component, pageProps }) => (
);
export default App;
`
1. Create mdx-components.tsx.
`
import { mdComponents } from "cadells-vanilla-components"; export function useMDXComponents() {
return mdComponents;
}
``
References:
- https://nextjs.org/docs/advanced-features/using-mdx#setup-nextmdx-in-nextjs
- https://vanilla-extract.style/documentation/integrations/next/
Creating this package was a bit harder than first thought but I'm happy with the result and I think emojis.cadell.dev looks great. I originally tried using tsdx but the typescript types weren't correct and I couldn't work out why. I decided to change direction and started looking for a tool based on esbuild instead of Rollup and found tsup which pretty much worked out of the box. Happy days! Tsdx did introduce me to np for publishing to NPM so I kept that and it works really well. Everything happened for a reason.
- https://github.com/seek-oss/vanilla-extract/discussions/281
- https://github.com/egoist/tsup
- https://github.com/sindresorhus/np
- Couldn't get this method working: https://souporserious.com/bundling-typescript-with-esbuild-for-npm/