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: multi-instance bug and unhandled error for live demo feature #2078

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions src/assetParsers/block.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parseCodeFrontmatter } from '@/utils';
import { build } from '@umijs/bundler-utils/compiled/esbuild';
import assert from 'assert';
import { IDumiConfig } from 'dist/types';
import type { ExampleBlockAsset } from 'dumi-assets-types';
import type { sync } from 'enhanced-resolve';
import fs from 'fs';
Expand Down Expand Up @@ -32,6 +33,7 @@ async function parseBlockAsset(opts: {
entryPointCode?: string;
resolver: typeof sync;
techStack: IDumiTechStack;
externals: IDumiConfig['externals'];
}) {
const asset: IParsedBlockAsset['asset'] = {
type: 'BLOCK',
Expand Down Expand Up @@ -80,7 +82,11 @@ async function parseBlockAsset(opts: {
value: pkg.version,
};

result.resolveMap[args.path] = resolved;
result.resolveMap[args.path] = Object.keys(
Copy link
Member

Choose a reason for hiding this comment

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

线下讨论结果同步:改成直接用 args.path 而非绝对路径,也不需要判断 externals 了

opts.externals || {},
).includes(args.path)
? args.path
: resolved;
}

// make all deps external
Expand Down Expand Up @@ -179,7 +185,10 @@ async function parseBlockAsset(opts: {

// save file absolute path for load file via raw-loader
// to avoid bundle same file to save bundle size
if (!isEntryPoint || !opts.entryPointCode) {
if (
opts.techStack.runtimeOpts &&
(!isEntryPoint || !opts.entryPointCode)
) {
result.resolveMap[filename] = args.path;
}

Expand Down
13 changes: 12 additions & 1 deletion src/client/theme-api/useLiveDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,18 @@ export const useLiveDemo = (
oError.apply(console, args);

// check component is able to render, to avoid show react overlay error
(await renderToStaticMarkupDeferred)(newDemoNode);
try {
(await renderToStaticMarkupDeferred)(newDemoNode);
} catch (err: any) {
const shouldSkipError =
err.message.includes(
'Unable to find node on an unmounted component',
) ||
err.message.includes(
'Portals are not currently supported by the server renderer',
);
if (!shouldSkipError) throw err;
}
console.error = oError;

// set new demo node with passing source
Expand Down
1 change: 1 addition & 0 deletions src/features/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default (api: IApi) => {
techStacks,
cwd: api.cwd,
alias: api.config.alias,
externals: api.config.externals,
resolve: api.config.resolve,
extraRemarkPlugins: api.config.extraRemarkPlugins,
extraRehypePlugins: api.config.extraRehypePlugins,
Expand Down
9 changes: 7 additions & 2 deletions src/loaders/markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isTabRouteFile } from '@/features/tabs';
import type { IThemeLoadResult } from '@/features/theme/loader';
import { IDumiConfig } from '@/types';
import { generateMetaChunkName, getCache, getContentHash } from '@/utils';
import fs from 'fs';
import { Mustache, lodash, winPath } from 'umi/plugin-utils';
Expand All @@ -8,11 +9,11 @@ import transform, {
type IMdTransformerResult,
} from './transformer';
import { CONTENT_TEXTS_OBJ_NAME } from './transformer/rehypeText';

Copy link
Member

Choose a reason for hiding this comment

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

空行保留

interface IMdLoaderDefaultModeOptions
extends Omit<IMdTransformerOptions, 'fileAbsPath'> {
mode?: 'markdown';
builtins: IThemeLoadResult['builtins'];
externals: IDumiConfig['externals'];
onResolveDemos?: (
demos: NonNullable<IMdTransformerResult['meta']['demos']>,
) => void;
Expand Down Expand Up @@ -58,7 +59,11 @@ export type IMdLoaderOptions =
function getDemoSourceFiles(demos: IMdTransformerResult['meta']['demos'] = []) {
return demos.reduce<string[]>((ret, demo) => {
if ('resolveMap' in demo) {
ret.push(...Object.values(demo.resolveMap));
ret.push(
...Object.values(demo.resolveMap).filter((path) =>
path.startsWith('/'),
Copy link
Member

Choose a reason for hiding this comment

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

改用 path.isAbsolute

),
);
}

return ret;
Expand Down
2 changes: 2 additions & 0 deletions src/loaders/markdown/transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface IMdTransformerOptions {
cwd: string;
fileAbsPath: string;
alias: ResolveOptions['alias'];
externals: IDumiConfig['externals'];
parentAbsPath?: string;
techStacks: IDumiTechStack[];
resolve: IDumiConfig['resolve'];
Expand Down Expand Up @@ -190,6 +191,7 @@ export default async (raw: string, opts: IMdTransformerOptions) => {
fileLocale,
resolve: opts.resolve,
resolver,
externals: opts.externals,
})
.use(rehypeSlug)
.use(rehypeLink, {
Expand Down
3 changes: 3 additions & 0 deletions src/loaders/markdown/transformer/rehypeDemo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import parseBlockAsset from '@/assetParsers/block';
import type { IDumiDemoProps } from '@/client/theme-api/DumiDemo';
import { IDumiConfig } from '@/types';
import { getFileIdFromFsPath } from '@/utils';
import type { sync } from 'enhanced-resolve';
import type { Element, Root } from 'hast';
Expand Down Expand Up @@ -41,6 +42,7 @@ type IRehypeDemoOptions = Pick<
resolver: typeof sync;
fileLocaleLessPath: string;
fileLocale?: string;
externals: IDumiConfig['externals'];
};

/**
Expand Down Expand Up @@ -265,6 +267,7 @@ export default function rehypeDemo(
entryPointCode: codeType === 'external' ? undefined : codeValue,
resolver: opts.resolver,
techStack,
externals: opts.externals,
};

const runtimeOpts = techStack.runtimeOpts;
Expand Down
2 changes: 1 addition & 1 deletion suites/theme-mobile/src/builtins/Previewer/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
&:first-child + .dumi-default-previewer-sources {
border-top: 1px dashed @c-border-light;
margin-top: 42px;

overflow: auto;
@{dark-selector} & {
border-top-color: @c-border-less-dark;
}
Expand Down
Loading