Skip to content

Commit

Permalink
Merge pull request #9 from storybookjs/valentin/major-release
Browse files Browse the repository at this point in the history
Add docs
  • Loading branch information
valentinpalkovic authored Aug 5, 2024
2 parents e56bc45 + bba6403 commit 3b3b214
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 75 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pnpm dev
Run the tests in `./example` to test the plugin

```bash
pnpm test
pnpm test:all
```

OR
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Kitiho <https://github.com/kitiho>
Copyright (c) 2024 Storybook <https://github.com/storybookjs>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
98 changes: 97 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
# vite-plugin-storybook-nextjs

This is a Vite plugin that allows you to use Next.js features in Vite. The plugin is still in early development and is not yet ready for production use.
This is a Vite plugin that allows you to use Next.js features in Vite. It is the basis for `@storybook/nextjs-vite` and should be used when running portable stories in Vitest.

## Features

- **Next.js Integration**: Seamlessly integrate Next.js features into your Vite project.
- **Storybook Compatibility**: Acts as the foundation for `@storybook/nextjs-vite`, enabling you to use Storybook with Next.js in a Vite environment.
- **Portable Stories**: Ideal for running portable stories in Vitest, ensuring your components are tested in an environment that closely mirrors production.

## Installation

To install the plugin, use your package manager of choice:

```sh
npm install vite-plugin-storybook-nextjs
# or
yarn add vite-plugin-storybook-nextjs
# or
pnpm add vite-plugin-storybook-nextjs
```

## Usage

### Setup Vitest

To use the plugin, you need to set up Vitest in your project. You can do this by following the instructions in the [Vitest documentation](https://vitest.dev/guide/)

### Add the plugin to your vitest configuration

Add the plugin to your Vitest configuration file. This ensures that Vitest is aware of the Next.js features provided by the plugin.

```ts
// vitest.config.ts
import { defineConfig } from "vite";
import nextjs from "vite-plugin-storybook-nextjs";

export default defineConfig({
plugins: [nextjs()],
});
```

### Usage with portable stories

[Portable stories](https://storybook.js.org/docs/api/portable-stories/portable-stories-vitest) are Storybook stories which can be used in external environments, such as Vitest.

This plugin is necessary to run portable stories in Vitest, as it provides the necessary Next.js features to ensure that your components are tested in an environment that closely mirrors production.

#### Experimental @storybook/experimental-vitest-plugin

The experimental `@storybook/experimental-vitest-plugin` can be used to automatically transform your stories at Vitest runtime to in-memory test files. This allows you to run your stories in a Vitest environment without needing to manually transform your stories. Please visit https://github.com/storybookjs/vitest-plugin for more information.

## Configuration Options

You can configure the plugin using the following options:

```ts
type VitePluginOptions = {
/**
* Provide the path to your Next.js project directory
* @default process.cwd()
*/
dir?: string;
};
```

## Limitations and differences to the Webpack5-based integration of Next.js in Storybook

### next/font staticDir mapping obsolete

You don't need to map your custom font directory in Storybook's staticDir configuration. Vite will automatically serve the files in the public directory and provide assets during production build.

### CSS/SASS

The `sassOptions` in `next.config.js` is not supported. Please use Vite's configuration options to configure the Sass compiler:

```js
css: {
preprocessorOptions: {
scss: {
quietDeps: true
},
}
},
```

### Next.js: Server Actions

When using components that rely on Next.js Server Actions, there are some limitations to be aware of. Specifically, you need to ensure that your story files are set up to use the jsdom environment in Vitest for the case that your default Virtual DOM environment is set to happy-dom. This can be done by adding a special comment at the top of your story files:

```js
// @vitest-environment jsdom
```

This comment ensures that the components using Next.js Server Actions are properly handled in a jsdom environment, which is necessary for them to function correctly in Vitest.

## License

This project is licensed under the MIT License.
60 changes: 0 additions & 60 deletions TODO.md

This file was deleted.

15 changes: 3 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@
"name": "vite-plugin-storybook-nextjs",
"version": "0.0.15",
"description": "",
"keywords": [
"vite-plugin",
"nextjs",
"storybook",
"vitest"
],
"keywords": ["vite-plugin", "nextjs", "storybook", "vitest"],
"author": "Storybook Bot <[email protected]>",
"license": "MIT",
"sideEffects": [
"./src/polyfills/promise-with-resolvers.ts"
],
"files": [
"dist"
],
"sideEffects": ["./src/polyfills/promise-with-resolvers.ts"],
"files": ["dist"],
"type": "module",
"exports": {
".": {
Expand Down

0 comments on commit 3b3b214

Please sign in to comment.