Skip to content

Instantly share code, notes, and snippets.

@cometkim
Created September 8, 2020 10:48
Show Gist options
  • Save cometkim/242d759e76e23b2bdd1723be87ac3d84 to your computer and use it in GitHub Desktop.
Save cometkim/242d759e76e23b2bdd1723be87ac3d84 to your computer and use it in GitHub Desktop.
The minimal Storybook(v6) configuration for Gatsby + TypeScript + Pnpm
module.exports = {
stories: [
'../src/**/*.stories.mdx',
'../src/**/*.stories.@(js|jsx|ts|tsx)',
],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
],
webpackFinal: async config => {
// Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
// Use pnpm modules
config.module.rules[0].exclude = [/node_modules\/\.pnpm\/(?!(gatsby.*)\/)/];
config.module.rules[0].use[0].loader = require.resolve('babel-loader');
config.module.rules[0].use[0].options.presets = [
require.resolve('babel-preset-gatsby'),
require.resolve('@babel/preset-typescript'),
];
config.module.rules[0].use[0].options.plugins = [
require.resolve('babel-plugin-remove-graphql-queries'),
];
// Prefer Gatsby ES6 entrypoint (module) over commonjs (main) entrypoint
config.resolve.mainFields = ['browser', 'module', 'main'];
return config;
},
};
import { action } from '@storybook/addon-actions';
// Gatsby's Link overrides:
// Gatsby Link calls the `enqueue` & `hovering` methods on the global variable ___loader.
// This global object isn't set in storybook context, requiring you to override it to empty functions (no-op),
// so Gatsby Link doesn't throw any errors.
global.___loader = {
enqueue: () => {},
hovering: () => {},
};
// This global variable is prevents the "__BASE_PATH__ is not defined" error inside Storybook.
global.__BASE_PATH__ = '/';
// Navigating through a gatsby app using gatsby-link or any other gatsby component will use the `___navigate` method.
// In Storybook it makes more sense to log an action than doing an actual navigate. Checkout the actions addon docs for more info: https://github.com/storybookjs/storybook/tree/master/addons/actions.
window.___navigate = pathname => {
action('NavigateTo:')(pathname);
};
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment