Skip to content

Commit

Permalink
fix: Wrap vite-plugin-external in custom plugin and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stevezhu committed Jan 24, 2023
1 parent 3333047 commit 99b2657
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
25 changes: 25 additions & 0 deletions code/lib/builder-vite/src/plugins/external-globals-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { globals } from '@storybook/preview/globals';
import type { Plugin } from 'vite';
import { viteExternalsPlugin } from 'vite-plugin-externals';

type ConfigHookFn = Extract<
ReturnType<typeof viteExternalsPlugin>['config'],
(...args: any[]) => any
>;

export async function externalsGlobalsPlugin() {
const plugin = viteExternalsPlugin(globals, { useWindow: false });
return {
...plugin,
name: 'storybook:external-globals-plugin',
// wrap config hook to fix issues from `vite-plugin-externals`
async config(...configArgs) {
const config = await (plugin.config as ConfigHookFn)(...configArgs);
return {
resolve: {
alias: config?.resolve?.alias,
},
};
},
} satisfies Plugin;
}
1 change: 1 addition & 0 deletions code/lib/builder-vite/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './mdx-plugin';
export * from './strip-story-hmr-boundaries';
export * from './code-generator-plugin';
export * from './csf-plugin';
export * from './external-globals-plugin';
5 changes: 2 additions & 3 deletions code/lib/builder-vite/src/vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import type {
UserConfig as ViteConfig,
InlineConfig,
} from 'vite';
import { viteExternalsPlugin } from 'vite-plugin-externals';
import { isPreservingSymlinks, getFrameworkName, getBuilderOptions } from '@storybook/core-common';
import { globals } from '@storybook/preview/globals';
import type { Options } from '@storybook/types';
import {
codeGeneratorPlugin,
csfPlugin,
injectExportOrderPlugin,
mdxPlugin,
stripStoryHMRBoundary,
externalsGlobalsPlugin,
} from './plugins';
import type { BuilderOptions } from './types';

Expand Down Expand Up @@ -93,7 +92,7 @@ export async function pluginConfig(options: Options) {
}
},
},
viteExternalsPlugin(globals, { useWindow: false, disableInServe: true }),
externalsGlobalsPlugin(),
] as PluginOption[];

// TODO: framework doesn't exist, should move into framework when/if built
Expand Down

0 comments on commit 99b2657

Please sign in to comment.