Skip to content

Commit

Permalink
Merge pull request #1058 from chromaui/revert-1049-jmhobbs/cap-2186-g…
Browse files Browse the repository at this point in the history
…ha-doesnt-recognize-the-flag-stats-json

Revert "Use --stats-json flag for SB 8.0.0+"
  • Loading branch information
jmhobbs authored Sep 24, 2024
2 parents 2338808 + 805eee1 commit 77d4f8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 55 deletions.
37 changes: 0 additions & 37 deletions node-src/tasks/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,43 +113,6 @@ describe('setBuildCommand', () => {
'Storybook version 6.2.0 or later is required to use the --only-changed flag'
);
});

it('uses the correct flag for webpack stats for >= 8.0.0', async () => {
getCliCommand.mockReturnValue(Promise.resolve('npm run build:storybook'));

const ctx = {
sourceDir: './source-dir/',
options: { buildScriptName: 'build:storybook' },
storybook: { version: '8.0.0' },
git: { changedFiles: ['./index.js'] },
} as any;
await setBuildCommand(ctx);

expect(getCliCommand).toHaveBeenCalledWith(
expect.anything(),
['build:storybook', '--output-dir=./source-dir/', '--stats-json=./source-dir/'],
{ programmatic: true }
);
expect(ctx.buildCommand).toEqual('npm run build:storybook');
});

it('uses the old flag when it storybook version is undetected', async () => {
getCliCommand.mockReturnValue(Promise.resolve('npm run build:storybook'));

const ctx = {
sourceDir: './source-dir/',
options: { buildScriptName: 'build:storybook' },
git: { changedFiles: ['./index.js'] },
} as any;
await setBuildCommand(ctx);

expect(getCliCommand).toHaveBeenCalledWith(
expect.anything(),
['build:storybook', '--output-dir=./source-dir/', '--webpack-stats-json=./source-dir/'],
{ programmatic: true }
);
expect(ctx.buildCommand).toEqual('npm run build:storybook');
});
});

describe('buildStorybook', () => {
Expand Down
22 changes: 6 additions & 16 deletions node-src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,19 @@ export const setSourceDir = async (ctx: Context) => {
}
};

// Storybook 8.0.0 deprecated --webpack-stats-json in favor of --stats-json.
const getStatsFlag = (ctx: Context) => {
return ctx?.storybook?.version && semver.gte(semver.coerce(ctx.storybook.version), '8.0.0')
? '--stats-json'
: '--webpack-stats-json';
};

const isStatsFlagSupported = (ctx: Context) => {
return ctx.storybook && ctx.storybook.version
? semver.gte(semver.coerce(ctx.storybook.version), '6.2.0')
: true;
};

export const setBuildCommand = async (ctx: Context) => {
const statsFlagSupported = isStatsFlagSupported(ctx);
const webpackStatsSupported =
ctx.storybook && ctx.storybook.version
? semver.gte(semver.coerce(ctx.storybook.version), '6.2.0')
: true;

if (ctx.git.changedFiles && !statsFlagSupported) {
if (ctx.git.changedFiles && !webpackStatsSupported) {
ctx.log.warn('Storybook version 6.2.0 or later is required to use the --only-changed flag');
}

const buildCommandOptions = [
`--output-dir=${ctx.sourceDir}`,
ctx.git.changedFiles && statsFlagSupported && `${getStatsFlag(ctx)}=${ctx.sourceDir}`,
ctx.git.changedFiles && webpackStatsSupported && `--webpack-stats-json=${ctx.sourceDir}`,
].filter(Boolean);

ctx.buildCommand = await (isE2EBuild(ctx.options)
Expand Down
4 changes: 2 additions & 2 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ export interface Context {
matchesBranch?: (glob: boolean | string) => boolean;
packageMetadataChanges?: { changedFiles: string[]; commit: string }[];
};
storybook?: {
version?: string;
storybook: {
version: string;
configDir: string;
staticDir: string[];
viewLayer: string;
Expand Down

0 comments on commit 77d4f8f

Please sign in to comment.