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

fix: livedemo disabled when config babel-plugin-import #2195

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions docs/guide/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,14 @@ import './index.css';
## 是否支持三级导航?

不支持。如果文档目录结构的复杂度超过 3 级,应该考虑优化文档整体结构而非使用三级导航。如果有特殊场景需要,可以自定义主题实现。

## ## 为什么 live demo 和 babel-plugin-import 无法一起使用?

live demo 需要的正是全量引入,和 babel-plugin-import 的工作逻辑有冲突。

解决方案:

- 不需要 live demo:忽略警告即可
- 希望开启 live demo:
- style: false 直接去掉插件即可
Copy link
Member

Choose a reason for hiding this comment

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

我评论的只是个提纲…语句还是要完善下的,比如 style: false 这个估计没人看得懂,且默认就是 false

Copy link
Member Author

Choose a reason for hiding this comment

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

hahaha好的, 下次.

- 否则借助 .dumi/global.css 加载组件样式,可以参考 [and ssr 静态样式导出](https://ant.design/docs/blog/extract-ssr-cn#static-extract-style)提取`css`文件。
8 changes: 2 additions & 6 deletions src/features/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fs from 'fs';
import path from 'path';
import { addAtomMeta, addExampleAssets } from '../assets';
import { getLoadHook } from './makoHooks';
import { shouldDisabledLiveDemo } from './utils';
export const techStacks: IDumiTechStack[] = [];
export default (api: IApi) => {
api.describe({ key: 'dumi:compile' });
Expand Down Expand Up @@ -87,12 +88,6 @@ export default (api: IApi) => {
const babelInUmi = memo.module.rule('src').use('babel-loader').entries();
if (!babelInUmi) return memo;
const loaderPath = require.resolve('../../loaders/markdown');

// support require mjs packages(eg. element-plus/es)
// memo.resolve.byDependency.set('commonjs', {
// conditionNames: ['require', 'node', 'import'],
// });

const loaderBaseOpts: Partial<IMdLoaderOptions> = {
techStacks,
cwd: api.cwd,
Expand All @@ -103,6 +98,7 @@ export default (api: IApi) => {
routes: api.appData.routes,
locales: api.config.locales,
pkg: api.pkg,
disableLiveDemo: shouldDisabledLiveDemo(api),
};
memo.module
.rule('watch-parent')
Expand Down
3 changes: 3 additions & 0 deletions src/features/compile/makoHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import url from 'url';
import { techStacks } from '.';
import { RunLoaderOption, runLoaders } from '../../utils';
import { addAtomMeta, addExampleAssets } from '../assets';
import { shouldDisabledLiveDemo } from './utils';

interface ICustomerRunLoaderInterface extends RunLoaderOption {
type?: 'css' | 'js' | 'jsx';
Expand All @@ -31,6 +32,7 @@ const customRunLoaders = async (options: ICustomerRunLoaderInterface) => {
const mdLoaderPath = require.resolve('../../loaders/markdown');

export const getLoadHook = (api: IApi) => {
const disableLiveDemo = shouldDisabledLiveDemo(api);
return async (filePath: string) => {
const loaderBaseOpts: Partial<IMdLoaderOptions> = {
techStacks,
Expand All @@ -42,6 +44,7 @@ export const getLoadHook = (api: IApi) => {
routes: api.appData.routes,
locales: api.config.locales || [],
pkg: api.pkg,
disableLiveDemo,
};

const requestUrl = url.parse(filePath);
Expand Down
17 changes: 17 additions & 0 deletions src/features/compile/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { logger } from '@umijs/utils';
import { isArray } from '@umijs/utils/compiled/lodash';
import { IApi } from 'umi';
export const shouldDisabledLiveDemo = (api: IApi) => {
const extraBabelPlugins = api.userConfig.extraBabelPlugins;
const disableFlag =
isArray(extraBabelPlugins) &&
extraBabelPlugins!.some((p: any) =>
/^import$|babel-plugin-import/.test(p[0]),
);
if (disableFlag) {
logger.warn(
'live demo feature has been automatically disabled since babel-plugin-import be registered, if you want to enable live demo feature, checkout: https://d.umijs.org/guide/faq',
Copy link
Member

Choose a reason for hiding this comment

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

链接加一下锚点

Copy link
Member Author

Choose a reason for hiding this comment

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

嗯, 想的是有中文且encode后太长了.

Copy link
Member

Choose a reason for hiding this comment

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

应该可以给文本加超链接,其他项目里看到过

);
}
return disableFlag;
};
42 changes: 24 additions & 18 deletions src/loaders/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface IMdLoaderDefaultModeOptions
atomId: string,
meta: IMdTransformerResult['meta']['frontmatter'],
) => void;
disableLiveDemo: boolean;
}

interface IMdLoaderDemosModeOptions
Expand Down Expand Up @@ -166,21 +167,21 @@ function emitDemo(
}
});

const dedupedDemosDeps = Object.entries(demoDepsMap).reduce<
IDemoDependency[]
>((acc, [, deps]) => {
return acc.concat(
Object.entries(deps)
.map(([key, specifier]) => {
const existingIndex = acc.findIndex((obj) => obj.key === key);
if (existingIndex === -1) {
return { key, specifier };
}
return undefined;
})
.filter((item) => item !== undefined),
);
}, []);
const dedupedDemosDeps = opts.disableLiveDemo
? []
: Object.entries(demoDepsMap).reduce<IDemoDependency[]>((acc, [, deps]) => {
return acc.concat(
Object.entries(deps)
.map(([key, specifier]) => {
const existingIndex = acc.findIndex((obj) => obj.key === key);
if (existingIndex === -1) {
return { key, specifier };
}
return undefined;
})
.filter((item) => item !== undefined),
);
}, []);
return Mustache.render(
`import React from 'react';
import '${winPath(this.getDependencies()[0])}?watch=parent';
Expand Down Expand Up @@ -231,8 +232,13 @@ export const demos = {
renderContext: function renderContext(
this: NonNullable<typeof demos>[0],
) {
// do not render context for inline demo
if (!('resolveMap' in this) || !('asset' in this)) return 'undefined';
// do not render context for inline demo && config babel-import-plugin project
if (
!('resolveMap' in this) ||
!('asset' in this) ||
opts.disableLiveDemo
)
return 'undefined';
const context = Object.entries(demoDepsMap[this.id]).reduce(
(acc, [key, specifier]) => ({
...acc,
Expand All @@ -245,7 +251,7 @@ export const demos = {
renderRenderOpts: function renderRenderOpts(
this: NonNullable<typeof demos>[0],
) {
if (!('renderOpts' in this)) {
if (!('renderOpts' in this) || opts.disableLiveDemo) {
return 'undefined';
}
const renderOpts = this.renderOpts;
Expand Down
Loading