diff --git a/packages/storybook/src/generators/configuration/__snapshots__/configuration-v7.spec.ts.snap b/packages/storybook/src/generators/configuration/__snapshots__/configuration-v7.spec.ts.snap index f9576de26bd30..f8cad2b5943ba 100644 --- a/packages/storybook/src/generators/configuration/__snapshots__/configuration-v7.spec.ts.snap +++ b/packages/storybook/src/generators/configuration/__snapshots__/configuration-v7.spec.ts.snap @@ -5,8 +5,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 basic functionalities sh import type { StorybookConfig, Options } from '@storybook/core-common'; - - const config: StorybookConfig = { ...rootMain, stories: [ @@ -14,10 +12,12 @@ const config: StorybookConfig = { '../**/*.stories.@(js|jsx|ts|tsx|mdx)' ], addons: [...(rootMain.addons || []) - ] + ], framework: { name: '@storybook/angular', - options: {}, + options: { + + }, }, }; @@ -26,6 +26,7 @@ module.exports = config; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -138,8 +139,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 basic functionalities sh import type { StorybookConfig, Options } from '@storybook/core-common'; - - const config: StorybookConfig = { ...rootMain, stories: [ @@ -147,10 +146,12 @@ const config: StorybookConfig = { '../**/*.stories.@(js|jsx|ts|tsx|mdx)' ], addons: [...(rootMain.addons || []) - ] + ], framework: { name: '@storybook/angular', - options: {}, + options: { + + }, }, }; @@ -159,6 +160,7 @@ module.exports = config; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -191,9 +193,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 basic functionalities sh exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook configuration for all types of projects should contain the correct configuration in "apps/main-vite/.storybook/" 1`] = ` "const rootMain = require('../../../.storybook/main'); -const { mergeConfig } = require('vite'); -const viteTsConfigPaths = require('vite-tsconfig-paths').default; - module.exports = { ...rootMain, @@ -204,18 +203,15 @@ module.exports = { addons: [...rootMain.addons ], - async viteFinal(config, { configType }) { - return mergeConfig(config, { - plugins: [ - viteTsConfigPaths({ - root: '../../../', - }), - ], - }); - }, framework: { name: '@storybook/react-vite', - options: {}, + options: { + + builder: { + viteConfigPath: 'apps/main-vite/vite.config.ts', + }, + + }, }, }; @@ -223,6 +219,7 @@ module.exports = { // To customize your Vite configuration you can use the viteFinal field. // Check https://storybook.js.org/docs/react/builders/vite#configuration +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " `; @@ -253,9 +250,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook configuration for all types of projects should contain the correct configuration in "apps/main-vite-ts/.storybook/" 1`] = ` "const rootMain = require('../../../.storybook/main'); -const { mergeConfig } = require('vite'); -const viteTsConfigPaths = require('vite-tsconfig-paths').default; - module.exports = { ...rootMain, @@ -266,18 +260,15 @@ module.exports = { addons: [...rootMain.addons ], - async viteFinal(config, { configType }) { - return mergeConfig(config, { - plugins: [ - viteTsConfigPaths({ - root: '../../../', - }), - ], - }); - }, framework: { name: '@storybook/react-vite', - options: {}, + options: { + + builder: { + viteConfigPath: 'apps/main-vite-ts/vite.config.custom.ts', + }, + + }, }, }; @@ -285,6 +276,7 @@ module.exports = { // To customize your Vite configuration you can use the viteFinal field. // Check https://storybook.js.org/docs/react/builders/vite#configuration +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " `; @@ -316,9 +308,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi "const rootMain = require('../../../.storybook/main'); - - - module.exports = { ...rootMain, stories: [ @@ -327,15 +316,18 @@ module.exports = { ], addons: [...rootMain.addons , '@nrwl/react/plugins/storybook' - ] + ], framework: { name: '@storybook/react-webpack5', - options: {}, + options: { + + }, }, }; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -369,9 +361,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi "const rootMain = require('../../../.storybook/main'); - - - module.exports = { ...rootMain, stories: [ @@ -380,15 +369,18 @@ module.exports = { ], addons: [...rootMain.addons - ] + ], framework: { name: '@storybook/nextjs', - options: {}, + options: { + + }, }, }; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -418,9 +410,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi "const rootMain = require('../../../.storybook/main'); - - - module.exports = { ...rootMain, stories: [ @@ -429,15 +418,18 @@ module.exports = { ], addons: [...rootMain.addons , '@nrwl/react/plugins/storybook' , 'storybook-addon-swc' - ] + ], framework: { name: '@storybook/react-webpack5', - options: {}, + options: { + + }, }, }; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -470,9 +462,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook configuration for all types of projects should contain the correct configuration in "apps/wv1/.storybook/" 1`] = ` "const rootMain = require('../../../.storybook/main'); -const { mergeConfig } = require('vite'); -const viteTsConfigPaths = require('vite-tsconfig-paths').default; - module.exports = { ...rootMain, @@ -483,18 +472,15 @@ module.exports = { addons: [...rootMain.addons ], - async viteFinal(config, { configType }) { - return mergeConfig(config, { - plugins: [ - viteTsConfigPaths({ - root: '../../../', - }), - ], - }); - }, framework: { name: '@storybook/web-components-vite', - options: {}, + options: { + + builder: { + viteConfigPath: 'apps/wv1/vite.config.custom.ts', + }, + + }, }, }; @@ -502,6 +488,7 @@ module.exports = { // To customize your Vite configuration you can use the viteFinal field. // Check https://storybook.js.org/docs/react/builders/vite#configuration +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " `; @@ -529,9 +516,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi "const rootMain = require('../../../.storybook/main'); - - - module.exports = { ...rootMain, stories: [ @@ -540,15 +524,18 @@ module.exports = { ], addons: [...rootMain.addons - ] + ], framework: { name: '@storybook/web-components-webpack5', - options: {}, + options: { + + }, }, }; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -578,9 +565,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi "const rootMain = require('../../../.storybook/main'); - - - module.exports = { ...rootMain, stories: [ @@ -589,15 +573,18 @@ module.exports = { ], addons: [...rootMain.addons , '@nrwl/react/plugins/storybook' - ] + ], framework: { name: '@storybook/react-webpack5', - options: {}, + options: { + + }, }, }; // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " @@ -630,9 +617,6 @@ exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook confi exports[`@nrwl/storybook:configuration for Storybook v7 generate Storybook configuration for all types of projects should contain the correct configuration in "libs/react-vite/.storybook/" 1`] = ` "const rootMain = require('../../../.storybook/main'); -const { mergeConfig } = require('vite'); -const viteTsConfigPaths = require('vite-tsconfig-paths').default; - module.exports = { ...rootMain, @@ -643,18 +627,15 @@ module.exports = { addons: [...rootMain.addons ], - async viteFinal(config, { configType }) { - return mergeConfig(config, { - plugins: [ - viteTsConfigPaths({ - root: '../../../', - }), - ], - }); - }, framework: { name: '@storybook/react-vite', - options: {}, + options: { + + builder: { + viteConfigPath: 'libs/react-vite/vite.config.ts', + }, + + }, }, }; @@ -662,6 +643,7 @@ module.exports = { // To customize your Vite configuration you can use the viteFinal field. // Check https://storybook.js.org/docs/react/builders/vite#configuration +// and https://nx.dev/packages/storybook/documents/custom-builder-configs " `; @@ -711,6 +693,7 @@ Map { "defaultConfiguration": "production", "executor": "@nrwl/vite:build", "options": Object { + "configFile": "apps/main-vite/vite.config.ts", "outputPath": "dist/apps/main-vite", }, "outputs": Array [ @@ -841,6 +824,7 @@ Map { "defaultConfiguration": "production", "executor": "@nrwl/vite:build", "options": Object { + "configFile": "apps/main-vite-ts/vite.config.custom.ts", "outputPath": "dist/apps/main-vite-ts", }, "outputs": Array [ @@ -1723,6 +1707,7 @@ Map { "defaultConfiguration": "production", "executor": "@nrwl/vite:build", "options": Object { + "configFile": "apps/reapp/vite.config.ts", "outputPath": "dist/apps/reapp", }, "outputs": Array [ @@ -2015,6 +2000,7 @@ Map { "defaultConfiguration": "production", "executor": "@nrwl/vite:build", "options": Object { + "configFile": "apps/wv1/vite.config.custom.ts", "outputPath": "dist/apps/wv1", }, "outputs": Array [ diff --git a/packages/storybook/src/generators/configuration/configuration-v7.spec.ts b/packages/storybook/src/generators/configuration/configuration-v7.spec.ts index 843c3fb581aac..8a20abbcbf5f1 100644 --- a/packages/storybook/src/generators/configuration/configuration-v7.spec.ts +++ b/packages/storybook/src/generators/configuration/configuration-v7.spec.ts @@ -287,6 +287,15 @@ describe('@nrwl/storybook:configuration for Storybook v7', () => { ); } + tree.write('libs/react-vite/vite.config.ts', 'export default {}'); + tree.write('apps/main-vite/vite.config.ts', 'export default {}'); + tree.write( + 'apps/main-vite-ts/vite.config.custom.ts', + 'export default {}' + ); + tree.write('apps/reapp/vite.config.ts', 'export default {}'); + tree.write('apps/wv1/vite.config.custom.ts', 'export default {}'); + await configurationGenerator(tree, { name: 'main-vite', tsConfiguration: false, @@ -313,6 +322,7 @@ describe('@nrwl/storybook:configuration for Storybook v7', () => { storybook7betaConfiguration: true, storybook7UiFramework: '@storybook/react-webpack5', }); + await configurationGenerator(tree, { name: 'react-vite', uiFramework: '@storybook/react', diff --git a/packages/storybook/src/generators/configuration/configuration.ts b/packages/storybook/src/generators/configuration/configuration.ts index ec5b199a752c6..1154dc09d41f7 100644 --- a/packages/storybook/src/generators/configuration/configuration.ts +++ b/packages/storybook/src/generators/configuration/configuration.ts @@ -23,6 +23,7 @@ import { createRootStorybookDir, createRootStorybookDirForRootProject, getE2EProjectName, + getViteConfigFilePath, projectIsRootProjectInNestedWorkspace, updateLintConfig, } from './util-functions'; @@ -72,6 +73,8 @@ export async function configurationGenerator( ); } + let viteConfigFilePath: string | undefined; + if (viteBuildTarget) { if (schema.bundler !== 'vite') { logger.info( @@ -80,6 +83,12 @@ export async function configurationGenerator( ); schema.bundler = 'vite'; } + + viteConfigFilePath = getViteConfigFilePath( + tree, + root, + targets[viteBuildTarget]?.options?.configFile + ); } if (schema.storybook7betaConfiguration) { @@ -146,7 +155,8 @@ export async function configurationGenerator( !!nextBuildTarget, compiler === 'swc', schema.bundler === 'vite', - schema.storybook7betaConfiguration + schema.storybook7betaConfiguration, + viteConfigFilePath ); } else { createRootStorybookDir(tree, schema.js, schema.tsConfiguration); @@ -161,7 +171,8 @@ export async function configurationGenerator( !!nextBuildTarget, compiler === 'swc', schema.bundler === 'vite', - schema.storybook7betaConfiguration + schema.storybook7betaConfiguration, + viteConfigFilePath ); } diff --git a/packages/storybook/src/generators/configuration/project-files-7-ts/.storybook/main.ts__tmpl__ b/packages/storybook/src/generators/configuration/project-files-7-ts/.storybook/main.ts__tmpl__ index beebca15532d3..ac99343175c22 100644 --- a/packages/storybook/src/generators/configuration/project-files-7-ts/.storybook/main.ts__tmpl__ +++ b/packages/storybook/src/generators/configuration/project-files-7-ts/.storybook/main.ts__tmpl__ @@ -1,8 +1,6 @@ <% if (!isRootProject){ %>import { rootMain } from '<%= offsetFromRoot %>../.storybook/<%= rootMainName %>';<% } %> <% if (isRootProject){ %>import { rootMain } from './main.root';<% } %> import type { StorybookConfig, Options } from '@storybook/core-common'; -<% if (usesVite){ %>import { mergeConfig } from 'vite';<% } %> -<% if (usesVite){ %>import viteTsConfigPaths from 'vite-tsconfig-paths';<% } %> const config: StorybookConfig = { ...rootMain, @@ -13,19 +11,16 @@ const config: StorybookConfig = { <% } %>], addons: [...(rootMain.addons || []) <% if(uiFramework === '@storybook/react-webpack5') { %>, '@nrwl/react/plugins/storybook' <% } %><% if(uiFramework === '@storybook/react-native') { %>, '@storybook/addon-ondevice-actions', '@storybook/addon-ondevice-backgrounds', '@storybook/addon-ondevice-controls', '@storybook/addon-ondevice-notes' <% } %> <% if(usesSwc) { %>, 'storybook-addon-swc' <% } %> - ]<% if(usesVite) { %>, - async viteFinal(config: any) { - return mergeConfig(config, { - plugins: [ - viteTsConfigPaths({ - root: '<%= offsetFromRoot %>../', - }), - ], - }); - },<% } %> + ], framework: { name: '<%= uiFramework %>', - options: {}, + options: { + <% if (usesVite) { %> + builder: { + viteConfigPath: '<%= viteConfigFilePath %>', + }, + <% } %> + }, }, }; @@ -34,9 +29,11 @@ module.exports = config; <% if(!usesVite) { %> // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs <% } %> <% if(usesVite) { %> // To customize your Vite configuration you can use the viteFinal field. // Check https://storybook.js.org/docs/react/builders/vite#configuration +// and https://nx.dev/packages/storybook/documents/custom-builder-configs <% } %> \ No newline at end of file diff --git a/packages/storybook/src/generators/configuration/project-files-7/.storybook/main.js__tmpl__ b/packages/storybook/src/generators/configuration/project-files-7/.storybook/main.js__tmpl__ index 3ee5cd53fca23..fb5a06b917494 100644 --- a/packages/storybook/src/generators/configuration/project-files-7/.storybook/main.js__tmpl__ +++ b/packages/storybook/src/generators/configuration/project-files-7/.storybook/main.js__tmpl__ @@ -1,8 +1,5 @@ <% if (!isRootProject){ %>const rootMain = require('<%= offsetFromRoot %>../.storybook/<%= rootMainName %>');<% } %> <% if (isRootProject){ %>const rootMain = require('./main.root');<% } %> -<% if (usesVite){ %>const { mergeConfig } = require('vite');<% } %> -<% if (usesVite){ %>const viteTsConfigPaths = require('vite-tsconfig-paths').default;<% } %> -<% if (existsRootWebpackConfig && !usesVite){ %>const rootWebpackConfig = require('<%= offsetFromRoot %>../.storybook/webpack.config'); <% } %> module.exports = { ...rootMain, @@ -13,27 +10,26 @@ module.exports = { <% } %>], addons: [...rootMain.addons <% if(uiFramework === '@storybook/react-webpack5') { %>, '@nrwl/react/plugins/storybook' <% } %><% if(uiFramework === '@storybook/react-native') { %>, '@storybook/addon-ondevice-actions', '@storybook/addon-ondevice-backgrounds', '@storybook/addon-ondevice-controls', '@storybook/addon-ondevice-notes' <% } %> <% if(usesSwc) { %>, 'storybook-addon-swc' <% } %> - ]<% if(usesVite) { %>, - async viteFinal(config, { configType }) { - return mergeConfig(config, { - plugins: [ - viteTsConfigPaths({ - root: '<%= offsetFromRoot %>../', - }), - ], - }); - },<% } %> + ], framework: { name: '<%= uiFramework %>', - options: {}, + options: { + <% if (usesVite) { %> + builder: { + viteConfigPath: '<%= viteConfigFilePath %>', + }, + <% } %> + }, }, }; <% if(!usesVite) { %> // To customize your webpack configuration you can use the webpackFinal field. // Check https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config +// and https://nx.dev/packages/storybook/documents/custom-builder-configs <% } %> <% if(usesVite) { %> // To customize your Vite configuration you can use the viteFinal field. // Check https://storybook.js.org/docs/react/builders/vite#configuration +// and https://nx.dev/packages/storybook/documents/custom-builder-configs <% } %> \ No newline at end of file diff --git a/packages/storybook/src/generators/configuration/test-configs/various-projects.json b/packages/storybook/src/generators/configuration/test-configs/various-projects.json index 528b02fb6c205..7d8dfae252fa8 100644 --- a/packages/storybook/src/generators/configuration/test-configs/various-projects.json +++ b/packages/storybook/src/generators/configuration/test-configs/various-projects.json @@ -9,7 +9,10 @@ "executor": "@nrwl/vite:build", "outputs": ["{options.outputPath}"], "defaultConfiguration": "production", - "options": { "outputPath": "dist/apps/main-vite" }, + "options": { + "outputPath": "dist/apps/main-vite", + "configFile": "apps/main-vite/vite.config.ts" + }, "configurations": { "development": { "mode": "development" }, "production": { "mode": "production" } @@ -105,7 +108,10 @@ "executor": "@nrwl/vite:build", "outputs": ["{options.outputPath}"], "defaultConfiguration": "production", - "options": { "outputPath": "dist/apps/main-vite-ts" }, + "options": { + "outputPath": "dist/apps/main-vite-ts", + "configFile": "apps/main-vite-ts/vite.config.custom.ts" + }, "configurations": { "development": { "mode": "development" }, "production": { "mode": "production" } @@ -763,7 +769,10 @@ "executor": "@nrwl/vite:build", "outputs": ["{options.outputPath}"], "defaultConfiguration": "production", - "options": { "outputPath": "dist/apps/reapp" }, + "options": { + "outputPath": "dist/apps/reapp", + "configFile": "apps/reapp/vite.config.ts" + }, "configurations": { "development": { "mode": "development" }, "production": { "mode": "production" } @@ -978,7 +987,10 @@ "executor": "@nrwl/vite:build", "outputs": ["{options.outputPath}"], "defaultConfiguration": "production", - "options": { "outputPath": "dist/apps/wv1" }, + "options": { + "outputPath": "dist/apps/wv1", + "configFile": "apps/wv1/vite.config.custom.ts" + }, "configurations": { "development": { "mode": "development" }, "production": { "mode": "production" } diff --git a/packages/storybook/src/generators/configuration/util-functions.ts b/packages/storybook/src/generators/configuration/util-functions.ts index 31c8322745f70..031d4f9d54ddf 100644 --- a/packages/storybook/src/generators/configuration/util-functions.ts +++ b/packages/storybook/src/generators/configuration/util-functions.ts @@ -342,7 +342,8 @@ export function createRootStorybookDirForRootProject( isNextJs?: boolean, usesSwc?: boolean, usesVite?: boolean, - usesV7?: boolean + usesV7?: boolean, + viteConfigFilePath?: string ) { const rootConfigExists = tree.exists('.storybook/main.root.js') || @@ -397,6 +398,7 @@ export function createRootStorybookDirForRootProject( usesSwc, usesVite, isRootProject: true, + viteConfigFilePath, }); if (js) { @@ -413,7 +415,8 @@ export function createProjectStorybookDir( isNextJs?: boolean, usesSwc?: boolean, usesVite?: boolean, - usesV7?: boolean + usesV7?: boolean, + viteConfigFilePath?: string ) { const { root, projectType } = readProjectConfiguration(tree, projectName); @@ -462,6 +465,7 @@ export function createProjectStorybookDir( usesSwc, usesVite, isRootProject: false, + viteConfigFilePath, }); if (js) { @@ -568,3 +572,17 @@ export async function getE2EProjectName( ); return e2eProject; } + +export function getViteConfigFilePath( + tree: Tree, + projectRoot: string, + configFile?: string +): string | undefined { + return configFile && tree.exists(configFile) + ? configFile + : tree.exists(joinPathFragments(`${projectRoot}/vite.config.ts`)) + ? joinPathFragments(`${projectRoot}/vite.config.ts`) + : tree.exists(joinPathFragments(`${projectRoot}/vite.config.js`)) + ? joinPathFragments(`${projectRoot}/vite.config.js`) + : undefined; +}