Skip to content
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

fix(cli): Fix Storybook CRA sink related bugs #170

Merged
merged 1 commit into from
Mar 5, 2021
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
25 changes: 24 additions & 1 deletion packages/cli/fakeStorybookAddons.js
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
module.exports.makeDecorator = () => story => story();
const makeDecorator = () => story => story();
const getChannel = () => ({
addListener: () => {},
addPeerListener: () => {},
emit: () => {},
last: () => {},
eventNames: () => {},
listenerCount: () => {},
listeners: () => {},
once: () => {},
removeAllListeners: () => {},
removeListener: () => {},
on: () => {},
off: () => {},
})

const addons = {
makeDecorator,
getChannel,
};

module.exports.addons = addons;
module.exports.makeDecorator = makeDecorator;
module.exports.getChannel = getChannel;
6 changes: 5 additions & 1 deletion packages/cli/fakeStorybookReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ module.exports.getStorybook = () => [];
module.exports.raw = () => [];
module.exports.setAddon = () => {};
module.exports.storiesOf = () => {};
module.exports.forceReRender = () => {};
module.exports.forceReRender = () => {
if (window && window.bojagi) {
window.bojagi.forceReRender();
}
};
module.exports.configure = () => {};
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ test('get components API output', () => {
},
],
},
emptyFile: {
fileName: 'empty',
title: 'FILE EMPTY',
filePath: '/some/path/some/empty_file.ts',
storyItems: [],
},
};

const result = serveStoriesApi({
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/containers/PreviewContainer/serveStoryApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StoryCollectionMetadata } from '../../steps/analyze';
import { filterEmptyStories } from '../../utils/filterEmptyStories';
import { SetupApiOptions } from './setupApi';

export function serveStoriesApi({ storiesMetadata, getFiles, getAssets }: SetupApiOptions) {
Expand All @@ -7,6 +8,7 @@ export function serveStoriesApi({ storiesMetadata, getFiles, getAssets }: SetupA
return {
files: getFiles().map(({ url, name }) => ({ name, url })),
stories: Object.entries(storiesMetadata)
.filter(([, meta]) => filterEmptyStories(meta))
.sort(sortStory)
.map(([filePath, meta]) => {
return {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/steps/writeFiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { normalizeFilePath } from '../../utils/normalizeFilePath';
import { getStepOutputFiles } from '../../utils/getOutputFiles';
import { getStepOutputStories } from '../../utils/getOutputStories';
import { getStepOutputDependencies } from '../../utils/getOutputDependencies';
import { filterEmptyStories } from '../../utils/filterEmptyStories';

export type WriteFilesStepOutput = StepOutput & {};

Expand Down Expand Up @@ -66,6 +67,7 @@ async function action({ config, stepOutputs }: StepRunnerActionOptions<Dependenc
...item,
};
})
.filter(filterEmptyStories)
.map(mapObjectWithWhitelist(STORY_PROPERTY_WHITELIST));

const dependenciesOutput = getStepOutputDependencies(stepOutputs);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/composeWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const composeWebpackConfig = (
},
plugins: [
new webpack.NormalModuleReplacementPlugin(
/@storybook\/addons/,
/@storybook\/addons$/,
require.resolve('@bojagi/cli/fakeStorybookAddons.js')
),
new webpack.NormalModuleReplacementPlugin(
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/utils/filterEmptyStories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function filterEmptyStories(story) {
return !!story.storyItems && story.storyItems.length;
}
1 change: 1 addition & 0 deletions packages/integration-tests/src/components/Empty.bojagi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Doesn't export anything, should be ignored by the CLI
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ exports[`run bundle command 2`] = `
\\"namespace\\": \\"default\\",
\\"outputFilePath\\": \\"files/Button.bojagi.js\\"
},
{
\\"name\\": \\"Empty.bojagi.js\\",
\\"namespace\\": \\"default\\",
\\"outputFilePath\\": \\"files/Empty.bojagi.js\\"
},
{
\\"name\\": \\"Hooks.bojagi.js\\",
\\"namespace\\": \\"default\\",
Expand All @@ -73,9 +78,9 @@ exports[`run bundle command 2`] = `
\\"outputFilePath\\": \\"files/PingPong.bojagi.js\\"
},
{
\\"name\\": \\"9.js\\",
\\"name\\": \\"10.js\\",
\\"namespace\\": \\"default\\",
\\"outputFilePath\\": \\"files/9.js\\"
\\"outputFilePath\\": \\"files/10.js\\"
},
{
\\"name\\": \\"./index.html\\",
Expand Down Expand Up @@ -551,6 +556,14 @@ exports[`run bundle command 4`] = `
}
]
},
\\"packages/integration-tests/src/components/Empty.bojagi.js\\": {
\\"id\\": \\"packages/integration-tests/src/components/Empty.bojagi.js\\",
\\"isExternal\\": false,
\\"isNodeModule\\": false,
\\"filePath\\": \\"src/components/Empty.bojagi.js\\",
\\"gitPath\\": \\"packages/integration-tests/src/components/Empty.bojagi.js\\",
\\"dependencies\\": []
},
\\"packages/integration-tests/src/components/Hooks.bojagi.js\\": {
\\"id\\": \\"packages/integration-tests/src/components/Hooks.bojagi.js\\",
\\"isExternal\\": false,
Expand Down