-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
How to enable storybook on node/server? #4082
Comments
You can mock anything using babel or webpack. But IMO the proper solution is to isolate UI out of any logic that is using state and of course file system. |
email is just a react component, and we use renderToString to send in email. the problem is that it can use any function of our server codebase, doing this we hit this problem do you have any example of how to mock using babel or webpack? |
The path I'm trying to follow is a custom webpack config I've tried some approaches: 1 - use fs: 'empty': module.exports = (storybookBaseConfig, configType) => {
storybookBaseConfig.node = {
fs: 'empty',
global: true,
};
return storybookBaseConfig;
}; this gave me
module.exports = (storybookBaseConfig, configType) => {
storybookBaseConfig.target = 'node'
return storybookBaseConfig;
}; then I've got Try to fix this, using DefinePlugin new webpack.DefinePlugin({
'global': {},
}), then I've got a |
let's say you have
fsMock.js: module.exports = {
readFileSync: () => 'mocked file',
// other things in fs that you are using
} webpack.config.js: const path = require('path');
module.exports = (storybookBaseConfig, configType) => {
storybookBaseConfig.resolve.alias = {
...storybookBaseConfig.resolve.alias,
'fs': path.resolve(__dirname, 'fsMock.js')
};
return storybookBaseConfig;
}; |
it worked well, tks |
can we use WebpackNodeExternals https://github.com/liady/webpack-node-externals/issues with storybook? |
What do you mean? |
instead of using I've tried, but got |
Can you share an example of the usage? |
const path = require('path');
const WebpackNodeExternals = require('webpack-node-externals');
module.exports = (storybookBaseConfig, configType) => {
storybookBaseConfig.resolve.alias = {
...storybookBaseConfig.resolve.alias,
'fs': path.resolve(__dirname, 'fsMock.js')
};
storybookBaseConfig.externals: [
WebpackNodeExternals({
whitelist: ['webpack/hot/poll?1000'],
}),
],
return storybookBaseConfig;
}; related to this https://github.com/entria/entria-fullstack/blob/master/packages/server/webpack/webpack.dev.js#L16 |
What is the error log by the way? |
Seems like you have to mock |
Here is a better solution for it: |
Putting together answers from @igor-dv and @Drillibit, and bumping to Storybook v5+: Add a Webpack config override as
You can replace/add Docs on overriding Storybook's Webpack config here |
Slightly better, and documented version of /**
* The doc doesn't really mention using webpack.config.js, but .storybook/main.js instead.
*
* Nevertheless, configuring the webpack.config.js seems to work fine.
*
* @param config
* @param mode
* @return {Promise<*>}
* @see https://storybook.js.org/docs/react/configure/webpack
* @see https://storybook.js.org/docs/react/configure/webpack#using-your-existing-config
*/
module.exports = async ({
config,
mode,
}) => {
/**
* Fixes npm packages that depend on `fs` module, etc.
*
* E.g: "winston" would fail to load without this, because it relies on fs, which isn't available during browser build.
*
* @see https://github.com/storybookjs/storybook/issues/4082#issuecomment-495370896
*/
config.node = {
fs: 'empty',
tls: 'empty',
net: 'empty',
module: 'empty',
console: true,
};
return config;
}; See full PR, adding Storybook to Next.js (TypeScript): UnlyEd/next-right-now#251 |
config.node = {
fs: 'empty',
tls: 'empty',
net: 'empty',
module: 'empty',
console: true,
}; does not work with webpack 5, is there a workaround for webpack 5? |
Can anybody explain why it works? |
it replaces the module to something else |
@PeiLiao Instead of trying to load That's my own understanding, not an authoritative answer. |
Hi @sibelius, I had the same issue until I discovered this note in the migration guide:
I ended up adding this to my /**
* Fixes issue with `next-i18next` and is ready for webpack@5
* @see https://github.com/isaachinman/next-i18next/issues/1012#issuecomment-792697008
* @see https://github.com/storybookjs/storybook/issues/4082#issuecomment-758272734
* @see https://webpack.js.org/migrate/5/
*/
config.resolve.fallback = {
fs: false,
tls: false,
net: false,
module: false,
path: require.resolve('path-browserify'),
} |
I did a lot of fallback and it is compiling now the only problem is |
It will end up looking like this
|
I'd like to run storybook on a server project, so I can have a storybook of all emails that we send
the problem with run at is that some dependencies requires node dependencies like this error message:
how can we solve this?
can we polyfill these?
The text was updated successfully, but these errors were encountered: