@rspress/plugin-preview 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.
- package/dist/index.d.ts +68 -67
- package/dist/index.js +544 -707
- package/dist/utils.d.ts +19 -16
- package/dist/utils.js +51 -48
- package/package.json +7 -6
- package/static/global-components/Device.tsx +1 -0
- package/dist/index.js.map +0 -1
- package/dist/utils.js.map +0 -1
package/dist/index.js
CHANGED
@@ -1,766 +1,603 @@
|
|
1
1
|
"use strict";
|
2
|
-
var
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
19
|
-
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
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_node_net = __toESM(require("net"));
|
37
|
-
var import_node_path4 = require("path");
|
38
|
-
var import_core = require("@rsbuild/core");
|
39
|
-
var import_plugin_babel = require("@rsbuild/plugin-babel");
|
40
|
-
var import_plugin_react = require("@rsbuild/plugin-react");
|
41
|
-
var import_plugin_solid = require("@rsbuild/plugin-solid");
|
42
|
-
var import_shared3 = require("@rspress/shared");
|
43
|
-
var import_lodash = require("lodash");
|
44
|
-
|
45
|
-
// src/constant.ts
|
46
|
-
var import_node_path = __toESM(require("path"));
|
47
|
-
var import_shared = require("@rspress/shared");
|
48
|
-
var staticPath = import_node_path.default.join(__dirname, "..", "static");
|
49
|
-
var demoBlockComponentPath = import_node_path.default.join(
|
50
|
-
staticPath,
|
51
|
-
"global-components",
|
52
|
-
"DemoBlock.tsx"
|
53
|
-
);
|
54
|
-
var virtualDir = import_node_path.default.join(
|
55
|
-
process.cwd(),
|
56
|
-
"node_modules",
|
57
|
-
import_shared.RSPRESS_TEMP_DIR,
|
58
|
-
"virtual-demo"
|
59
|
-
);
|
60
|
-
|
61
|
-
// src/generate-entry.ts
|
62
|
-
var import_node_fs = require("fs");
|
63
|
-
var import_node_path2 = require("path");
|
64
|
-
|
65
|
-
// src/utils.ts
|
66
|
-
var toValidVarName = (str) => {
|
67
|
-
if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) {
|
68
|
-
return str;
|
69
|
-
}
|
70
|
-
return str.replace(/[^0-9a-zA-Z_$]/g, "_").replace(/^([0-9])/, "_$1");
|
71
|
-
};
|
72
|
-
var generateId = (pageName, index) => {
|
73
|
-
return `_${toValidVarName(pageName)}_${index}`;
|
2
|
+
var __webpack_modules__ = {
|
3
|
+
"@rsbuild/plugin-less": function(module) {
|
4
|
+
module.exports = import("@rsbuild/plugin-less");
|
5
|
+
},
|
6
|
+
"@rsbuild/plugin-sass": function(module) {
|
7
|
+
module.exports = import("@rsbuild/plugin-sass");
|
8
|
+
}
|
74
9
|
};
|
75
|
-
var
|
76
|
-
|
77
|
-
|
10
|
+
var __webpack_module_cache__ = {};
|
11
|
+
function __webpack_require__(moduleId) {
|
12
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
13
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
14
|
+
var module = __webpack_module_cache__[moduleId] = {
|
15
|
+
exports: {}
|
16
|
+
};
|
17
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
18
|
+
return module.exports;
|
19
|
+
}
|
20
|
+
(()=>{
|
21
|
+
__webpack_require__.n = function(module) {
|
22
|
+
var getter = module && module.__esModule ? function() {
|
23
|
+
return module['default'];
|
24
|
+
} : function() {
|
25
|
+
return module;
|
26
|
+
};
|
27
|
+
__webpack_require__.d(getter, {
|
28
|
+
a: getter
|
29
|
+
});
|
30
|
+
return getter;
|
31
|
+
};
|
32
|
+
})();
|
33
|
+
(()=>{
|
34
|
+
__webpack_require__.d = function(exports1, definition) {
|
35
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
36
|
+
enumerable: true,
|
37
|
+
get: definition[key]
|
38
|
+
});
|
39
|
+
};
|
40
|
+
})();
|
41
|
+
(()=>{
|
42
|
+
__webpack_require__.o = function(obj, prop) {
|
43
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
44
|
+
};
|
45
|
+
})();
|
46
|
+
(()=>{
|
47
|
+
__webpack_require__.r = function(exports1) {
|
48
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
49
|
+
value: 'Module'
|
50
|
+
});
|
51
|
+
Object.defineProperty(exports1, '__esModule', {
|
52
|
+
value: true
|
53
|
+
});
|
54
|
+
};
|
55
|
+
})();
|
56
|
+
var __webpack_exports__ = {};
|
57
|
+
(()=>{
|
58
|
+
__webpack_require__.r(__webpack_exports__);
|
59
|
+
__webpack_require__.d(__webpack_exports__, {
|
60
|
+
pluginPreview: ()=>pluginPreview
|
61
|
+
});
|
62
|
+
const external_node_net_namespaceObject = require("node:net");
|
63
|
+
var external_node_net_default = /*#__PURE__*/ __webpack_require__.n(external_node_net_namespaceObject);
|
64
|
+
const external_node_path_namespaceObject = require("node:path");
|
65
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
66
|
+
const core_namespaceObject = require("@rsbuild/core");
|
67
|
+
const plugin_babel_namespaceObject = require("@rsbuild/plugin-babel");
|
68
|
+
const plugin_react_namespaceObject = require("@rsbuild/plugin-react");
|
69
|
+
const plugin_solid_namespaceObject = require("@rsbuild/plugin-solid");
|
70
|
+
const shared_namespaceObject = require("@rspress/shared");
|
71
|
+
const external_lodash_namespaceObject = require("lodash");
|
72
|
+
const staticPath = external_node_path_default().join(__dirname, '..', 'static');
|
73
|
+
const demoBlockComponentPath = external_node_path_default().join(staticPath, 'global-components', 'DemoBlock.tsx');
|
74
|
+
const virtualDir = external_node_path_default().join(process.cwd(), 'node_modules', shared_namespaceObject.RSPRESS_TEMP_DIR, 'virtual-demo');
|
75
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
76
|
+
const toValidVarName = (str)=>{
|
77
|
+
if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) return str;
|
78
|
+
return str.replace(/[^0-9a-zA-Z_$]/g, '_').replace(/^([0-9])/, '_$1');
|
79
|
+
};
|
80
|
+
const generateId = (pageName, index)=>`_${toValidVarName(pageName)}_${index}`;
|
81
|
+
const injectDemoBlockImport = (str, path)=>`
|
82
|
+
import DemoBlock from ${JSON.stringify(path)};
|
78
83
|
${str}
|
79
84
|
`;
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
routes.forEach((route) => {
|
89
|
-
const { id, path: demoPath } = route;
|
90
|
-
const entry = (0, import_node_path2.join)(virtualDir, `${id}.entry.tsx`);
|
91
|
-
const solidEntry = `
|
85
|
+
function generateEntry(demos, framework, position) {
|
86
|
+
const sourceEntry = {};
|
87
|
+
const entryCssPath = (0, external_node_path_namespaceObject.join)(staticPath, 'global-styles', 'entry.css');
|
88
|
+
if ('follow' === position) Object.values(demos).forEach((routes)=>{
|
89
|
+
routes.forEach((route)=>{
|
90
|
+
const { id, path: demoPath } = route;
|
91
|
+
const entry = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.entry.tsx`);
|
92
|
+
const solidEntry = `
|
92
93
|
import { render } from 'solid-js/web';
|
93
94
|
import ${JSON.stringify(entryCssPath)};
|
94
95
|
import Demo from ${JSON.stringify(demoPath)};
|
95
96
|
render(() => <Demo />, document.getElementById('root'));
|
96
97
|
`;
|
97
|
-
|
98
|
+
const reactEntry = `
|
98
99
|
import { render } from 'react-dom';
|
99
100
|
import ${JSON.stringify(entryCssPath)};
|
100
101
|
import Demo from ${JSON.stringify(demoPath)};
|
101
102
|
render(<Demo />, document.getElementById('root'));
|
102
103
|
`;
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
return;
|
112
|
-
}
|
113
|
-
const reactContent = `
|
104
|
+
const entryContent = 'react' === framework ? reactEntry : solidEntry;
|
105
|
+
(0, external_node_fs_namespaceObject.writeFileSync)(entry, entryContent);
|
106
|
+
sourceEntry[id] = entry;
|
107
|
+
});
|
108
|
+
});
|
109
|
+
else Object.entries(demos).forEach(([key, routes])=>{
|
110
|
+
if (0 === routes.length) return;
|
111
|
+
const reactContent = `
|
114
112
|
import { render } from 'react-dom';
|
115
113
|
import ${JSON.stringify(entryCssPath)};
|
116
|
-
${routes.map((demo, index)
|
117
|
-
return `import Demo_${index} from ${JSON.stringify(demo.path)}`;
|
118
|
-
}).join("\n")}
|
114
|
+
${routes.map((demo, index)=>`import Demo_${index} from ${JSON.stringify(demo.path)}`).join('\n')}
|
119
115
|
function App() {
|
120
116
|
return (
|
121
117
|
<div className="preview-container">
|
122
118
|
<div className="preview-nav">{"${routes[0].title}"}</div>
|
123
|
-
${routes.map((demo, index)
|
124
|
-
return `<Demo_${index} />`;
|
125
|
-
}).join("\n")}
|
119
|
+
${routes.map((demo, index)=>`<Demo_${index} />`).join('\n')}
|
126
120
|
</div>
|
127
121
|
)
|
128
122
|
}
|
129
123
|
render(<App /> , document.getElementById('root'));
|
130
124
|
`;
|
131
|
-
|
125
|
+
const solidContent = `
|
132
126
|
import { render } from 'solid-js/web';
|
133
127
|
import ${JSON.stringify(entryCssPath)};
|
134
|
-
${routes.map((demo, index)
|
135
|
-
return `import Demo_${index} from ${JSON.stringify(demo.path)}`;
|
136
|
-
}).join("\n")}
|
128
|
+
${routes.map((demo, index)=>`import Demo_${index} from ${JSON.stringify(demo.path)}`).join('\n')}
|
137
129
|
function App() {
|
138
130
|
return (
|
139
131
|
<div class="preview-container">
|
140
132
|
<div class="preview-nav">{"${routes[0].title}"}</div>
|
141
|
-
${routes.map((_, index)
|
142
|
-
return `<Demo_${index} />`;
|
143
|
-
}).join("\n")}
|
133
|
+
${routes.map((_, index)=>`<Demo_${index} />`).join('\n')}
|
144
134
|
</div>
|
145
135
|
)
|
146
136
|
}
|
147
137
|
render(() => <App /> , document.getElementById('root'));
|
148
138
|
`;
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
return sourceEntry;
|
157
|
-
}
|
158
|
-
|
159
|
-
// src/remarkPlugin.ts
|
160
|
-
var import_node_path3 = require("path");
|
161
|
-
var import_shared2 = require("@rspress/shared");
|
162
|
-
var import_fs_extra = __toESM(require("@rspress/shared/fs-extra"));
|
163
|
-
|
164
|
-
// ../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/lib/index.js
|
165
|
-
var convert = (
|
166
|
-
/**
|
167
|
-
* @type {(
|
168
|
-
* (<Kind extends Node>(test: PredicateTest<Kind>) => AssertPredicate<Kind>) &
|
169
|
-
* ((test?: Test) => AssertAnything)
|
170
|
-
* )}
|
171
|
-
*/
|
172
|
-
/**
|
173
|
-
* @param {Test} [test]
|
174
|
-
* @returns {AssertAnything}
|
175
|
-
*/
|
176
|
-
function(test) {
|
177
|
-
if (test === void 0 || test === null) {
|
178
|
-
return ok;
|
139
|
+
const renderContent = 'solid' === framework ? solidContent : reactContent;
|
140
|
+
const id = `_${toValidVarName(key)}`;
|
141
|
+
const entry = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.entry.tsx`);
|
142
|
+
(0, external_node_fs_namespaceObject.writeFileSync)(entry, renderContent);
|
143
|
+
sourceEntry[id] = entry;
|
144
|
+
});
|
145
|
+
return sourceEntry;
|
179
146
|
}
|
180
|
-
|
181
|
-
|
147
|
+
const fs_extra_namespaceObject = require("@rspress/shared/fs-extra");
|
148
|
+
var fs_extra_default = /*#__PURE__*/ __webpack_require__.n(fs_extra_namespaceObject);
|
149
|
+
const convert = function(test) {
|
150
|
+
if (null == test) return ok;
|
151
|
+
if ('string' == typeof test) return typeFactory(test);
|
152
|
+
if ('object' == typeof test) return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
|
153
|
+
if ('function' == typeof test) return castFactory(test);
|
154
|
+
throw new Error('Expected function, string, or object as test');
|
155
|
+
};
|
156
|
+
function anyFactory(tests) {
|
157
|
+
const checks = [];
|
158
|
+
let index = -1;
|
159
|
+
while(++index < tests.length)checks[index] = convert(tests[index]);
|
160
|
+
return castFactory(any);
|
161
|
+
function any(...parameters) {
|
162
|
+
let index = -1;
|
163
|
+
while(++index < checks.length)if (checks[index].call(this, ...parameters)) return true;
|
164
|
+
return false;
|
165
|
+
}
|
182
166
|
}
|
183
|
-
|
184
|
-
|
167
|
+
function propsFactory(check) {
|
168
|
+
return castFactory(all);
|
169
|
+
function all(node) {
|
170
|
+
let key;
|
171
|
+
for(key in check)if (node[key] !== check[key]) return false;
|
172
|
+
return true;
|
173
|
+
}
|
185
174
|
}
|
186
|
-
|
187
|
-
|
175
|
+
function typeFactory(check) {
|
176
|
+
return castFactory(type);
|
177
|
+
function type(node) {
|
178
|
+
return node && node.type === check;
|
179
|
+
}
|
188
180
|
}
|
189
|
-
|
190
|
-
|
191
|
-
)
|
192
|
-
|
193
|
-
|
194
|
-
let index = -1;
|
195
|
-
while (++index < tests.length) {
|
196
|
-
checks[index] = convert(tests[index]);
|
197
|
-
}
|
198
|
-
return castFactory(any);
|
199
|
-
function any(...parameters) {
|
200
|
-
let index2 = -1;
|
201
|
-
while (++index2 < checks.length) {
|
202
|
-
if (checks[index2].call(this, ...parameters))
|
203
|
-
return true;
|
181
|
+
function castFactory(check) {
|
182
|
+
return assertion;
|
183
|
+
function assertion(node, ...parameters) {
|
184
|
+
return Boolean(node && 'object' == typeof node && 'type' in node && Boolean(check.call(this, node, ...parameters)));
|
185
|
+
}
|
204
186
|
}
|
205
|
-
|
206
|
-
|
207
|
-
}
|
208
|
-
function propsFactory(check) {
|
209
|
-
return castFactory(all);
|
210
|
-
function all(node) {
|
211
|
-
let key;
|
212
|
-
for (key in check) {
|
213
|
-
if (node[key] !== check[key])
|
214
|
-
return false;
|
187
|
+
function ok() {
|
188
|
+
return true;
|
215
189
|
}
|
216
|
-
|
217
|
-
|
218
|
-
}
|
219
|
-
function typeFactory(check) {
|
220
|
-
return castFactory(type);
|
221
|
-
function type(node) {
|
222
|
-
return node && node.type === check;
|
223
|
-
}
|
224
|
-
}
|
225
|
-
function castFactory(check) {
|
226
|
-
return assertion;
|
227
|
-
function assertion(node, ...parameters) {
|
228
|
-
return Boolean(
|
229
|
-
node && typeof node === "object" && "type" in node && // @ts-expect-error: fine.
|
230
|
-
Boolean(check.call(this, node, ...parameters))
|
231
|
-
);
|
232
|
-
}
|
233
|
-
}
|
234
|
-
function ok() {
|
235
|
-
return true;
|
236
|
-
}
|
237
|
-
|
238
|
-
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/color.js
|
239
|
-
function color(d) {
|
240
|
-
return "\x1B[33m" + d + "\x1B[39m";
|
241
|
-
}
|
242
|
-
|
243
|
-
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/index.js
|
244
|
-
var CONTINUE = true;
|
245
|
-
var EXIT = false;
|
246
|
-
var SKIP = "skip";
|
247
|
-
var visitParents = (
|
248
|
-
/**
|
249
|
-
* @type {(
|
250
|
-
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
|
251
|
-
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
|
252
|
-
* )}
|
253
|
-
*/
|
254
|
-
/**
|
255
|
-
* @param {Node} tree
|
256
|
-
* @param {Test} test
|
257
|
-
* @param {Visitor<Node>} visitor
|
258
|
-
* @param {boolean | null | undefined} [reverse]
|
259
|
-
* @returns {void}
|
260
|
-
*/
|
261
|
-
function(tree, test, visitor, reverse) {
|
262
|
-
if (typeof test === "function" && typeof visitor !== "function") {
|
263
|
-
reverse = visitor;
|
264
|
-
visitor = test;
|
265
|
-
test = null;
|
190
|
+
function color(d) {
|
191
|
+
return '\u001B[33m' + d + '\u001B[39m';
|
266
192
|
}
|
267
|
-
const
|
268
|
-
const
|
269
|
-
|
270
|
-
function
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
typeof value.tagName === "string" ? value.tagName : (
|
276
|
-
// `xast`
|
277
|
-
typeof value.name === "string" ? value.name : void 0
|
278
|
-
)
|
279
|
-
);
|
280
|
-
Object.defineProperty(visit2, "name", {
|
281
|
-
value: "node (" + color(node.type + (name ? "<" + name + ">" : "")) + ")"
|
282
|
-
});
|
283
|
-
}
|
284
|
-
return visit2;
|
285
|
-
function visit2() {
|
286
|
-
let result = [];
|
287
|
-
let subresult;
|
288
|
-
let offset;
|
289
|
-
let grandparents;
|
290
|
-
if (!test || is2(node, index, parents[parents.length - 1] || null)) {
|
291
|
-
result = toResult(visitor(node, parents));
|
292
|
-
if (result[0] === EXIT) {
|
293
|
-
return result;
|
294
|
-
}
|
193
|
+
const CONTINUE = true;
|
194
|
+
const EXIT = false;
|
195
|
+
const SKIP = 'skip';
|
196
|
+
const visitParents = function(tree, test, visitor, reverse) {
|
197
|
+
if ('function' == typeof test && 'function' != typeof visitor) {
|
198
|
+
reverse = visitor;
|
199
|
+
visitor = test;
|
200
|
+
test = null;
|
295
201
|
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
if (
|
302
|
-
|
202
|
+
const is = convert(test);
|
203
|
+
const step = reverse ? -1 : 1;
|
204
|
+
factory(tree, void 0, [])();
|
205
|
+
function factory(node, index, parents) {
|
206
|
+
const value = node && 'object' == typeof node ? node : {};
|
207
|
+
if ('string' == typeof value.type) {
|
208
|
+
const name = 'string' == typeof value.tagName ? value.tagName : 'string' == typeof value.name ? value.name : void 0;
|
209
|
+
Object.defineProperty(visit, 'name', {
|
210
|
+
value: 'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'
|
211
|
+
});
|
303
212
|
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
);
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
/**
|
325
|
-
* @type {(
|
326
|
-
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &
|
327
|
-
* (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)
|
328
|
-
* )}
|
329
|
-
*/
|
330
|
-
/**
|
331
|
-
* @param {Node} tree
|
332
|
-
* @param {Test} test
|
333
|
-
* @param {Visitor} visitor
|
334
|
-
* @param {boolean | null | undefined} [reverse]
|
335
|
-
* @returns {void}
|
336
|
-
*/
|
337
|
-
function(tree, test, visitor, reverse) {
|
338
|
-
if (typeof test === "function" && typeof visitor !== "function") {
|
339
|
-
reverse = visitor;
|
340
|
-
visitor = test;
|
341
|
-
test = null;
|
342
|
-
}
|
343
|
-
visitParents(tree, test, overload, reverse);
|
344
|
-
function overload(node, parents) {
|
345
|
-
const parent = parents[parents.length - 1];
|
346
|
-
return visitor(
|
347
|
-
node,
|
348
|
-
parent ? parent.children.indexOf(node) : null,
|
349
|
-
parent
|
350
|
-
);
|
351
|
-
}
|
352
|
-
}
|
353
|
-
);
|
354
|
-
|
355
|
-
// src/ast-helpers.ts
|
356
|
-
var getASTNodeImport = (name, from) => ({
|
357
|
-
type: "mdxjsEsm",
|
358
|
-
value: `import ${name} from ${JSON.stringify(from)}`,
|
359
|
-
data: {
|
360
|
-
estree: {
|
361
|
-
type: "Program",
|
362
|
-
sourceType: "module",
|
363
|
-
body: [
|
364
|
-
{
|
365
|
-
type: "ImportDeclaration",
|
366
|
-
specifiers: [
|
367
|
-
{
|
368
|
-
type: "ImportDefaultSpecifier",
|
369
|
-
local: { type: "Identifier", name }
|
213
|
+
return visit;
|
214
|
+
function visit() {
|
215
|
+
let result = [];
|
216
|
+
let subresult;
|
217
|
+
let offset;
|
218
|
+
let grandparents;
|
219
|
+
if (!test || is(node, index, parents[parents.length - 1] || null)) {
|
220
|
+
result = toResult(visitor(node, parents));
|
221
|
+
if (result[0] === EXIT) return result;
|
222
|
+
}
|
223
|
+
if (node.children && result[0] !== SKIP) {
|
224
|
+
offset = (reverse ? node.children.length : -1) + step;
|
225
|
+
grandparents = parents.concat(node);
|
226
|
+
while(offset > -1 && offset < node.children.length){
|
227
|
+
subresult = factory(node.children[offset], offset, grandparents)();
|
228
|
+
if (subresult[0] === EXIT) return subresult;
|
229
|
+
offset = 'number' == typeof subresult[1] ? subresult[1] : offset + step;
|
230
|
+
}
|
231
|
+
}
|
232
|
+
return result;
|
370
233
|
}
|
371
|
-
],
|
372
|
-
source: {
|
373
|
-
type: "Literal",
|
374
|
-
value: from,
|
375
|
-
raw: `${JSON.stringify(from)}`
|
376
|
-
}
|
377
234
|
}
|
378
|
-
|
235
|
+
};
|
236
|
+
function toResult(value) {
|
237
|
+
if (Array.isArray(value)) return value;
|
238
|
+
if ('number' == typeof value) return [
|
239
|
+
CONTINUE,
|
240
|
+
value
|
241
|
+
];
|
242
|
+
return [
|
243
|
+
value
|
244
|
+
];
|
379
245
|
}
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
{
|
400
|
-
type: "mdxJsxAttribute",
|
401
|
-
name: "className",
|
402
|
-
value: "language-tsx"
|
403
|
-
},
|
404
|
-
{
|
405
|
-
type: "mdxJsxAttribute",
|
406
|
-
name: "children",
|
407
|
-
value: {
|
408
|
-
type: "mdxJsxExpressionAttribute",
|
409
|
-
value: tempVar,
|
410
|
-
data: {
|
411
|
-
estree: {
|
412
|
-
type: "Program",
|
246
|
+
const lib_visit = function(tree, test, visitor, reverse) {
|
247
|
+
if ('function' == typeof test && 'function' != typeof visitor) {
|
248
|
+
reverse = visitor;
|
249
|
+
visitor = test;
|
250
|
+
test = null;
|
251
|
+
}
|
252
|
+
visitParents(tree, test, overload, reverse);
|
253
|
+
function overload(node, parents) {
|
254
|
+
const parent = parents[parents.length - 1];
|
255
|
+
return visitor(node, parent ? parent.children.indexOf(node) : null, parent);
|
256
|
+
}
|
257
|
+
};
|
258
|
+
const getASTNodeImport = (name, from)=>({
|
259
|
+
type: 'mdxjsEsm',
|
260
|
+
value: `import ${name} from ${JSON.stringify(from)}`,
|
261
|
+
data: {
|
262
|
+
estree: {
|
263
|
+
type: 'Program',
|
264
|
+
sourceType: 'module',
|
413
265
|
body: [
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
266
|
+
{
|
267
|
+
type: 'ImportDeclaration',
|
268
|
+
specifiers: [
|
269
|
+
{
|
270
|
+
type: 'ImportDefaultSpecifier',
|
271
|
+
local: {
|
272
|
+
type: 'Identifier',
|
273
|
+
name
|
274
|
+
}
|
275
|
+
}
|
276
|
+
],
|
277
|
+
source: {
|
278
|
+
type: 'Literal',
|
279
|
+
value: from,
|
280
|
+
raw: `${JSON.stringify(from)}`
|
281
|
+
}
|
419
282
|
}
|
420
|
-
}
|
421
283
|
]
|
422
|
-
}
|
423
284
|
}
|
424
|
-
}
|
425
285
|
}
|
426
|
-
]
|
427
|
-
}
|
428
|
-
]
|
429
|
-
}
|
430
|
-
]
|
431
|
-
});
|
432
|
-
|
433
|
-
// src/remarkPlugin.ts
|
434
|
-
var demos = {};
|
435
|
-
var remarkCodeToDemo = function({
|
436
|
-
getRouteMeta,
|
437
|
-
previewMode,
|
438
|
-
defaultRenderMode,
|
439
|
-
position,
|
440
|
-
previewLanguages,
|
441
|
-
previewCodeTransform
|
442
|
-
}) {
|
443
|
-
const routeMeta2 = getRouteMeta();
|
444
|
-
import_fs_extra.default.ensureDirSync(virtualDir);
|
445
|
-
const data = this.data();
|
446
|
-
return (tree, vfile) => {
|
447
|
-
const demoMdx = [];
|
448
|
-
const route = routeMeta2.find(
|
449
|
-
(meta) => (0, import_shared2.normalizePosixPath)(meta.absolutePath) === (0, import_shared2.normalizePosixPath)(vfile.path || vfile.history[0])
|
450
|
-
);
|
451
|
-
if (!route) {
|
452
|
-
return;
|
453
|
-
}
|
454
|
-
const { pageName } = route;
|
455
|
-
demos[pageName] = [];
|
456
|
-
let title = pageName;
|
457
|
-
let index = 1;
|
458
|
-
let externalDemoIndex = 0;
|
459
|
-
function constructDemoNode(demoId, demoPath, currentNode, isMobileMode, externalDemoIndex2) {
|
460
|
-
if (isMobileMode) {
|
461
|
-
const relativePathReg = new RegExp(/^\.\.?\/.*$/);
|
462
|
-
demos[pageName].push({
|
463
|
-
title,
|
464
|
-
id: demoId,
|
465
|
-
path: relativePathReg.test(demoPath) ? (0, import_node_path3.resolve)(vfile.dirname || (0, import_node_path3.dirname)(vfile.path), demoPath) : demoPath
|
466
286
|
});
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
287
|
+
const getExternalDemoContent = (tempVar)=>({
|
288
|
+
type: 'mdxJsxFlowElement',
|
289
|
+
name: '',
|
290
|
+
attributes: [],
|
291
|
+
children: [
|
292
|
+
{
|
293
|
+
type: 'mdxJsxFlowElement',
|
294
|
+
name: 'pre',
|
295
|
+
attributes: [],
|
296
|
+
children: [
|
297
|
+
{
|
298
|
+
type: 'mdxJsxFlowElement',
|
299
|
+
name: 'code',
|
300
|
+
attributes: [
|
301
|
+
{
|
302
|
+
type: 'mdxJsxAttribute',
|
303
|
+
name: 'className',
|
304
|
+
value: 'language-tsx'
|
305
|
+
},
|
306
|
+
{
|
307
|
+
type: 'mdxJsxAttribute',
|
308
|
+
name: 'children',
|
309
|
+
value: {
|
310
|
+
type: 'mdxJsxExpressionAttribute',
|
311
|
+
value: tempVar,
|
312
|
+
data: {
|
313
|
+
estree: {
|
314
|
+
type: 'Program',
|
315
|
+
body: [
|
316
|
+
{
|
317
|
+
type: 'ExpressionStatement',
|
318
|
+
expression: {
|
319
|
+
type: 'Identifier',
|
320
|
+
name: tempVar
|
321
|
+
}
|
322
|
+
}
|
323
|
+
]
|
324
|
+
}
|
325
|
+
}
|
326
|
+
}
|
327
|
+
}
|
328
|
+
]
|
329
|
+
}
|
330
|
+
]
|
331
|
+
}
|
332
|
+
]
|
505
333
|
});
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
});
|
570
|
-
tree.children.unshift(...demoMdx);
|
571
|
-
if (demos[pageName].length > 0) {
|
572
|
-
data.pageMeta.haveDemos = true;
|
573
|
-
}
|
574
|
-
};
|
575
|
-
};
|
576
|
-
|
577
|
-
// src/index.ts
|
578
|
-
var routeMeta;
|
579
|
-
var DEFAULT_PREVIEW_LANGUAGES = ["jsx", "tsx"];
|
580
|
-
function pluginPreview(options) {
|
581
|
-
const {
|
582
|
-
isMobile = false,
|
583
|
-
iframeOptions = {},
|
584
|
-
iframePosition = "follow",
|
585
|
-
defaultRenderMode = "preview",
|
586
|
-
previewLanguages = DEFAULT_PREVIEW_LANGUAGES,
|
587
|
-
previewCodeTransform = ({ code }) => code
|
588
|
-
} = options ?? {};
|
589
|
-
const previewMode = options?.previewMode ?? (isMobile ? "iframe" : "internal");
|
590
|
-
const {
|
591
|
-
devPort = 7890,
|
592
|
-
framework = "react",
|
593
|
-
position = iframePosition,
|
594
|
-
builderConfig = {}
|
595
|
-
} = iframeOptions;
|
596
|
-
const globalUIComponents = position === "fixed" ? [(0, import_node_path4.join)(staticPath, "global-components", "Device.tsx")] : [];
|
597
|
-
const getRouteMeta = () => routeMeta;
|
598
|
-
let lastDemos;
|
599
|
-
let devServer;
|
600
|
-
let clientConfig;
|
601
|
-
const port = devPort;
|
602
|
-
return {
|
603
|
-
name: "@rspress/plugin-preview",
|
604
|
-
config(config) {
|
605
|
-
config.markdown = config.markdown || {};
|
606
|
-
config.markdown.mdxRs = false;
|
607
|
-
return config;
|
608
|
-
},
|
609
|
-
routeGenerated(routes) {
|
610
|
-
routeMeta = routes;
|
611
|
-
},
|
612
|
-
async beforeBuild(_, isProd) {
|
613
|
-
if (!isProd) {
|
614
|
-
try {
|
615
|
-
await new Promise((resolve2, reject) => {
|
616
|
-
const server = import_node_net.default.createServer();
|
617
|
-
server.unref();
|
618
|
-
server.on("error", reject);
|
619
|
-
server.listen({ port, host: "0.0.0.0" }, () => {
|
620
|
-
server.close(resolve2);
|
334
|
+
const remarkPlugin_demos = {};
|
335
|
+
const remarkCodeToDemo = function({ getRouteMeta, previewMode, defaultRenderMode, position, previewLanguages, previewCodeTransform }) {
|
336
|
+
const routeMeta = getRouteMeta();
|
337
|
+
fs_extra_default().ensureDirSync(virtualDir);
|
338
|
+
const data = this.data();
|
339
|
+
return (tree, vfile)=>{
|
340
|
+
const demoMdx = [];
|
341
|
+
const route = routeMeta.find((meta)=>(0, shared_namespaceObject.normalizePosixPath)(meta.absolutePath) === (0, shared_namespaceObject.normalizePosixPath)(vfile.path || vfile.history[0]));
|
342
|
+
if (!route) return;
|
343
|
+
const { pageName } = route;
|
344
|
+
remarkPlugin_demos[pageName] = [];
|
345
|
+
let title = pageName;
|
346
|
+
let index = 1;
|
347
|
+
let externalDemoIndex = 0;
|
348
|
+
function constructDemoNode(demoId, demoPath, currentNode, isMobileMode, externalDemoIndex) {
|
349
|
+
if (isMobileMode) {
|
350
|
+
const relativePathReg = new RegExp(/^\.\.?\/.*$/);
|
351
|
+
remarkPlugin_demos[pageName].push({
|
352
|
+
title,
|
353
|
+
id: demoId,
|
354
|
+
path: relativePathReg.test(demoPath) ? (0, external_node_path_namespaceObject.resolve)(vfile.dirname || (0, external_node_path_namespaceObject.dirname)(vfile.path), demoPath) : demoPath
|
355
|
+
});
|
356
|
+
} else demoMdx.push(getASTNodeImport(`Demo${demoId}`, demoPath));
|
357
|
+
const tempVar = `externalDemoContent${externalDemoIndex}`;
|
358
|
+
if (void 0 !== externalDemoIndex) demoMdx.push(getASTNodeImport(tempVar, `!!${demoPath}?raw`));
|
359
|
+
if (isMobileMode && 'fixed' === position) void 0 !== externalDemoIndex && Object.assign(currentNode, getExternalDemoContent(tempVar));
|
360
|
+
else Object.assign(currentNode, {
|
361
|
+
type: 'mdxJsxFlowElement',
|
362
|
+
name: 'Container',
|
363
|
+
attributes: [
|
364
|
+
{
|
365
|
+
type: 'mdxJsxAttribute',
|
366
|
+
name: 'isMobile',
|
367
|
+
value: isMobileMode
|
368
|
+
},
|
369
|
+
{
|
370
|
+
type: 'mdxJsxAttribute',
|
371
|
+
name: 'demoId',
|
372
|
+
value: demoId
|
373
|
+
}
|
374
|
+
],
|
375
|
+
children: [
|
376
|
+
void 0 === externalDemoIndex ? {
|
377
|
+
...currentNode,
|
378
|
+
hasVisited: true
|
379
|
+
} : getExternalDemoContent(tempVar),
|
380
|
+
isMobileMode ? {
|
381
|
+
type: 'mdxJsxFlowElement',
|
382
|
+
name: null
|
383
|
+
} : {
|
384
|
+
type: 'mdxJsxFlowElement',
|
385
|
+
name: `Demo${demoId}`
|
386
|
+
}
|
387
|
+
]
|
388
|
+
});
|
389
|
+
}
|
390
|
+
lib_visit(tree, 'heading', (node)=>{
|
391
|
+
if (1 === node.depth) {
|
392
|
+
if (node.children) {
|
393
|
+
var _node_children_;
|
394
|
+
title = (null === (_node_children_ = node.children[0]) || void 0 === _node_children_ ? void 0 : _node_children_.value) || title;
|
395
|
+
}
|
396
|
+
}
|
621
397
|
});
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
async afterBuild(config, isProd) {
|
634
|
-
if ((0, import_lodash.isEqual)(demos, lastDemos)) {
|
635
|
-
return;
|
636
|
-
}
|
637
|
-
lastDemos = (0, import_lodash.cloneDeep)(demos);
|
638
|
-
await devServer?.server?.close();
|
639
|
-
devServer = void 0;
|
640
|
-
const sourceEntry = generateEntry(demos, framework, position);
|
641
|
-
const outDir = (0, import_node_path4.join)(config.outDir ?? "doc_build", "~demo");
|
642
|
-
if (Object.keys(sourceEntry).length === 0) {
|
643
|
-
return;
|
644
|
-
}
|
645
|
-
const { html, source, output, performance } = clientConfig ?? {};
|
646
|
-
const rsbuildConfig = (0, import_core.mergeRsbuildConfig)(
|
647
|
-
{
|
648
|
-
dev: {
|
649
|
-
progressBar: false
|
650
|
-
},
|
651
|
-
server: {
|
652
|
-
port: devPort,
|
653
|
-
printUrls: () => void 0,
|
654
|
-
strictPort: true
|
655
|
-
},
|
656
|
-
performance: {
|
657
|
-
...performance,
|
658
|
-
printFileSize: false
|
659
|
-
},
|
660
|
-
html,
|
661
|
-
source: {
|
662
|
-
...source,
|
663
|
-
entry: sourceEntry
|
664
|
-
},
|
665
|
-
output: {
|
666
|
-
...output,
|
667
|
-
assetPrefix: output?.assetPrefix ? `${(0, import_shared3.removeTrailingSlash)(output.assetPrefix)}/~demo` : "/~demo",
|
668
|
-
distPath: {
|
669
|
-
root: outDir
|
670
|
-
},
|
671
|
-
// not copy files again
|
672
|
-
copy: void 0
|
673
|
-
},
|
674
|
-
plugins: config?.builderPlugins
|
675
|
-
},
|
676
|
-
builderConfig
|
677
|
-
);
|
678
|
-
const rsbuildInstance = await (0, import_core.createRsbuild)({
|
679
|
-
rsbuildConfig
|
680
|
-
});
|
681
|
-
const { pluginSass } = await import("@rsbuild/plugin-sass");
|
682
|
-
const { pluginLess } = await import("@rsbuild/plugin-less");
|
683
|
-
rsbuildInstance.addPlugins([pluginSass(), pluginLess()]);
|
684
|
-
if (framework === "solid") {
|
685
|
-
rsbuildInstance.addPlugins([
|
686
|
-
(0, import_plugin_babel.pluginBabel)({
|
687
|
-
include: /\.(?:jsx|tsx)$/
|
688
|
-
}),
|
689
|
-
(0, import_plugin_solid.pluginSolid)()
|
690
|
-
]);
|
691
|
-
}
|
692
|
-
if (framework === "react") {
|
693
|
-
rsbuildInstance.addPlugins([(0, import_plugin_react.pluginReact)()]);
|
694
|
-
}
|
695
|
-
if (isProd) {
|
696
|
-
rsbuildInstance.build();
|
697
|
-
} else {
|
698
|
-
devServer = await rsbuildInstance.startDevServer();
|
699
|
-
}
|
700
|
-
},
|
701
|
-
builderConfig: {
|
702
|
-
source: {
|
703
|
-
include: [(0, import_node_path4.join)(__dirname, "..")]
|
704
|
-
},
|
705
|
-
tools: {
|
706
|
-
bundlerChain(chain) {
|
707
|
-
chain.module.rule("Raw").resourceQuery(/raw/).type("asset/source").end();
|
708
|
-
chain.resolve.extensions.prepend(".md").prepend(".mdx");
|
709
|
-
},
|
710
|
-
rspack: {
|
711
|
-
watchOptions: {
|
712
|
-
ignored: /\.git/
|
713
|
-
}
|
714
|
-
}
|
715
|
-
},
|
716
|
-
plugins: [
|
717
|
-
{
|
718
|
-
name: "close-demo-server",
|
719
|
-
setup: (api) => {
|
720
|
-
api.modifyRsbuildConfig((config) => {
|
721
|
-
if (config.output?.target === "web") {
|
722
|
-
clientConfig = config;
|
723
|
-
}
|
398
|
+
lib_visit(tree, 'mdxJsxFlowElement', (node)=>{
|
399
|
+
if ('code' === node.name) {
|
400
|
+
var _node_attributes_find, _node_attributes_find1, _node_attributes_find2;
|
401
|
+
const src = null === (_node_attributes_find = node.attributes.find((attr)=>'src' === attr.name)) || void 0 === _node_attributes_find ? void 0 : _node_attributes_find.value;
|
402
|
+
if (!src) return;
|
403
|
+
const currentMode = (null === (_node_attributes_find1 = node.attributes.find((attr)=>'previewMode' === attr.name)) || void 0 === _node_attributes_find1 ? void 0 : _node_attributes_find1.value) ?? previewMode;
|
404
|
+
let isMobileMode = null === (_node_attributes_find2 = node.attributes.find((attr)=>'isMobile' === attr.name)) || void 0 === _node_attributes_find2 ? void 0 : _node_attributes_find2.value;
|
405
|
+
isMobileMode = void 0 === isMobileMode ? 'iframe' === currentMode : null === isMobileMode || ('object' == typeof isMobileMode ? 'false' !== isMobileMode.value : 'false' !== isMobileMode);
|
406
|
+
const id = generateId(pageName, index++);
|
407
|
+
constructDemoNode(id, src, node, isMobileMode, externalDemoIndex++);
|
408
|
+
}
|
724
409
|
});
|
725
|
-
|
726
|
-
|
727
|
-
|
410
|
+
lib_visit(tree, 'code', (node)=>{
|
411
|
+
if ('hasVisited' in node) return;
|
412
|
+
if (node.lang && previewLanguages.includes(node.lang)) {
|
413
|
+
var _node_meta, _node_meta1, _node_meta2, _node_meta3, _node_meta4, _node_meta5;
|
414
|
+
if ((null == node ? void 0 : null === (_node_meta = node.meta) || void 0 === _node_meta ? void 0 : _node_meta.includes('pure')) || !(null == node ? void 0 : null === (_node_meta1 = node.meta) || void 0 === _node_meta1 ? void 0 : _node_meta1.includes('preview')) && 'pure' === defaultRenderMode) return;
|
415
|
+
const value = injectDemoBlockImport(previewCodeTransform({
|
416
|
+
language: node.lang,
|
417
|
+
code: node.value
|
418
|
+
}), demoBlockComponentPath);
|
419
|
+
const isMobileMode = (null == node ? void 0 : null === (_node_meta2 = node.meta) || void 0 === _node_meta2 ? void 0 : _node_meta2.includes('mobile')) || (null == node ? void 0 : null === (_node_meta3 = node.meta) || void 0 === _node_meta3 ? void 0 : _node_meta3.includes('iframe')) || !(null == node ? void 0 : null === (_node_meta4 = node.meta) || void 0 === _node_meta4 ? void 0 : _node_meta4.includes('web')) && !(null == node ? void 0 : null === (_node_meta5 = node.meta) || void 0 === _node_meta5 ? void 0 : _node_meta5.includes('internal')) && 'iframe' === previewMode;
|
420
|
+
const id = generateId(pageName, index++);
|
421
|
+
const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.tsx`);
|
422
|
+
constructDemoNode(id, virtualModulePath, node, isMobileMode);
|
423
|
+
if (fs_extra_default().existsSync(virtualModulePath)) {
|
424
|
+
const content = fs_extra_default().readFileSync(virtualModulePath, 'utf-8');
|
425
|
+
if (content === value) return;
|
426
|
+
}
|
427
|
+
fs_extra_default().writeFileSync(virtualModulePath, value);
|
428
|
+
}
|
728
429
|
});
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
}
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
430
|
+
tree.children.unshift(...demoMdx);
|
431
|
+
if (remarkPlugin_demos[pageName].length > 0) data.pageMeta.haveDemos = true;
|
432
|
+
};
|
433
|
+
};
|
434
|
+
let src_rslib_entry_routeMeta;
|
435
|
+
const DEFAULT_PREVIEW_LANGUAGES = [
|
436
|
+
'jsx',
|
437
|
+
'tsx'
|
438
|
+
];
|
439
|
+
function pluginPreview(options) {
|
440
|
+
const { isMobile = false, iframeOptions = {}, iframePosition = 'follow', defaultRenderMode = 'preview', previewLanguages = DEFAULT_PREVIEW_LANGUAGES, previewCodeTransform = ({ code })=>code } = options ?? {};
|
441
|
+
const previewMode = (null == options ? void 0 : options.previewMode) ?? (isMobile ? 'iframe' : 'internal');
|
442
|
+
const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {} } = iframeOptions;
|
443
|
+
const globalUIComponents = 'fixed' === position ? [
|
444
|
+
(0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Device.tsx')
|
445
|
+
] : [];
|
446
|
+
const getRouteMeta = ()=>src_rslib_entry_routeMeta;
|
447
|
+
let lastDemos;
|
448
|
+
let devServer;
|
449
|
+
let clientConfig;
|
450
|
+
const port = devPort;
|
451
|
+
return {
|
452
|
+
name: '@rspress/plugin-preview',
|
453
|
+
config (config) {
|
454
|
+
config.markdown = config.markdown || {};
|
455
|
+
config.markdown.mdxRs = false;
|
456
|
+
return config;
|
457
|
+
},
|
458
|
+
routeGenerated (routes) {
|
459
|
+
src_rslib_entry_routeMeta = routes;
|
460
|
+
},
|
461
|
+
async beforeBuild (_, isProd) {
|
462
|
+
if (!isProd) try {
|
463
|
+
await new Promise((resolve, reject)=>{
|
464
|
+
const server = external_node_net_default().createServer();
|
465
|
+
server.unref();
|
466
|
+
server.on('error', reject);
|
467
|
+
server.listen({
|
468
|
+
port,
|
469
|
+
host: '0.0.0.0'
|
470
|
+
}, ()=>{
|
471
|
+
server.close(resolve);
|
472
|
+
});
|
473
|
+
});
|
474
|
+
} catch (e) {
|
475
|
+
if ('EADDRINUSE' !== e.code) throw e;
|
476
|
+
throw new Error(`Port "${port}" is occupied, please choose another one.`);
|
477
|
+
}
|
478
|
+
},
|
479
|
+
async afterBuild (config, isProd) {
|
480
|
+
var _devServer_server;
|
481
|
+
if ((0, external_lodash_namespaceObject.isEqual)(remarkPlugin_demos, lastDemos)) return;
|
482
|
+
lastDemos = (0, external_lodash_namespaceObject.cloneDeep)(remarkPlugin_demos);
|
483
|
+
await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
|
484
|
+
devServer = void 0;
|
485
|
+
const sourceEntry = generateEntry(remarkPlugin_demos, framework, position);
|
486
|
+
const outDir = (0, external_node_path_namespaceObject.join)(config.outDir ?? 'doc_build', '~demo');
|
487
|
+
if (0 === Object.keys(sourceEntry).length) return;
|
488
|
+
const { html, source, output, performance } = clientConfig ?? {};
|
489
|
+
const rsbuildConfig = (0, core_namespaceObject.mergeRsbuildConfig)({
|
490
|
+
dev: {
|
491
|
+
progressBar: false
|
492
|
+
},
|
493
|
+
server: {
|
494
|
+
port: devPort,
|
495
|
+
printUrls: ()=>void 0,
|
496
|
+
strictPort: true
|
497
|
+
},
|
498
|
+
performance: {
|
499
|
+
...performance,
|
500
|
+
printFileSize: false
|
501
|
+
},
|
502
|
+
html,
|
503
|
+
source: {
|
504
|
+
...source,
|
505
|
+
entry: sourceEntry
|
506
|
+
},
|
507
|
+
output: {
|
508
|
+
...output,
|
509
|
+
assetPrefix: (null == output ? void 0 : output.assetPrefix) ? `${(0, shared_namespaceObject.removeTrailingSlash)(output.assetPrefix)}/~demo` : '/~demo',
|
510
|
+
distPath: {
|
511
|
+
root: outDir
|
512
|
+
},
|
513
|
+
copy: void 0
|
514
|
+
},
|
515
|
+
plugins: null == config ? void 0 : config.builderPlugins
|
516
|
+
}, builderConfig);
|
517
|
+
const rsbuildInstance = await (0, core_namespaceObject.createRsbuild)({
|
518
|
+
rsbuildConfig
|
519
|
+
});
|
520
|
+
const { pluginSass } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@rsbuild/plugin-sass"));
|
521
|
+
const { pluginLess } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@rsbuild/plugin-less"));
|
522
|
+
rsbuildInstance.addPlugins([
|
523
|
+
pluginSass(),
|
524
|
+
pluginLess()
|
525
|
+
]);
|
526
|
+
if ('solid' === framework) rsbuildInstance.addPlugins([
|
527
|
+
(0, plugin_babel_namespaceObject.pluginBabel)({
|
528
|
+
include: /\.(?:jsx|tsx)$/
|
529
|
+
}),
|
530
|
+
(0, plugin_solid_namespaceObject.pluginSolid)()
|
531
|
+
]);
|
532
|
+
if ('react' === framework) rsbuildInstance.addPlugins([
|
533
|
+
(0, plugin_react_namespaceObject.pluginReact)()
|
534
|
+
]);
|
535
|
+
if (isProd) rsbuildInstance.build();
|
536
|
+
else devServer = await rsbuildInstance.startDevServer();
|
537
|
+
},
|
538
|
+
builderConfig: {
|
539
|
+
source: {
|
540
|
+
include: [
|
541
|
+
(0, external_node_path_namespaceObject.join)(__dirname, '..')
|
542
|
+
]
|
543
|
+
},
|
544
|
+
tools: {
|
545
|
+
bundlerChain (chain) {
|
546
|
+
chain.module.rule('Raw').resourceQuery(/raw/).type('asset/source').end();
|
547
|
+
chain.resolve.extensions.prepend('.md').prepend('.mdx');
|
548
|
+
},
|
549
|
+
rspack: {
|
550
|
+
watchOptions: {
|
551
|
+
ignored: /\.git/
|
552
|
+
}
|
553
|
+
}
|
554
|
+
},
|
555
|
+
plugins: [
|
556
|
+
{
|
557
|
+
name: 'close-demo-server',
|
558
|
+
setup: (api)=>{
|
559
|
+
api.modifyRsbuildConfig((config)=>{
|
560
|
+
var _config_output;
|
561
|
+
if ((null === (_config_output = config.output) || void 0 === _config_output ? void 0 : _config_output.target) === 'web') clientConfig = config;
|
562
|
+
});
|
563
|
+
api.onCloseDevServer(async ()=>{
|
564
|
+
var _devServer_server;
|
565
|
+
await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
|
566
|
+
devServer = void 0;
|
567
|
+
});
|
568
|
+
}
|
569
|
+
}
|
570
|
+
]
|
571
|
+
},
|
572
|
+
extendPageData (pageData, isProd) {
|
573
|
+
if (!isProd) pageData.devPort = port;
|
574
|
+
pageData.extraHighlightLanguages = previewLanguages;
|
575
|
+
},
|
576
|
+
markdown: {
|
577
|
+
remarkPlugins: [
|
578
|
+
[
|
579
|
+
remarkCodeToDemo,
|
580
|
+
{
|
581
|
+
getRouteMeta,
|
582
|
+
position,
|
583
|
+
previewMode,
|
584
|
+
defaultRenderMode,
|
585
|
+
previewLanguages,
|
586
|
+
previewCodeTransform
|
587
|
+
}
|
588
|
+
]
|
589
|
+
],
|
590
|
+
globalComponents: [
|
591
|
+
(0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Container.tsx')
|
592
|
+
]
|
593
|
+
},
|
594
|
+
globalUIComponents,
|
595
|
+
globalStyles: (0, external_node_path_namespaceObject.join)(staticPath, 'global-styles', `${previewMode}.css`)
|
596
|
+
};
|
597
|
+
}
|
598
|
+
})();
|
599
|
+
var __webpack_export_target__ = exports;
|
600
|
+
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
601
|
+
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
602
|
+
value: true
|
764
603
|
});
|
765
|
-
|
766
|
-
//# sourceMappingURL=index.js.map
|