-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c125ec6
commit f18d9d3
Showing
3 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
code/lib/cli/src/automigrate/fixes/webpack5-compiler-setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import execa from 'execa'; | ||
import prompts from 'prompts'; | ||
import type { SupportedFrameworks } from '@storybook/types'; | ||
import { frameworkPackages } from '@storybook/core-common'; | ||
import type { Fix } from '../types'; | ||
import { getFrameworkPackageName } from '../helpers/mainConfigFile'; | ||
import { frameworkToDefaultBuilder } from '../../helpers'; | ||
import { CoreBuilder } from '../../project_types'; | ||
|
||
type Options = { | ||
shouldAddSWC: boolean; | ||
shouldRemoveSWCFlag: boolean; | ||
isNextJs: boolean; | ||
}; | ||
|
||
export const webpack5Migration: Fix<Options> = { | ||
id: 'webpack5-compiler-setup', | ||
|
||
async check({ mainConfig, packageManager }) { | ||
const frameworkPackageName = getFrameworkPackageName(mainConfig); | ||
const frameworkName = Object.entries(frameworkPackages).find( | ||
([name]) => name === frameworkPackageName | ||
)?.[1]; | ||
|
||
const builder = frameworkName | ||
? // TODO: Consider using also custom builders | ||
frameworkToDefaultBuilder[frameworkName] | ||
: await (async () => { | ||
const webpackVersion = await packageManager.getPackageVersion('webpack'); | ||
return !!webpackVersion ? CoreBuilder.Webpack5 : CoreBuilder.Vite; | ||
})(); | ||
|
||
if (builder !== CoreBuilder.Webpack5) { | ||
return null; | ||
} | ||
|
||
const excludedFrameworks: SupportedFrameworks[] = ['angular', 'ember', 'nextjs']; | ||
|
||
const isExcludedFramework = frameworkName ? excludedFrameworks.includes(frameworkName) : false; | ||
|
||
if (isExcludedFramework) { | ||
return null; | ||
} | ||
|
||
const usesSWC = mainConfig.framework?.options?.builder?.useSWC; | ||
|
||
if (!isExcludedFramework) { | ||
return { | ||
shouldAddSWC: usesSWC === true, | ||
shouldRemoveSWCFlag: 'useSWC' in mainConfig.framework?.options?.builder, | ||
isNextJs: packageManager.type === 'next.js', | ||
}; | ||
} | ||
|
||
return {}; | ||
}, | ||
|
||
async prompt({ shouldAddSWC, shouldRemoveSWCFlag, isNextJs }) { | ||
let message = 'We need to update your Storybook configuration for Webpack 5.\n'; | ||
if (shouldRemoveSWCFlag) { | ||
message += 'The "frameworks.options.builder.useSWC" flag will be removed.\n'; | ||
} | ||
|
||
if (shouldAddSWC !== undefined) { | ||
const addonName = shouldAddSWC | ||
? '@storybook/addon-webpack5-compiler-swc' | ||
: '@storybook/addon-webpack5-compiler-babel'; | ||
message += `The "${addonName}" will be added to your project.\n`; | ||
|
||
message += | ||
'After the migration, you can switch compilers if needed by manually adding the alternative addon.'; | ||
} | ||
|
||
if (isNextJs) { | ||
message += 'Next.js-specific migrations will be applied.'; | ||
} | ||
|
||
return message; | ||
}, | ||
|
||
async run({ result, mainConfigPath, dryRun }) { | ||
const { shouldAddSWC, shouldRemoveSWCFlag, isNextJs } = result; | ||
|
||
if (shouldRemoveSWCFlag && !dryRun) { | ||
// Logic for remove `frameworks.options.builder.useSWC` from mainConfig | ||
} | ||
|
||
if (shouldAddSWC !== undefined && !dryRun) { | ||
const compiler = shouldAddSWC ? 'swc' : 'babel'; | ||
const addonName = `@storybook/addon-webpack5-compiler-${compiler}`; | ||
const command = `npx storybook@next add ${addonName}`; | ||
|
||
try { | ||
await execa.command(command, { stdio: 'inherit' }); | ||
} catch (error) { | ||
console.error(`Failed to install ${addonName}`, error); | ||
throw new Error(`Failed to install ${addonName}`); | ||
} | ||
} | ||
|
||
if (isNextJs && !dryRun) { | ||
// Next.js-specific migration logic | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters