@rspress/plugin-playground 1.40.2 → 1.40.3-cannary-20250126

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.
@@ -1,385 +1,319 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
- var __publicField = (obj, key, value) => {
31
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
32
- return value;
33
- };
34
-
35
- // src/web/index.ts
36
- var web_exports = {};
37
- __export(web_exports, {
38
- Editor: () => Editor,
39
- MonacoEditor: () => import_react4.default,
40
- MonacoEditorLoader: () => import_react4.loader,
41
- Runner: () => Runner
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = function(module) {
5
+ var getter = module && module.__esModule ? function() {
6
+ return module['default'];
7
+ } : function() {
8
+ return module;
9
+ };
10
+ __webpack_require__.d(getter, {
11
+ a: getter
12
+ });
13
+ return getter;
14
+ };
15
+ })();
16
+ (()=>{
17
+ __webpack_require__.d = function(exports1, definition) {
18
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
19
+ enumerable: true,
20
+ get: definition[key]
21
+ });
22
+ };
23
+ })();
24
+ (()=>{
25
+ __webpack_require__.o = function(obj, prop) {
26
+ return Object.prototype.hasOwnProperty.call(obj, prop);
27
+ };
28
+ })();
29
+ (()=>{
30
+ __webpack_require__.r = function(exports1) {
31
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
32
+ value: 'Module'
33
+ });
34
+ Object.defineProperty(exports1, '__esModule', {
35
+ value: true
36
+ });
37
+ };
38
+ })();
39
+ var __webpack_exports__ = {};
40
+ __webpack_require__.r(__webpack_exports__);
41
+ __webpack_require__.d(__webpack_exports__, {
42
+ Editor: ()=>Editor,
43
+ MonacoEditor: ()=>react_default(),
44
+ MonacoEditorLoader: ()=>react_namespaceObject.loader,
45
+ Runner: ()=>Runner
42
46
  });
43
- module.exports = __toCommonJS(web_exports);
44
- var import_react4 = __toESM(require("@monaco-editor/react"));
45
-
46
- // src/web/editor.tsx
47
- var import_react = __toESM(require("@monaco-editor/react"));
48
- var import_runtime = require("@rspress/core/runtime");
49
- var import_react2 = require("react");
50
-
51
- // src/web/constant.ts
52
- var DEFAULT_MONACO_URL = "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.43.0/min/vs";
53
-
54
- // src/web/editor.tsx
55
- var import_jsx_runtime = require("react/jsx-runtime");
47
+ const react_namespaceObject = require("@monaco-editor/react");
48
+ var react_default = /*#__PURE__*/ __webpack_require__.n(react_namespaceObject);
49
+ const runtime_namespaceObject = require("@rspress/core/runtime");
50
+ const external_react_namespaceObject = require("react");
51
+ var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
52
+ const DEFAULT_MONACO_URL = 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.43.0/min/vs';
56
53
  function initLoader() {
57
- let loaderConfig = {
58
- paths: {
59
- vs: DEFAULT_MONACO_URL
60
- }
61
- };
62
- try {
63
- const keys = Object.keys(__PLAYGROUND_MONACO_LOADER__);
64
- if (keys.length > 0) {
65
- loaderConfig = __PLAYGROUND_MONACO_LOADER__;
66
- }
67
- } catch (e) {
68
- }
69
- import_react.loader.config(loaderConfig);
54
+ let loaderConfig = {
55
+ paths: {
56
+ vs: DEFAULT_MONACO_URL
57
+ }
58
+ };
59
+ try {
60
+ const keys = Object.keys(__PLAYGROUND_MONACO_LOADER__);
61
+ if (keys.length > 0) loaderConfig = __PLAYGROUND_MONACO_LOADER__;
62
+ } catch (e) {}
63
+ react_namespaceObject.loader.config(loaderConfig);
70
64
  }
71
65
  initLoader();
72
66
  function getMonacoOptions() {
73
- try {
74
- return __PLAYGROUND_MONACO_OPTIONS__;
75
- } catch (e) {
76
- }
77
- return {};
67
+ try {
68
+ return __PLAYGROUND_MONACO_OPTIONS__;
69
+ } catch (e) {}
70
+ return {};
78
71
  }
79
72
  function Editor(props) {
80
- const { options, className = "", theme: themeProp, ...rest } = props || {};
81
- const dark = (0, import_runtime.useDark)();
82
- const theme = (0, import_react2.useMemo)(() => {
83
- if (themeProp) {
84
- return themeProp;
85
- }
86
- return dark ? "vs-dark" : "light";
87
- }, [themeProp, dark]);
88
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: `rspress-playground-editor ${className}`, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
89
- import_react.default,
90
- {
91
- ...rest,
92
- theme,
93
- options: {
94
- minimap: {
95
- enabled: true,
96
- autohide: true
97
- },
98
- fontSize: 14,
99
- lineNumbersMinChars: 7,
100
- scrollBeyondLastLine: false,
101
- automaticLayout: true,
102
- wordBasedSuggestions: true,
103
- quickSuggestions: true,
104
- scrollbar: {
105
- verticalScrollbarSize: 8,
106
- horizontalScrollbarSize: 8
107
- },
108
- scrollPredominantAxis: false,
109
- ...getMonacoOptions(),
110
- ...options
111
- }
112
- }
113
- ) });
73
+ const { options, className = '', theme: themeProp, ...rest } = props || {};
74
+ const dark = (0, runtime_namespaceObject.useDark)();
75
+ const theme = (0, external_react_namespaceObject.useMemo)(()=>{
76
+ if (themeProp) return themeProp;
77
+ return dark ? 'vs-dark' : 'light';
78
+ }, [
79
+ themeProp,
80
+ dark
81
+ ]);
82
+ return /*#__PURE__*/ external_react_default().createElement("div", {
83
+ className: `rspress-playground-editor ${className}`
84
+ }, /*#__PURE__*/ external_react_default().createElement(react_default(), {
85
+ ...rest,
86
+ theme: theme,
87
+ options: {
88
+ minimap: {
89
+ enabled: true,
90
+ autohide: true
91
+ },
92
+ fontSize: 14,
93
+ lineNumbersMinChars: 7,
94
+ scrollBeyondLastLine: false,
95
+ automaticLayout: true,
96
+ wordBasedSuggestions: true,
97
+ quickSuggestions: true,
98
+ scrollbar: {
99
+ verticalScrollbarSize: 8,
100
+ horizontalScrollbarSize: 8
101
+ },
102
+ scrollPredominantAxis: false,
103
+ ...getMonacoOptions(),
104
+ ...options
105
+ }
106
+ }));
114
107
  }
115
-
116
- // src/web/runner.tsx
117
- var import_react3 = __toESM(require("react"));
118
-
119
- // src/web/ast.ts
120
108
  function createVariableDeclaration(id, init) {
121
- return {
122
- type: "VariableDeclaration",
123
- declarations: [
124
- {
125
- type: "VariableDeclarator",
126
- id: typeof id === "string" ? {
127
- type: "Identifier",
128
- name: id
129
- } : id,
130
- init
131
- }
132
- ],
133
- kind: "const"
134
- };
109
+ return {
110
+ type: 'VariableDeclaration',
111
+ declarations: [
112
+ {
113
+ type: 'VariableDeclarator',
114
+ id: 'string' == typeof id ? {
115
+ type: 'Identifier',
116
+ name: id
117
+ } : id,
118
+ init
119
+ }
120
+ ],
121
+ kind: 'const'
122
+ };
135
123
  }
136
124
  function createIdentifier(name) {
137
- return {
138
- type: "Identifier",
139
- name
140
- };
125
+ return {
126
+ type: 'Identifier',
127
+ name
128
+ };
141
129
  }
142
130
  function createObjectProperty(key, value) {
143
- return {
144
- type: "ObjectProperty",
145
- key: createIdentifier(key),
146
- computed: false,
147
- shorthand: key === value,
148
- value: createIdentifier(value)
149
- };
131
+ return {
132
+ type: 'ObjectProperty',
133
+ key: createIdentifier(key),
134
+ computed: false,
135
+ shorthand: key === value,
136
+ value: createIdentifier(value)
137
+ };
150
138
  }
151
139
  function createObjectPattern(names) {
152
- return {
153
- type: "ObjectPattern",
154
- properties: names.map(
155
- (name) => Array.isArray(name) ? createObjectProperty(name[0], name[1]) : createObjectProperty(name, name)
156
- )
157
- };
140
+ return {
141
+ type: 'ObjectPattern',
142
+ properties: names.map((name)=>Array.isArray(name) ? createObjectProperty(name[0], name[1]) : createObjectProperty(name, name))
143
+ };
158
144
  }
159
145
  function createGetImport(name, getDefault) {
160
- return {
161
- type: "CallExpression",
162
- callee: {
163
- type: "Identifier",
164
- name: "__get_import"
165
- },
166
- arguments: [
167
- {
168
- type: "StringLiteral",
169
- value: name
170
- },
171
- {
172
- type: "BooleanLiteral",
173
- value: Boolean(getDefault)
174
- }
175
- ]
176
- };
146
+ return {
147
+ type: 'CallExpression',
148
+ callee: {
149
+ type: 'Identifier',
150
+ name: '__get_import'
151
+ },
152
+ arguments: [
153
+ {
154
+ type: 'StringLiteral',
155
+ value: name
156
+ },
157
+ {
158
+ type: 'BooleanLiteral',
159
+ value: Boolean(getDefault)
160
+ }
161
+ ]
162
+ };
177
163
  }
178
-
179
- // src/web/babel.ts
180
164
  async function loadUmdBabelModule() {
181
- const data = await fetch(__PLAYGROUND_BABEL_URL__);
182
- const umdSourceCode = await data.text();
183
- const run = new Function(
184
- "exports",
185
- "module",
186
- "require",
187
- `with(exports, module, require) {${umdSourceCode}}`
188
- );
189
- const exports = {};
190
- const module2 = { exports };
191
- const require2 = () => {
192
- };
193
- run(exports, module2, require2);
194
- return exports;
165
+ const data = await fetch(__PLAYGROUND_BABEL_URL__);
166
+ const umdSourceCode = await data.text();
167
+ const run = new Function('exports', 'module', 'require', `with(exports, module, require) {${umdSourceCode}}`);
168
+ const exports1 = {};
169
+ const module = {
170
+ exports: exports1
171
+ };
172
+ const require1 = ()=>{};
173
+ run(exports1, module, require1);
174
+ return exports1;
195
175
  }
196
- var loadBabelPromise = null;
176
+ let loadBabelPromise = null;
197
177
  async function getBabel() {
198
- if (window.Babel) {
199
- return window.Babel;
200
- }
201
- if (loadBabelPromise) {
202
- return loadBabelPromise;
203
- }
204
- loadBabelPromise = loadUmdBabelModule();
205
- try {
206
- const Babel = await loadBabelPromise;
207
- window.Babel = Babel;
208
- return Babel;
209
- } catch (e) {
210
- loadBabelPromise = null;
211
- throw e;
212
- }
178
+ if (window.Babel) return window.Babel;
179
+ if (loadBabelPromise) return loadBabelPromise;
180
+ loadBabelPromise = loadUmdBabelModule();
181
+ try {
182
+ const Babel = await loadBabelPromise;
183
+ window.Babel = Babel;
184
+ return Babel;
185
+ } catch (e) {
186
+ loadBabelPromise = null;
187
+ throw e;
188
+ }
213
189
  }
214
-
215
- // src/web/runner.tsx
216
- var import_jsx_runtime2 = require("react/jsx-runtime");
217
- var DEBOUNCE_TIME = 800;
218
- var Runner = class extends import_react3.Component {
219
- constructor(props) {
220
- super(props);
221
- __publicField(this, "timer");
222
- this.state = {
223
- error: void 0,
224
- comp: null
225
- };
226
- this.doCompile = this.doCompile.bind(this);
227
- this.waitCompile = this.waitCompile.bind(this);
228
- }
229
- static getDerivedStateFromError(error) {
230
- return {
231
- error,
232
- comp: null
233
- };
234
- }
235
- waitCompile(targetCode) {
236
- if (this.timer) {
237
- clearTimeout(this.timer);
190
+ const DEBOUNCE_TIME = 800;
191
+ class Runner extends external_react_namespaceObject.Component {
192
+ static getDerivedStateFromError(error) {
193
+ return {
194
+ error,
195
+ comp: null
196
+ };
238
197
  }
239
- this.timer = setTimeout(() => {
240
- this.timer = null;
241
- this.doCompile(targetCode);
242
- }, DEBOUNCE_TIME);
243
- }
244
- async doCompile(targetCode) {
245
- const { language, getImport } = this.props;
246
- const babel = await getBabel();
247
- try {
248
- const presets = [
249
- [babel.availablePresets.react],
250
- [babel.availablePresets.env, { modules: "commonjs" }]
251
- ];
252
- if (language === "tsx" || language === "ts") {
253
- presets.unshift([
254
- babel.availablePresets.typescript,
255
- {
256
- allExtensions: true,
257
- isTSX: language === "tsx"
258
- }
259
- ]);
260
- }
261
- const result = babel.transform(targetCode, {
262
- sourceType: "module",
263
- sourceMaps: "inline",
264
- presets,
265
- plugins: [
266
- {
267
- pre() {
268
- this.hasReactImported = false;
269
- },
270
- visitor: {
271
- ImportDeclaration(path) {
272
- const pkg = path.node.source.value;
273
- const code = [];
274
- const specifiers = [];
275
- for (const specifier of path.node.specifiers) {
276
- if (specifier.local.name === "React") {
277
- this.hasReactImported = true;
278
- }
279
- if (specifier.type === "ImportDefaultSpecifier") {
280
- code.push(
281
- createVariableDeclaration(
282
- specifier.local.name,
283
- createGetImport(pkg, true)
284
- )
285
- );
286
- }
287
- if (specifier.type === "ImportNamespaceSpecifier") {
288
- code.push(
289
- createVariableDeclaration(
290
- specifier.local.name,
291
- createGetImport(pkg)
292
- )
293
- );
294
- }
295
- if (specifier.type === "ImportSpecifier") {
296
- if ("name" in specifier.imported && specifier.imported.name !== specifier.local.name) {
297
- specifiers.push([
298
- specifier.imported.name,
299
- specifier.local.name
300
- ]);
301
- } else {
302
- specifiers.push(specifier.local.name);
198
+ waitCompile(targetCode) {
199
+ if (this.timer) clearTimeout(this.timer);
200
+ this.timer = setTimeout(()=>{
201
+ this.timer = null;
202
+ this.doCompile(targetCode);
203
+ }, DEBOUNCE_TIME);
204
+ }
205
+ async doCompile(targetCode) {
206
+ const { language, getImport } = this.props;
207
+ const babel = await getBabel();
208
+ try {
209
+ const presets = [
210
+ [
211
+ babel.availablePresets.react
212
+ ],
213
+ [
214
+ babel.availablePresets.env,
215
+ {
216
+ modules: 'commonjs'
303
217
  }
304
- }
218
+ ]
219
+ ];
220
+ if ('tsx' === language || 'ts' === language) presets.unshift([
221
+ babel.availablePresets.typescript,
222
+ {
223
+ allExtensions: true,
224
+ isTSX: 'tsx' === language
305
225
  }
306
- if (specifiers.length > 0) {
307
- code.push(
308
- createVariableDeclaration(
309
- createObjectPattern(specifiers),
310
- createGetImport(pkg)
311
- )
312
- );
313
- }
314
- path.replaceWithMultiple(code);
315
- }
316
- },
317
- post(file) {
318
- if (!this.hasReactImported) {
319
- file.ast.program.body.unshift(
320
- createVariableDeclaration(
321
- "React",
322
- createGetImport("react", true)
323
- )
324
- );
325
- }
226
+ ]);
227
+ const result = babel.transform(targetCode, {
228
+ sourceType: 'module',
229
+ sourceMaps: 'inline',
230
+ presets,
231
+ plugins: [
232
+ {
233
+ pre () {
234
+ this.hasReactImported = false;
235
+ },
236
+ visitor: {
237
+ ImportDeclaration (path) {
238
+ const pkg = path.node.source.value;
239
+ const code = [];
240
+ const specifiers = [];
241
+ for (const specifier of path.node.specifiers){
242
+ if ('React' === specifier.local.name) this.hasReactImported = true;
243
+ if ('ImportDefaultSpecifier' === specifier.type) code.push(createVariableDeclaration(specifier.local.name, createGetImport(pkg, true)));
244
+ if ('ImportNamespaceSpecifier' === specifier.type) code.push(createVariableDeclaration(specifier.local.name, createGetImport(pkg)));
245
+ if ('ImportSpecifier' === specifier.type) {
246
+ if ('name' in specifier.imported && specifier.imported.name !== specifier.local.name) specifiers.push([
247
+ specifier.imported.name,
248
+ specifier.local.name
249
+ ]);
250
+ else specifiers.push(specifier.local.name);
251
+ }
252
+ }
253
+ if (specifiers.length > 0) code.push(createVariableDeclaration(createObjectPattern(specifiers), createGetImport(pkg)));
254
+ path.replaceWithMultiple(code);
255
+ }
256
+ },
257
+ post (file) {
258
+ if (!this.hasReactImported) file.ast.program.body.unshift(createVariableDeclaration('React', createGetImport('react', true)));
259
+ }
260
+ }
261
+ ]
262
+ });
263
+ if (targetCode !== this.props.code || !result || !result.code) return;
264
+ const runExports = {};
265
+ const func = new Function('__get_import', 'exports', result.code);
266
+ func(getImport, runExports);
267
+ if (runExports.default) {
268
+ this.setState({
269
+ error: void 0,
270
+ comp: /*#__PURE__*/ external_react_default().createElement(runExports.default)
271
+ });
272
+ return;
326
273
  }
327
- }
328
- ]
329
- });
330
- if (targetCode !== this.props.code || !result || !result.code) {
331
- return;
332
- }
333
- const runExports = {};
334
- const func = new Function("__get_import", "exports", result.code);
335
- func(getImport, runExports);
336
- if (runExports.default) {
337
- this.setState({
338
- error: void 0,
339
- comp: import_react3.default.createElement(runExports.default)
340
- });
341
- return;
342
- }
343
- this.setState({
344
- error: new Error("No default export")
345
- });
346
- } catch (e) {
347
- if (targetCode !== this.props.code) {
348
- return;
349
- }
350
- console.error(e);
351
- this.setState({
352
- error: e
353
- });
274
+ this.setState({
275
+ error: new Error('No default export')
276
+ });
277
+ } catch (e) {
278
+ if (targetCode !== this.props.code) return;
279
+ console.error(e);
280
+ this.setState({
281
+ error: e
282
+ });
283
+ }
284
+ }
285
+ componentDidCatch(error, errorInfo) {
286
+ console.error(error);
287
+ console.error(errorInfo);
288
+ }
289
+ componentDidMount() {
290
+ this.doCompile(this.props.code);
354
291
  }
355
- }
356
- componentDidCatch(error, errorInfo) {
357
- console.error(error);
358
- console.error(errorInfo);
359
- }
360
- componentDidMount() {
361
- this.doCompile(this.props.code);
362
- }
363
- componentDidUpdate(prevProps) {
364
- if (prevProps.code !== this.props.code) {
365
- this.waitCompile(this.props.code);
292
+ componentDidUpdate(prevProps) {
293
+ if (prevProps.code !== this.props.code) this.waitCompile(this.props.code);
366
294
  }
367
- }
368
- render() {
369
- const { className = "", code, language, getImport, ...rest } = this.props;
370
- const { error, comp } = this.state;
371
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: `rspress-playground-runner ${className}`, ...rest, children: [
372
- comp,
373
- error && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("pre", { className: "rspress-playground-error", children: error.message })
374
- ] });
375
- }
376
- };
377
- // Annotate the CommonJS export names for ESM import in node:
378
- 0 && (module.exports = {
379
- Editor,
380
- MonacoEditor,
381
- MonacoEditorLoader,
382
- Runner
295
+ render() {
296
+ const { className = '', code, language, getImport, ...rest } = this.props;
297
+ const { error, comp } = this.state;
298
+ return /*#__PURE__*/ external_react_default().createElement("div", {
299
+ className: `rspress-playground-runner ${className}`,
300
+ ...rest
301
+ }, comp, error && /*#__PURE__*/ external_react_default().createElement("pre", {
302
+ className: "rspress-playground-error"
303
+ }, error.message));
304
+ }
305
+ constructor(props){
306
+ super(props);
307
+ this.state = {
308
+ error: void 0,
309
+ comp: null
310
+ };
311
+ this.doCompile = this.doCompile.bind(this);
312
+ this.waitCompile = this.waitCompile.bind(this);
313
+ }
314
+ }
315
+ var __webpack_export_target__ = exports;
316
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
317
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
318
+ value: true
383
319
  });
384
-
385
- //# sourceMappingURL=index.js.map