Skip to content

Commit

Permalink
feat: reuse @rsbuild/plugin-type-check to type check
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed May 26, 2024
1 parent 4938dbf commit b25a7a3
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 86 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ jobs:
cache: 'pnpm'

- name: Install Dependencies
run: pnpm install
run: |
pnpm install --frozen-lockfile
pnpm check
- name: Build Test
run: |
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"prettier": "^3.2.5",
"simple-git-hooks": "^2.11.1",
"sort-package-json": "^2.10.0",
"typescript": "^5.3.2",
"vitest": "^1.6.0"
},
"packageManager": "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion packages/builder-rsbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"prep": "node --loader ../../scripts/node_modules/esbuild-register/loader.js -r ../../scripts/node_modules/esbuild-register/register.js ../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@rsbuild/plugin-type-check": "0.6.15",
"@storybook/addon-docs": "^8.0.10",
"@storybook/channels": "^8.0.10",
"@storybook/client-logger": "^8.0.10",
Expand All @@ -78,7 +79,6 @@
"css-loader": "^6.7.1",
"es-module-lexer": "^1.5.0",
"express": "^4.17.3",
"fork-ts-checker-webpack-plugin": "^8.0.0",
"fs-extra": "^11.1.0",
"magic-string": "^0.30.5",
"path-browserify": "^1.0.1",
Expand Down
7 changes: 4 additions & 3 deletions packages/builder-rsbuild/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
WebpackInvocationError,
} from '@storybook/core-events/server-errors'
import type { RsbuildBuilder } from './types'
import rsbuildConfig from './preview/iframe-rsbuild.config'
import rsbuildConfig, {
type RsbuildBuilderOptions,
} from './preview/iframe-rsbuild.config'

import prettyTime from 'pretty-hrtime'

Expand Down Expand Up @@ -39,9 +41,8 @@ export const executor = {
},
}

export const rsbuild = async (_: unknown, options: Options) => {
export const rsbuild = async (_: unknown, options: RsbuildBuilderOptions) => {
const defaultConfig = await rsbuildConfig(options)

const { presets } = options
const finalDefaultConfig = await presets.apply(
'rsbuildFinal',
Expand Down
17 changes: 12 additions & 5 deletions packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ 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'
import { pluginTypeCheck } from '@rsbuild/plugin-type-check'
import type { TypescriptOptions } from '../types'

const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any
Expand Down Expand Up @@ -48,7 +49,13 @@ const storybookPaths: Record<string, string> = {
...(themingPath ? { [`@storybook/theming`]: themingPath } : {}),
}

export default async (options: Options): Promise<RsbuildConfig> => {
export type RsbuildBuilderOptions = Options & {
typescriptOptions: TypescriptOptions
}

export default async (
options: RsbuildBuilderOptions,
): Promise<RsbuildConfig> => {
const appliedDocsWebpack = await docsWebpack({}, options)
const {
outputDir = join('.', 'public'),
Expand Down Expand Up @@ -201,6 +208,9 @@ export default async (options: Options): Promise<RsbuildConfig> => {
},
},
},
plugins: [shouldCheckTs ? pluginTypeCheck(tsCheckOptions) : null].filter(
Boolean,
),
tools: {
rspack: (config, { addRules, appendPlugins, rspack, mergeConfig }) => {
// TODO: Rspack doesn't support `unknownContextCritical` yet
Expand Down Expand Up @@ -258,9 +268,6 @@ export default async (options: Options): Promise<RsbuildConfig> => {
process: require.resolve('process/browser.js'),
}),
new CaseSensitivePathsPlugin(),
shouldCheckTs
? new ForkTsCheckerWebpackPlugin(tsCheckOptions)
: null,
].filter(Boolean),
)

Expand Down
6 changes: 3 additions & 3 deletions packages/builder-rsbuild/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
Options,
TypescriptOptions as TypeScriptOptionsBase,
} from '@storybook/types'
import type ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import type { PluginTypeCheckerOptions } from '@rsbuild/plugin-type-check'

// Storybook's Stats are optional Webpack related property
type RsbuildStats = {
Expand All @@ -16,9 +16,9 @@ type RsbuildStats = {
*/
export interface TypescriptOptions extends TypeScriptOptionsBase {
/**
* Configures `fork-ts-checker-webpack-plugin`
* Configures `@rsbuild/plugin-type-check`, using `fork-ts-checker-webpack-plugin` under the hood.
*/
checkOptions?: ConstructorParameters<typeof ForkTsCheckerWebpackPlugin>[0]
checkOptions?: PluginTypeCheckerOptions
}

export type RsbuildBuilder = Builder<RsbuildConfig, RsbuildStats>
Expand Down
1 change: 1 addition & 0 deletions packages/react-rsbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"devDependencies": {
"@storybook/types": "^8.0.10",
"@types/resolve": "^1.20.6",
"add": "^2.0.6",
"typescript": "^5.3.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-rsbuild/src/loaders/react-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import * as TsconfigPaths from 'tsconfig-paths'
import findUp from 'find-up'
import MagicString from 'magic-string'
// @ts-expect-error
// @ts-expect-error can not reexport `LoaderContext` from @rsbuild/core
import type { LoaderContext } from 'webpack'
import type {
Handler,
Expand Down
114 changes: 44 additions & 70 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sandboxes/react-rsbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
"@types/react-dom": "^18",
"storybook": "8.1.1",
"storybook-react-rsbuild": "workspace:*",
"typescript": "^4.8"
"typescript": "^5.3.2"
}
}
Loading

0 comments on commit b25a7a3

Please sign in to comment.