@rspress/plugin-preview 0.0.0-next-20231207111357 → 0.0.0-next-20240226105749
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/dist/index.d.ts +35 -23
- package/dist/index.js +414 -402
- package/dist/index.js.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +9 -5
- package/static/global-components/Container.tsx +9 -6
- package/static/global-components/Device.tsx +13 -11
- package/static/global-components/common/index.scss +2 -2
- package/static/global-components/common/mobile-operation.tsx +3 -10
- package/static/global-styles/entry.css +20 -0
- package/static/global-styles/{web.css → internal.css} +1 -1
- package/static/modern-app-env.d.ts +1 -1
- package/dist/virtual-demo.js +0 -73
- package/mdx-meta-loader.cjs +0 -27
- /package/static/global-styles/{mobile.css → iframe.css} +0 -0
package/dist/index.js
CHANGED
@@ -5,9 +5,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __esm = (fn, res) => function __init() {
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
10
|
-
};
|
11
8
|
var __export = (target, all) => {
|
12
9
|
for (var name in all)
|
13
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -30,7 +27,51 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
30
27
|
));
|
31
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
32
29
|
|
30
|
+
// src/index.ts
|
31
|
+
var src_exports = {};
|
32
|
+
__export(src_exports, {
|
33
|
+
pluginPreview: () => pluginPreview
|
34
|
+
});
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
36
|
+
var import_path4 = require("path");
|
37
|
+
var import_node_net = __toESM(require("net"));
|
38
|
+
var import_core = require("@rsbuild/core");
|
39
|
+
var import_plugin_solid = require("@rsbuild/plugin-solid");
|
40
|
+
var import_plugin_babel = require("@rsbuild/plugin-babel");
|
41
|
+
var import_plugin_react = require("@rsbuild/plugin-react");
|
42
|
+
var import_lodash = require("lodash");
|
43
|
+
|
44
|
+
// src/remarkPlugin.ts
|
45
|
+
var import_path2 = require("path");
|
46
|
+
|
33
47
|
// ../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/lib/index.js
|
48
|
+
var convert = (
|
49
|
+
/**
|
50
|
+
* @type {(
|
51
|
+
* (<Kind extends Node>(test: PredicateTest<Kind>) => AssertPredicate<Kind>) &
|
52
|
+
* ((test?: Test) => AssertAnything)
|
53
|
+
* )}
|
54
|
+
*/
|
55
|
+
/**
|
56
|
+
* @param {Test} [test]
|
57
|
+
* @returns {AssertAnything}
|
58
|
+
*/
|
59
|
+
function(test) {
|
60
|
+
if (test === void 0 || test === null) {
|
61
|
+
return ok;
|
62
|
+
}
|
63
|
+
if (typeof test === "string") {
|
64
|
+
return typeFactory(test);
|
65
|
+
}
|
66
|
+
if (typeof test === "object") {
|
67
|
+
return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
|
68
|
+
}
|
69
|
+
if (typeof test === "function") {
|
70
|
+
return castFactory(test);
|
71
|
+
}
|
72
|
+
throw new Error("Expected function, string, or object as test");
|
73
|
+
}
|
74
|
+
);
|
34
75
|
function anyFactory(tests) {
|
35
76
|
const checks = [];
|
36
77
|
let index = -1;
|
@@ -76,57 +117,81 @@ function castFactory(check) {
|
|
76
117
|
function ok() {
|
77
118
|
return true;
|
78
119
|
}
|
79
|
-
var convert;
|
80
|
-
var init_lib = __esm({
|
81
|
-
"../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/lib/index.js"() {
|
82
|
-
"use strict";
|
83
|
-
convert = /**
|
84
|
-
* @type {(
|
85
|
-
* (<Kind extends Node>(test: PredicateTest<Kind>) => AssertPredicate<Kind>) &
|
86
|
-
* ((test?: Test) => AssertAnything)
|
87
|
-
* )}
|
88
|
-
*/
|
89
|
-
/**
|
90
|
-
* @param {Test} [test]
|
91
|
-
* @returns {AssertAnything}
|
92
|
-
*/
|
93
|
-
function(test) {
|
94
|
-
if (test === void 0 || test === null) {
|
95
|
-
return ok;
|
96
|
-
}
|
97
|
-
if (typeof test === "string") {
|
98
|
-
return typeFactory(test);
|
99
|
-
}
|
100
|
-
if (typeof test === "object") {
|
101
|
-
return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
|
102
|
-
}
|
103
|
-
if (typeof test === "function") {
|
104
|
-
return castFactory(test);
|
105
|
-
}
|
106
|
-
throw new Error("Expected function, string, or object as test");
|
107
|
-
};
|
108
|
-
}
|
109
|
-
});
|
110
|
-
|
111
|
-
// ../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/index.js
|
112
|
-
var init_unist_util_is = __esm({
|
113
|
-
"../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/index.js"() {
|
114
|
-
"use strict";
|
115
|
-
init_lib();
|
116
|
-
}
|
117
|
-
});
|
118
120
|
|
119
121
|
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/color.js
|
120
122
|
function color(d) {
|
121
123
|
return "\x1B[33m" + d + "\x1B[39m";
|
122
124
|
}
|
123
|
-
var init_color = __esm({
|
124
|
-
"../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/color.js"() {
|
125
|
-
"use strict";
|
126
|
-
}
|
127
|
-
});
|
128
125
|
|
129
126
|
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/index.js
|
127
|
+
var CONTINUE = true;
|
128
|
+
var EXIT = false;
|
129
|
+
var SKIP = "skip";
|
130
|
+
var visitParents = (
|
131
|
+
/**
|
132
|
+
* @type {(
|
133
|
+
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
|
134
|
+
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
|
135
|
+
* )}
|
136
|
+
*/
|
137
|
+
/**
|
138
|
+
* @param {Node} tree
|
139
|
+
* @param {Test} test
|
140
|
+
* @param {Visitor<Node>} visitor
|
141
|
+
* @param {boolean | null | undefined} [reverse]
|
142
|
+
* @returns {void}
|
143
|
+
*/
|
144
|
+
function(tree, test, visitor, reverse) {
|
145
|
+
if (typeof test === "function" && typeof visitor !== "function") {
|
146
|
+
reverse = visitor;
|
147
|
+
visitor = test;
|
148
|
+
test = null;
|
149
|
+
}
|
150
|
+
const is2 = convert(test);
|
151
|
+
const step = reverse ? -1 : 1;
|
152
|
+
factory(tree, void 0, [])();
|
153
|
+
function factory(node, index, parents) {
|
154
|
+
const value = node && typeof node === "object" ? node : {};
|
155
|
+
if (typeof value.type === "string") {
|
156
|
+
const name = (
|
157
|
+
// `hast`
|
158
|
+
typeof value.tagName === "string" ? value.tagName : (
|
159
|
+
// `xast`
|
160
|
+
typeof value.name === "string" ? value.name : void 0
|
161
|
+
)
|
162
|
+
);
|
163
|
+
Object.defineProperty(visit2, "name", {
|
164
|
+
value: "node (" + color(node.type + (name ? "<" + name + ">" : "")) + ")"
|
165
|
+
});
|
166
|
+
}
|
167
|
+
return visit2;
|
168
|
+
function visit2() {
|
169
|
+
let result = [];
|
170
|
+
let subresult;
|
171
|
+
let offset;
|
172
|
+
let grandparents;
|
173
|
+
if (!test || is2(node, index, parents[parents.length - 1] || null)) {
|
174
|
+
result = toResult(visitor(node, parents));
|
175
|
+
if (result[0] === EXIT) {
|
176
|
+
return result;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
if (node.children && result[0] !== SKIP) {
|
180
|
+
offset = (reverse ? node.children.length : -1) + step;
|
181
|
+
grandparents = parents.concat(node);
|
182
|
+
while (offset > -1 && offset < node.children.length) {
|
183
|
+
subresult = factory(node.children[offset], offset, grandparents)();
|
184
|
+
if (subresult[0] === EXIT) {
|
185
|
+
return subresult;
|
186
|
+
}
|
187
|
+
offset = typeof subresult[1] === "number" ? subresult[1] : offset + step;
|
188
|
+
}
|
189
|
+
}
|
190
|
+
return result;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
}
|
194
|
+
);
|
130
195
|
function toResult(value) {
|
131
196
|
if (Array.isArray(value)) {
|
132
197
|
return value;
|
@@ -136,154 +201,42 @@ function toResult(value) {
|
|
136
201
|
}
|
137
202
|
return [value];
|
138
203
|
}
|
139
|
-
var CONTINUE, EXIT, SKIP, visitParents;
|
140
|
-
var init_lib2 = __esm({
|
141
|
-
"../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/index.js"() {
|
142
|
-
"use strict";
|
143
|
-
init_unist_util_is();
|
144
|
-
init_color();
|
145
|
-
CONTINUE = true;
|
146
|
-
EXIT = false;
|
147
|
-
SKIP = "skip";
|
148
|
-
visitParents = /**
|
149
|
-
* @type {(
|
150
|
-
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
|
151
|
-
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
|
152
|
-
* )}
|
153
|
-
*/
|
154
|
-
/**
|
155
|
-
* @param {Node} tree
|
156
|
-
* @param {Test} test
|
157
|
-
* @param {Visitor<Node>} visitor
|
158
|
-
* @param {boolean | null | undefined} [reverse]
|
159
|
-
* @returns {void}
|
160
|
-
*/
|
161
|
-
function(tree, test, visitor, reverse) {
|
162
|
-
if (typeof test === "function" && typeof visitor !== "function") {
|
163
|
-
reverse = visitor;
|
164
|
-
visitor = test;
|
165
|
-
test = null;
|
166
|
-
}
|
167
|
-
const is2 = convert(test);
|
168
|
-
const step = reverse ? -1 : 1;
|
169
|
-
factory(tree, void 0, [])();
|
170
|
-
function factory(node, index, parents) {
|
171
|
-
const value = node && typeof node === "object" ? node : {};
|
172
|
-
if (typeof value.type === "string") {
|
173
|
-
const name = (
|
174
|
-
// `hast`
|
175
|
-
typeof value.tagName === "string" ? value.tagName : (
|
176
|
-
// `xast`
|
177
|
-
typeof value.name === "string" ? value.name : void 0
|
178
|
-
)
|
179
|
-
);
|
180
|
-
Object.defineProperty(visit2, "name", {
|
181
|
-
value: "node (" + color(node.type + (name ? "<" + name + ">" : "")) + ")"
|
182
|
-
});
|
183
|
-
}
|
184
|
-
return visit2;
|
185
|
-
function visit2() {
|
186
|
-
let result = [];
|
187
|
-
let subresult;
|
188
|
-
let offset;
|
189
|
-
let grandparents;
|
190
|
-
if (!test || is2(node, index, parents[parents.length - 1] || null)) {
|
191
|
-
result = toResult(visitor(node, parents));
|
192
|
-
if (result[0] === EXIT) {
|
193
|
-
return result;
|
194
|
-
}
|
195
|
-
}
|
196
|
-
if (node.children && result[0] !== SKIP) {
|
197
|
-
offset = (reverse ? node.children.length : -1) + step;
|
198
|
-
grandparents = parents.concat(node);
|
199
|
-
while (offset > -1 && offset < node.children.length) {
|
200
|
-
subresult = factory(node.children[offset], offset, grandparents)();
|
201
|
-
if (subresult[0] === EXIT) {
|
202
|
-
return subresult;
|
203
|
-
}
|
204
|
-
offset = typeof subresult[1] === "number" ? subresult[1] : offset + step;
|
205
|
-
}
|
206
|
-
}
|
207
|
-
return result;
|
208
|
-
}
|
209
|
-
}
|
210
|
-
};
|
211
|
-
}
|
212
|
-
});
|
213
|
-
|
214
|
-
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/index.js
|
215
|
-
var init_unist_util_visit_parents = __esm({
|
216
|
-
"../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/index.js"() {
|
217
|
-
"use strict";
|
218
|
-
init_lib2();
|
219
|
-
}
|
220
|
-
});
|
221
204
|
|
222
205
|
// ../../node_modules/.pnpm/unist-util-visit@4.1.1/node_modules/unist-util-visit/index.js
|
223
|
-
var
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
visitor = test;
|
252
|
-
test = null;
|
253
|
-
}
|
254
|
-
visitParents(tree, test, overload, reverse);
|
255
|
-
function overload(node, parents) {
|
256
|
-
const parent = parents[parents.length - 1];
|
257
|
-
return visitor(
|
258
|
-
node,
|
259
|
-
parent ? parent.children.indexOf(node) : null,
|
260
|
-
parent
|
261
|
-
);
|
262
|
-
}
|
263
|
-
};
|
206
|
+
var visit = (
|
207
|
+
/**
|
208
|
+
* @type {(
|
209
|
+
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types.js').BuildVisitor<Tree, Check>, reverse?: boolean) => void) &
|
210
|
+
* (<Tree extends Node>(tree: Tree, visitor: import('./complex-types.js').BuildVisitor<Tree>, reverse?: boolean) => void)
|
211
|
+
* )}
|
212
|
+
*/
|
213
|
+
/**
|
214
|
+
* @param {Node} tree
|
215
|
+
* @param {Test} test
|
216
|
+
* @param {import('./complex-types.js').Visitor} visitor
|
217
|
+
* @param {boolean} [reverse]
|
218
|
+
*/
|
219
|
+
function(tree, test, visitor, reverse) {
|
220
|
+
if (typeof test === "function" && typeof visitor !== "function") {
|
221
|
+
reverse = visitor;
|
222
|
+
visitor = test;
|
223
|
+
test = null;
|
224
|
+
}
|
225
|
+
visitParents(tree, test, overload, reverse);
|
226
|
+
function overload(node, parents) {
|
227
|
+
const parent = parents[parents.length - 1];
|
228
|
+
return visitor(
|
229
|
+
node,
|
230
|
+
parent ? parent.children.indexOf(node) : null,
|
231
|
+
parent
|
232
|
+
);
|
233
|
+
}
|
264
234
|
}
|
265
|
-
|
235
|
+
);
|
266
236
|
|
267
|
-
// src/
|
268
|
-
var src_exports = {};
|
269
|
-
__export(src_exports, {
|
270
|
-
demoMeta: () => demoMeta,
|
271
|
-
demoRoutes: () => demoRoutes,
|
272
|
-
normalizeId: () => normalizeId,
|
273
|
-
pluginPreview: () => pluginPreview,
|
274
|
-
routeMeta: () => routeMeta
|
275
|
-
});
|
276
|
-
module.exports = __toCommonJS(src_exports);
|
277
|
-
var import_path3 = require("path");
|
278
|
-
var import_rspack_plugin_virtual_module = require("rspack-plugin-virtual-module");
|
237
|
+
// src/remarkPlugin.ts
|
279
238
|
var import_shared2 = require("@rspress/shared");
|
280
|
-
var
|
281
|
-
|
282
|
-
// src/codeToDemo.ts
|
283
|
-
var import_path2 = require("path");
|
284
|
-
init_unist_util_visit();
|
285
|
-
var import_fs_extra = __toESM(require("@modern-js/utils/fs-extra"));
|
286
|
-
var import_shared = require("@rspress/shared");
|
239
|
+
var import_fs_extra = __toESM(require("@rspress/shared/fs-extra"));
|
287
240
|
|
288
241
|
// src/utils.ts
|
289
242
|
var toValidVarName = (str) => {
|
@@ -296,63 +249,73 @@ var toValidVarName = (str) => {
|
|
296
249
|
var generateId = (pageName, index) => {
|
297
250
|
return `_${toValidVarName(pageName)}_${index}`;
|
298
251
|
};
|
299
|
-
var normalizeId = (routePath) => {
|
300
|
-
const result = routePath.replace(/\.(.*)?$/, "");
|
301
|
-
return toValidVarName(result);
|
302
|
-
};
|
303
252
|
var injectDemoBlockImport = (str, path2) => {
|
304
253
|
return `
|
305
|
-
import DemoBlock from
|
254
|
+
import DemoBlock from ${JSON.stringify(path2)};
|
306
255
|
${str}
|
307
256
|
`;
|
308
257
|
};
|
309
258
|
|
310
259
|
// src/constant.ts
|
311
260
|
var import_path = __toESM(require("path"));
|
261
|
+
var import_shared = require("@rspress/shared");
|
312
262
|
var staticPath = import_path.default.join(__dirname, "..", "static");
|
313
|
-
var demoComponentPath = import_path.default.join(
|
314
|
-
__dirname,
|
315
|
-
"..",
|
316
|
-
"dist",
|
317
|
-
"virtual-demo.js"
|
318
|
-
);
|
319
263
|
var demoBlockComponentPath = import_path.default.join(
|
320
264
|
staticPath,
|
321
265
|
"global-components",
|
322
266
|
"DemoBlock.tsx"
|
323
267
|
);
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
268
|
+
var virtualDir = import_path.default.join(
|
269
|
+
process.cwd(),
|
270
|
+
"node_modules",
|
271
|
+
import_shared.RSPRESS_TEMP_DIR,
|
272
|
+
"virtual-demo"
|
328
273
|
);
|
329
|
-
|
274
|
+
|
275
|
+
// src/virtual-module.ts
|
276
|
+
var import_rspack_plugin_virtual_module = __toESM(require("rspack-plugin-virtual-module"));
|
277
|
+
var demos = {};
|
278
|
+
var demoRuntimeModule = new import_rspack_plugin_virtual_module.default({
|
279
|
+
"virtual-meta": "export const demos = {}"
|
280
|
+
});
|
281
|
+
|
282
|
+
// src/remarkPlugin.ts
|
283
|
+
var remarkCodeToDemo = ({
|
284
|
+
getRouteMeta,
|
285
|
+
previewMode,
|
286
|
+
defaultRenderMode,
|
287
|
+
position
|
288
|
+
}) => {
|
330
289
|
const routeMeta2 = getRouteMeta();
|
290
|
+
import_fs_extra.default.ensureDirSync(virtualDir);
|
331
291
|
return (tree, vfile) => {
|
332
|
-
const
|
292
|
+
const demoMdx = [];
|
333
293
|
const route = routeMeta2.find(
|
334
|
-
(
|
294
|
+
(meta2) => (0, import_shared2.normalizePosixPath)(meta2.absolutePath) === (0, import_shared2.normalizePosixPath)(vfile.path || vfile.history[0])
|
335
295
|
);
|
336
296
|
if (!route) {
|
337
297
|
return;
|
338
298
|
}
|
339
299
|
const { pageName } = route;
|
300
|
+
demos[pageName] = [];
|
301
|
+
let title = pageName;
|
340
302
|
let index = 1;
|
341
303
|
let externalDemoIndex = 0;
|
342
304
|
function constructDemoNode(demoId, demoPath, currentNode, isMobileMode, externalDemoIndex2) {
|
343
|
-
const demoRoute = `/~demo/${demoId}`;
|
344
305
|
if (isMobileMode) {
|
345
|
-
|
346
|
-
|
306
|
+
demos[pageName].push({
|
307
|
+
title,
|
308
|
+
id: demoId,
|
309
|
+
path: (0, import_path2.isAbsolute)(demoPath) ? demoPath : (0, import_path2.join)(vfile.dirname || (0, import_path2.dirname)(vfile.path), demoPath)
|
347
310
|
});
|
348
311
|
} else {
|
349
|
-
|
312
|
+
demoMdx.push(getASTNodeImport(`Demo${demoId}`, demoPath));
|
350
313
|
}
|
351
314
|
const tempVar = `externalDemoContent${externalDemoIndex2}`;
|
352
315
|
if (externalDemoIndex2 !== void 0) {
|
353
|
-
|
316
|
+
demoMdx.push(getASTNodeImport(tempVar, `!!${demoPath}?raw`));
|
354
317
|
}
|
355
|
-
if (isMobileMode &&
|
318
|
+
if (isMobileMode && position === "fixed") {
|
356
319
|
externalDemoIndex2 !== void 0 && Object.assign(currentNode, getExternalDemoContent(tempVar));
|
357
320
|
} else {
|
358
321
|
Object.assign(currentNode, {
|
@@ -366,18 +329,8 @@ var remarkCodeToDemo = ({ isMobile, getRouteMeta, iframePosition }) => {
|
|
366
329
|
},
|
367
330
|
{
|
368
331
|
type: "mdxJsxAttribute",
|
369
|
-
name: "
|
370
|
-
value:
|
371
|
-
},
|
372
|
-
{
|
373
|
-
type: "mdxJsxAttribute",
|
374
|
-
name: "content",
|
375
|
-
value: currentNode.value
|
376
|
-
},
|
377
|
-
{
|
378
|
-
type: "mdxJsxAttribute",
|
379
|
-
name: "packageName",
|
380
|
-
value: json.name
|
332
|
+
name: "demoId",
|
333
|
+
value: demoId
|
381
334
|
}
|
382
335
|
],
|
383
336
|
children: [
|
@@ -396,17 +349,36 @@ var remarkCodeToDemo = ({ isMobile, getRouteMeta, iframePosition }) => {
|
|
396
349
|
});
|
397
350
|
}
|
398
351
|
}
|
352
|
+
visit(tree, "heading", (node) => {
|
353
|
+
if (node.depth === 1) {
|
354
|
+
if (node.children) {
|
355
|
+
title = node.children[0]?.value || title;
|
356
|
+
}
|
357
|
+
}
|
358
|
+
});
|
399
359
|
visit(tree, "mdxJsxFlowElement", (node) => {
|
400
360
|
if (node.name === "code") {
|
401
361
|
const src = node.attributes.find(
|
402
362
|
(attr) => attr.name === "src"
|
403
363
|
)?.value;
|
404
|
-
const isMobileMode = node.attributes.find(
|
405
|
-
(attr) => attr.name === "isMobile"
|
406
|
-
)?.value ?? isMobile;
|
407
364
|
if (!src) {
|
408
365
|
return;
|
409
366
|
}
|
367
|
+
const currtentMode = node.attributes.find(
|
368
|
+
(attr) => attr.name === "previewMode"
|
369
|
+
)?.value ?? previewMode;
|
370
|
+
let isMobileMode = node.attributes.find(
|
371
|
+
(attr) => attr.name === "isMobile"
|
372
|
+
)?.value;
|
373
|
+
if (isMobileMode === void 0) {
|
374
|
+
isMobileMode = currtentMode === "iframe";
|
375
|
+
} else if (isMobileMode === null) {
|
376
|
+
isMobileMode = true;
|
377
|
+
} else if (typeof isMobileMode === "object") {
|
378
|
+
isMobileMode = isMobileMode.value !== "false";
|
379
|
+
} else {
|
380
|
+
isMobileMode = isMobileMode !== "false";
|
381
|
+
}
|
410
382
|
const id = generateId(pageName, index++);
|
411
383
|
constructDemoNode(id, src, node, isMobileMode, externalDemoIndex++);
|
412
384
|
}
|
@@ -417,30 +389,27 @@ var remarkCodeToDemo = ({ isMobile, getRouteMeta, iframePosition }) => {
|
|
417
389
|
}
|
418
390
|
if (node.lang === "jsx" || node.lang === "tsx") {
|
419
391
|
const value = injectDemoBlockImport(node.value, demoBlockComponentPath);
|
420
|
-
|
421
|
-
if (isPure) {
|
392
|
+
if (node?.meta?.includes("pure") || !node?.meta?.includes("preview") && defaultRenderMode === "pure") {
|
422
393
|
return;
|
423
394
|
}
|
424
|
-
const isMobileMode = node?.meta?.includes("mobile") || !node?.meta?.includes("web") &&
|
425
|
-
const demoDir = (0, import_path2.join)(
|
426
|
-
process.cwd(),
|
427
|
-
"node_modules",
|
428
|
-
import_shared.RSPRESS_TEMP_DIR,
|
429
|
-
`virtual-demo`
|
430
|
-
);
|
395
|
+
const isMobileMode = node?.meta?.includes("mobile") || node?.meta?.includes("iframe") || !node?.meta?.includes("web") && !node?.meta?.includes("internal") && previewMode === "iframe";
|
431
396
|
const id = generateId(pageName, index++);
|
432
|
-
const virtualModulePath = (0, import_path2.join)(
|
433
|
-
|
397
|
+
const virtualModulePath = (0, import_path2.join)(virtualDir, `${id}.tsx`);
|
398
|
+
constructDemoNode(id, virtualModulePath, node, isMobileMode);
|
434
399
|
if (import_fs_extra.default.existsSync(virtualModulePath)) {
|
435
400
|
const content = import_fs_extra.default.readFileSync(virtualModulePath, "utf-8");
|
436
|
-
if (content
|
437
|
-
|
401
|
+
if (content === value) {
|
402
|
+
return;
|
438
403
|
}
|
439
404
|
}
|
440
|
-
|
405
|
+
import_fs_extra.default.writeFileSync(virtualModulePath, value);
|
441
406
|
}
|
442
407
|
});
|
443
|
-
tree.children.unshift(...
|
408
|
+
tree.children.unshift(...demoMdx);
|
409
|
+
const meta = `
|
410
|
+
export const demos = ${JSON.stringify(demos)}
|
411
|
+
`;
|
412
|
+
demoRuntimeModule.writeModule("virtual-meta", meta);
|
444
413
|
};
|
445
414
|
};
|
446
415
|
var getASTNodeImport = (name, from) => ({
|
@@ -520,17 +489,105 @@ var getExternalDemoContent = (tempVar) => ({
|
|
520
489
|
]
|
521
490
|
});
|
522
491
|
|
492
|
+
// src/generate-entry.ts
|
493
|
+
var import_path3 = require("path");
|
494
|
+
var import_fs = require("fs");
|
495
|
+
function generateEntry(demos2, framework, position) {
|
496
|
+
const sourceEntry = {};
|
497
|
+
const entryCssPath = (0, import_path3.join)(staticPath, "global-styles", "entry.css");
|
498
|
+
if (position === "follow") {
|
499
|
+
Object.values(demos2).forEach((routes) => {
|
500
|
+
routes.forEach((route) => {
|
501
|
+
const { id, path: demoPath } = route;
|
502
|
+
const entry = (0, import_path3.join)(virtualDir, `${id}.entry.tsx`);
|
503
|
+
const solidEntry = `
|
504
|
+
import { render } from 'solid-js/web';
|
505
|
+
import ${JSON.stringify(entryCssPath)};
|
506
|
+
import Demo from ${JSON.stringify(demoPath)};
|
507
|
+
render(() => <Demo />, document.getElementById('root'));
|
508
|
+
`;
|
509
|
+
const reactEntry = `
|
510
|
+
import { render } from 'react-dom';
|
511
|
+
import ${JSON.stringify(entryCssPath)};
|
512
|
+
import Demo from ${JSON.stringify(demoPath)};
|
513
|
+
render(<Demo />, document.getElementById('root'));
|
514
|
+
`;
|
515
|
+
const entryContent = framework === "react" ? reactEntry : solidEntry;
|
516
|
+
(0, import_fs.writeFileSync)(entry, entryContent);
|
517
|
+
sourceEntry[id] = entry;
|
518
|
+
});
|
519
|
+
});
|
520
|
+
} else {
|
521
|
+
Object.entries(demos2).forEach(([key, routes]) => {
|
522
|
+
if (routes.length === 0) {
|
523
|
+
return;
|
524
|
+
}
|
525
|
+
const reactContent = `
|
526
|
+
import { render } from 'react-dom';
|
527
|
+
import ${JSON.stringify(entryCssPath)};
|
528
|
+
${routes.map((demo, index) => {
|
529
|
+
return `import Demo_${index} from ${JSON.stringify(demo.path)}`;
|
530
|
+
}).join("\n")}
|
531
|
+
function App() {
|
532
|
+
return (
|
533
|
+
<div className="preview-container">
|
534
|
+
<div className="preview-nav">{"${routes[0].title}"}</div>
|
535
|
+
${routes.map((demo, index) => {
|
536
|
+
return `<Demo_${index} />`;
|
537
|
+
}).join("\n")}
|
538
|
+
</div>
|
539
|
+
)
|
540
|
+
}
|
541
|
+
render(<App /> , document.getElementById('root'));
|
542
|
+
`;
|
543
|
+
const solidContent = `
|
544
|
+
import { render } from 'solid-js/web';
|
545
|
+
import ${JSON.stringify(entryCssPath)};
|
546
|
+
${routes.map((demo, index) => {
|
547
|
+
return `import Demo_${index} from ${JSON.stringify(demo.path)}`;
|
548
|
+
}).join("\n")}
|
549
|
+
function App() {
|
550
|
+
return (
|
551
|
+
<div class="preview-container">
|
552
|
+
<div class="preview-nav">{"${routes[0].title}"}</div>
|
553
|
+
${routes.map((_, index) => {
|
554
|
+
return `<Demo_${index} />`;
|
555
|
+
}).join("\n")}
|
556
|
+
</div>
|
557
|
+
)
|
558
|
+
}
|
559
|
+
render(() => <App /> , document.getElementById('root'));
|
560
|
+
`;
|
561
|
+
const renderContent = framework === "solid" ? solidContent : reactContent;
|
562
|
+
const id = `_${toValidVarName(key)}`;
|
563
|
+
const entry = (0, import_path3.join)(virtualDir, `${id}.entry.tsx`);
|
564
|
+
(0, import_fs.writeFileSync)(entry, renderContent);
|
565
|
+
sourceEntry[id] = entry;
|
566
|
+
});
|
567
|
+
}
|
568
|
+
return sourceEntry;
|
569
|
+
}
|
570
|
+
|
523
571
|
// src/index.ts
|
524
572
|
var routeMeta;
|
525
|
-
var demoRoutes = [];
|
526
|
-
var demoMeta = {};
|
527
573
|
function pluginPreview(options) {
|
528
|
-
const
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
574
|
+
const {
|
575
|
+
isMobile = false,
|
576
|
+
iframeOptions = {},
|
577
|
+
iframePosition = "follow",
|
578
|
+
defaultRenderMode = "preview"
|
579
|
+
} = options ?? {};
|
580
|
+
const previewMode = options?.previewMode ?? isMobile ? "iframe" : "internal";
|
581
|
+
const {
|
582
|
+
devPort = 7890,
|
583
|
+
framework = "react",
|
584
|
+
position = iframePosition
|
585
|
+
} = iframeOptions;
|
586
|
+
const globalUIComponents = iframePosition === "fixed" ? [(0, import_path4.join)(staticPath, "global-components", "Device.tsx")] : [];
|
533
587
|
const getRouteMeta = () => routeMeta;
|
588
|
+
let lastDemos;
|
589
|
+
let devServer;
|
590
|
+
const port = devPort;
|
534
591
|
return {
|
535
592
|
name: "@rspress/plugin-preview",
|
536
593
|
config(config) {
|
@@ -538,186 +595,141 @@ function pluginPreview(options) {
|
|
538
595
|
config.markdown.mdxRs = false;
|
539
596
|
return config;
|
540
597
|
},
|
541
|
-
|
542
|
-
return [
|
543
|
-
{
|
544
|
-
routePath: "/~demo/:id",
|
545
|
-
content: `---
|
546
|
-
pageType: "blank"
|
547
|
-
---
|
548
|
-
|
549
|
-
import Demo from ${JSON.stringify(demoComponentPath)}
|
550
|
-
|
551
|
-
<Demo iframePosition="${iframePosition}"/>
|
552
|
-
`
|
553
|
-
}
|
554
|
-
];
|
555
|
-
},
|
556
|
-
async routeGenerated(routes) {
|
598
|
+
routeGenerated(routes) {
|
557
599
|
routeMeta = routes;
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
const { default: remarkGFM } = await import("remark-gfm");
|
569
|
-
let title = (0, import_path3.parse)(filepath).name;
|
570
|
-
try {
|
571
|
-
const processor = createProcessor({
|
572
|
-
format: (0, import_path3.extname)(filepath).slice(1),
|
573
|
-
remarkPlugins: [remarkGFM]
|
600
|
+
},
|
601
|
+
async beforeBuild(_, isProd) {
|
602
|
+
if (!isProd) {
|
603
|
+
try {
|
604
|
+
await new Promise((resolve, reject) => {
|
605
|
+
const server = import_node_net.default.createServer();
|
606
|
+
server.unref();
|
607
|
+
server.on("error", reject);
|
608
|
+
server.listen({ port, host: "0.0.0.0" }, () => {
|
609
|
+
server.close(resolve);
|
574
610
|
});
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
611
|
+
});
|
612
|
+
} catch (e) {
|
613
|
+
if (e.code !== "EADDRINUSE") {
|
614
|
+
throw e;
|
615
|
+
} else {
|
616
|
+
throw new Error(
|
617
|
+
`Port "${port}" is occupied, please choose another one.`
|
580
618
|
);
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
const hasPureMeta = meta?.includes("pure");
|
622
|
-
const hasPreviewMeta = meta?.includes("preview");
|
623
|
-
let noTransform;
|
624
|
-
switch (defaultRenderMode) {
|
625
|
-
case "pure":
|
626
|
-
noTransform = !hasPreviewMeta;
|
627
|
-
break;
|
628
|
-
case "preview":
|
629
|
-
noTransform = hasPureMeta;
|
630
|
-
break;
|
631
|
-
default:
|
632
|
-
break;
|
633
|
-
}
|
634
|
-
if (noTransform) {
|
635
|
-
return;
|
636
|
-
}
|
637
|
-
const isMobileMode = node?.meta?.includes("mobile") || !node?.meta?.includes("web") && isMobile;
|
638
|
-
const { pageName: pageName2 } = routeMeta.find(
|
639
|
-
(meta2) => (0, import_shared2.normalizePosixPath)(meta2.absolutePath) === (0, import_shared2.normalizePosixPath)(filepath)
|
640
|
-
);
|
641
|
-
const id = generateId(pageName2, index++);
|
642
|
-
const demoDir = (0, import_path3.join)(
|
643
|
-
process.cwd(),
|
644
|
-
"node_modules",
|
645
|
-
import_shared2.RSPRESS_TEMP_DIR,
|
646
|
-
`virtual-demo`
|
647
|
-
);
|
648
|
-
const virtualModulePath = (0, import_path3.join)(demoDir, `${id}.tsx`);
|
649
|
-
registerDemo(id, virtualModulePath, isMobileMode);
|
650
|
-
fs2.ensureDirSync((0, import_path3.join)(demoDir));
|
651
|
-
fs2.writeFileSync(
|
652
|
-
virtualModulePath,
|
653
|
-
injectDemoBlockImport(value, demoBlockComponentPath)
|
654
|
-
);
|
619
|
+
}
|
620
|
+
}
|
621
|
+
}
|
622
|
+
},
|
623
|
+
async afterBuild(config, isProd) {
|
624
|
+
if ((0, import_lodash.isEqual)(demos, lastDemos)) {
|
625
|
+
return;
|
626
|
+
}
|
627
|
+
lastDemos = (0, import_lodash.cloneDeep)(demos);
|
628
|
+
await devServer?.server?.close();
|
629
|
+
const sourceEntry = generateEntry(demos, framework, position);
|
630
|
+
const outDir = (0, import_path4.join)(config.outDir ?? "doc_build", "~demo");
|
631
|
+
if (Object.keys(sourceEntry).length === 0) {
|
632
|
+
return;
|
633
|
+
}
|
634
|
+
const rsbuildInstance = await (0, import_core.createRsbuild)({
|
635
|
+
rsbuildConfig: {
|
636
|
+
dev: {
|
637
|
+
progressBar: false
|
638
|
+
},
|
639
|
+
server: {
|
640
|
+
port: devPort,
|
641
|
+
printUrls: () => void 0,
|
642
|
+
strictPort: true
|
643
|
+
},
|
644
|
+
performance: {
|
645
|
+
printFileSize: false
|
646
|
+
},
|
647
|
+
source: {
|
648
|
+
entry: sourceEntry
|
649
|
+
},
|
650
|
+
output: {
|
651
|
+
distPath: {
|
652
|
+
root: outDir
|
653
|
+
}
|
654
|
+
},
|
655
|
+
tools: {
|
656
|
+
rspack: {
|
657
|
+
output: {
|
658
|
+
publicPath: "/~demo"
|
655
659
|
}
|
656
|
-
}
|
657
|
-
} catch (e) {
|
658
|
-
console.error(e);
|
659
|
-
throw e;
|
660
|
+
}
|
660
661
|
}
|
661
|
-
}
|
662
|
-
);
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
662
|
+
}
|
663
|
+
});
|
664
|
+
if (framework === "solid") {
|
665
|
+
rsbuildInstance.addPlugins([
|
666
|
+
(0, import_plugin_babel.pluginBabel)({
|
667
|
+
include: /\.(?:jsx|tsx)$/
|
668
|
+
}),
|
669
|
+
(0, import_plugin_solid.pluginSolid)()
|
670
|
+
]);
|
671
|
+
}
|
672
|
+
if (framework === "react") {
|
673
|
+
rsbuildInstance.addPlugins([(0, import_plugin_react.pluginReact)()]);
|
674
|
+
}
|
675
|
+
if (isProd) {
|
676
|
+
rsbuildInstance.build();
|
677
|
+
} else {
|
678
|
+
devServer = await rsbuildInstance.startDevServer();
|
679
|
+
}
|
670
680
|
},
|
671
681
|
builderConfig: {
|
672
682
|
source: {
|
673
|
-
include: [(0,
|
683
|
+
include: [(0, import_path4.join)(__dirname, "..")]
|
674
684
|
},
|
675
685
|
tools: {
|
676
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
|
677
|
-
// @ts-ignore
|
678
686
|
rspack: {
|
679
687
|
plugins: [demoRuntimeModule]
|
680
688
|
},
|
681
689
|
bundlerChain(chain) {
|
682
|
-
chain.module.rule("Raw").resourceQuery(/raw/).type("asset/source").end()
|
690
|
+
chain.module.rule("Raw").resourceQuery(/raw/).type("asset/source").end();
|
683
691
|
chain.resolve.extensions.prepend(".md").prepend(".mdx");
|
684
692
|
}
|
693
|
+
},
|
694
|
+
plugins: [
|
695
|
+
{
|
696
|
+
name: "close-demo-server",
|
697
|
+
setup: (api) => {
|
698
|
+
api.onCloseDevServer(async () => {
|
699
|
+
await devServer?.server?.close();
|
700
|
+
});
|
701
|
+
}
|
702
|
+
}
|
703
|
+
]
|
704
|
+
},
|
705
|
+
extendPageData(pageData, isProd) {
|
706
|
+
if (!isProd) {
|
707
|
+
pageData.devPort = port;
|
685
708
|
}
|
686
709
|
},
|
687
710
|
markdown: {
|
688
711
|
remarkPlugins: [
|
689
|
-
[
|
712
|
+
[
|
713
|
+
remarkCodeToDemo,
|
714
|
+
{
|
715
|
+
getRouteMeta,
|
716
|
+
position,
|
717
|
+
previewMode,
|
718
|
+
defaultRenderMode
|
719
|
+
}
|
720
|
+
]
|
690
721
|
],
|
691
722
|
globalComponents: [
|
692
|
-
(0,
|
723
|
+
(0, import_path4.join)(staticPath, "global-components", "Container.tsx")
|
693
724
|
]
|
694
725
|
},
|
695
726
|
globalUIComponents,
|
696
|
-
globalStyles: (0,
|
697
|
-
staticPath,
|
698
|
-
"global-styles",
|
699
|
-
`${isMobile ? "mobile" : "web"}.css`
|
700
|
-
),
|
701
|
-
addSSGRoutes() {
|
702
|
-
if (iframePosition === "fixed") {
|
703
|
-
const normalizeRoutes = demoRoutes.map(({ path: item }) => {
|
704
|
-
const fragments = item.split("/").filter(Boolean);
|
705
|
-
const demoId = fragments.pop()?.replace(/_(\d+)$/, "");
|
706
|
-
return { path: `/${fragments.join("/")}/${demoId}` };
|
707
|
-
});
|
708
|
-
return (0, import_lodash.uniqBy)(normalizeRoutes, "path");
|
709
|
-
}
|
710
|
-
return demoRoutes;
|
711
|
-
}
|
727
|
+
globalStyles: (0, import_path4.join)(staticPath, "global-styles", `${previewMode}.css`)
|
712
728
|
};
|
713
729
|
}
|
714
730
|
// Annotate the CommonJS export names for ESM import in node:
|
715
731
|
0 && (module.exports = {
|
716
|
-
|
717
|
-
demoRoutes,
|
718
|
-
normalizeId,
|
719
|
-
pluginPreview,
|
720
|
-
routeMeta
|
732
|
+
pluginPreview
|
721
733
|
});
|
722
734
|
|
723
735
|
//# sourceMappingURL=index.js.map
|