Skip to content

Commit

Permalink
feat(cra): move to react-docgen-typescript-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb committed Jun 15, 2020
1 parent 8aec1d9 commit 00846be
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 437 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Builds
packages/*/dist
packages/*/storybook-static
examples/*/build
examples/*/storybook-static
4 changes: 2 additions & 2 deletions examples/cra-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@storybook/addon-docs": "5.3.17",
"@storybook/addon-docs": "5.3.19",
"@storybook/preset-create-react-app": "*",
"@storybook/react": "5.3.17",
"@storybook/react": "5.3.19",
"@types/jest": "25.1.4",
"@types/node": "13.9.1",
"@types/react": "16.9.23",
Expand Down
10 changes: 5 additions & 5 deletions examples/cra-ts/src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function register(config?: Config) {
function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
.then((registration) => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand Down Expand Up @@ -101,23 +101,23 @@ function registerValidSW(swUrl: string, config?: Config) {
};
};
})
.catch(error => {
.catch((error) => {
console.error('Error during service worker registration:', error);
});
}

function checkValidServiceWorker(swUrl: string, config?: Config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
});
Expand All @@ -136,7 +136,7 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {

export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister();
});
}
Expand Down
4 changes: 2 additions & 2 deletions examples/cra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@storybook/addon-docs": "5.3.17",
"@storybook/addon-docs": "5.3.19",
"@storybook/preset-create-react-app": "*",
"@storybook/react": "5.3.17",
"@storybook/react": "5.3.19",
"prop-types": "^15.7.2",
"react": "^16.13.0",
"react-dom": "^16.13.0",
Expand Down
10 changes: 5 additions & 5 deletions examples/cra/src/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function register(config) {
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
.then((registration) => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand Down Expand Up @@ -93,23 +93,23 @@ function registerValidSW(swUrl, config) {
};
};
})
.catch(error => {
.catch((error) => {
console.error('Error during service worker registration:', error);
});
}

function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
});
Expand All @@ -128,7 +128,7 @@ function checkValidServiceWorker(swUrl, config) {

export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister();
});
}
Expand Down
4 changes: 2 additions & 2 deletions examples/ts-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"dependencies": {
"@babel/core": "^7.8.7",
"@storybook/addon-docs": "^5.3.14",
"@storybook/addon-docs": "^5.3.19",
"@storybook/preset-typescript": "*",
"@storybook/react": "^5.3.17",
"@storybook/react": "^5.3.19",
"@types/react": "^16.9.23",
"@types/react-dom": "^16.9.5",
"babel-loader": "^8.0.6",
Expand Down
4 changes: 2 additions & 2 deletions examples/ts-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"dependencies": {
"@babel/core": "^7.8.7",
"@storybook/addon-docs": "^5.3.14",
"@storybook/addon-docs": "^5.3.19",
"@storybook/preset-typescript": "*",
"@storybook/vue": "^5.3.14",
"@storybook/vue": "^5.3.19",
"babel-loader": "^8.0.6",
"babel-preset-vue": "^2.0.2",
"typescript": "^3.8.3",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"husky": "^4.2.3",
"jest": "24.9.0",
"lint-staged": "^10.0.8",
"prettier": "^1.19.1",
"prettier": "^2.0.5",
"shelljs": "^0.8.2"
}
}
3 changes: 3 additions & 0 deletions packages/preset-create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
},
"devDependencies": {
"@types/semver": "^7.2.0",
"react-docgen-typescript-plugin": "^0.4.0",
"typescript": "^3.9.3"
},
"peerDependencies": {
"@babel/core": "*",
"@storybook/react": ">=5.2",
"react-docgen-typescript-plugin": ">=0.4.0",
"react-scripts": ">=3.0.0"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-create-react-app/src/helpers/checkPresets.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { join, resolve } from 'path';
import { logger } from '@storybook/node-logger';
import { Preset, Options } from '../options';
import { Preset, StorybookConfig } from '../types';

const incompatiblePresets = [
'@storybook/preset-scss',
'@storybook/preset-typescript',
];

export const checkPresets = (options: Options): void => {
export const checkPresets = (options: StorybookConfig): void => {
let presetsList: Preset[] = options.presetsList || [];

// Look for a legacy presets file if one exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const mergePlugins = (
[...basePlugins, ...additionalPlugins].reduce((plugins, plugin) => {
if (
plugins.some(
includedPlugin =>
(includedPlugin) =>
includedPlugin.constructor.name === plugin.constructor.name,
)
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'path';
import { Configuration, RuleSetRule } from 'webpack'; // eslint-disable-line import/no-extraneous-dependencies
import semver from 'semver';
import { Options } from '../options';
import { StorybookConfig } from '../types';

const isRegExp = (value: RegExp | unknown): value is RegExp =>
value instanceof RegExp;
Expand All @@ -13,13 +13,13 @@ const isString = (value: string | unknown): value is string =>
const testMatch = (rule: RuleSetRule, string: string): boolean => {
if (!rule.test) return false;
return Array.isArray(rule.test)
? rule.test.some(test => isRegExp(test) && test.test(string))
? rule.test.some((test) => isRegExp(test) && test.test(string))
: isRegExp(rule.test) && rule.test.test(string);
};

export const processCraConfig = (
craWebpackConfig: Configuration,
options: Options,
options: StorybookConfig,
): RuleSetRule[] => {
const configDir = resolve(options.configDir);

Expand Down
33 changes: 16 additions & 17 deletions packages/preset-create-react-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join, relative, resolve, dirname } from 'path';
import { Configuration } from 'webpack'; // eslint-disable-line import/no-extraneous-dependencies
import { logger } from '@storybook/node-logger';
import PnpWebpackPlugin from 'pnp-webpack-plugin';
import ReactDocgenTypescriptPlugin from 'react-docgen-typescript-plugin';
import { mergePlugins } from './helpers/mergePlugins';
import {
getReactScriptsPath,
Expand All @@ -10,7 +11,7 @@ import {
import { processCraConfig } from './helpers/processCraConfig';
import { checkPresets } from './helpers/checkPresets';
import { getModulePath } from './helpers/getModulePath';
import { Options } from './options';
import { StorybookConfig } from './types';

const CWD = process.cwd();
// When operating under PnP environments, this value will be set to a number
Expand Down Expand Up @@ -53,7 +54,7 @@ export const managerWebpack = (
// Update the core Webpack config.
export const webpack = (
webpackConfig: Configuration = {},
options: Options,
options: StorybookConfig,
): Configuration => {
let scriptsPath = REACT_SCRIPTS_PATH;

Expand Down Expand Up @@ -118,21 +119,19 @@ export const webpack = (
},
);

// A temporary fix to align with Storybook 6.
const { typescriptOptions } = options;
const tsDocgenRule =
!typescriptOptions ||
// NOTE: These are set by default in Storybook 6.
const {
typescriptOptions = {
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {},
},
} = options;
const tsDocgenPlugin =
typescriptOptions.reactDocgen === 'react-docgen-typescript'
? [
{
test: /\.tsx?$/,
use: [
{
loader: require.resolve('react-docgen-typescript-loader'),
options: typescriptOptions?.reactDocgenTypescriptOptions,
},
],
},
new ReactDocgenTypescriptPlugin(
typescriptOptions.reactDocgenTypescriptOptions,
),
]
: [];

Expand All @@ -141,7 +140,7 @@ export const webpack = (
...webpackConfig,
module: {
...webpackConfig.module,
rules: [...(filteredRules || []), ...craRules, ...tsDocgenRule],
rules: [...(filteredRules || []), ...craRules],
},
plugins: mergePlugins(webpackConfig.plugins, craWebpackConfig.plugins),
resolve: {
Expand All @@ -152,7 +151,7 @@ export const webpack = (
join(REACT_SCRIPTS_PATH, 'node_modules'),
...getModulePath(CWD),
],
plugins: [...plugins, PnpWebpackPlugin],
plugins: [...plugins, ...tsDocgenPlugin, PnpWebpackPlugin],
},
resolveLoader,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { PluginItem } from '@babel/core'; // eslint-disable-line import/no-extraneous-dependencies
/* eslint-disable import/no-extraneous-dependencies */
import type { PluginItem } from '@babel/core';
import type { PluginOptions } from 'react-docgen-typescript-plugin';

export type Preset = string | { name: string };

export interface Options {
export interface StorybookConfig {
/**
* Optionally set the package name of a react-scripts fork.
* In most cases, the package is located automatically by this preset.
Expand All @@ -16,7 +18,7 @@ export interface Options {
fileLoaderExcludes?: string[];
};

// TODO: Expose these from Storybook.
// TODO: Expose these from Storybook, will require Storybook 6.
configDir: string;
babelOptions: {
extends: string | null;
Expand All @@ -27,9 +29,9 @@ export interface Options {
packageJson: {
version: string;
};
typescriptOptions: {
// eslint-disable-next-line
reactDocgenTypescriptOptions: any;
reactDocgen: 'react-docgen' | 'react-docgen-typescript' | null;
// This always exists from Storybook 6, but not for older versions.
typescriptOptions?: {
reactDocgen: 'react-docgen-typescript' | 'react-docgen' | false;
reactDocgenTypescriptOptions: PluginOptions;
};
}
4 changes: 2 additions & 2 deletions packages/preset-typescript/src/helpers/processConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const processConfig = (
const { resolve = {}, plugins = [], module } = webpackConfig;
const { forkTsCheckerWebpackPluginOptions, include = [] } = options;

const rules = module?.rules.map(rule => {
const rules = module?.rules.map((rule) => {
// Find the rule containing 'babel-loader'
const isBabelRule =
Array.isArray(rule.use) &&
rule.use.find(
use => isLoader(use) && use.loader?.includes('babel-loader'),
(use) => isLoader(use) && use.loader?.includes('babel-loader'),
);

// If this is the
Expand Down
Loading

0 comments on commit 00846be

Please sign in to comment.