-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(react-storybook-addon-export-to-sandbox): make addon generic so it can be published and used outside this repo #29674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hotell
merged 1 commit into
microsoft:master
from
Hotell:hotell/storybook/make-export-to-sandbox-addon-generic
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
2 changes: 0 additions & 2 deletions
2
packages/react-components/babel-preset-storybook-full-source/src/index.dev.d.ts
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
packages/react-components/babel-preset-storybook-full-source/src/index.dev.js
This file was deleted.
Oops, something went wrong.
4 changes: 3 additions & 1 deletion
4
packages/react-components/babel-preset-storybook-full-source/src/index.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export { fullSourcePlugin } from './fullsource'; | ||
| import { fullSourcePlugin } from './fullsource'; | ||
|
|
||
| export type { BabelPluginOptions } from './types'; | ||
| export default fullSourcePlugin; |
This file contains hidden or 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
6 changes: 5 additions & 1 deletion
6
packages/react-components/react-storybook-addon-export-to-sandbox/preset.js
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| /* eslint-disable no-shadow */ | ||
|
|
||
| const preset = require('./lib/preset/preset'); | ||
|
|
||
| function config(entry = []) { | ||
| return [...entry, require.resolve('./lib/preset/preview')]; | ||
| } | ||
|
|
||
| module.exports = { config }; | ||
| module.exports = { config, ...preset }; |
5 changes: 5 additions & 0 deletions
5
packages/react-components/react-storybook-addon-export-to-sandbox/src/preset/preset.ts
This file contains hidden or 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,5 @@ | ||
| import { webpack, WebpackFinalConfig, WebpackFinalOptions } from '../webpack'; | ||
|
|
||
| export function webpackFinal(config: WebpackFinalConfig, options: WebpackFinalOptions) { | ||
| return webpack(config, options); | ||
| } |
16 changes: 16 additions & 0 deletions
16
packages/react-components/react-storybook-addon-export-to-sandbox/src/preset/preview.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| import type { Parameters } from '@storybook/addons'; | ||
| import type { StoryContextForEnhancers } from '@storybook/csf'; | ||
|
|
||
| import { withExportToSandboxButton } from '../decorators/with-export-to-sandbox-button'; | ||
|
|
||
| export const decorators = [withExportToSandboxButton]; | ||
|
|
||
| export const parameters: Parameters = { | ||
| docs: { | ||
| /** | ||
| * Override source code shown within "Show Code" Docs tab. | ||
| * @see https://github.com/storybookjs/storybook/blob/release-6-5/addons/docs/docs/recipes.md#customizing-source-snippets | ||
| */ | ||
| transformSource: (source: string, storyContext: StoryContextForEnhancers) => { | ||
| // This config renders story source generated via `fullSource` parameter that is being added by @fluentui/babel-preset-storybook-full-source plugin, which is registered as part of this preset | ||
| return storyContext.parameters.fullSource; | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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
4 changes: 2 additions & 2 deletions
4
packages/react-components/react-storybook-addon-export-to-sandbox/src/types.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| import type { StoryContext as StoryContextOrigin, Parameters } from '@storybook/addons'; | ||
| import type { ParametersExtension } from './public-types'; | ||
| import type { ParametersExtension, PresetConfig } from './public-types'; | ||
|
|
||
| export interface StoryContext extends StoryContextOrigin { | ||
| parameters: Parameters & ParametersExtension; | ||
| } | ||
|
|
||
| export type { ParametersExtension }; | ||
| export type { ParametersExtension, PresetConfig }; |
68 changes: 68 additions & 0 deletions
68
packages/react-components/react-storybook-addon-export-to-sandbox/src/webpack.spec.ts
This file contains hidden or 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,68 @@ | ||
| import { PresetConfig } from './public-types'; | ||
| import { webpack, WebpackFinalOptions } from './webpack'; | ||
| describe(`webpack`, () => { | ||
| it(`should register webpack preset with defaults`, () => { | ||
| const actual = webpack({ module: { rules: [] } }, { | ||
| presetsList: [ | ||
| { | ||
| name: 'node_modules/@fluentui/react-storybook-addon-export-to-sandbox/lib/preset.js', | ||
| preset: {}, | ||
| options: {}, | ||
| }, | ||
| ], | ||
| } as WebpackFinalOptions); | ||
|
|
||
| expect(actual.module?.rules).toEqual([ | ||
| { | ||
| enforce: 'post', | ||
| test: /\.stories\.(jsx?$|tsx?$)/, | ||
| use: { | ||
| loader: 'babel-loader', | ||
| options: { | ||
| plugins: [[expect.stringContaining('babel-preset-storybook-full-source'), undefined]], | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it(`should register webpack preset with user provided options`, () => { | ||
| const actual = webpack({ module: { rules: [] } }, { | ||
| presetsList: [ | ||
| { | ||
| name: 'node_modules/@fluentui/react-storybook-addon-export-to-sandbox/lib/preset.js', | ||
| preset: {}, | ||
| options: { | ||
| importMappings: { | ||
| '@proj/foo': { replace: '@proj/moo' }, | ||
| }, | ||
| webpackRule: { test: /\.stories\.tsx?/, include: /foo-stories/ }, | ||
| babelLoaderOptionsUpdater: value => { | ||
| return Object.assign(value, { presets: ['babel-foo-bar-preset'] }); | ||
| }, | ||
| } as PresetConfig, | ||
| }, | ||
| ], | ||
| } as WebpackFinalOptions); | ||
|
|
||
| expect(actual.module?.rules).toEqual([ | ||
| { | ||
| enforce: 'post', | ||
| test: /\.stories\.tsx?/, | ||
| include: /foo-stories/, | ||
| use: { | ||
| loader: 'babel-loader', | ||
| options: { | ||
| plugins: [ | ||
| [ | ||
| expect.stringContaining('babel-preset-storybook-full-source'), | ||
| { '@proj/foo': { replace: '@proj/moo' } }, | ||
| ], | ||
| ], | ||
| presets: ['babel-foo-bar-preset'], | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
| }); | ||
| }); |
70 changes: 70 additions & 0 deletions
70
packages/react-components/react-storybook-addon-export-to-sandbox/src/webpack.ts
This file contains hidden or 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,70 @@ | ||
| import type { PresetConfig } from './types'; | ||
|
|
||
| type WebpackFinalFn = NonNullable<import('@storybook/core-common').StorybookConfig['webpackFinal']>; | ||
| export type WebpackFinalConfig = Parameters<WebpackFinalFn>[0]; | ||
| export type WebpackFinalOptions = Parameters<WebpackFinalFn>[1]; | ||
|
|
||
| export function webpack(config: WebpackFinalConfig, options: WebpackFinalOptions) { | ||
| const addonPresetConfig = getAddonOptions(options); | ||
|
|
||
| registerRules({ config, rules: [createRule(addonPresetConfig)] }); | ||
|
|
||
| return config; | ||
| } | ||
|
|
||
| const identity = <T extends unknown>(value: T) => value; | ||
| const addonFilePattern = /react-storybook-addon-export-to-sandbox\/[a-z/]+.[jt]s$/; | ||
| const defaultOptions = { | ||
| webpackRule: {}, | ||
| babelLoaderOptionsUpdater: identity, | ||
| }; | ||
|
|
||
| const PLUGIN_PATH = | ||
| process.env.NODE_ENV !== 'production' | ||
| ? '@fluentui/babel-preset-storybook-full-source/__dev' | ||
| : '@fluentui/babel-preset-storybook-full-source'; | ||
|
|
||
| function createRule(config: Required<PresetConfig>): import('webpack').RuleSetRule { | ||
| const { babelLoaderOptionsUpdater, importMappings, webpackRule } = config; | ||
|
|
||
| const plugin = [require.resolve(PLUGIN_PATH), importMappings]; | ||
|
|
||
| return { | ||
| test: /\.stories\.(jsx?$|tsx?$)/, | ||
| ...webpackRule, | ||
| /** | ||
| * why the usage of 'post' ? - we need to run this loader after all storybook webpack rules/loaders have been executed. | ||
| * while we can use Array.prototype.unshift to "override" the indexes this approach is more declarative without additional hacks. | ||
| */ | ||
| enforce: 'post', | ||
| use: { | ||
| loader: 'babel-loader', | ||
| options: babelLoaderOptionsUpdater({ | ||
| plugins: [plugin], | ||
| }), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * register custom Webpack Rules to webpack config | ||
| */ | ||
| function registerRules(options: { rules: import('webpack').RuleSetRule[]; config: import('webpack').Configuration }) { | ||
| const { config, rules } = options; | ||
| config.module = config.module ?? {}; | ||
| config.module.rules = config.module.rules ?? []; | ||
| config.module.rules.push(...rules); | ||
|
|
||
| return config; | ||
| } | ||
|
|
||
| function getAddonOptions(options: WebpackFinalOptions): Required<PresetConfig> { | ||
| const presetRegistration = options.presetsList?.find(preset => { | ||
| return addonFilePattern.test(preset.name); | ||
| }); | ||
|
|
||
| const addonOptions = presetRegistration?.options ?? {}; | ||
|
|
||
| return { ...defaultOptions, ...addonOptions }; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| const rules = require('./rules'); | ||
| const { getPackageStoriesGlob, loadWorkspaceAddon, registerRules, registerTsPaths } = require('./utils'); | ||
| const { | ||
| getPackageStoriesGlob, | ||
| loadWorkspaceAddon, | ||
| registerRules, | ||
| registerTsPaths, | ||
| processBabelLoaderOptions, | ||
| getImportMappingsForExportToSandboxAddon, | ||
| } = require('./utils'); | ||
|
|
||
| module.exports = { | ||
| getPackageStoriesGlob, | ||
| loadWorkspaceAddon, | ||
| registerRules, | ||
| registerTsPaths, | ||
| rules, | ||
| getImportMappingsForExportToSandboxAddon, | ||
| processBabelLoaderOptions, | ||
| }; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.