dumi 2.3.0-beta.7 → 2.3.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiled/crates/swc_plugin_react_demo.wasm +0 -0
- package/dist/assetParsers/BaseParser.d.ts +55 -0
- package/dist/assetParsers/BaseParser.js +126 -0
- package/dist/assetParsers/atom.d.ts +18 -27
- package/dist/assetParsers/atom.js +84 -110
- package/dist/assetParsers/block.d.ts +2 -0
- package/dist/assetParsers/block.js +29 -16
- package/dist/assetParsers/utils.d.ts +79 -0
- package/dist/assetParsers/utils.js +112 -0
- package/dist/client/pages/Demo/index.js +11 -5
- package/dist/client/theme-api/DumiDemo/index.js +12 -4
- package/dist/client/theme-api/context.d.ts +1 -2
- package/dist/client/theme-api/index.d.ts +1 -1
- package/dist/client/theme-api/types.d.ts +12 -5
- package/dist/client/theme-api/useLiveDemo.js +114 -46
- package/dist/client/theme-api/useRenderer.d.ts +5 -0
- package/dist/client/theme-api/useRenderer.js +88 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +13 -0
- package/dist/features/compile/index.js +15 -13
- package/dist/features/parser.js +9 -5
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -0
- package/dist/loaders/markdown/index.js +24 -5
- package/dist/loaders/markdown/transformer/index.d.ts +11 -2
- package/dist/loaders/markdown/transformer/index.js +3 -0
- package/dist/loaders/markdown/transformer/rehypeDemo.js +22 -7
- package/dist/techStacks/react.js +6 -23
- package/dist/techStacks/utils.d.ts +18 -0
- package/dist/techStacks/utils.js +85 -0
- package/dist/types.d.ts +26 -3
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +2 -2
- package/package.json +7 -3
- package/tech-stack-utils.d.ts +12 -0
- package/tech-stack-utils.js +9 -0
- package/theme-default/builtins/API/index.d.ts +1 -0
- package/theme-default/builtins/API/index.js +12 -6
- package/theme-default/builtins/Previewer/index.less +1 -0
- package/theme-default/builtins/SourceCode/index.js +3 -2
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/assetParsers/utils.ts
|
|
30
|
+
var utils_exports = {};
|
|
31
|
+
__export(utils_exports, {
|
|
32
|
+
createApiParser: () => createApiParser,
|
|
33
|
+
createExposedClass: () => createExposedClass,
|
|
34
|
+
createRemoteClass: () => createRemoteClass
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(utils_exports);
|
|
37
|
+
var Comlink = __toESM(require("comlink"));
|
|
38
|
+
var import_node_adapter = __toESM(require("comlink/dist/umd/node-adapter"));
|
|
39
|
+
var import_plugin_utils = require("umi/plugin-utils");
|
|
40
|
+
var import_worker_threads = require("worker_threads");
|
|
41
|
+
var import_BaseParser = require("./BaseParser");
|
|
42
|
+
function createExposedClass(ClassConstructor, publicMethods = ["parse", "destroy", "patch"]) {
|
|
43
|
+
let realInstance;
|
|
44
|
+
const exposedClass = class {
|
|
45
|
+
constructor(...params) {
|
|
46
|
+
realInstance = new ClassConstructor(...params);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
publicMethods.forEach((method) => {
|
|
50
|
+
Object.assign(exposedClass.prototype, {
|
|
51
|
+
[method](...args) {
|
|
52
|
+
return realInstance[method](...args);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
return exposedClass;
|
|
57
|
+
}
|
|
58
|
+
function createRemoteClass(filename, ClassConstructor, opts = { destroyMethod: "destroy" }) {
|
|
59
|
+
if (!import_worker_threads.isMainThread) {
|
|
60
|
+
if (import_worker_threads.parentPort) {
|
|
61
|
+
Comlink.expose(
|
|
62
|
+
createExposedClass(ClassConstructor, opts.publicMethods),
|
|
63
|
+
(0, import_node_adapter.default)(import_worker_threads.parentPort)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return ClassConstructor;
|
|
67
|
+
}
|
|
68
|
+
const worker = new import_worker_threads.Worker(filename);
|
|
69
|
+
const RemoteClass = Comlink.wrap((0, import_node_adapter.default)(worker));
|
|
70
|
+
let pendingInstance;
|
|
71
|
+
let instance = null;
|
|
72
|
+
return class {
|
|
73
|
+
constructor(...params) {
|
|
74
|
+
pendingInstance = new RemoteClass(...params);
|
|
75
|
+
return new Proxy(this, {
|
|
76
|
+
get: (_, key) => {
|
|
77
|
+
return async function(...args) {
|
|
78
|
+
if (!instance) {
|
|
79
|
+
instance = await pendingInstance;
|
|
80
|
+
}
|
|
81
|
+
const originalMethod = instance[key];
|
|
82
|
+
if (import_plugin_utils.lodash.isFunction(originalMethod)) {
|
|
83
|
+
const p = originalMethod.apply(instance, args);
|
|
84
|
+
if (key === opts.destroyMethod) {
|
|
85
|
+
return p.then(async () => {
|
|
86
|
+
await worker.terminate();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return p;
|
|
90
|
+
}
|
|
91
|
+
return originalMethod;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function createApiParser(options) {
|
|
99
|
+
const { filename, worker, parseOptions } = options;
|
|
100
|
+
const ParserClass = createRemoteClass(filename, worker);
|
|
101
|
+
return (...args) => new import_BaseParser.BaseAtomAssetsParser({
|
|
102
|
+
...args == null ? void 0 : args[0],
|
|
103
|
+
parser: new ParserClass(...args),
|
|
104
|
+
...parseOptions
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
+
0 && (module.exports = {
|
|
109
|
+
createApiParser,
|
|
110
|
+
createExposedClass,
|
|
111
|
+
createRemoteClass
|
|
112
|
+
});
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { useDemo, useLiveDemo, useParams } from 'dumi';
|
|
2
2
|
import { createElement, useEffect } from 'react';
|
|
3
|
+
import { useRenderer } from "../../theme-api/useRenderer";
|
|
3
4
|
import "./index.less";
|
|
4
5
|
var DemoRenderPage = function DemoRenderPage() {
|
|
5
6
|
var _useParams = useParams(),
|
|
6
7
|
id = _useParams.id;
|
|
7
|
-
var
|
|
8
|
-
|
|
8
|
+
var demo = useDemo(id);
|
|
9
|
+
var canvasRef = useRenderer(demo);
|
|
10
|
+
var _ref = demo || {},
|
|
11
|
+
component = _ref.component,
|
|
12
|
+
renderOpts = _ref.renderOpts;
|
|
9
13
|
var _useLiveDemo = useLiveDemo(id),
|
|
10
14
|
liveDemoNode = _useLiveDemo.node,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var finalNode = liveDemoNode ||
|
|
15
|
+
setSource = _useLiveDemo.setSource,
|
|
16
|
+
liveDemoError = _useLiveDemo.error;
|
|
17
|
+
var finalNode = liveDemoNode || (renderOpts !== null && renderOpts !== void 0 && renderOpts.renderer ? /*#__PURE__*/createElement('div', {
|
|
18
|
+
ref: canvasRef
|
|
19
|
+
}) : component && /*#__PURE__*/createElement(component));
|
|
14
20
|
|
|
15
21
|
// listen message event for setSource
|
|
16
22
|
useEffect(function () {
|
|
@@ -3,19 +3,27 @@ import { SP_ROUTE_PREFIX } from "../../../constants";
|
|
|
3
3
|
import { useAppData, useDemo, useSiteData } from 'dumi';
|
|
4
4
|
import React, { createElement } from 'react';
|
|
5
5
|
import Previewer from 'dumi/theme/builtins/Previewer';
|
|
6
|
+
import { useRenderer } from "../useRenderer";
|
|
6
7
|
import DemoErrorBoundary from "./DemoErrorBoundary";
|
|
7
8
|
var InternalDumiDemo = function InternalDumiDemo(props) {
|
|
8
9
|
var _useSiteData = useSiteData(),
|
|
9
10
|
historyType = _useSiteData.historyType;
|
|
10
11
|
var _useAppData = useAppData(),
|
|
11
12
|
basename = _useAppData.basename;
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
var id = props.demo.id;
|
|
14
|
+
var demo = useDemo(id);
|
|
15
|
+
var component = demo.component,
|
|
16
|
+
asset = demo.asset,
|
|
17
|
+
renderOpts = demo.renderOpts;
|
|
18
|
+
var canvasRef = useRenderer(Object.assign(demo, {
|
|
19
|
+
id: id
|
|
20
|
+
}));
|
|
15
21
|
|
|
16
22
|
// hide debug demo in production
|
|
17
23
|
if (process.env.NODE_ENV === 'production' && props.previewerProps.debug) return null;
|
|
18
|
-
var demoNode = /*#__PURE__*/React.createElement(DemoErrorBoundary, null, /*#__PURE__*/createElement(
|
|
24
|
+
var demoNode = /*#__PURE__*/React.createElement(DemoErrorBoundary, null, renderOpts !== null && renderOpts !== void 0 && renderOpts.renderer ? /*#__PURE__*/React.createElement("div", {
|
|
25
|
+
ref: canvasRef
|
|
26
|
+
}) : /*#__PURE__*/createElement(component));
|
|
19
27
|
if (props.demo.inline) {
|
|
20
28
|
return demoNode;
|
|
21
29
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import type { PICKED_PKG_FIELDS } from "../../constants";
|
|
3
3
|
import type { AtomComponentAsset } from 'dumi-assets-types';
|
|
4
4
|
import type { IDemoData, ILocalesConfig, IThemeConfig } from './types';
|
|
5
|
-
interface ISiteContext {
|
|
5
|
+
export interface ISiteContext {
|
|
6
6
|
pkg: Partial<Record<keyof typeof PICKED_PKG_FIELDS, any>>;
|
|
7
7
|
historyType: 'browser' | 'hash' | 'memory';
|
|
8
8
|
entryExports: Record<string, any>;
|
|
@@ -20,4 +20,3 @@ interface ISiteContext {
|
|
|
20
20
|
}
|
|
21
21
|
export declare const SiteContext: import("react").Context<ISiteContext>;
|
|
22
22
|
export declare const useSiteData: () => ISiteContext;
|
|
23
|
-
export {};
|
|
@@ -6,7 +6,7 @@ export { DumiPage } from './DumiPage';
|
|
|
6
6
|
export { useSiteData } from './context';
|
|
7
7
|
export { openCodeSandbox } from './openCodeSandbox';
|
|
8
8
|
export { openStackBlitz } from './openStackBlitz';
|
|
9
|
-
export type { IPreviewerProps } from './types';
|
|
9
|
+
export type { IDemoCancelableFn, IPreviewerProps } from './types';
|
|
10
10
|
export { useAtomAssets } from './useAtomAssets';
|
|
11
11
|
export { useLiveDemo } from './useLiveDemo';
|
|
12
12
|
export { useLocale } from './useLocale';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExampleBlockAsset } from 'dumi-assets-types';
|
|
2
|
-
import type { ComponentType, ReactNode } from 'react';
|
|
2
|
+
import type { ComponentType as ReactComponentType, ReactNode } from 'react';
|
|
3
3
|
export interface IPreviewerProps {
|
|
4
4
|
/**
|
|
5
5
|
* title of current demo
|
|
@@ -125,9 +125,9 @@ export interface IRouteMeta {
|
|
|
125
125
|
title?: string;
|
|
126
126
|
titleIntlId?: string;
|
|
127
127
|
components: {
|
|
128
|
-
default:
|
|
129
|
-
Extra:
|
|
130
|
-
Action:
|
|
128
|
+
default: ReactComponentType;
|
|
129
|
+
Extra: ReactComponentType;
|
|
130
|
+
Action: ReactComponentType;
|
|
131
131
|
};
|
|
132
132
|
meta: {
|
|
133
133
|
frontmatter: Omit<IRouteMeta['frontmatter'], 'description' | 'keywords' | 'nav' | 'group' | 'hero' | 'features'>;
|
|
@@ -219,11 +219,17 @@ export type IRoutesById = Record<string, {
|
|
|
219
219
|
redirect?: string;
|
|
220
220
|
[key: string]: any;
|
|
221
221
|
}>;
|
|
222
|
+
export type AgnosticComponentModule = {
|
|
223
|
+
default?: any;
|
|
224
|
+
[key: string]: any;
|
|
225
|
+
};
|
|
226
|
+
export type AgnosticComponentType = Promise<AgnosticComponentModule> | AgnosticComponentModule;
|
|
222
227
|
export type IDemoCompileFn = (code: string, opts: {
|
|
223
228
|
filename: string;
|
|
224
229
|
}) => Promise<string>;
|
|
230
|
+
export type IDemoCancelableFn = (canvas: HTMLElement, component: AgnosticComponentModule) => (() => void) | Promise<() => void>;
|
|
225
231
|
export type IDemoData = {
|
|
226
|
-
component:
|
|
232
|
+
component: ReactComponentType | AgnosticComponentType;
|
|
227
233
|
asset: IPreviewerProps['asset'];
|
|
228
234
|
routeId: string;
|
|
229
235
|
context?: Record<string, unknown>;
|
|
@@ -232,6 +238,7 @@ export type IDemoData = {
|
|
|
232
238
|
* provide a runtime compile function for compile demo code for live preview
|
|
233
239
|
*/
|
|
234
240
|
compile?: IDemoCompileFn;
|
|
241
|
+
renderer?: IDemoCancelableFn;
|
|
235
242
|
};
|
|
236
243
|
};
|
|
237
244
|
export {};
|
|
@@ -2,6 +2,11 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
|
|
|
2
2
|
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
3
3
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
4
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
6
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
5
10
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
11
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
7
12
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
@@ -12,33 +17,56 @@ import { useDemo } from 'dumi';
|
|
|
12
17
|
import throttle from 'lodash.throttle';
|
|
13
18
|
import { createElement, useCallback, useRef, useState } from 'react';
|
|
14
19
|
import DemoErrorBoundary from "./DumiDemo/DemoErrorBoundary";
|
|
20
|
+
import { useRenderer } from "./useRenderer";
|
|
15
21
|
var THROTTLE_WAIT = 500;
|
|
22
|
+
function evalCommonJS(js, _ref) {
|
|
23
|
+
var module = _ref.module,
|
|
24
|
+
exports = _ref.exports,
|
|
25
|
+
require = _ref.require;
|
|
26
|
+
new Function('module', 'exports', 'require', js)(module, exports, require);
|
|
27
|
+
}
|
|
16
28
|
export var useLiveDemo = function useLiveDemo(id, opts) {
|
|
17
|
-
var
|
|
18
|
-
context = _ref.context,
|
|
19
|
-
asset = _ref.asset,
|
|
20
|
-
renderOpts = _ref.renderOpts;
|
|
29
|
+
var demo = useDemo(id);
|
|
21
30
|
var _useState = useState(false),
|
|
22
31
|
_useState2 = _slicedToArray(_useState, 2),
|
|
23
32
|
loading = _useState2[0],
|
|
24
33
|
setLoading = _useState2[1];
|
|
25
34
|
var loadingTimer = useRef();
|
|
26
35
|
var taskToken = useRef();
|
|
36
|
+
var _demo$context = demo.context,
|
|
37
|
+
context = _demo$context === void 0 ? {} : _demo$context,
|
|
38
|
+
asset = demo.asset,
|
|
39
|
+
renderOpts = demo.renderOpts;
|
|
27
40
|
var _useState3 = useState(),
|
|
28
41
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var
|
|
42
|
+
component = _useState4[0],
|
|
43
|
+
setComponent = _useState4[1];
|
|
44
|
+
var ref = useRenderer(component ? _objectSpread(_objectSpread({
|
|
45
|
+
id: id
|
|
46
|
+
}, demo), {}, {
|
|
47
|
+
component: component
|
|
48
|
+
}) : Object.assign(demo, {
|
|
49
|
+
id: id
|
|
50
|
+
}));
|
|
51
|
+
var _useState5 = useState(),
|
|
32
52
|
_useState6 = _slicedToArray(_useState5, 2),
|
|
33
|
-
|
|
34
|
-
|
|
53
|
+
demoNode = _useState6[0],
|
|
54
|
+
setDemoNode = _useState6[1];
|
|
55
|
+
var _useState7 = useState(null),
|
|
56
|
+
_useState8 = _slicedToArray(_useState7, 2),
|
|
57
|
+
error = _useState8[0],
|
|
58
|
+
setError = _useState8[1];
|
|
35
59
|
var setSource = useCallback(throttle( /*#__PURE__*/function () {
|
|
36
60
|
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(source) {
|
|
37
61
|
var _opts$containerRef;
|
|
38
|
-
var iframeWindow, entryFileName, require, exports, module,
|
|
62
|
+
var resetLoadingStatus, iframeWindow, entryFileName, require, token, entryFileCode, exports, module, renderToStaticMarkupDeferred, _exports, _module, newDemoNode, oError;
|
|
39
63
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
40
64
|
while (1) switch (_context.prev = _context.next) {
|
|
41
65
|
case 0:
|
|
66
|
+
resetLoadingStatus = function _resetLoadingStatus() {
|
|
67
|
+
clearTimeout(loadingTimer.current);
|
|
68
|
+
setLoading(false);
|
|
69
|
+
};
|
|
42
70
|
// set loading status if still compiling after 499ms
|
|
43
71
|
loadingTimer.current = window.setTimeout(function () {
|
|
44
72
|
setLoading(true);
|
|
@@ -46,11 +74,11 @@ export var useLiveDemo = function useLiveDemo(id, opts) {
|
|
|
46
74
|
// make sure timer be fired before next throttle
|
|
47
75
|
THROTTLE_WAIT - 1);
|
|
48
76
|
if (!(opts !== null && opts !== void 0 && opts.iframe && opts !== null && opts !== void 0 && (_opts$containerRef = opts.containerRef) !== null && _opts$containerRef !== void 0 && _opts$containerRef.current)) {
|
|
49
|
-
_context.next =
|
|
77
|
+
_context.next = 8;
|
|
50
78
|
break;
|
|
51
79
|
}
|
|
52
80
|
iframeWindow = opts.containerRef.current.querySelector('iframe').contentWindow;
|
|
53
|
-
_context.next =
|
|
81
|
+
_context.next = 6;
|
|
54
82
|
return new Promise(function (resolve) {
|
|
55
83
|
var handler = function handler(ev) {
|
|
56
84
|
if (ev.data.type.startsWith('dumi.liveDemo.compileDone')) {
|
|
@@ -65,10 +93,10 @@ export var useLiveDemo = function useLiveDemo(id, opts) {
|
|
|
65
93
|
value: source
|
|
66
94
|
});
|
|
67
95
|
});
|
|
68
|
-
case
|
|
69
|
-
_context.next =
|
|
96
|
+
case 6:
|
|
97
|
+
_context.next = 50;
|
|
70
98
|
break;
|
|
71
|
-
case
|
|
99
|
+
case 8:
|
|
72
100
|
entryFileName = Object.keys(asset.dependencies).find(function (k) {
|
|
73
101
|
return asset.dependencies[k].type === 'FILE';
|
|
74
102
|
});
|
|
@@ -76,33 +104,75 @@ export var useLiveDemo = function useLiveDemo(id, opts) {
|
|
|
76
104
|
if (v in context) return context[v];
|
|
77
105
|
throw new Error("Cannot find module: ".concat(v));
|
|
78
106
|
};
|
|
79
|
-
exports = {};
|
|
80
|
-
module = {
|
|
81
|
-
exports: exports
|
|
82
|
-
};
|
|
83
107
|
token = taskToken.current = Math.random();
|
|
84
108
|
entryFileCode = source[entryFileName];
|
|
109
|
+
if (!(renderOpts !== null && renderOpts !== void 0 && renderOpts.compile)) {
|
|
110
|
+
_context.next = 24;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
85
113
|
_context.prev = 13;
|
|
86
|
-
|
|
87
|
-
renderToStaticMarkupDeferred = import('react-dom/server').then(function (_ref3) {
|
|
88
|
-
var renderToStaticMarkup = _ref3.renderToStaticMarkup;
|
|
89
|
-
return renderToStaticMarkup;
|
|
90
|
-
}); // compile entry file code
|
|
91
|
-
_context.next = 17;
|
|
114
|
+
_context.next = 16;
|
|
92
115
|
return renderOpts.compile(entryFileCode, {
|
|
93
116
|
filename: entryFileName
|
|
94
117
|
});
|
|
95
|
-
case
|
|
118
|
+
case 16:
|
|
96
119
|
entryFileCode = _context.sent;
|
|
120
|
+
_context.next = 24;
|
|
121
|
+
break;
|
|
122
|
+
case 19:
|
|
123
|
+
_context.prev = 19;
|
|
124
|
+
_context.t0 = _context["catch"](13);
|
|
125
|
+
setError(_context.t0);
|
|
126
|
+
resetLoadingStatus();
|
|
127
|
+
return _context.abrupt("return");
|
|
128
|
+
case 24:
|
|
129
|
+
if (!(renderOpts !== null && renderOpts !== void 0 && renderOpts.renderer && renderOpts !== null && renderOpts !== void 0 && renderOpts.compile)) {
|
|
130
|
+
_context.next = 28;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
exports = {};
|
|
135
|
+
module = {
|
|
136
|
+
exports: exports
|
|
137
|
+
};
|
|
138
|
+
evalCommonJS(entryFileCode, {
|
|
139
|
+
exports: exports,
|
|
140
|
+
module: module,
|
|
141
|
+
require: require
|
|
142
|
+
});
|
|
143
|
+
setComponent(exports);
|
|
144
|
+
setDemoNode( /*#__PURE__*/createElement('div', {
|
|
145
|
+
ref: ref
|
|
146
|
+
}));
|
|
147
|
+
setError(null);
|
|
148
|
+
} catch (err) {
|
|
149
|
+
setError(err);
|
|
150
|
+
}
|
|
151
|
+
resetLoadingStatus();
|
|
152
|
+
return _context.abrupt("return");
|
|
153
|
+
case 28:
|
|
154
|
+
_context.prev = 28;
|
|
155
|
+
// load renderToStaticMarkup in async way
|
|
156
|
+
renderToStaticMarkupDeferred = import('react-dom/server').then(function (_ref3) {
|
|
157
|
+
var renderToStaticMarkup = _ref3.renderToStaticMarkup;
|
|
158
|
+
return renderToStaticMarkup;
|
|
159
|
+
}); // skip current task if another task is running
|
|
97
160
|
if (!(token !== taskToken.current)) {
|
|
98
|
-
_context.next =
|
|
161
|
+
_context.next = 32;
|
|
99
162
|
break;
|
|
100
163
|
}
|
|
101
164
|
return _context.abrupt("return");
|
|
102
|
-
case
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
165
|
+
case 32:
|
|
166
|
+
_exports = {};
|
|
167
|
+
_module = {
|
|
168
|
+
exports: _exports
|
|
169
|
+
}; // initial component with fake runtime
|
|
170
|
+
evalCommonJS(entryFileCode, {
|
|
171
|
+
exports: _exports,
|
|
172
|
+
module: _module,
|
|
173
|
+
require: require
|
|
174
|
+
});
|
|
175
|
+
newDemoNode = /*#__PURE__*/createElement(DemoErrorBoundary, null, /*#__PURE__*/createElement(_exports.default));
|
|
106
176
|
oError = console.error; // hijack console.error to avoid useLayoutEffect error
|
|
107
177
|
console.error = function () {
|
|
108
178
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
@@ -112,31 +182,29 @@ export var useLiveDemo = function useLiveDemo(id, opts) {
|
|
|
112
182
|
};
|
|
113
183
|
|
|
114
184
|
// check component is able to render, to avoid show react overlay error
|
|
115
|
-
_context.next =
|
|
185
|
+
_context.next = 40;
|
|
116
186
|
return renderToStaticMarkupDeferred;
|
|
117
|
-
case
|
|
118
|
-
_context.
|
|
119
|
-
(0, _context.
|
|
187
|
+
case 40:
|
|
188
|
+
_context.t1 = _context.sent;
|
|
189
|
+
(0, _context.t1)(newDemoNode);
|
|
120
190
|
console.error = oError;
|
|
121
191
|
|
|
122
192
|
// set new demo node with passing source
|
|
123
193
|
setDemoNode(newDemoNode);
|
|
124
194
|
setError(null);
|
|
125
|
-
_context.next =
|
|
195
|
+
_context.next = 50;
|
|
126
196
|
break;
|
|
127
|
-
case
|
|
128
|
-
_context.prev =
|
|
129
|
-
_context.
|
|
130
|
-
setError(_context.
|
|
131
|
-
case
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
setLoading(false);
|
|
135
|
-
case 38:
|
|
197
|
+
case 47:
|
|
198
|
+
_context.prev = 47;
|
|
199
|
+
_context.t2 = _context["catch"](28);
|
|
200
|
+
setError(_context.t2);
|
|
201
|
+
case 50:
|
|
202
|
+
resetLoadingStatus();
|
|
203
|
+
case 51:
|
|
136
204
|
case "end":
|
|
137
205
|
return _context.stop();
|
|
138
206
|
}
|
|
139
|
-
}, _callee, null, [[13,
|
|
207
|
+
}, _callee, null, [[13, 19], [28, 47]]);
|
|
140
208
|
}));
|
|
141
209
|
return function (_x2) {
|
|
142
210
|
return _ref2.apply(this, arguments);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
import { useEffect, useRef } from 'react';
|
|
6
|
+
// maintain all the mounted instance
|
|
7
|
+
var map = new Map();
|
|
8
|
+
export var useRenderer = function useRenderer(_ref) {
|
|
9
|
+
var id = _ref.id,
|
|
10
|
+
component = _ref.component,
|
|
11
|
+
renderOpts = _ref.renderOpts;
|
|
12
|
+
var canvasRef = useRef(null);
|
|
13
|
+
var teardownRef = useRef(function () {});
|
|
14
|
+
var prevComponent = useRef(component);
|
|
15
|
+
|
|
16
|
+
// forcibly destroyed
|
|
17
|
+
if (prevComponent.current !== component) {
|
|
18
|
+
var teardown = map.get(id);
|
|
19
|
+
teardown === null || teardown === void 0 ? void 0 : teardown();
|
|
20
|
+
prevComponent.current = component;
|
|
21
|
+
}
|
|
22
|
+
var renderer = renderOpts === null || renderOpts === void 0 ? void 0 : renderOpts.renderer;
|
|
23
|
+
useEffect(function () {
|
|
24
|
+
function resolveRender() {
|
|
25
|
+
return _resolveRender.apply(this, arguments);
|
|
26
|
+
}
|
|
27
|
+
function _resolveRender() {
|
|
28
|
+
_resolveRender = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
29
|
+
var _module$default;
|
|
30
|
+
var module, teardown;
|
|
31
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
32
|
+
while (1) switch (_context.prev = _context.next) {
|
|
33
|
+
case 0:
|
|
34
|
+
if (!(!canvasRef.current || !renderer || !component)) {
|
|
35
|
+
_context.next = 2;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
return _context.abrupt("return");
|
|
39
|
+
case 2:
|
|
40
|
+
if (!map.get(id)) {
|
|
41
|
+
_context.next = 4;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
return _context.abrupt("return");
|
|
45
|
+
case 4:
|
|
46
|
+
map.set(id, function () {});
|
|
47
|
+
if (!(component instanceof Promise)) {
|
|
48
|
+
_context.next = 11;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
_context.next = 8;
|
|
52
|
+
return component;
|
|
53
|
+
case 8:
|
|
54
|
+
_context.t0 = _context.sent;
|
|
55
|
+
_context.next = 12;
|
|
56
|
+
break;
|
|
57
|
+
case 11:
|
|
58
|
+
_context.t0 = component;
|
|
59
|
+
case 12:
|
|
60
|
+
module = _context.t0;
|
|
61
|
+
module = (_module$default = module.default) !== null && _module$default !== void 0 ? _module$default : module;
|
|
62
|
+
_context.next = 16;
|
|
63
|
+
return renderer(canvasRef.current, module);
|
|
64
|
+
case 16:
|
|
65
|
+
teardown = _context.sent;
|
|
66
|
+
// remove instance when react component is unmounted
|
|
67
|
+
teardownRef.current = function () {
|
|
68
|
+
teardown();
|
|
69
|
+
map.delete(id);
|
|
70
|
+
};
|
|
71
|
+
map.set(id, teardownRef.current);
|
|
72
|
+
case 19:
|
|
73
|
+
case "end":
|
|
74
|
+
return _context.stop();
|
|
75
|
+
}
|
|
76
|
+
}, _callee);
|
|
77
|
+
}));
|
|
78
|
+
return _resolveRender.apply(this, arguments);
|
|
79
|
+
}
|
|
80
|
+
resolveRender();
|
|
81
|
+
}, [canvasRef.current, component, renderer]);
|
|
82
|
+
useEffect(function () {
|
|
83
|
+
return function () {
|
|
84
|
+
return teardownRef.current();
|
|
85
|
+
};
|
|
86
|
+
}, []);
|
|
87
|
+
return canvasRef;
|
|
88
|
+
};
|
package/dist/constants.d.ts
CHANGED
|
@@ -17,4 +17,6 @@ export declare const PICKED_PKG_FIELDS: {
|
|
|
17
17
|
export declare const USELESS_TMP_FILES: string[];
|
|
18
18
|
export declare const VERSION_2_LEVEL_NAV = "^2.2.0";
|
|
19
19
|
export declare const VERSION_2_DEPRECATE_SOFT_BREAKS = "^2.2.0";
|
|
20
|
+
export declare const DEFAULT_DEMO_MODULE_EXTENSIONS: string[];
|
|
21
|
+
export declare const DEFAULT_DEMO_PLAIN_TEXT_EXTENSIONS: string[];
|
|
20
22
|
export declare const FS_CACHE_DIR = "node_modules/.cache/dumi";
|