Storybook preset for seamless integration with Create React App projects
npx @tessl/cli install tessl/npm-storybook--preset-create-react-app@9.1.0The @storybook/preset-create-react-app package provides a Storybook preset that enables seamless integration with Create React App (CRA) projects. It automatically configures Webpack and Babel settings to match the host CRA application's build configuration, ensuring consistent behavior between the main app and Storybook environment.
npm install -D @storybook/preset-create-react-appThis preset is typically configured in Storybook's main configuration file and does not export user-facing functions for direct import. The preset works automatically when added to your Storybook configuration.
Note: This preset does not export functions for direct use. It works as a Storybook addon/preset that modifies webpack configuration automatically.
The simplest way to use this preset is by adding it to your Storybook configuration:
// .storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
'@storybook/preset-create-react-app',
'@storybook/addon-essentials',
],
};For advanced customization with options:
// .storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
{
name: '@storybook/preset-create-react-app',
options: {
scriptsPackageName: '@my/react-scripts',
craOverrides: {
fileLoaderExcludes: ['pdf'],
},
},
},
'@storybook/addon-essentials',
],
};The preset provides core Storybook configuration that disables default webpack settings to allow full CRA integration. This is handled internally by the preset.
/**
* Internal core configuration - automatically applied by the preset
* Disables Storybook's default webpack configuration to prevent conflicts with CRA
*/
interface CoreConfiguration {
disableWebpackDefaults: true;
}The preset automatically merges CRA and Storybook webpack configurations. This processing is handled internally by the preset when it's loaded.
/**
* Internal webpack configuration processing - automatically applied by the preset
* Merges CRA's webpack configuration with Storybook's configuration
*/
interface WebpackProcessing {
// Automatically processes and merges:
// - JavaScript/TypeScript compilation rules
// - CSS processing rules
// - Asset handling rules
// - Plugin merging with conflict resolution
// - Module resolution paths
}Configuration interface for customizing the preset behavior.
// Plugin configuration options interface
interface PluginOptions {
configDir: string;
packageJson?: { version?: string };
presets: {
apply: (key: string) => Promise<any>;
};
presetsList?: Array<string | { name: string }>;
/**
* Optionally set the package name of a react-scripts fork. In most cases, the package is located
* automatically by this preset.
*/
scriptsPackageName?: string;
/** Overrides for Create React App's Webpack configuration. */
craOverrides?: {
fileLoaderExcludes?: string[];
};
typescriptOptions?: {
reactDocgen: 'react-docgen-typescript' | 'react-docgen' | false;
reactDocgenTypescriptOptions: RDTSPluginOptions;
};
}
// Type definitions for react-docgen-typescript plugin options
interface RDTSPluginOptions {
[key: string]: any;
}The preset includes several internal capabilities that work automatically:
// .storybook/main.js
module.exports = {
addons: ['@storybook/preset-create-react-app'],
};// .storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
'@storybook/preset-create-react-app',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-links',
],
framework: {
name: '@storybook/react-webpack5',
options: {},
},
};// .storybook/main.js - For projects using forked react-scripts
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
{
name: '@storybook/preset-create-react-app',
options: {
scriptsPackageName: '@my/react-scripts', // Custom react-scripts fork
},
},
'@storybook/addon-essentials',
],
framework: {
name: '@storybook/react-webpack5',
options: {},
},
};// .storybook/main.js - Exclude additional file types from asset processing
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
{
name: '@storybook/preset-create-react-app',
options: {
craOverrides: {
fileLoaderExcludes: ['pdf', 'doc', 'docx', 'zip'], // Additional exclusions
},
},
},
'@storybook/addon-essentials',
],
framework: {
name: '@storybook/react-webpack5',
options: {},
},
};// .storybook/main.js - Full configuration example
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
{
name: '@storybook/preset-create-react-app',
options: {
scriptsPackageName: 'react-scripts', // Can be customized for forks
craOverrides: {
fileLoaderExcludes: ['pdf', 'doc'], // Additional file exclusions
},
typescriptOptions: {
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
shouldExtractLiteralValuesFromEnum: true,
propFilter: (prop) => {
if (prop.parent) {
return !prop.parent.fileName.includes('node_modules');
}
return true;
},
},
},
},
},
'@storybook/addon-essentials',
'@storybook/addon-interactions',
],
framework: {
name: '@storybook/react-webpack5',
options: {},
},
typescript: {
check: false,
checkOptions: {},
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
shouldExtractLiteralValuesFromEnum: true,
propFilter: (prop) => {
if (prop.parent) {
return !prop.parent.fileName.includes('node_modules');
}
return true;
},
},
},
};The preset includes built-in error handling for common scenarios: