Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/core/src/common/utils/load-main-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import { validateConfigurationFiles } from './validate-configuration-files';
export async function loadMainConfig({
configDir = '.storybook',
cwd,
skipCache,
}: {
configDir: string;
cwd?: string;
skipCache?: boolean;
}): Promise<StorybookConfig> {
await validateConfigurationFiles(configDir, cwd);

const mainPath = getInterpretedFile(resolve(configDir, 'main')) as string;

try {
const out = await importModule(mainPath);
const out = await importModule(mainPath, { skipCache });
return out;
} catch (e) {
if (!(e instanceof Error)) {
Expand Down
12 changes: 10 additions & 2 deletions code/core/src/shared/utils/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ let isTypescriptLoaderRegistered = false;
* // Returns the default export or the entire module
* ```
*/
export async function importModule(path: string) {
export async function importModule(
path: string,
{ skipCache = false }: { skipCache?: boolean } = {}
) {
if (!isTypescriptLoaderRegistered) {
const typescriptLoaderUrl = importMetaResolve('storybook/internal/bin/loader');
register(typescriptLoaderUrl, import.meta.url);
Expand All @@ -61,12 +64,17 @@ export async function importModule(path: string) {
let mod;
try {
const resolvedPath = win32.isAbsolute(path) ? pathToFileURL(path).href : path;
mod = await import(resolvedPath);
// When applicable, add a hash to the import URL to bypass cache
const importUrl = skipCache ? `${resolvedPath}?${Date.now()}` : resolvedPath;
mod = await import(importUrl);
} catch (importError) {
try {
// fallback to require to support older behavior
// this is relevant for presets that are only available with the "require" condition in a package's export map
const require = createRequire(import.meta.url);
if (skipCache) {
delete require.cache[require.resolve(path)];
}
mod = require(path);
} catch (requireError) {
Comment on lines 54 to 79

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

okay, I assume you've tested this, ✅

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For sure, all cases are tested

/*
Expand Down
7 changes: 3 additions & 4 deletions code/lib/cli-storybook/src/add.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { isAbsolute, join } from 'node:path';

import {
type PackageManagerName,
serverRequire,
loadMainConfig,
syncStorybookAddons,
versions,
} from 'storybook/internal/common';
Expand Down Expand Up @@ -182,7 +180,8 @@ export async function add(

// TODO: remove try/catch once CSF factories is shipped, for now gracefully handle any error
try {
await syncStorybookAddons(mainConfig, previewConfigPath!, configDir);
const newMainConfig = await loadMainConfig({ configDir, skipCache: true });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So this is where I was worried about: that loadMainConfig would have a module-cache in NodeJS.

await syncStorybookAddons(newMainConfig, previewConfigPath!, configDir);
} catch (e) {
//
}
Expand Down