Skip to content

Commit

Permalink
fix: livedemo disabled when config babel-plugin-import
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinbao1001 committed Sep 6, 2024
1 parent 2559335 commit 40581fc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
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 { 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) {
api.logger.warn(
"dumi don't suggest to use babel-plugin-import, liveDemo will be disabled because of this config.",
);
}
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

0 comments on commit 40581fc

Please sign in to comment.