Skip to content

Commit

Permalink
feat: support fork-ts-checker-webpack-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed May 24, 2024
1 parent 6498da5 commit 332bef7
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ The function should return the updated Rsbuild configuration.

## Troubleshooting

### Error caused by bundling unwanted modules
### Error caused by bundling unexpected files

Because Rspack temporarily does not support the `webpackInclude` magic comment, non-story files may be bundled, which could lead to build failures. These files can be ignored using `rspack.IgnorePlugin`.

Expand Down Expand Up @@ -157,8 +157,8 @@ export default {

### Features

- [ ] Support TS type check (fork-ts-checker-webpack-plugin)
- [ ] Support more frameworks
- [x] Support TS type check (fork-ts-checker-webpack-plugin)
- [ ] Support more frameworks (Preact / Svelte / vanilla html / Lit)

### Rspack support

Expand Down
1 change: 1 addition & 0 deletions packages/builder-rsbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"css-loader": "^6.7.1",
"es-module-lexer": "^1.5.0",
"express": "^4.17.3",
"fork-ts-checker-webpack-plugin": "^8.0.0",

This comment has been minimized.

Copy link
@tmkx

tmkx May 24, 2024

Contributor

why not use version 9.x so we can reuse the same dependencies as the rsbuild plugin?
https://github.com/web-infra-dev/rsbuild/blob/edd280b6942751600105b3be988b65c8d7997240/packages/plugin-type-check/package.json#L31

"fs-extra": "^11.1.0",
"magic-string": "^0.30.5",
"path-browserify": "^1.0.1",
Expand Down
16 changes: 10 additions & 6 deletions packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extname, dirname, join, resolve } from 'path'
import { dirname, join, resolve } from 'path'
// @ts-expect-error (I removed this on purpose, because it's incorrect)
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin'
import type { Options } from '@storybook/types'
Expand All @@ -15,6 +15,7 @@ import { getVirtualModules } from './virtual-module-mapping'
import { loadConfig, mergeRsbuildConfig } from '@rsbuild/core'
import type { RsbuildConfig } from '@rsbuild/core'
import { webpack as docsWebpack } from '@storybook/addon-docs/dist/preset'
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'

const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any
Expand Down Expand Up @@ -56,7 +57,7 @@ export default async (options: Options): Promise<RsbuildConfig> => {
configType,
presets,
previewUrl,
// typescriptOptions,
typescriptOptions,
features,
} = options

Expand Down Expand Up @@ -100,12 +101,12 @@ export default async (options: Options): Promise<RsbuildConfig> => {
})

// TODO: not inclined to support fork-ts-checker-webpack-plugin
// const builderOptions = await getBuilderOptions<BuilderOptions>(options)
// const shouldCheckTs =
// typescriptOptions.check && !typescriptOptions.skipCompiler
// const tsCheckOptions = typescriptOptions.checkOptions || {}
const shouldCheckTs =
typescriptOptions.check && !typescriptOptions.skipCompiler
const tsCheckOptions = typescriptOptions.checkOptions || {}

// TODO: Rspack doesn't support persistent cache yet
// const builderOptions = await getBuilderOptions<BuilderOptions>(options)
// const cacheConfig = builderOptions.fsCache
// ? { cache: { type: 'filesystem' as const } }
// : {}
Expand Down Expand Up @@ -257,6 +258,9 @@ export default async (options: Options): Promise<RsbuildConfig> => {
process: require.resolve('process/browser.js'),
}),
new CaseSensitivePathsPlugin(),
shouldCheckTs
? new ForkTsCheckerWebpackPlugin(tsCheckOptions)
: null,
].filter(Boolean),
)

Expand Down
17 changes: 16 additions & 1 deletion packages/builder-rsbuild/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import type { RsbuildConfig } from '@rsbuild/core'
import type { Builder, Options } from '@storybook/types'
import type {
Builder,
Options,
TypescriptOptions as TypeScriptOptionsBase,
} from '@storybook/types'
import type ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'

// Storybook's Stats are optional Webpack related property
type RsbuildStats = {
toJson: () => any
}

/**
* Options for TypeScript usage within Storybook.
*/
export interface TypescriptOptions extends TypeScriptOptionsBase {
/**
* Configures `fork-ts-checker-webpack-plugin`
*/
checkOptions?: ConstructorParameters<typeof ForkTsCheckerWebpackPlugin>[0]
}

export type RsbuildBuilder = Builder<RsbuildConfig, RsbuildStats>

export type RsbuildFinal = (
Expand Down
45 changes: 25 additions & 20 deletions packages/react-rsbuild/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type {
StorybookConfigRsbuild,
BuilderOptions,
TypescriptOptions as TypescriptOptionsBuilder,
} from 'storybook-builder-rsbuild'
import type { StorybookConfig as StorybookConfigBase } from '@storybook/types'
import type {
StorybookConfig as StorybookConfigBase,
TypescriptOptions as TypescriptOptionsBase,
} from '@storybook/types'
import type { PluginOptions as ReactDocgenTypescriptOptions } from '@storybook/react-docgen-typescript-plugin'

type FrameworkName = 'storybook-react-rsbuild'
Expand All @@ -21,6 +25,22 @@ export type FrameworkOptions = {
legacyRootApi?: boolean
}

type TypescriptOptionsReact = {
/**
* Sets the type of Docgen when working with React and TypeScript
*
* @default `'react-docgen'`
*/
reactDocgen: 'react-docgen-typescript' | 'react-docgen' | false
/**
* Configures `react-docgen-typescript-plugin`
*
* @default
* @see https://github.com/storybookjs/storybook/blob/next/code/builders/builder-webpack5/src/config/defaults.js#L4-L6
*/
reactDocgenTypescriptOptions: ReactDocgenTypescriptOptions
}

type StorybookConfigFramework = {
framework:
| FrameworkName
Expand All @@ -36,22 +56,9 @@ type StorybookConfigFramework = {
options: BuilderOptions
}
}
}

type TypescriptOptions = StorybookConfigBase['typescript'] & {
/**
* Sets the type of Docgen when working with React and TypeScript
*
* @default `'react-docgen'`
*/
reactDocgen: 'react-docgen-typescript' | 'react-docgen' | false
/**
* Configures `react-docgen-typescript-plugin`
*
* @default
* @see https://github.com/storybookjs/storybook/blob/next/code/builders/builder-webpack5/src/config/defaults.js#L4-L6
*/
reactDocgenTypescriptOptions: ReactDocgenTypescriptOptions
typescript?: Partial<
TypescriptOptionsBase & TypescriptOptionsBuilder & TypescriptOptionsReact
>
}

/**
Expand All @@ -62,6 +69,4 @@ export type StorybookConfig = Omit<
keyof StorybookConfigRsbuild | keyof StorybookConfigFramework
> &
StorybookConfigRsbuild &
StorybookConfigFramework & {
typescript?: Partial<TypescriptOptions>
}
StorybookConfigFramework
8 changes: 6 additions & 2 deletions packages/vue3-rsbuild/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { StorybookConfig as StorybookConfigBase } from '@storybook/types'
import type {
StorybookConfig as StorybookConfigBase,
TypescriptOptions as TypescriptOptionsBaseAndVue,
} from '@storybook/types'
import type {
StorybookConfigRsbuild,
TypescriptOptions as TypescriptOptionsBuilder,
BuilderOptions,
} from 'storybook-builder-rsbuild'

Expand All @@ -26,7 +30,7 @@ type StorybookConfigFramework = {
options: BuilderOptions
}
}
typescript?: StorybookConfigBase['typescript']
typescript?: Partial<TypescriptOptionsBaseAndVue & TypescriptOptionsBuilder>
}

/**
Expand Down
Loading

0 comments on commit 332bef7

Please sign in to comment.