vite-plugin-storybook-nextjs 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -30
- package/dist/index.cjs +9 -2
- package/dist/index.js +9 -2
- package/dist/mocks/storybook.global.cjs +1 -0
- package/dist/mocks/storybook.global.js +1 -0
- package/dist/plugins/next-image/alias/next-image.d.cts +1 -1
- package/dist/plugins/next-image/alias/next-image.d.ts +1 -1
- package/dist/plugins/next-mocks/alias/headers/cookies.d.cts +1 -1
- package/dist/plugins/next-mocks/alias/headers/cookies.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,12 +5,12 @@ This is a Vite plugin that allows you to use Next.js features in Vite. It is the
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- **Next.js Integration**: Seamlessly integrate Next.js features into your Vite project.
|
|
8
|
-
- **Storybook Compatibility**: Acts as the foundation for `@storybook/experimental-nextjs-vite
|
|
8
|
+
- **Storybook Compatibility**: Acts as the foundation for [the `@storybook/experimental-nextjs-vite` framework](https://storybook.js.org/docs/get-started/frameworks/nextjs#with-vite), enabling you to use Storybook with Next.js in a Vite environment.
|
|
9
9
|
- **Portable Stories**: Ideal for running portable stories in Vitest, ensuring your components are tested in an environment that closely mirrors production.
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Install the plugin using your preferred package manager:
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
16
|
npm install vite-plugin-storybook-nextjs
|
|
@@ -22,11 +22,11 @@ pnpm add vite-plugin-storybook-nextjs
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
-
###
|
|
25
|
+
### Set up Vitest
|
|
26
26
|
|
|
27
|
-
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/)
|
|
27
|
+
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/).
|
|
28
28
|
|
|
29
|
-
### Add the plugin to your
|
|
29
|
+
### Add the plugin to your Vitest configuration
|
|
30
30
|
|
|
31
31
|
Add the plugin to your Vitest configuration file. This ensures that Vitest is aware of the Next.js features provided by the plugin.
|
|
32
32
|
|
|
@@ -40,16 +40,6 @@ export default defineConfig({
|
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
### Usage with portable stories
|
|
44
|
-
|
|
45
|
-
[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.
|
|
46
|
-
|
|
47
|
-
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.
|
|
48
|
-
|
|
49
|
-
#### Experimental @storybook/experimental-vitest-plugin
|
|
50
|
-
|
|
51
|
-
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.
|
|
52
|
-
|
|
53
43
|
## Configuration Options
|
|
54
44
|
|
|
55
45
|
You can configure the plugin using the following options:
|
|
@@ -64,39 +54,65 @@ type VitePluginOptions = {
|
|
|
64
54
|
};
|
|
65
55
|
```
|
|
66
56
|
|
|
57
|
+
## Usage with portable stories
|
|
58
|
+
|
|
59
|
+
[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.
|
|
60
|
+
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
## Automatic story transformation
|
|
64
|
+
|
|
65
|
+
(⚠️ **Experimental**)
|
|
66
|
+
|
|
67
|
+
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.
|
|
68
|
+
|
|
67
69
|
## Limitations and differences to the Webpack5-based integration of Next.js in Storybook
|
|
68
70
|
|
|
69
|
-
### next/font staticDir mapping obsolete
|
|
71
|
+
### `next/font` `staticDir` mapping obsolete
|
|
70
72
|
|
|
71
|
-
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
|
|
73
|
+
You don't need to map your custom font directory in Storybook's `staticDir` configuration. Instead, Vite will automatically serve the files in the `public` directory and provide assets during production builds.
|
|
72
74
|
|
|
73
75
|
### CSS/SASS
|
|
74
76
|
|
|
75
|
-
The `sassOptions` in `next.config.js` is not supported. Please use Vite's configuration options to configure the Sass compiler:
|
|
77
|
+
The `sassOptions` property in `next.config.js` is not supported. Please use Vite's configuration options to configure the Sass compiler:
|
|
76
78
|
|
|
77
79
|
```js
|
|
78
80
|
css: {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
preprocessorOptions: {
|
|
82
|
+
scss: {
|
|
83
|
+
quietDeps: true
|
|
84
|
+
},
|
|
85
|
+
}
|
|
84
86
|
},
|
|
85
87
|
```
|
|
86
88
|
|
|
87
89
|
### Next.js: Server Actions
|
|
88
90
|
|
|
89
|
-
When
|
|
91
|
+
When testing components that rely on Next.js Server Actions, you need to ensure that your story files are [set up to use the `jsdom` environment in Vitest](https://vitest.dev/config/#environment). This can be done in two ways:
|
|
90
92
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
```
|
|
93
|
+
1. To apply it to individual story files, add a special comment at the top of each file:
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
// @vitest-environment jsdom
|
|
97
|
+
```
|
|
98
|
+
2. To apply it to all tests, adjust your Vitest configuration:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// vitest.config.ts
|
|
102
|
+
import { defineConfig } from "vite";
|
|
103
|
+
import nextjs from "vite-plugin-storybook-nextjs";
|
|
94
104
|
|
|
95
|
-
|
|
105
|
+
export default defineConfig({
|
|
106
|
+
plugins: [nextjs()],
|
|
107
|
+
vitest: {
|
|
108
|
+
environment: "jsdom", // 👈 Add this
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
```
|
|
96
112
|
|
|
97
|
-
|
|
113
|
+
### SWC Mode
|
|
98
114
|
|
|
99
|
-
Only Next.js in SWC mode is supported. If
|
|
115
|
+
Only [Next.js in SWC mode](https://nextjs.org/docs/architecture/nextjs-compiler) is supported. If your project was forced to opt out of Babel for some reason, you will very likely encounter issues with this plugin (e.g., Emotion support in SWC is still lagging behind).
|
|
100
116
|
|
|
101
117
|
## License
|
|
102
118
|
|
package/dist/index.cjs
CHANGED
|
@@ -753,6 +753,9 @@ var vitePluginNextDynamic = () => ({
|
|
|
753
753
|
// src/utils.ts
|
|
754
754
|
var VITEST_PLUGIN_NAME = "vite-plugin-storybook-nextjs";
|
|
755
755
|
var isVitestEnv = process.env.VITEST === "true";
|
|
756
|
+
function getExecutionEnvironment(config) {
|
|
757
|
+
return isVitestEnv && config.test?.browser?.enabled !== true ? "node" : "browser";
|
|
758
|
+
}
|
|
756
759
|
var require3 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)));
|
|
757
760
|
var getEntryPoint = (subPath, env) => require3.resolve(`${VITEST_PLUGIN_NAME}/${env}/mocks/${subPath}`);
|
|
758
761
|
var getAlias = (env) => ({
|
|
@@ -905,8 +908,8 @@ var getAlias2 = (env) => ({
|
|
|
905
908
|
});
|
|
906
909
|
var vitePluginNextMocks = () => ({
|
|
907
910
|
name: "vite-plugin-next-mocks",
|
|
908
|
-
config: (
|
|
909
|
-
const aliasEnv =
|
|
911
|
+
config: (config) => {
|
|
912
|
+
const aliasEnv = getExecutionEnvironment(config);
|
|
910
913
|
return {
|
|
911
914
|
resolve: {
|
|
912
915
|
alias: getAlias2(aliasEnv)
|
|
@@ -927,6 +930,7 @@ function VitePlugin({ dir = process.cwd() } = {}) {
|
|
|
927
930
|
async config(config, env) {
|
|
928
931
|
const phase = env.mode === "development" ? constants_js.PHASE_DEVELOPMENT_SERVER : env.mode === "test" ? constants_js.PHASE_TEST : constants_js.PHASE_PRODUCTION_BUILD;
|
|
929
932
|
nextConfigResolver.resolve(await loadConfig__default.default(phase, resolvedDir));
|
|
933
|
+
const executionEnvironment = getExecutionEnvironment(config);
|
|
930
934
|
return {
|
|
931
935
|
...!isVitestEnv && {
|
|
932
936
|
resolve: {
|
|
@@ -945,6 +949,9 @@ function VitePlugin({ dir = process.cwd() } = {}) {
|
|
|
945
949
|
"next/dist/compiled/react/jsx-runtime.js"
|
|
946
950
|
),
|
|
947
951
|
react: require6.resolve("next/dist/compiled/react"),
|
|
952
|
+
"react-dom/server": require6.resolve(
|
|
953
|
+
executionEnvironment === "node" ? "next/dist/compiled/react-dom/server.js" : "next/dist/compiled/react-dom/server.browser.js"
|
|
954
|
+
),
|
|
948
955
|
"react-dom/test-utils": require6.resolve(
|
|
949
956
|
"next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js"
|
|
950
957
|
),
|
package/dist/index.js
CHANGED
|
@@ -738,6 +738,9 @@ var vitePluginNextDynamic = () => ({
|
|
|
738
738
|
// src/utils.ts
|
|
739
739
|
var VITEST_PLUGIN_NAME = "vite-plugin-storybook-nextjs";
|
|
740
740
|
var isVitestEnv = process.env.VITEST === "true";
|
|
741
|
+
function getExecutionEnvironment(config) {
|
|
742
|
+
return isVitestEnv && config.test?.browser?.enabled !== true ? "node" : "browser";
|
|
743
|
+
}
|
|
741
744
|
var require3 = createRequire(import.meta.url);
|
|
742
745
|
var getEntryPoint = (subPath, env) => require3.resolve(`${VITEST_PLUGIN_NAME}/${env}/mocks/${subPath}`);
|
|
743
746
|
var getAlias = (env) => ({
|
|
@@ -890,8 +893,8 @@ var getAlias2 = (env) => ({
|
|
|
890
893
|
});
|
|
891
894
|
var vitePluginNextMocks = () => ({
|
|
892
895
|
name: "vite-plugin-next-mocks",
|
|
893
|
-
config: (
|
|
894
|
-
const aliasEnv =
|
|
896
|
+
config: (config) => {
|
|
897
|
+
const aliasEnv = getExecutionEnvironment(config);
|
|
895
898
|
return {
|
|
896
899
|
resolve: {
|
|
897
900
|
alias: getAlias2(aliasEnv)
|
|
@@ -912,6 +915,7 @@ function VitePlugin({ dir = process.cwd() } = {}) {
|
|
|
912
915
|
async config(config, env) {
|
|
913
916
|
const phase = env.mode === "development" ? PHASE_DEVELOPMENT_SERVER : env.mode === "test" ? PHASE_TEST : PHASE_PRODUCTION_BUILD;
|
|
914
917
|
nextConfigResolver.resolve(await loadConfig(phase, resolvedDir));
|
|
918
|
+
const executionEnvironment = getExecutionEnvironment(config);
|
|
915
919
|
return {
|
|
916
920
|
...!isVitestEnv && {
|
|
917
921
|
resolve: {
|
|
@@ -930,6 +934,9 @@ function VitePlugin({ dir = process.cwd() } = {}) {
|
|
|
930
934
|
"next/dist/compiled/react/jsx-runtime.js"
|
|
931
935
|
),
|
|
932
936
|
react: require6.resolve("next/dist/compiled/react"),
|
|
937
|
+
"react-dom/server": require6.resolve(
|
|
938
|
+
executionEnvironment === "node" ? "next/dist/compiled/react-dom/server.js" : "next/dist/compiled/react-dom/server.browser.js"
|
|
939
|
+
),
|
|
933
940
|
"react-dom/test-utils": require6.resolve(
|
|
934
941
|
"next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js"
|
|
935
942
|
),
|
|
@@ -60,6 +60,7 @@ var getAlias2 = (env) => ({
|
|
|
60
60
|
var require4 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)));
|
|
61
61
|
moduleAlias__default.default.addAliases({
|
|
62
62
|
react: "next/dist/compiled/react",
|
|
63
|
+
"react-dom/server": "next/dist/compiled/react-dom/server.js",
|
|
63
64
|
"react-dom/test-utils": require4.resolve(
|
|
64
65
|
"next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js"
|
|
65
66
|
),
|
|
@@ -53,6 +53,7 @@ var getAlias2 = (env) => ({
|
|
|
53
53
|
var require4 = createRequire(import.meta.url);
|
|
54
54
|
moduleAlias.addAliases({
|
|
55
55
|
react: "next/dist/compiled/react",
|
|
56
|
+
"react-dom/server": "next/dist/compiled/react-dom/server.js",
|
|
56
57
|
"react-dom/test-utils": require4.resolve(
|
|
57
58
|
"next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js"
|
|
58
59
|
),
|
|
@@ -2,7 +2,7 @@ import * as next_dist_shared_lib_get_img_props from 'next/dist/shared/lib/get-im
|
|
|
2
2
|
import * as _NextImage from 'next/image';
|
|
3
3
|
import React__default from 'next/dist/compiled/react';
|
|
4
4
|
|
|
5
|
-
declare const MockedNextImage: React__default.ForwardRefExoticComponent<Omit<React__default.DetailedHTMLProps<React__default.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, "
|
|
5
|
+
declare const MockedNextImage: React__default.ForwardRefExoticComponent<Omit<React__default.DetailedHTMLProps<React__default.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, "src" | "srcSet" | "ref" | "width" | "height" | "loading" | "alt"> & {
|
|
6
6
|
src: string | next_dist_shared_lib_get_img_props.StaticImport;
|
|
7
7
|
alt: string;
|
|
8
8
|
width?: number | `${number}` | undefined;
|
|
@@ -2,7 +2,7 @@ import * as next_dist_shared_lib_get_img_props from 'next/dist/shared/lib/get-im
|
|
|
2
2
|
import * as _NextImage from 'next/image';
|
|
3
3
|
import React__default from 'next/dist/compiled/react';
|
|
4
4
|
|
|
5
|
-
declare const MockedNextImage: React__default.ForwardRefExoticComponent<Omit<React__default.DetailedHTMLProps<React__default.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, "
|
|
5
|
+
declare const MockedNextImage: React__default.ForwardRefExoticComponent<Omit<React__default.DetailedHTMLProps<React__default.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, "src" | "srcSet" | "ref" | "width" | "height" | "loading" | "alt"> & {
|
|
6
6
|
src: string | next_dist_shared_lib_get_img_props.StaticImport;
|
|
7
7
|
alt: string;
|
|
8
8
|
width?: number | `${number}` | undefined;
|
|
@@ -4,7 +4,7 @@ import { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies/index.j
|
|
|
4
4
|
|
|
5
5
|
declare class RequestCookiesMock extends RequestCookies {
|
|
6
6
|
get: vitest.Mock<[name: string] | [next_dist_compiled__edge_runtime_cookies.RequestCookie], next_dist_compiled__edge_runtime_cookies.RequestCookie | undefined>;
|
|
7
|
-
getAll: vitest.Mock<[next_dist_compiled__edge_runtime_cookies.RequestCookie] | [name: string]
|
|
7
|
+
getAll: vitest.Mock<[] | [next_dist_compiled__edge_runtime_cookies.RequestCookie] | [name: string], next_dist_compiled__edge_runtime_cookies.RequestCookie[]>;
|
|
8
8
|
has: vitest.Mock<[name: string], boolean>;
|
|
9
9
|
set: vitest.Mock<[key: string, value: string] | [options: next_dist_compiled__edge_runtime_cookies.RequestCookie], this>;
|
|
10
10
|
delete: vitest.Mock<[names: string | string[]], boolean | boolean[]>;
|
|
@@ -4,7 +4,7 @@ import { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies/index.j
|
|
|
4
4
|
|
|
5
5
|
declare class RequestCookiesMock extends RequestCookies {
|
|
6
6
|
get: vitest.Mock<[name: string] | [next_dist_compiled__edge_runtime_cookies.RequestCookie], next_dist_compiled__edge_runtime_cookies.RequestCookie | undefined>;
|
|
7
|
-
getAll: vitest.Mock<[next_dist_compiled__edge_runtime_cookies.RequestCookie] | [name: string]
|
|
7
|
+
getAll: vitest.Mock<[] | [next_dist_compiled__edge_runtime_cookies.RequestCookie] | [name: string], next_dist_compiled__edge_runtime_cookies.RequestCookie[]>;
|
|
8
8
|
has: vitest.Mock<[name: string], boolean>;
|
|
9
9
|
set: vitest.Mock<[key: string, value: string] | [options: next_dist_compiled__edge_runtime_cookies.RequestCookie], this>;
|
|
10
10
|
delete: vitest.Mock<[names: string | string[]], boolean | boolean[]>;
|