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

Storysource: Support CSF3 object exports, co-exist with addon-docs #20799

Merged
merged 2 commits into from
Jan 26, 2023
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
15 changes: 15 additions & 0 deletions code/__mocks__/inject-decorator.ts.csf3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";
import { action } from "@storybook/addon-actions";
import { Button } from "@storybook/react/demo";

export default {
title: "Button",
};

export const Basic = {};

export const Emoji = {
args: {
children: '😀 😎 👍 💯'
}
};
1 change: 0 additions & 1 deletion code/addons/storysource/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function webpack(
{
test: [/\.stories\.(jsx?$|tsx?$)/],
...rule,
enforce: 'pre',
use: [
{
loader: require.resolve('@storybook/source-loader'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe('inject-decorator', () => {

expect(result.source).toEqual(expect.stringContaining('"source": "import React from'));
});

it('includes storySource parameter in CSf3', async () => {
const mockFilePath = './__mocks__/inject-decorator.ts.csf3.txt';
const source = await readFile(mockFilePath, 'utf-8');
const result = injectDecorator(source, path.resolve(__dirname, mockFilePath), {
parser: 'typescript',
});

expect(getParser('typescript').parse(result.source)).toBeTruthy();

expect(result.source).toEqual(expect.stringContaining('"source": "import React from'));
});
});

describe('injectStoryParameters - ts - csf', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ function isFunctionVariable(declarations, includeExclude) {
declarations[0].id &&
declarations[0].id.name &&
declarations[0].init &&
['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(
declarations[0].init.type
) &&
[
'CallExpression',
'ArrowFunctionExpression',
'FunctionExpression',
'ObjectExpression', // CSF3
].includes(declarations[0].init.type) &&
isExportStory(declarations[0].id.name, includeExclude)
);
}
Expand Down Expand Up @@ -171,9 +174,12 @@ export function findExportsMap(ast) {
node.declaration.declarations[0].id &&
node.declaration.declarations[0].id.name &&
node.declaration.declarations[0].init &&
['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(
node.declaration.declarations[0].init.type
);
[
'CallExpression',
'ArrowFunctionExpression',
'FunctionExpression',
'ObjectExpression', // CSF3
].includes(node.declaration.declarations[0].init.type);

const isFunctionDeclarationExport =
isNamedExport &&
Expand Down
8 changes: 7 additions & 1 deletion code/lib/source-loader/src/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { readFile } from 'fs/promises';
import { readStory } from './dependencies-lookup/readAsObject';
import { sanitizeSource } from './abstract-syntax-tree/generate-helpers';

export async function transform(inputSource) {
const sourceObject = await readStory(this, inputSource);
Expand All @@ -13,11 +15,15 @@ export async function transform(inputSource) {

const { source, sourceJson, addsMap } = sourceObject;

// We do this so the source we display doesn't get clobbered by csf-plugin
const rawSource = await readFile(this.resourcePath, 'utf-8');
const rawJson = sanitizeSource(rawSource);

const preamble = `
/* eslint-disable */
// @ts-nocheck
// @ts-expect-error (Converted from ts-ignore)
var __STORY__ = ${sourceJson};
var __STORY__ = ${rawJson};
// @ts-expect-error (Converted from ts-ignore)
var __LOCATIONS_MAP__ = ${JSON.stringify(addsMap, null, 2).trim()};
`;
Expand Down