Skip to content

Commit

Permalink
Merge pull request #1133 from chromaui/cody/cap-2599-turbosnap-exit-o…
Browse files Browse the repository at this point in the history
…n-storybook-config-does-not-work-as-expected

Bail on preview file changes
  • Loading branch information
codykaup authored Jan 3, 2025
2 parents 730a7aa + 300222f commit 43a9b94
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
27 changes: 27 additions & 0 deletions node-src/lib/getDependentStoryFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,33 @@ describe('getDependentStoryFiles', () => {
);
});

it('bails on changed preview.js file', async () => {
const changedFiles = ['src/foo.stories.js', 'path/to/storybook-config/preview.js'];
const modules = [
{
id: './path/to/storybook-config/preview.js',
name: './path/to/storybook-config/preview.js',
reasons: [{ moduleName: './path/to/storybook-config/generated-stories-entry.js' }],
},
{
id: CSF_GLOB,
name: CSF_GLOB,
reasons: [{ moduleName: './path/to/storybook-config/generated-stories-entry.js' }],
},
];
const ctx = getContext({ configDir: 'path/to/storybook-config' });
const result = await getDependentStoryFiles(ctx, { modules }, statsPath, changedFiles);
expect(result).toBeUndefined();
expect(ctx.turboSnap.bailReason).toEqual({
changedStorybookFiles: ['path/to/storybook-config/preview.js'],
});
expect(ctx.log.warn).toHaveBeenCalledWith(
expect.stringContaining(
chalk`Found a Storybook config change in {bold path/to/storybook-config/preview.js}`
)
);
});

it('bails on changed dependency of config file', async () => {
const changedFiles = ['src/styles.js'];
const modules = [
Expand Down
12 changes: 9 additions & 3 deletions node-src/lib/getDependentStoryFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,13 @@ export async function getDependentStoryFiles(
const reasonsById = new Map<Module['id'], NormalizedName[]>();
const csfGlobsByName = new Set<NormalizedName>();

const isStorybookFile = (name: string) =>
name && name.startsWith(`${storybookDirectory}/`) && !storiesEntryFiles.has(name);

stats.modules
.filter((module_) => isUserModule(module_))
// TODO: refactor this function
// eslint-disable-next-line complexity
.map((module_) => {
const normalizedName = normalize(module_.name);
modulesByName.set(normalizedName, module_);
Expand Down Expand Up @@ -176,7 +181,10 @@ export async function getDependentStoryFiles(
reasonsById.set(module_.id, normalizedReasons);
}

if (reasonsById.get(module_.id)?.some((reason) => storiesEntryFiles.has(reason))) {
if (
!isStorybookFile(normalizedName) &&
reasonsById.get(module_.id)?.some((reason) => storiesEntryFiles.has(reason))
) {
csfGlobsByName.add(normalizedName);
}
});
Expand All @@ -203,8 +211,6 @@ export async function getDependentStoryFiles(
}

const isCsfGlob = (name: NormalizedName) => csfGlobsByName.has(name);
const isStorybookFile = (name: string) =>
name && name.startsWith(`${storybookDirectory}/`) && !storiesEntryFiles.has(name);
const isStaticFile = (name: string) =>
staticDirectories.some((directory) => name && name.startsWith(`${directory}/`));

Expand Down

0 comments on commit 43a9b94

Please sign in to comment.