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

Core: Zero-config TypeScript loading #10813

Merged
merged 42 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8310948
feat: add root TypeScript support
mrmckeb May 17, 2020
e7a460f
Add vue typescript component to kitchen sink
graup May 18, 2020
bb84352
Fix docgen preset
shilman May 18, 2020
322380f
Clean up typescriptOptions handling for docgen
shilman May 18, 2020
267a01e
Clean up `react-ts` example and addon-essentials for testing
shilman May 18, 2020
688dc43
React: Switch to react-docgen as the default config
shilman May 18, 2020
daf193a
Update failing tests
shilman May 18, 2020
1e621e3
Update main.js typescript docgen handling
shilman May 18, 2020
0b5b8c8
Update addon-jest.testresults.json
shilman May 18, 2020
d8431d3
Merge pull request #10816 from storybookjs/10790-fix-react-docgen-preset
shilman May 18, 2020
e88c51d
Merge branch 'next' into mrmckeb/issue10790
shilman May 18, 2020
6a19a43
Update package.json
shilman May 18, 2020
811b66b
FIX angular example tests
ndelangen May 18, 2020
5eda5b9
FIX problem with building all examples in CI (offset was wrong)
ndelangen May 18, 2020
ef2d51c
Add vue-cli example application
graup May 18, 2020
14d3e1b
Vue: Remove babel-preset-vue #4475
graup May 18, 2020
1ea6595
feat: update types and defaults
mrmckeb May 19, 2020
cb1a515
Merge pull request #10821 from graup/graup/vue-cli-example
shilman May 19, 2020
5270f07
Update package.json
shilman May 19, 2020
5949c37
Merge branch 'next' into mrmckeb/issue10790
ndelangen May 19, 2020
8bc3c9f
FIX tests
ndelangen May 19, 2020
a38d6cf
fix babel-loader-preview
tooppaaa May 19, 2020
8a98ecf
fix missing deps
tooppaaa May 19, 2020
d3c9910
fix deepscan
tooppaaa May 19, 2020
9b41d91
fix yarn lock
tooppaaa May 19, 2020
03136b7
Debugging CI
shilman May 20, 2020
ce38dee
Debug CI
shilman May 20, 2020
7948a71
Fix typescript-preset filtering
shilman May 20, 2020
bdbd62a
Build: Increase parallelism
shilman May 20, 2020
ebd8535
Attempt to unify ts-loader version for e2e tests
shilman May 20, 2020
5f95289
Update yarn.lock
shilman May 20, 2020
c7dfb35
Increase cypress navigation timeout for debugging
shilman May 20, 2020
bd7d479
Fix presets for windows paths
shilman May 20, 2020
3f4d4ef
Typescript: Filter out node_modules for docgen by default
shilman May 20, 2020
ee085e5
feat: improve shared types
mrmckeb May 20, 2020
4a4ccb4
Zero-config typescript: Updated MIGRATION and storybook documentation
shilman May 20, 2020
1336f76
Merge branch 'mrmckeb/issue10790' of github.com:storybookjs/storybook…
shilman May 20, 2020
6dfb786
fix: correct TSDoc error in shared types
mrmckeb May 20, 2020
3671955
Migrate sfc_vue fixture to use babel-preset-vue
shilman May 20, 2020
7b8e614
Apply babel fix to vue fixture as well
graup May 20, 2020
b50442f
Revert removal of babel-preset-vue
graup May 20, 2020
7a4724b
Revert removel of babel-preset-vue from app/vue
graup May 20, 2020
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
1 change: 0 additions & 1 deletion app/angular/src/server/framework-preset-angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function webpack(
},
resolve: {
...config.resolve,
extensions: ['.ts', '.tsx', ...config.resolve.extensions],
},
plugins: [
...config.plugins,
Expand Down
1 change: 1 addition & 0 deletions app/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"lodash": "^4.17.15",
"prop-types": "^15.7.2",
"react-dev-utils": "^10.0.0",
"react-docgen-typescript-loader": "^3.7.2",
"regenerator-runtime": "^0.13.3",
"semver": "^7.3.2",
"ts-dedent": "^1.1.1",
Expand Down
72 changes: 16 additions & 56 deletions app/react/src/server/framework-preset-react-docgen.test.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,36 @@
import { TransformOptions } from '@babel/core';
import * as preset from './framework-preset-react-docgen';

describe('framework-preset-react-docgen', () => {
const babelPluginReactDocgenPath = require.resolve('babel-plugin-react-docgen');

it('should return the config with the extra plugins when `plugins` is an array.', () => {
it('should return the config with the extra plugin', () => {
const babelConfig = {
babelrc: false,
presets: ['env', 'foo-preset'],
plugins: ['foo-plugin'],
};

const config = preset.babel(babelConfig);

expect(config).toEqual({
babelrc: false,
plugins: [
'foo-plugin',
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
],
presets: ['env', 'foo-preset'],
const config = preset.babel(babelConfig, {
typescriptOptions: { check: false, reactDocgen: 'react-docgen' },
});
});

it('should return the config with the extra plugins when `plugins` is not an array.', () => {
const babelConfig: TransformOptions = {
babelrc: false,
presets: ['env', 'foo-preset'],
plugins: ['bar-plugin'],
};

const config = preset.babel(babelConfig);

expect(config).toEqual({
babelrc: false,
plugins: [
'bar-plugin',
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
],
presets: ['env', 'foo-preset'],
});
});

it('should return the config only with the extra plugins when `plugins` is not present.', () => {
const babelConfig = {
babelrc: false,
plugins: ['foo-plugin'],
presets: ['env', 'foo-preset'],
};

const config = preset.babel(babelConfig);

expect(config).toEqual({
babelrc: false,
plugins: [
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
overrides: [
{
test: /\.(mjs|tsx?|jsx?)$/,
plugins: [
[
babelPluginReactDocgenPath,
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
],
},
],
presets: ['env', 'foo-preset'],
});
});
});
54 changes: 41 additions & 13 deletions app/react/src/server/framework-preset-react-docgen.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
import { TransformOptions } from '@babel/core';
import type { TransformOptions } from '@babel/core';
import type { Configuration } from 'webpack';
import type { StorybookOptions } from '@storybook/core/types';

export function babel(config: TransformOptions) {
// Ensure plugins are defined or fallback to an array to avoid empty values.
const babelConfigPlugins = config.plugins || [];

const extraPlugins = [
[
require.resolve('babel-plugin-react-docgen'),
export function babel(config: TransformOptions, { typescriptOptions }: StorybookOptions) {
const { reactDocgen } = typescriptOptions;
if (!reactDocgen) {
return config;
}
return {
...config,
overrides: [
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
test: reactDocgen === 'react-docgen' ? /\.(mjs|tsx?|jsx?)$/ : /\.(mjs|jsx?)$/,
plugins: [
[
require.resolve('babel-plugin-react-docgen'),
{
DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES',
},
],
],
},
],
];
};
}

// If `babelConfigPlugins` is not an `Array`, calling `concat` will inject it
// as a single value, if it is an `Array` it will spread.
export function webpackFinal(config: Configuration, { typescriptOptions }: StorybookOptions) {
const { reactDocgen, reactDocgenTypescriptOptions } = typescriptOptions;
if (reactDocgen !== 'react-docgen-typescript') return config;
return {
...config,
plugins: [].concat(babelConfigPlugins, extraPlugins),
module: {
...config.module,
rules: [
...config.module.rules,
{
test: /\.tsx?$/,
// include: path.resolve(__dirname, "../src"),
use: [
{
loader: require.resolve('react-docgen-typescript-loader'),
options: reactDocgenTypescriptOptions,
},
],
},
],
},
};
}
3 changes: 2 additions & 1 deletion app/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"global": "^4.3.2",
"regenerator-runtime": "^0.13.3",
"ts-dedent": "^1.1.1",
"ts-loader": "^7.0.4",
"webpack": "^4.43.0"
},
"devDependencies": {
"@types/node": "^13.13.4",
"@types/webpack": "^4.41.12",
"babel-preset-vue": "^2.0.2",
"vue": "^2.6.8",
"vue-loader": "^15.7.0",
"vue-template-compiler": "^2.6.8"
Expand All @@ -57,6 +57,7 @@
"css-loader": "*",
"react": "*",
"react-dom": "*",
"ts-loader": "^7.0.4",
"vue": "^2.6.8",
"vue-loader": "^15.7.0",
"vue-template-compiler": "^2.6.8"
Expand Down
19 changes: 12 additions & 7 deletions app/vue/src/server/framework-preset-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ export function webpack(config: Configuration) {
loader: require.resolve('vue-loader'),
options: {},
},
{
test: /\.tsx?$/,
use: [
{
loader: require.resolve('ts-loader'),
options: {
transpileOnly: true,
appendTsSuffixTo: [/\.vue$/],
Copy link
Member

Choose a reason for hiding this comment

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

@mrmckeb @graup will this run ts-loader on all *.vue even if the project is not a TS project? Is that an issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question. I'm not 100% sure, but if you remove lang="ts" from a TS SCF it stops working, so I assume ts-loader only gets applied if you specify that. Anyway, ts-loader without type checking is basically a no-op for .js files, so it wouldn't cause any issues.

Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't tested, this was from the PR @graup did for the TS preset - my understanding from the docs is that it should operate as @graup says above.

},
},
],
},
],
},
resolve: {
Expand All @@ -26,10 +38,3 @@ export function webpack(config: Configuration) {
},
};
}

export function babelDefault(config: any) {
return {
...config,
presets: [...config.presets, require.resolve('babel-preset-vue')],
};
}
3 changes: 3 additions & 0 deletions examples/react-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Storybook TS example

This Storybook demonstrates support for TypeScript in Storybook without additional configuration.
13 changes: 13 additions & 0 deletions examples/react-ts/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { StorybookConfig } from '@storybook/core/types';

module.exports = {
stories: ['./src/*.stories.*'],
addons: ['@storybook/addon-essentials'],
typescript: {
check: true,
checkOptions: {},
reactDocgenTypescriptOptions: {
propFilter: (prop) => ['label', 'disabled'].includes(prop.name),
},
},
} as StorybookConfig;
21 changes: 21 additions & 0 deletions examples/react-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@storybook/example-react-ts",
"version": "6.0.0-beta.8",
"private": true,
"scripts": {
"build-storybook": "cross-env STORYBOOK_DISPLAY_WARNING=true DISPLAY_WARNING=true build-storybook -c ./",
"debug": "cross-env NODE_OPTIONS=--inspect-brk STORYBOOK_DISPLAY_WARNING=true DISPLAY_WARNING=true start-storybook -p 9011 -c ./ --no-dll",
"storybook": "cross-env STORYBOOK_DISPLAY_WARNING=true DISPLAY_WARNING=true start-storybook -p 9011 -c ./ --no-dll"
},
"dependencies": {
"@storybook/addon-essentials": "6.0.0-beta.8",
"@storybook/react": "6.0.0-beta.8",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
"prop-types": "15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"typescript": "^3.9.2",
"webpack": "^4.43.0"
}
}
8 changes: 8 additions & 0 deletions examples/react-ts/src/button.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import { argsStory } from '@storybook/react';
import { Button } from './button';

export default { component: Button, title: 'Examples / Button' };

export const WithArgs = argsStory({ label: 'With args' });
export const Basic = () => <Button label="Click me" />;
14 changes: 14 additions & 0 deletions examples/react-ts/src/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { ButtonHTMLAttributes } from 'react';

export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
/**
* A label to show on the button
*/
label: string;
}

export const Button = ({ label = 'Hello', ...props }: ButtonProps) => (
<button type="button" {...props}>
{label}
</button>
);
19 changes: 19 additions & 0 deletions examples/react-ts/src/emoji-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import PropTypes from 'prop-types';

export const EmojiButton = ({ label, ...props }) => (
<button type="button" {...props}>
⚠️ {label}
</button>
);

EmojiButton.propTypes = {
/**
* A label to show on the button
*/
label: PropTypes.string,
};

EmojiButton.defaultProps = {
label: 'Hello',
};
8 changes: 8 additions & 0 deletions examples/react-ts/src/emoji-button.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import { argsStory } from '@storybook/react';
import { EmojiButton } from './emoji-button';

export default { component: EmojiButton, title: 'Examples / Emoji Button' };

export const WithArgs = argsStory({ label: 'With args' });
export const Basic = () => <EmojiButton label="Click me" />;
10 changes: 10 additions & 0 deletions examples/react-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"jsx": "preserve",
"skipLibCheck": true,
"strict": true
},
"include": ["src/*", "main.ts"]
}
4 changes: 4 additions & 0 deletions examples/vue-cli/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
stories: ['../src/**/*.stories.(ts|js|mdx)'],
addons: ['@storybook/addon-docs', '@storybook/addon-storysource', '@storybook/preset-scss'],
};
6 changes: 6 additions & 0 deletions examples/vue-cli/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const parameters = {
passArgsFirst: true,
docs: {
iframeHeight: '60px',
},
};
8 changes: 8 additions & 0 deletions examples/vue-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Storybook Demo

This is a demo app to test Vue-CLI integration with Storybook.
Please check vue-kitchen-sink for more examples of different ways to integrate with Storybook.

Run `yarn install` to sync Storybook module with the source code and run `yarn storybook` to start the Storybook.

You can also try `yarn serve` to see that vue-cli service also works.
3 changes: 3 additions & 0 deletions examples/vue-cli/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@vue/cli-plugin-babel/preset'],
};
28 changes: 28 additions & 0 deletions examples/vue-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "vue-cli-example",
"version": "6.0.0-beta.8",
"private": true,
"scripts": {
"build": "vue-cli-service build",
"build-storybook": "build-storybook -s public",
"serve": "vue-cli-service serve",
"storybook": "start-storybook -p 9009 -s public"
},
"dependencies": {
"core-js": "^3.6.4",
"vue": "^2.6.8",
"vue-class-component": "^7.2.3",
"vue-property-decorator": "^8.4.1"
},
"devDependencies": {
"@storybook/addon-essentials": "6.0.0-beta.8",
"@storybook/preset-scss": "^1.0.2",
"@storybook/source-loader": "6.0.0-beta.8",
"@storybook/vue": "6.0.0-beta.8",
"@vue/cli-plugin-babel": "~4.3.0",
"@vue/cli-plugin-typescript": "~4.3.0",
"@vue/cli-service": "~4.3.0",
"typescript": "^3.8.3",
"vue-template-compiler": "^2.6.11"
}
}
Loading