Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example using @next/plugin-storybook + fix webpack rules #23020

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/with-storybook-advanced/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": []
}
37 changes: 37 additions & 0 deletions examples/with-storybook-advanced/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel

# Storybook
/storybook-static
8 changes: 8 additions & 0 deletions examples/with-storybook-advanced/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
stories: ['../stories/*.stories.@(ts|tsx|js|jsx|mdx)'],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
'@next/plugin-storybook',
],
}
16 changes: 16 additions & 0 deletions examples/with-storybook-advanced/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const parameters = {
options: {
storySort: (a, b) => {
// We want the Welcome story at the top
if (b[1].kind === 'Welcome') {
return 1
}

// Sort the other stories by ID
// https://github.com/storybookjs/storybook/issues/548#issuecomment-530305279
return a[1].kind === b[1].kind
? 0
: a[1].id.localeCompare(b[1].id, { numeric: true })
},
},
}
43 changes: 43 additions & 0 deletions examples/with-storybook-advanced/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Example app with Storybook

This example shows a default set up of Storybook that includes the same build features as Next (built-in CSS support, module path aliases...).

/!\ This example includes experimental features. Use `with-storybook` for a simpler but safer example.

## TypeScript

As of v6.0, Storybook has built-in TypeScript support, so no configuration is needed. If you want to customize the default configuration, refer to the [TypeScript docs](https://storybook.js.org/docs/react/configure/typescript).

## Deploy your own

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-storybook&project-name=with-storybook&repository-name=with-storybook)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example with-storybook with-storybook-app
# or
yarn create next-app --example with-storybook with-storybook-app
```

### Run Storybook

```bash
npm run storybook
# or
yarn storybook
```

### Build Static Storybook

```bash
npm run build-storybook
# or
yarn build-storybook
```

You can use [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) to deploy Storybook. Specify `storybook-static` as the output directory.
5 changes: 5 additions & 0 deletions examples/with-storybook-advanced/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import helloWorld from '@/hello'

export default function Home() {
return <div>{helloWorld}</div>
}
8 changes: 8 additions & 0 deletions examples/with-storybook-advanced/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["lib/*"]
}
}
}
1 change: 1 addition & 0 deletions examples/with-storybook-advanced/lib/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Hello world'
25 changes: 25 additions & 0 deletions examples/with-storybook-advanced/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "with-storybook",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
},
"license": "MIT",
"devDependencies": {
"@next/plugin-storybook": "^10.0.8",
"@storybook/addon-essentials": "6.0.26",
"@storybook/addon-links": "6.0.26",
"@storybook/react": "6.0.26",
"babel-loader": "^8.0.5"
}
}
10 changes: 10 additions & 0 deletions examples/with-storybook-advanced/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import HelloWorld from '../components'

export default function Home() {
return (
<div>
<h1>Simple Storybook Example</h1>
<HelloWorld />
</div>
)
}
25 changes: 25 additions & 0 deletions examples/with-storybook-advanced/stories/button.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import { Button } from '@storybook/react/demo'

export default {
title: 'Button',
argTypes: { onClick: { action: 'clicked' } },
}

const TemplateWithText = (args) => <Button {...args}>Hello Button</Button>

const TemplateWithEmoji = (args) => (
<Button {...args}>
<span role="img" aria-label="so cool">
😀 😎 👍 💯
</span>
</Button>
)

export const withText = TemplateWithText.bind({})

withText.args = {}

export const withSomeEmoji = TemplateWithEmoji.bind({})

withSomeEmoji.args = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react'
import HelloWorld from '../components'

export default { title: 'Hello World' }

export const simpleComponent = () => <HelloWorld />
7 changes: 7 additions & 0 deletions examples/with-storybook-advanced/stories/welcome.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'
import { linkTo } from '@storybook/addon-links'
import { Welcome } from '@storybook/react/demo'

export default { title: 'Welcome' }

export const toStorybook = () => <Welcome showApp={linkTo('Button')} />
4 changes: 2 additions & 2 deletions packages/next-plugin-storybook/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function webpackFinal(config) {
...nextWebpackConfig.resolve,
}

config.module.rules = {
config.module.rules = [
...filterModuleRules(config),
...nextWebpackConfig.module.rules.map((rule) => {
// we need to resolve next-babel-loader since it's not available
Expand All @@ -37,7 +37,7 @@ async function webpackFinal(config) {
}
return rule
}),
}
]

return config
}
Expand Down