Skip to content
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
32 changes: 32 additions & 0 deletions packages/next/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,37 @@ const config: StorybookConfig = {
},
},
}),
webpackFinal: async (config) => {
// Exclude tailwind.css from all existing CSS rules
config.module?.rules?.forEach((rule) => {
if (
rule &&
typeof rule === 'object' &&
'test' in rule &&
rule.test?.toString().includes('css')
) {
rule.exclude = rule.exclude
? [rule.exclude, /tailwind\.css$/].flat()
: /tailwind\.css$/
}
})

// Add our custom rule for tailwind.css
config.module?.rules?.push({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You never want optional chaining here and instead is non-null assertions. The rule must always be appended.

test: /tailwind\.css$/,
use: [
{
loader: 'style-loader',
options: {
injectType: 'lazyStyleTag',
insert: require.resolve('../next-devtools-inject-tailwind.js'),
},
},
'css-loader',
'postcss-loader',
],
})
return config
},
}
export default config
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export function ShadowPortal({ children }: { children: React.ReactNode }) {
if (portalNode.current.shadowRoot === null) {
shadowNode.current = portalNode.current.attachShadow({ mode: 'open' })

// Storybook injects the Tailwind style tag from .storybook/main.ts on each full load.
// When navigating to a different story, the injected style tag is removed within the iframe.
// To re-inject the style tag, we need to call `unuse()` since the `use()` function only runs
// once unless unused.
if (process.env.STORYBOOK) {
tailwindCss.unuse()
}
Comment on lines +44 to +46

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this problematic because it leaks into our published bundle? Why can't this be handled within Storybook?

Whatever is injecting the stylesheet in our user bundle should always be used in our Storybook instead of having custom Storybook specific logic in the bundle.


// Injecting Tailwind to the Shadow DOM with Webpack style-loader.
// The target is passed to the next-devtools-inject-tailwind.js file which runs on the browser.
// x-ref: https://webpack.js.org/loaders/style-loader/#lazystyletag
Expand Down