@rspress/plugin-playground 1.0.0-beta.5 → 1.0.2
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/README.md +4 -0
- package/dist/cli/cjs/index.js +7 -0
- package/dist/cli/cjs/index.js.map +1 -1
- package/dist/cli/esm/index.js +144 -145
- package/dist/cli/esm/index.js.map +1 -1
- package/dist/web/cjs/index.js +7 -0
- package/dist/web/cjs/index.js.map +1 -1
- package/dist/web/esm/index.js +55 -62
- package/dist/web/esm/index.js.map +1 -1
- package/package.json +4 -4
package/dist/cli/esm/index.js
CHANGED
|
@@ -45,8 +45,10 @@ function typeFactory(check) {
|
|
|
45
45
|
function castFactory(check) {
|
|
46
46
|
return assertion;
|
|
47
47
|
function assertion(node, ...parameters) {
|
|
48
|
-
return Boolean(
|
|
49
|
-
|
|
48
|
+
return Boolean(
|
|
49
|
+
node && typeof node === "object" && "type" in node && // @ts-expect-error: fine.
|
|
50
|
+
Boolean(check.call(this, node, ...parameters))
|
|
51
|
+
);
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
function ok() {
|
|
@@ -55,16 +57,17 @@ function ok() {
|
|
|
55
57
|
var convert;
|
|
56
58
|
var init_lib = __esm({
|
|
57
59
|
"../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/lib/index.js"() {
|
|
60
|
+
"use strict";
|
|
58
61
|
convert = /**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
* @type {(
|
|
63
|
+
* (<Kind extends Node>(test: PredicateTest<Kind>) => AssertPredicate<Kind>) &
|
|
64
|
+
* ((test?: Test) => AssertAnything)
|
|
65
|
+
* )}
|
|
66
|
+
*/
|
|
64
67
|
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
* @param {Test} [test]
|
|
69
|
+
* @returns {AssertAnything}
|
|
70
|
+
*/
|
|
68
71
|
function(test) {
|
|
69
72
|
if (test === void 0 || test === null) {
|
|
70
73
|
return ok;
|
|
@@ -86,6 +89,7 @@ var init_lib = __esm({
|
|
|
86
89
|
// ../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/index.js
|
|
87
90
|
var init_unist_util_is = __esm({
|
|
88
91
|
"../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/index.js"() {
|
|
92
|
+
"use strict";
|
|
89
93
|
init_lib();
|
|
90
94
|
}
|
|
91
95
|
});
|
|
@@ -96,6 +100,7 @@ function color(d) {
|
|
|
96
100
|
}
|
|
97
101
|
var init_color = __esm({
|
|
98
102
|
"../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/color.js"() {
|
|
103
|
+
"use strict";
|
|
99
104
|
}
|
|
100
105
|
});
|
|
101
106
|
|
|
@@ -105,36 +110,32 @@ function toResult(value) {
|
|
|
105
110
|
return value;
|
|
106
111
|
}
|
|
107
112
|
if (typeof value === "number") {
|
|
108
|
-
return [
|
|
109
|
-
CONTINUE,
|
|
110
|
-
value
|
|
111
|
-
];
|
|
113
|
+
return [CONTINUE, value];
|
|
112
114
|
}
|
|
113
|
-
return [
|
|
114
|
-
value
|
|
115
|
-
];
|
|
115
|
+
return [value];
|
|
116
116
|
}
|
|
117
117
|
var CONTINUE, EXIT, SKIP, visitParents;
|
|
118
118
|
var init_lib2 = __esm({
|
|
119
119
|
"../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/index.js"() {
|
|
120
|
+
"use strict";
|
|
120
121
|
init_unist_util_is();
|
|
121
122
|
init_color();
|
|
122
123
|
CONTINUE = true;
|
|
123
124
|
EXIT = false;
|
|
124
125
|
SKIP = "skip";
|
|
125
126
|
visitParents = /**
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
* @type {(
|
|
128
|
+
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
|
|
129
|
+
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
|
|
130
|
+
* )}
|
|
131
|
+
*/
|
|
131
132
|
/**
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
* @param {Node} tree
|
|
134
|
+
* @param {Test} test
|
|
135
|
+
* @param {Visitor<Node>} visitor
|
|
136
|
+
* @param {boolean | null | undefined} [reverse]
|
|
137
|
+
* @returns {void}
|
|
138
|
+
*/
|
|
138
139
|
function(tree, test, visitor, reverse) {
|
|
139
140
|
if (typeof test === "function" && typeof visitor !== "function") {
|
|
140
141
|
reverse = visitor;
|
|
@@ -149,7 +150,10 @@ var init_lib2 = __esm({
|
|
|
149
150
|
if (typeof value.type === "string") {
|
|
150
151
|
const name = (
|
|
151
152
|
// `hast`
|
|
152
|
-
typeof value.tagName === "string" ? value.tagName :
|
|
153
|
+
typeof value.tagName === "string" ? value.tagName : (
|
|
154
|
+
// `xast`
|
|
155
|
+
typeof value.name === "string" ? value.name : void 0
|
|
156
|
+
)
|
|
153
157
|
);
|
|
154
158
|
Object.defineProperty(visit2, "name", {
|
|
155
159
|
value: "node (" + color(node.type + (name ? "<" + name + ">" : "")) + ")"
|
|
@@ -188,6 +192,7 @@ var init_lib2 = __esm({
|
|
|
188
192
|
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/index.js
|
|
189
193
|
var init_unist_util_visit_parents = __esm({
|
|
190
194
|
"../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/index.js"() {
|
|
195
|
+
"use strict";
|
|
191
196
|
init_lib2();
|
|
192
197
|
}
|
|
193
198
|
});
|
|
@@ -203,20 +208,21 @@ __export(unist_util_visit_exports, {
|
|
|
203
208
|
var visit;
|
|
204
209
|
var init_unist_util_visit = __esm({
|
|
205
210
|
"../../node_modules/.pnpm/unist-util-visit@4.1.1/node_modules/unist-util-visit/index.js"() {
|
|
211
|
+
"use strict";
|
|
206
212
|
init_unist_util_visit_parents();
|
|
207
213
|
init_unist_util_visit_parents();
|
|
208
214
|
visit = /**
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
215
|
+
* @type {(
|
|
216
|
+
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types.js').BuildVisitor<Tree, Check>, reverse?: boolean) => void) &
|
|
217
|
+
* (<Tree extends Node>(tree: Tree, visitor: import('./complex-types.js').BuildVisitor<Tree>, reverse?: boolean) => void)
|
|
218
|
+
* )}
|
|
219
|
+
*/
|
|
214
220
|
/**
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
221
|
+
* @param {Node} tree
|
|
222
|
+
* @param {Test} test
|
|
223
|
+
* @param {import('./complex-types.js').Visitor} visitor
|
|
224
|
+
* @param {boolean} [reverse]
|
|
225
|
+
*/
|
|
220
226
|
function(tree, test, visitor, reverse) {
|
|
221
227
|
if (typeof test === "function" && typeof visitor !== "function") {
|
|
222
228
|
reverse = visitor;
|
|
@@ -226,7 +232,11 @@ var init_unist_util_visit = __esm({
|
|
|
226
232
|
visitParents(tree, test, overload, reverse);
|
|
227
233
|
function overload(node, parents) {
|
|
228
234
|
const parent = parents[parents.length - 1];
|
|
229
|
-
return visitor(
|
|
235
|
+
return visitor(
|
|
236
|
+
node,
|
|
237
|
+
parent ? parent.children.indexOf(node) : null,
|
|
238
|
+
parent
|
|
239
|
+
);
|
|
230
240
|
}
|
|
231
241
|
};
|
|
232
242
|
}
|
|
@@ -257,14 +267,18 @@ var parseImports = (code, sourceExt) => {
|
|
|
257
267
|
return result;
|
|
258
268
|
};
|
|
259
269
|
var getNodeAttribute = (node, attrName) => {
|
|
260
|
-
return node.attributes.find(
|
|
270
|
+
return node.attributes.find(
|
|
271
|
+
(attr) => attr.name === attrName
|
|
272
|
+
)?.value;
|
|
261
273
|
};
|
|
262
274
|
var getNodeMeta = (node, metaName) => {
|
|
263
275
|
if (!node.meta) {
|
|
264
276
|
return;
|
|
265
277
|
}
|
|
266
278
|
const meta = node.meta.split(" ");
|
|
267
|
-
const item = meta.find(
|
|
279
|
+
const item = meta.find(
|
|
280
|
+
(x) => x.startsWith(metaName)
|
|
281
|
+
);
|
|
268
282
|
if (item?.startsWith(`${metaName}=`)) {
|
|
269
283
|
return item.substring(metaName.length + 1);
|
|
270
284
|
}
|
|
@@ -286,10 +300,16 @@ function createPlaygroundNode(currentNode, attrs) {
|
|
|
286
300
|
}))
|
|
287
301
|
});
|
|
288
302
|
}
|
|
289
|
-
var remarkPlugin = ({
|
|
303
|
+
var remarkPlugin = ({
|
|
304
|
+
getRouteMeta,
|
|
305
|
+
defaultDirection,
|
|
306
|
+
editorPosition
|
|
307
|
+
}) => {
|
|
290
308
|
const routeMeta2 = getRouteMeta();
|
|
291
309
|
return (tree, vfile) => {
|
|
292
|
-
const route = routeMeta2.find(
|
|
310
|
+
const route = routeMeta2.find(
|
|
311
|
+
(meta) => meta.absolutePath === (vfile.path || vfile.history[0])
|
|
312
|
+
);
|
|
293
313
|
if (!route) {
|
|
294
314
|
return;
|
|
295
315
|
}
|
|
@@ -309,22 +329,10 @@ var remarkPlugin = ({ getRouteMeta, defaultDirection, editorPosition }) => {
|
|
|
309
329
|
});
|
|
310
330
|
const language = src.substr(src.lastIndexOf(".") + 1);
|
|
311
331
|
createPlaygroundNode(node, [
|
|
312
|
-
[
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
]
|
|
316
|
-
[
|
|
317
|
-
"language",
|
|
318
|
-
language
|
|
319
|
-
],
|
|
320
|
-
[
|
|
321
|
-
"direction",
|
|
322
|
-
direction
|
|
323
|
-
],
|
|
324
|
-
[
|
|
325
|
-
"editorPosition",
|
|
326
|
-
editorPosition
|
|
327
|
-
]
|
|
332
|
+
["code", code],
|
|
333
|
+
["language", language],
|
|
334
|
+
["direction", direction],
|
|
335
|
+
["editorPosition", editorPosition]
|
|
328
336
|
]);
|
|
329
337
|
}
|
|
330
338
|
});
|
|
@@ -336,22 +344,10 @@ var remarkPlugin = ({ getRouteMeta, defaultDirection, editorPosition }) => {
|
|
|
336
344
|
}
|
|
337
345
|
const direction = getNodeMeta(node, "direction") || defaultDirection;
|
|
338
346
|
createPlaygroundNode(node, [
|
|
339
|
-
[
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
]
|
|
343
|
-
[
|
|
344
|
-
"language",
|
|
345
|
-
node.lang
|
|
346
|
-
],
|
|
347
|
-
[
|
|
348
|
-
"direction",
|
|
349
|
-
direction
|
|
350
|
-
],
|
|
351
|
-
[
|
|
352
|
-
"editorPosition",
|
|
353
|
-
editorPosition
|
|
354
|
-
]
|
|
347
|
+
["code", node.value],
|
|
348
|
+
["language", node.lang],
|
|
349
|
+
["direction", direction],
|
|
350
|
+
["editorPosition", editorPosition]
|
|
355
351
|
]);
|
|
356
352
|
}
|
|
357
353
|
});
|
|
@@ -370,11 +366,21 @@ function normalizeUrl(u) {
|
|
|
370
366
|
// src/cli/index.ts
|
|
371
367
|
var routeMeta;
|
|
372
368
|
function pluginPlayground(options) {
|
|
373
|
-
const {
|
|
369
|
+
const {
|
|
370
|
+
render = "",
|
|
371
|
+
include,
|
|
372
|
+
defaultDirection = "horizontal",
|
|
373
|
+
editorPosition = "left",
|
|
374
|
+
babelUrl = "",
|
|
375
|
+
monacoLoader = {},
|
|
376
|
+
monacoOptions = {}
|
|
377
|
+
} = options || {};
|
|
374
378
|
const playgroundVirtualModule = new RspackVirtualModulePlugin({});
|
|
375
379
|
const getRouteMeta = () => routeMeta;
|
|
376
380
|
if (render && !/Playground\.(jsx?|tsx?)$/.test(render)) {
|
|
377
|
-
throw new Error(
|
|
381
|
+
throw new Error(
|
|
382
|
+
"[Playground]: render should ends with Playground.(jsx?|tsx?)"
|
|
383
|
+
);
|
|
378
384
|
}
|
|
379
385
|
const preloads = [];
|
|
380
386
|
preloads.push(babelUrl || DEFAULT_BABEL_URL);
|
|
@@ -388,64 +394,64 @@ function pluginPlayground(options) {
|
|
|
388
394
|
routeMeta = routes;
|
|
389
395
|
const files = routes.map((route) => route.absolutePath);
|
|
390
396
|
const imports = {};
|
|
391
|
-
await Promise.all(
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
remarkGFM
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
const demoPath = join2(path3.dirname(filepath), src);
|
|
415
|
-
if (!fs2.existsSync(demoPath)) {
|
|
416
|
-
return;
|
|
417
|
-
}
|
|
418
|
-
const code2 = fs2.readFileSync(demoPath, {
|
|
419
|
-
encoding: "utf8"
|
|
420
|
-
});
|
|
421
|
-
const thisImports = parseImports(code2, path3.extname(demoPath));
|
|
422
|
-
thisImports.forEach((x) => {
|
|
423
|
-
if (typeof imports[x] === "undefined") {
|
|
424
|
-
imports[x] = x;
|
|
397
|
+
await Promise.all(
|
|
398
|
+
files.map(async (filepath, _index) => {
|
|
399
|
+
const isMdxFile = /\.mdx?$/.test(filepath);
|
|
400
|
+
if (!isMdxFile) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const { createProcessor } = await import("@mdx-js/mdx");
|
|
404
|
+
const { visit: visit2 } = await Promise.resolve().then(() => (init_unist_util_visit(), unist_util_visit_exports));
|
|
405
|
+
const { default: remarkGFM } = await import("remark-gfm");
|
|
406
|
+
try {
|
|
407
|
+
const processor = createProcessor({
|
|
408
|
+
format: path3.extname(filepath).slice(1),
|
|
409
|
+
remarkPlugins: [remarkGFM]
|
|
410
|
+
});
|
|
411
|
+
const source = await fs2.readFile(filepath, "utf-8");
|
|
412
|
+
const ast = processor.parse(source);
|
|
413
|
+
visit2(ast, "mdxJsxFlowElement", (node) => {
|
|
414
|
+
if (node.name === "code") {
|
|
415
|
+
const src = getNodeAttribute(node, "src");
|
|
416
|
+
if (!src) {
|
|
417
|
+
return;
|
|
425
418
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
419
|
+
const demoPath = join2(path3.dirname(filepath), src);
|
|
420
|
+
if (!fs2.existsSync(demoPath)) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
const code2 = fs2.readFileSync(demoPath, {
|
|
424
|
+
encoding: "utf8"
|
|
425
|
+
});
|
|
426
|
+
const thisImports = parseImports(code2, path3.extname(demoPath));
|
|
427
|
+
thisImports.forEach((x) => {
|
|
428
|
+
if (typeof imports[x] === "undefined") {
|
|
429
|
+
imports[x] = x;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
435
432
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
433
|
+
});
|
|
434
|
+
visit2(ast, "code", (node) => {
|
|
435
|
+
if (node.lang === "jsx" || node.lang === "tsx") {
|
|
436
|
+
const { value } = node;
|
|
437
|
+
const isPure = node?.meta?.includes("pure");
|
|
438
|
+
if (isPure) {
|
|
439
|
+
return;
|
|
440
440
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
441
|
+
const thisImports = parseImports(value, node.lang);
|
|
442
|
+
thisImports.forEach((x) => {
|
|
443
|
+
if (typeof imports[x] === "undefined") {
|
|
444
|
+
imports[x] = x;
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
} catch (e) {
|
|
450
|
+
console.error(e);
|
|
451
|
+
throw e;
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
);
|
|
449
455
|
if (include) {
|
|
450
456
|
include.forEach((item) => {
|
|
451
457
|
if (typeof item === "string") {
|
|
@@ -457,7 +463,9 @@ function pluginPlayground(options) {
|
|
|
457
463
|
}
|
|
458
464
|
const importKeys = Object.keys(imports);
|
|
459
465
|
const code = [
|
|
460
|
-
...importKeys.map(
|
|
466
|
+
...importKeys.map(
|
|
467
|
+
(x, index) => `import * as i_${index} from '${imports[x]}';`
|
|
468
|
+
),
|
|
461
469
|
"const imports = new Map();",
|
|
462
470
|
...importKeys.map((x, index) => `imports.set('${x}', i_${index});`),
|
|
463
471
|
"function getImport(name, getDefault) {",
|
|
@@ -498,22 +506,13 @@ function pluginPlayground(options) {
|
|
|
498
506
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
|
|
499
507
|
// @ts-ignore
|
|
500
508
|
rspack: {
|
|
501
|
-
plugins: [
|
|
502
|
-
playgroundVirtualModule
|
|
503
|
-
]
|
|
509
|
+
plugins: [playgroundVirtualModule]
|
|
504
510
|
}
|
|
505
511
|
}
|
|
506
512
|
},
|
|
507
513
|
markdown: {
|
|
508
514
|
remarkPlugins: [
|
|
509
|
-
[
|
|
510
|
-
remarkPlugin,
|
|
511
|
-
{
|
|
512
|
-
getRouteMeta,
|
|
513
|
-
defaultDirection,
|
|
514
|
-
editorPosition
|
|
515
|
-
}
|
|
516
|
-
]
|
|
515
|
+
[remarkPlugin, { getRouteMeta, defaultDirection, editorPosition }]
|
|
517
516
|
],
|
|
518
517
|
globalComponents: [
|
|
519
518
|
render ? render : path3.join(staticPath, "global-components", "Playground.tsx")
|