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

Conversation

aromanarguello
Copy link
Contributor

Issue: #5030

@vercel vercel bot temporarily deployed to staging November 9, 2019 01:31 Inactive
@vercel
Copy link

vercel bot commented Nov 9, 2019

This pull request is being automatically deployed with ZEIT Now (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://zeit.co/storybook/monorepo/f132e08ce
🌍 Preview: https://monorepo-git-fork-aromanarguello-sveltetypescriptmigration.storybook.now.sh

@aromanarguello aromanarguello force-pushed the svelteTypescriptMigration branch from 769ce18 to 5c91031 Compare November 9, 2019 01:34
@vercel vercel bot temporarily deployed to staging November 9, 2019 01:34 Inactive
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

Great job @aromanarguello!!! A few minor comments above

app/svelte/package.json Outdated Show resolved Hide resolved
"@storybook/core": "5.3.0-alpha.41",
"core-js": "^3.0.1",
"global": "^4.3.2",
"regenerator-runtime": "^0.13.3",
"ts-dedent": "^1.1.0"
"ts-dedent": "^1.1.0",
"webpack": "^4.33.0"
Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added webpack to be able to import the Configuration type

import { Configuration } from 'webpack';
export function webpack(config: Configuration) {

in the framework-preset-svelte. What would be the best alternative to this? @shilman

Copy link
Member

@kroeder kroeder Nov 10, 2019

Choose a reason for hiding this comment

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

Does webpack now ships its own types? There's also https://www.npmjs.com/package/@types/webpack

In app/angular @ndelangen added it to devDeps and peerDeps along with @types/webpack-env 🤔 (according to git blame)
https://github.com/storybookjs/storybook/blame/next/app/angular/package.json

In app/react webpack it is a direct dependency and also includes @types/webpack as a devDep

in app/vue it's the same as in app/react

Back to this PR: would it work with only @types/webpack as devDependency?

Shipping new direct dependencies is sometimes the root cause of new issues. Do svelte apps install it's own webpack dependency? If yes, then we should ship it as a peerDependency. If not then something under the hood of svelte might already ship it as a direct dependency (kind of like angular)

If svelte does not ship webpack at all it might be ok to ship a directDependency of webpack

@gaetanmaisse Can you assist on this as well?

Copy link
Member

@gaetanmaisse gaetanmaisse Nov 10, 2019

Choose a reason for hiding this comment

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

I think there is no need to add webpack as dependency neither as devDependency. What leads me to that is that:

  • Configuration type is used in server part of @storybook/svelte which is not exported to the outside world via types/main properties of package.json, only client is.
  • This package does not use webpack directly, it just override/create a webpack config.
  • This package has @storybook/core as a dependency, the latter uses webpack and have it as dependencies

However, just removing webpack from package.json leads to a no-extraneous-dependencies eslint error in server/framework-preset-svelte.ts. But maybe our context is a good reason to disable this rule for this import?

We should maybe add @types/webpack as devDependency as we are using Configuration type in svelte/src/server codebase 🤷‍♂ even if everything looks good to TS compiler without adding it (surely because @types/webpack is required by another module of SB monorepo). Does anyone know TS best practices when we want to use some dependency typings but not the dependency itself?

What's your mind about it?

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with ignoring the no-extraneous-dependencies warning

Copy link
Member

Choose a reason for hiding this comment

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

Rethinking about it, this issue will maybe be solved when @storybook/core will be migrated to TS.

If @storybook/core defines and exports a WebpackConfiguration type:

import { Configuration } from 'webpack';

export type WebpackConfiguration = Configuration

then using it in @storybook/svelte will not cause no-extraneous-dependencies as @storybook/core is a dependency. And everything should be good for TS if @storybook/core will have webpack and @types/webpack as dependencies.

Copy link
Contributor Author

@aromanarguello aromanarguello Nov 11, 2019

Choose a reason for hiding this comment

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

How do we want to proceed in the meantime? Should we mark config:any until @storybook/core is migrated, or ignoring the no-extraneous-dependencies warning?

Copy link
Member

Choose a reason for hiding this comment

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

@aromanarguello You can add the comment to ignore no-extraneous-dependencies rule for this line, remove webpack from dependencies and 🚀

app/svelte/src/client/preview/render.ts Outdated Show resolved Hide resolved
@shilman shilman added maintenance User-facing maintenance tasks typescript labels Nov 9, 2019
@shilman shilman added this to the 5.3.0 milestone Nov 9, 2019
@shilman shilman changed the title Migrate @storybook/svelte to Typescript Svelte: Migrate @storybook/svelte to Typescript Nov 9, 2019
@shilman shilman added the svelte label Nov 9, 2019
@aromanarguello aromanarguello force-pushed the svelteTypescriptMigration branch from 379777c to bed95e7 Compare November 9, 2019 16:40
@vercel vercel bot temporarily deployed to staging November 9, 2019 16:40 Inactive
@aromanarguello aromanarguello force-pushed the svelteTypescriptMigration branch from bed95e7 to e67b92e Compare November 9, 2019 16:42
@vercel vercel bot temporarily deployed to staging November 9, 2019 16:42 Inactive
@vercel vercel bot temporarily deployed to staging November 9, 2019 16:43 Inactive
@aromanarguello aromanarguello force-pushed the svelteTypescriptMigration branch from c1ea4b1 to 35a6011 Compare November 9, 2019 16:52
@vercel vercel bot temporarily deployed to staging November 9, 2019 16:52 Inactive
@aromanarguello aromanarguello force-pushed the svelteTypescriptMigration branch from b735fbb to 7d04427 Compare November 9, 2019 18:13
@vercel vercel bot temporarily deployed to staging November 9, 2019 18:13 Inactive
@aromanarguello
Copy link
Contributor Author

All comments addressed :) LMK if any other changes are necessary @shilman

@@ -0,0 +1,2 @@
declare module '@storybook/core/*';
Copy link
Member

Choose a reason for hiding this comment

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

This file should be named app/svelte/src/typings.d.ts instead of app/svelte/src/index.d.ts

{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
Copy link
Member

Choose a reason for hiding this comment

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

Use should set :rootDir": "./src", otherwise dist folder generated during TS "compilation" is not as expected:
image

In order to be able to do that you have to use const packageJson = require('../../package.json'); in src/server/options.ts instead of import packageJson from '../../package.json';. As it is done for react for instance:

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.

BTW, not related at all to your PR, there is a weird thing with *.d.ts files, they are compiled to JS files while they shouldn't. I will take a look when I have time to. cc @ndelangen @kroeder

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! Thanks for your advice and tips, I am learning a lot :)

@gaetanmaisse
Copy link
Member

@aromanarguello Thanks a lot for your work 👏 ! I just made 2 TS related comments that you can easily fix 💪 . Your PR also puts in the light the webpack dep "issue", which is more global than just your PR 😉

@@ -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.

@shilman
Copy link
Member

shilman commented Nov 12, 2019

@gaetanmaisse @aromanarguello are we good to merge this now?

@gaetanmaisse
Copy link
Member

@shilman, @aromanarguello will add one last commit for #8770 (comment) and we will be good

@vercel vercel bot temporarily deployed to staging November 12, 2019 13:19 Inactive
@aromanarguello
Copy link
Contributor Author

all done @shilman @gaetanmaisse :)

@shilman
Copy link
Member

shilman commented Nov 12, 2019

@aromanarguello Congratulations on your first OSS PR merge!!! 🎉🎉🎉

@gaetanmaisse Thanks for shepherding!!! 🙏🙏🙏

@shilman shilman merged commit 4f59937 into storybookjs:next Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants