Skip to content

Commit

Permalink
Merge branch 'main' into builder_doc_0219
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Feb 19, 2024
2 parents 7ea14aa + 5e7e505 commit 528eef7
Show file tree
Hide file tree
Showing 20 changed files with 408 additions and 265 deletions.
7 changes: 7 additions & 0 deletions .changeset/light-games-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/storybook-builder': minor
---

feat(storybook): use uni-builder instead of modern.js builder as build tool

feat(storybook): 使用 uni-builder 代替 modern.js builder 作为底层构建工具
6 changes: 6 additions & 0 deletions .changeset/silent-candles-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/module-tools': patch
---

fix(module-tools): add js extension when pkg type is module
fix(module-tools): 当包类型为 module 时,给产物里的相对路径补全文件后缀
2 changes: 1 addition & 1 deletion .changeset/slimy-dogs-hang.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
'@modern-js/builder-doc': patch
---

chore(plugin-vue): builder vue & vue2 plugin is no longer maintained,, please use rsbuild instead
chore(plugin-vue): builder vue & vue2 plugin is no longer maintained, please use rsbuild instead

chore(plugin-vue): builder vue & vue2 插件不再维护, 请使用 rsbuild 代替
27 changes: 17 additions & 10 deletions packages/document/module-doc/docs/en/guide/basic/use-module-doc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The sidebars corresponding in turn to the above file paths and routing paths are
### Configure sidebar

As shown in the figure above, the module documentation has automatically generated sidebars for file system routing, where the text of each column of the sidebar is determined by the file's first level title or directory name.
If you need to customize the sidebar, please use [_meta.json](https://rspress.dev/guide/basic/auto-nav-sidebar.html) or configure [sidebar](https://rspress.dev/api/config/config-theme.html#sidebar) directly.
If you need to customize the sidebar, please use [\_meta.json](https://rspress.dev/guide/basic/auto-nav-sidebar.html) or configure [sidebar](https://rspress.dev/api/config/config-theme.html#sidebar) directly.

:::info
If your document directory structure complies with internationalization, for example:
Expand Down Expand Up @@ -146,9 +146,9 @@ export default defineConfig({
modulePluginDoc({
/**
* Select the preview method
* @default web
* @default internal
*/
previewMode: 'mobile',
previewMode: 'iframe',
/**
* Select iframe position
* @default 'follow'
Expand All @@ -167,12 +167,12 @@ If you only want to change the way a particular `jsx` and `tsx` block is preview
// The content here will not be rendered
```

```jsx web
// Used to render desktop components
```jsx internal
// Used to render components in documentation
```

```jsx mobile
// Used to render mobile components
```jsx iframe
// Used to render components in iframe
```
````

Expand All @@ -186,6 +186,12 @@ If our demo is very complex, then it is recommended to write the demo separately
<code src="/path/demo.tsx"></code>
```

This also supports setting the preview method for each individual code block, for example:

```mdx
<code src="/path/demo.tsx" previewMode="iframe"></code>
```

## Using built-in components

The plugin implements some built-in components internally so that you can develop module documentation more easily.
Expand Down Expand Up @@ -398,10 +404,10 @@ type ParseToolOptions = {

### previewMode

- Type:`'mobile' | 'web'`
- Default: `'web'`
- Type:`'iframe' | 'internal'`
- Default: `'internal'`

In case of `web`, the component will be rendered directly in the page, otherwise it will be loaded through an iframe.
In case of `internal`, the component will be rendered directly in the page, otherwise it will be loaded through an iframe.

### deprecated: languages

Expand All @@ -417,6 +423,7 @@ or directly configure [sidebar](https://rspress.dev/api/config/config-theme.html
:::

## Scripts

- `modern dev`: Start dev server for doc site.
- `modern build --platform`: Build doc site in production, by default output directories is `doc_build`.

Expand Down
38 changes: 24 additions & 14 deletions packages/document/module-doc/docs/zh/guide/basic/use-module-doc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ docs
### 配置侧边栏

如上图所示,模块文档已经为文件系统路由自动生成了侧边栏,其中侧边栏每一栏的文本是由文件的一级标题或者目录名决定。
如果你需要自定义侧边栏,请使用 [_meta.json](https://rspress.dev/zh/guide/basic/auto-nav-sidebar.html) 或者直接配置 [sidebar](https://rspress.dev/zh/api/config/config-theme.html#sidebar)
如果你需要自定义侧边栏,请使用 [\_meta.json](https://rspress.dev/zh/guide/basic/auto-nav-sidebar.html) 或者直接配置 [sidebar](https://rspress.dev/zh/api/config/config-theme.html#sidebar)

:::info
如果你的文档目录结构是符合国际化的,例如:

```bash
docs
├── en
Expand All @@ -68,6 +69,7 @@ docs
├── button.mdx
├── index.mdx
```

你需要按照[国际化](https://rspress.dev/zh/guide/default-theme/i18n.html)章节,同时配置 `lang``locales`,否则模块自动生成的侧边栏不会处理 `zh``en` 这两个目录。
:::

Expand Down Expand Up @@ -139,9 +141,9 @@ export default defineConfig({
modulePluginDoc({
/**
* 选择预览方式
* @default web
* @default internal
*/
previewMode: 'mobile',
previewMode: 'iframe',
/**
* 设置 iframe 的位置
* @default 'follow'
Expand All @@ -160,12 +162,12 @@ export default defineConfig({
// 这里的内容不会被渲染
```

```jsx web
// 用来渲染桌面端组件
```jsx internal
// 内置在文档内容里渲染
```

```jsx mobile
// 用来渲染移动端组件
```jsx iframe
// 使用 iframe 渲染
```
````

Expand All @@ -179,6 +181,12 @@ export default defineConfig({
<code src="/path/demo.tsx"></code>
```

这同样支持单独设置代码块的预览方式,例如:

```mdx
<code src="/path/demo.tsx" previewMode="iframe"></code>
```

## 使用内置组件

插件内部实现了一部分内置组件,以便于你可以更轻松地开发模块文档。
Expand Down Expand Up @@ -245,15 +253,16 @@ export const Button = (props?: ButtonProps) => {};

:::warning
如果 Props 里使用了 React 的类型,你需要在 tsconfig.json 里添加 types ,否则会解析不到 React 命名空间下的类型。

```json
{
"compilerOptions": {
"types": ["react"],
},
"types": ["react"]
}
}
```
:::

:::

- `documentation`适用于工具库场景,用来解析 JSDoc 注释。

Expand Down Expand Up @@ -394,10 +403,10 @@ type ParseToolOptions = {

代码块预览方式。

- 类型:`'mobile' | 'web'`
- 默认值: `'web'`
- 类型:`'internal' | 'iframe'`
- 默认值: `'internal'`

`web`时,代码块内容将会直接渲染在页面中,反之将会通过 iframe 加载。
`internal`时,代码块内容将会直接渲染在页面中,反之将会通过 iframe 加载。

### deprecated: languages

Expand All @@ -408,11 +417,12 @@ type ParseToolOptions = {
### deprecated: useModuleSidebar

:::warning
从 MAJOR_VERSION.44.0 版本开始,内部实现了嗅探机制,请直接使用 [_meta.json](https://rspress.dev/zh/guide/basic/auto-nav-sidebar.html)
从 MAJOR_VERSION.44.0 版本开始,内部实现了嗅探机制,请直接使用 [\_meta.json](https://rspress.dev/zh/guide/basic/auto-nav-sidebar.html)
或者直接配置 [sidebar](https://rspress.dev/zh/api/config/config-theme.html#sidebar) 来实现自定义侧边栏。
:::

## 命令行

- `modern dev`: 启动文档站本地开发。
- `modern build --platform`: 构建生产环境产物。

Expand Down
10 changes: 8 additions & 2 deletions packages/solutions/module-tools/src/builder/feature/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function redirectImport(
filePath: string,
outputDir: string,
jsExtension: string,
isModule?: boolean,
matchPath?: MatchPath,
): Promise<MagicString> {
const str: MagicString = new MagicString(code);
Expand Down Expand Up @@ -94,7 +95,11 @@ async function redirectImport(
}

if (redirect.autoExtension) {
if (ext === '' && jsExtension !== '.js' && name.startsWith('.')) {
if (
ext === '' &&
name.startsWith('.') &&
(jsExtension !== '.js' || isModule)
) {
// add extension for relative path, no check if it's a directory.
str.overwrite(start, end, `${name}${jsExtension}`);
return;
Expand Down Expand Up @@ -267,7 +272,7 @@ export const redirect = {
if (!matchModule.length) {
return args;
}
const { jsExtension } = getDefaultOutExtension({
const { jsExtension, isModule } = getDefaultOutExtension({
format,
root,
autoExtension,
Expand All @@ -281,6 +286,7 @@ export const redirect = {
id,
dirname(outputPath),
jsExtension,
isModule,
matchPath,
);
return {
Expand Down
1 change: 1 addition & 0 deletions packages/solutions/module-tools/src/utils/outExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ export const getDefaultOutExtension = (options: {
return {
jsExtension,
dtsExtension,
isModule,
};
};
34 changes: 10 additions & 24 deletions packages/storybook/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@
},
"license": "MIT",
"dependencies": {
"@modern-js/builder": "workspace:*",
"@modern-js/builder-shared": "workspace:*",
"@rsbuild/shared": "0.4.3",
"@modern-js/uni-builder": "workspace:*",
"@modern-js/core": "workspace:*",
"@modern-js/runtime": "workspace:*",
"@modern-js/utils": "workspace:*",
"@rspack/plugin-react-refresh": "0.4.5",
"@storybook/components": "~7.6.1",
"@storybook/core-common": "~7.6.1",
"@storybook/csf-plugin": "~7.6.1",
"@storybook/components": "~7.6.12",
"@storybook/core-common": "~7.6.12",
"@storybook/csf-plugin": "~7.6.12",
"@storybook/global": "~5.0.0",
"@storybook/mdx1-csf": "~1.0.0",
"@storybook/mdx2-csf": "~1.1.0",
"@storybook/preview": "~7.6.1",
"@storybook/preview-api": "~7.6.1",
"@storybook/preview": "~7.6.12",
"@storybook/preview-api": "~7.6.12",
"@storybook/react-docgen-typescript-plugin": "1.0.6--canary.9.0c3f3b7.0",
"@storybook/router": "~7.6.1",
"@storybook/theming": "~7.6.1",
"@storybook/router": "~7.6.12",
"@storybook/theming": "~7.6.12",
"ast-types": "^0.14.2",
"minimatch": "^9.0.3",
"react-docgen": "6.0.0-alpha.3",
Expand All @@ -83,28 +83,14 @@
"webpack-hot-middleware": "^2.25.4"
},
"devDependencies": {
"@modern-js/builder-rspack-provider": "workspace:*",
"@modern-js/builder-webpack-provider": "workspace:*",
"@modern-js/core": "workspace:*",
"@scripts/build": "workspace:*",
"@storybook/types": "~7.6.1",
"@storybook/types": "~7.6.12",
"@types/babel__core": "^7.20.1",
"@types/serve-static": "^1.13.10",
"@types/webpack-hot-middleware": "^2.25.6",
"typescript": "^5.2.2"
},
"peerDependencies": {
"@modern-js/builder-webpack-provider": "workspace:^2.46.1",
"@modern-js/builder-rspack-provider": "workspace:^2.46.1"
},
"peerDependenciesMeta": {
"@modern-js/builder-webpack-provider": {
"optional": true
},
"@modern-js/builder-rspack-provider": {
"optional": true
}
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/storybook/builder/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, join, parse } from 'path';
import webpackDevMiddleware from '@modern-js/builder-shared/webpack-dev-middleware';
import webpackDevMiddleware from '@rsbuild/shared/webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import serveStatic from 'serve-static';
import type { Builder as RawStorybookBuilder, Stats } from '@storybook/types';
Expand Down
46 changes: 22 additions & 24 deletions packages/storybook/builder/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createBuilder, mergeBuilderConfig } from '@modern-js/builder';
import { createUniBuilder } from '@modern-js/uni-builder';
import { mergeRsbuildConfig, type RsbuildConfig } from '@rsbuild/shared';
import { loadConfig } from '@modern-js/core';
import type { Options } from '@storybook/types';
import type { Compiler } from '@modern-js/builder-shared/webpack-dev-middleware';
import type { BuilderConfig, BuilderOptions } from './types';
import { getConfigFileName, getProvider, runWithErrorMsg } from './utils';
import { pluginStorybook } from './plugin-storybook';
import type { Compiler } from '@rsbuild/shared/webpack-dev-middleware';
import type { BuilderOptions, BuilderConfig } from './types';
import { getConfigFileName, runWithErrorMsg } from './utils';
import { pluginStorybook, addonBabelAdapter } from './plugin-storybook';

export async function getCompiler(
cwd: string,
Expand All @@ -27,27 +28,24 @@ export async function getCompiler(
(await presets.apply<BuilderConfig | void>('modern', loadedConfig)) ||
loadedConfig;

const provider = await getProvider(
bundler,
mergeBuilderConfig(finalConfig, builderOptions.builderConfig) || {},
);
const uniBuilderConfig = builderOptions.builderConfig
? (mergeRsbuildConfig(
finalConfig as RsbuildConfig,
builderOptions.builderConfig as RsbuildConfig,
) as BuilderConfig)
: finalConfig || {};

const bundlerType = bundler || 'webpack';

if (!provider) {
if (bundler) {
throw new Error(
`You choose to use ${bundler}, but @modern-js/builder-${bundler}-provider not found in your project, please install it`,
);
} else {
throw new Error(
`Please install one provider first, try install @modern-js/builder-rspack-provider or @modern-js/builder-webpack-provider first`,
);
}
}

const builder = await createBuilder(provider, {
const builder = await createUniBuilder({
bundlerType,
cwd,
target: 'web',
framework: 'modern.js storybook',
frameworkConfigPath: res?.path ? res.path : undefined,
config:
bundlerType === 'webpack'
? await addonBabelAdapter(uniBuilderConfig, options)
: uniBuilderConfig,
entry: {
main: entries,
},
Expand All @@ -58,5 +56,5 @@ export async function getCompiler(
...(finalConfig.builderPlugins || []),
]);

return builder.createCompiler() as Promise<Compiler>;
return builder.createCompiler();
}
3 changes: 1 addition & 2 deletions packages/storybook/builder/src/docgen/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Options } from '@storybook/types';
import { CHAIN_ID, logger } from '@modern-js/utils';
import { RspackConfig } from '@modern-js/builder-rspack-provider';
import { WebpackChain } from '@modern-js/builder-webpack-provider';
import type { RspackConfig, WebpackChain } from '@rsbuild/shared';

export type DocgenOptions = {
reactDocgen?: 'react-docgen' | 'react-docgen-typescript' | false;
Expand Down
Loading

0 comments on commit 528eef7

Please sign in to comment.