Skip to content

Commit fdc233d

Browse files
authored
fix: livedemo disabled when config babel-plugin-import (#2195)
* fix: livedemo disabled when config babel-plugin-import * docs: guide to faq * docs: simple faq
1 parent d1be4a4 commit fdc233d

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

Diff for: docs/guide/faq.md

+11
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,14 @@ import './index.css';
218218
## 是否支持三级导航?
219219

220220
不支持。如果文档目录结构的复杂度超过 3 级,应该考虑优化文档整体结构而非使用三级导航。如果有特殊场景需要,可以自定义主题实现。
221+
222+
## ## 为什么 live demo 和 babel-plugin-import 无法一起使用?
223+
224+
live demo 需要的正是全量引入,和 babel-plugin-import 的工作逻辑有冲突。
225+
226+
解决方案:
227+
228+
- 不需要 live demo:忽略警告即可
229+
- 希望开启 live demo:
230+
- style: false 直接去掉插件即可
231+
- 否则借助 .dumi/global.css 加载组件样式,可以参考 [and ssr 静态样式导出](https://ant.design/docs/blog/extract-ssr-cn#static-extract-style)提取`css`文件。

Diff for: src/features/compile/index.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import fs from 'fs';
77
import path from 'path';
88
import { addAtomMeta, addExampleAssets } from '../assets';
99
import { getLoadHook } from './makoHooks';
10+
import { shouldDisabledLiveDemo } from './utils';
1011
export const techStacks: IDumiTechStack[] = [];
1112
export default (api: IApi) => {
1213
api.describe({ key: 'dumi:compile' });
@@ -87,12 +88,6 @@ export default (api: IApi) => {
8788
const babelInUmi = memo.module.rule('src').use('babel-loader').entries();
8889
if (!babelInUmi) return memo;
8990
const loaderPath = require.resolve('../../loaders/markdown');
90-
91-
// support require mjs packages(eg. element-plus/es)
92-
// memo.resolve.byDependency.set('commonjs', {
93-
// conditionNames: ['require', 'node', 'import'],
94-
// });
95-
9691
const loaderBaseOpts: Partial<IMdLoaderOptions> = {
9792
techStacks,
9893
cwd: api.cwd,
@@ -103,6 +98,7 @@ export default (api: IApi) => {
10398
routes: api.appData.routes,
10499
locales: api.config.locales,
105100
pkg: api.pkg,
101+
disableLiveDemo: shouldDisabledLiveDemo(api),
106102
};
107103
memo.module
108104
.rule('watch-parent')

Diff for: src/features/compile/makoHooks.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import url from 'url';
66
import { techStacks } from '.';
77
import { RunLoaderOption, runLoaders } from '../../utils';
88
import { addAtomMeta, addExampleAssets } from '../assets';
9+
import { shouldDisabledLiveDemo } from './utils';
910

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

3334
export const getLoadHook = (api: IApi) => {
35+
const disableLiveDemo = shouldDisabledLiveDemo(api);
3436
return async (filePath: string) => {
3537
const loaderBaseOpts: Partial<IMdLoaderOptions> = {
3638
techStacks,
@@ -42,6 +44,7 @@ export const getLoadHook = (api: IApi) => {
4244
routes: api.appData.routes,
4345
locales: api.config.locales || [],
4446
pkg: api.pkg,
47+
disableLiveDemo,
4548
};
4649

4750
const requestUrl = url.parse(filePath);

Diff for: src/features/compile/utils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { logger } from '@umijs/utils';
2+
import { isArray } from '@umijs/utils/compiled/lodash';
3+
import { IApi } from 'umi';
4+
export const shouldDisabledLiveDemo = (api: IApi) => {
5+
const extraBabelPlugins = api.userConfig.extraBabelPlugins;
6+
const disableFlag =
7+
isArray(extraBabelPlugins) &&
8+
extraBabelPlugins!.some((p: any) =>
9+
/^import$|babel-plugin-import/.test(p[0]),
10+
);
11+
if (disableFlag) {
12+
logger.warn(
13+
'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',
14+
);
15+
}
16+
return disableFlag;
17+
};

Diff for: src/loaders/markdown/index.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface IMdLoaderDefaultModeOptions
2121
atomId: string,
2222
meta: IMdTransformerResult['meta']['frontmatter'],
2323
) => void;
24+
disableLiveDemo: boolean;
2425
}
2526

2627
interface IMdLoaderDemosModeOptions
@@ -166,21 +167,21 @@ function emitDemo(
166167
}
167168
});
168169

169-
const dedupedDemosDeps = Object.entries(demoDepsMap).reduce<
170-
IDemoDependency[]
171-
>((acc, [, deps]) => {
172-
return acc.concat(
173-
Object.entries(deps)
174-
.map(([key, specifier]) => {
175-
const existingIndex = acc.findIndex((obj) => obj.key === key);
176-
if (existingIndex === -1) {
177-
return { key, specifier };
178-
}
179-
return undefined;
180-
})
181-
.filter((item) => item !== undefined),
182-
);
183-
}, []);
170+
const dedupedDemosDeps = opts.disableLiveDemo
171+
? []
172+
: Object.entries(demoDepsMap).reduce<IDemoDependency[]>((acc, [, deps]) => {
173+
return acc.concat(
174+
Object.entries(deps)
175+
.map(([key, specifier]) => {
176+
const existingIndex = acc.findIndex((obj) => obj.key === key);
177+
if (existingIndex === -1) {
178+
return { key, specifier };
179+
}
180+
return undefined;
181+
})
182+
.filter((item) => item !== undefined),
183+
);
184+
}, []);
184185
return Mustache.render(
185186
`import React from 'react';
186187
import '${winPath(this.getDependencies()[0])}?watch=parent';
@@ -231,8 +232,13 @@ export const demos = {
231232
renderContext: function renderContext(
232233
this: NonNullable<typeof demos>[0],
233234
) {
234-
// do not render context for inline demo
235-
if (!('resolveMap' in this) || !('asset' in this)) return 'undefined';
235+
// do not render context for inline demo && config babel-import-plugin project
236+
if (
237+
!('resolveMap' in this) ||
238+
!('asset' in this) ||
239+
opts.disableLiveDemo
240+
)
241+
return 'undefined';
236242
const context = Object.entries(demoDepsMap[this.id]).reduce(
237243
(acc, [key, specifier]) => ({
238244
...acc,
@@ -245,7 +251,7 @@ export const demos = {
245251
renderRenderOpts: function renderRenderOpts(
246252
this: NonNullable<typeof demos>[0],
247253
) {
248-
if (!('renderOpts' in this)) {
254+
if (!('renderOpts' in this) || opts.disableLiveDemo) {
249255
return 'undefined';
250256
}
251257
const renderOpts = this.renderOpts;

0 commit comments

Comments
 (0)