Skip to content

Commit

Permalink
feat(storybook): generate config for vite (#13381)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored Nov 25, 2022
1 parent 07584fd commit 2d860ea
Show file tree
Hide file tree
Showing 31 changed files with 767 additions and 234 deletions.
3 changes: 1 addition & 2 deletions docs/generated/packages/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -2266,8 +2266,7 @@
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
"description": "Add a Storybook Test-Runner target."
}
},
"additionalProperties": false,
Expand Down
9 changes: 7 additions & 2 deletions docs/generated/packages/react.json
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,13 @@
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
"description": "Add a Storybook Test-Runner target."
},
"bundler": {
"description": "The Storybook builder to use.",
"enum": ["vite", "webpack"],
"x-prompt": "Which Storybook builder do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
15 changes: 13 additions & 2 deletions docs/generated/packages/storybook.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@
"@storybook/react-native"
],
"x-prompt": "What UI framework plugin should storybook use?"
},
"bundler": {
"description": "The bundler to use.",
"enum": ["vite", "webpack"],
"x-prompt": "Which bundler do you want to use?",
"default": "webpack"
}
},
"presets": []
Expand Down Expand Up @@ -186,8 +192,13 @@
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
"description": "Add a Storybook Test-Runner target."
},
"bundler": {
"description": "The Storybook builder to use.",
"enum": ["vite", "webpack"],
"x-prompt": "Which Storybook builder do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ exports[`StorybookConfiguration generator should configure storybook to use webp
module.exports = {
...rootMain,
core: { ...rootMain.core, builder: 'webpack5' },
stories: [
...rootMain.stories,
'../**/*.stories.mdx',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
"description": "Add a Storybook Test-Runner target."
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { StorybookConfigureSchema } from './schema';
import storiesGenerator from '../stories/stories';
import {
convertNxGenerator,
logger,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import { configurationGenerator } from '@nrwl/storybook';
import { getE2eProjectName } from '@nrwl/cypress/src/utils/project-name';

async function generateStories(host: Tree, schema: StorybookConfigureSchema) {
const libConfig = readProjectConfiguration(host, schema.name);
const libRoot = libConfig.root;
const projectConfig = readProjectConfiguration(host, schema.name);
const cypressProject = getE2eProjectName(
schema.name,
libRoot,
projectConfig.root,
schema.cypressDirectory
);
await storiesGenerator(host, {
Expand All @@ -30,6 +30,22 @@ export async function storybookConfigurationGenerator(
host: Tree,
schema: StorybookConfigureSchema
) {
let bundler = schema.bundler;
const projectConfig = readProjectConfiguration(host, schema.name);

if (
projectConfig.projectType === 'application' &&
projectConfig.targets['build']?.executor === '@nrwl/vite:build'
) {
bundler = 'vite';
if (schema.bundler !== 'vite') {
logger.info(
`The project ${schema.name} is set up to use Vite. So
Storybook will be configured to use Vite as well.`
);
}
}

const installTask = await configurationGenerator(host, {
name: schema.name,
uiFramework: '@storybook/react',
Expand All @@ -39,6 +55,7 @@ export async function storybookConfigurationGenerator(
cypressDirectory: schema.cypressDirectory,
standaloneConfig: schema.standaloneConfig,
tsConfiguration: schema.tsConfiguration,
bundler,
});

if (schema.generateStories) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export interface StorybookConfigureSchema {
cypressDirectory?: string;
standaloneConfig?: boolean;
ignorePaths?: string[];
bundler?: 'webpack' | 'vite';
configureTestRunner?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@
},
"configureTestRunner": {
"type": "boolean",
"description": "Add a Storybook Test-Runner target.",
"x-prompt": "Add a Storybook Test-Runner target?"
"description": "Add a Storybook Test-Runner target."
},
"bundler": {
"description": "The Storybook builder to use.",
"enum": ["vite", "webpack"],
"x-prompt": "Which Storybook builder do you want to use?",
"default": "webpack"
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 1`] = `
"
import { rootMain } from './main.root';
import type { StorybookConfig, Options } from '@storybook/core-common';
const config: StorybookConfig = {
...rootMain,
core: { ...rootMain.core, builder: '@storybook/builder-vite' },
stories: [
...rootMain.stories,
'../src/app/**/*.stories.mdx',
'../src/app/**/*.stories.@(js|jsx|ts|tsx)'
],
addons: [...(rootMain.addons || []) , '@nrwl/react/plugins/storybook'
],
webpackFinal: async (config, { configType }: Options) => {
// apply any global webpack configs that might have been specified in .storybook/main.ts
if (rootMain.webpackFinal) {
config = await rootMain.webpackFinal(config, { configType } as Options);
}
// add your own webpack tweaks if needed
return config;
},
};
module.exports = config;
"
`;

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 2`] = `
"{
\\"extends\\": \\"../tsconfig.json\\",
\\"compilerOptions\\": {
\\"emitDecoratorMetadata\\": true
, \\"outDir\\": \\"\\"
},
\\"files\\": [
\\"../node_modules/@nrwl/react/typings/styled-jsx.d.ts\\",
\\"../node_modules/@nrwl/react/typings/cssmodule.d.ts\\",
\\"../node_modules/@nrwl/react/typings/image.d.ts\\"
],
\\"exclude\\": [\\"../**/*.spec.ts\\" , \\"../**/*.spec.js\\", \\"../**/*.spec.tsx\\", \\"../**/*.spec.jsx\\"],
\\"include\\": [
\\"../src/**/*.stories.ts\\",
\\"../src/**/*.stories.js\\",
\\"../src/**/*.stories.jsx\\",
\\"../src/**/*.stories.tsx\\",
\\"../src/**/*.stories.mdx\\",
\\"*.ts\\",
\\"*.js\\"]
}
"
`;

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 3`] = `""`;

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 4`] = `
"import type { StorybookConfig } from '@storybook/core-common';
export const rootMain: StorybookConfig = {
stories: [],
addons: ['@storybook/addon-essentials'],
// webpackFinal: async (config, { configType }) => {
// // Make whatever fine-grained changes you need that should apply to all storybook configs
// // Return the altered config
// return config;
// },
};
"
`;

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 5`] = `
"import { rootMain } from '../../../.storybook/main.root';
import type { StorybookConfig, Options } from '@storybook/core-common';
const config: StorybookConfig = {
...rootMain,
core: { ...rootMain.core, builder: 'webpack5' },
stories: [
...rootMain.stories,
'../src/app/**/*.stories.mdx',
'../src/app/**/*.stories.@(js|jsx|ts|tsx)'
],
addons: [...(rootMain.addons || []) , '@nrwl/react/plugins/storybook'
],
webpackFinal: async (config, { configType }: Options) => {
// apply any global webpack configs that might have been specified in .storybook/main.ts
if (rootMain.webpackFinal) {
config = await rootMain.webpackFinal(config, { configType } as Options);
}
// add your own webpack tweaks if needed
return config;
},
};
module.exports = config;
"
`;

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 6`] = `
"{
\\"extends\\": \\"../tsconfig.json\\",
\\"compilerOptions\\": {
\\"emitDecoratorMetadata\\": true
, \\"outDir\\": \\"\\"
},
\\"files\\": [
\\"../../../node_modules/@nrwl/react/typings/styled-jsx.d.ts\\",
\\"../../../node_modules/@nrwl/react/typings/cssmodule.d.ts\\",
\\"../../../node_modules/@nrwl/react/typings/image.d.ts\\"
],
\\"exclude\\": [\\"../**/*.spec.ts\\" , \\"../**/*.spec.js\\", \\"../**/*.spec.tsx\\", \\"../**/*.spec.jsx\\"],
\\"include\\": [
\\"../src/**/*.stories.ts\\",
\\"../src/**/*.stories.js\\",
\\"../src/**/*.stories.jsx\\",
\\"../src/**/*.stories.tsx\\",
\\"../src/**/*.stories.mdx\\",
\\"*.ts\\",
\\"*.js\\"]
}
"
`;

exports[`@nrwl/storybook:configuration for workspaces with Root project basic functionalities should generate Storybook files for nested first - then for root 7`] = `""`;
Loading

1 comment on commit 2d860ea

@vercel
Copy link

@vercel vercel bot commented on 2d860ea Nov 25, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.