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

Svelte: Migrate @storybook/svelte to Typescript #8770

Merged
merged 8 commits into from
Nov 12, 2019
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
2 changes: 2 additions & 0 deletions app/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"*.d.ts"
],
"main": "dist/client/index.js",
"types": "dist/client/index.d.ts",
"bin": {
"build-storybook": "./bin/build.js",
"start-storybook": "./bin/index.js",
Expand All @@ -32,6 +33,7 @@
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"@storybook/addons": "5.3.0-alpha.43",
"@storybook/core": "5.3.0-alpha.43",
"core-js": "^3.0.1",
"global": "^4.3.2",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const {
} = clientApi;

const framework = 'svelte';
export const storiesOf = (...args) => clientApi.storiesOf(...args).addParameters({ framework });
export const configure = (...args) => coreConfigure(...args, framework);
export const storiesOf = (...args: any) =>
clientApi.storiesOf(...args).addParameters({ framework });
export const configure = (...args: any) => coreConfigure(...args, framework);

export { forceReRender };
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { document } from 'global';
import dedent from 'ts-dedent';
import { MountViewArgs, RenderMainArgs } from './types';

let previousComponent = null;
type Component = any;

let previousComponent: Component = null;

function cleanUpPreviousStory() {
if (!previousComponent) {
return;
}

previousComponent.$destroy();
previousComponent = null;
}

function mountView({ Component, target, props, on, Wrapper, WrapperData }) {
let component;
function mountView({ Component, target, props, on, Wrapper, WrapperData }: MountViewArgs) {
let component: Component;

if (Wrapper) {
const fragment = document.createDocumentFragment();
Expand Down Expand Up @@ -47,8 +49,7 @@ export default function render({
selectedStory,
showMain,
showError,
// showException,
}) {
}: RenderMainArgs) {
const {
/** @type {SvelteComponent} */
Component,
Expand Down
33 changes: 33 additions & 0 deletions app/svelte/src/client/preview/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { StoryFn } from '@storybook/addons';

export interface ShowErrorArgs {
title: string;
description: string;
}

export interface RenderMainArgs {
storyFn: StoryFn<any>;
selectedKind: string;
selectedStory: string;
showMain: () => void;
showError: (args: ShowErrorArgs) => void;
}

export interface MountViewArgs {
Component: any;
target: any;
props: MountProps;
on: any;
Wrapper: any;
WrapperData: any;
}

interface MountProps {
rounded: boolean;
text: string;
}

interface WrapperData {
innerStyle: string;
style: string;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function webpack(config) {
import { Configuration } from 'webpack'; // eslint-disable-line

export function webpack(config: Configuration) {
return {
...config,
module: {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import packageJson from '../../package.json';
const packageJson = require('../../package.json');
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Just saw your comment. Still this seems weird to me. Any references I can look at?

Copy link
Member

Choose a reason for hiding this comment

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

By setting "rootDir": "./src" we are telling TS compiler that all sources will be in src folder. As package.json is not in src it can not be imported directly (i.e. at compile time) without throwing: File '/Path/to/package.json' is not under 'rootDir' '/Path/To/App/src/'. 'rootDir' is expected to contain all source files.

require will import the file at runtime so TS compiler will not try to load it and finally everyone is happy.


export default {
packageJson,
Expand Down
2 changes: 2 additions & 0 deletions app/svelte/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module '@storybook/core/*';
declare module 'global';
14 changes: 14 additions & 0 deletions app/svelte/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"types": ["webpack-env"],
"resolveJsonModule": true
},
"include": [
"src/**/*"
],
"exclude": [
"src/**/*.test.*"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,4 @@
]
]
}
}
}