@rspress/plugin-preview 1.40.2 → 1.41.0
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 +545 -707
- package/dist/utils.d.ts +19 -16
- package/dist/utils.js +43 -48
- package/package.json +10 -9
- 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,604 @@
|
|
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
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
77
|
+
const toValidVarName = (str)=>{
|
78
|
+
if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) return str;
|
79
|
+
return str.replace(/[^0-9a-zA-Z_$]/g, '_').replace(/^([0-9])/, '_$1');
|
80
|
+
};
|
81
|
+
const generateId = (pageName, index)=>`_${toValidVarName(pageName)}_${index}`;
|
82
|
+
const injectDemoBlockImport = (str, path)=>`
|
83
|
+
import DemoBlock from ${JSON.stringify(path)};
|
78
84
|
${str}
|
79
85
|
`;
|
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 = `
|
86
|
+
function generateEntry(demos, framework, position) {
|
87
|
+
const sourceEntry = {};
|
88
|
+
const entryCssPath = (0, external_node_path_namespaceObject.join)(staticPath, 'global-styles', 'entry.css');
|
89
|
+
if ('follow' === position) Object.values(demos).forEach((routes)=>{
|
90
|
+
routes.forEach((route)=>{
|
91
|
+
const { id, path: demoPath } = route;
|
92
|
+
const entry = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.entry.tsx`);
|
93
|
+
const solidEntry = `
|
92
94
|
import { render } from 'solid-js/web';
|
93
95
|
import ${JSON.stringify(entryCssPath)};
|
94
96
|
import Demo from ${JSON.stringify(demoPath)};
|
95
97
|
render(() => <Demo />, document.getElementById('root'));
|
96
98
|
`;
|
97
|
-
|
99
|
+
const reactEntry = `
|
98
100
|
import { render } from 'react-dom';
|
99
101
|
import ${JSON.stringify(entryCssPath)};
|
100
102
|
import Demo from ${JSON.stringify(demoPath)};
|
101
103
|
render(<Demo />, document.getElementById('root'));
|
102
104
|
`;
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
return;
|
112
|
-
}
|
113
|
-
const reactContent = `
|
105
|
+
const entryContent = 'react' === framework ? reactEntry : solidEntry;
|
106
|
+
(0, external_node_fs_namespaceObject.writeFileSync)(entry, entryContent);
|
107
|
+
sourceEntry[id] = entry;
|
108
|
+
});
|
109
|
+
});
|
110
|
+
else Object.entries(demos).forEach(([key, routes])=>{
|
111
|
+
if (0 === routes.length) return;
|
112
|
+
const reactContent = `
|
114
113
|
import { render } from 'react-dom';
|
115
114
|
import ${JSON.stringify(entryCssPath)};
|
116
|
-
${routes.map((demo, index)
|
117
|
-
return `import Demo_${index} from ${JSON.stringify(demo.path)}`;
|
118
|
-
}).join("\n")}
|
115
|
+
${routes.map((demo, index)=>`import Demo_${index} from ${JSON.stringify(demo.path)}`).join('\n')}
|
119
116
|
function App() {
|
120
117
|
return (
|
121
118
|
<div className="preview-container">
|
122
119
|
<div className="preview-nav">{"${routes[0].title}"}</div>
|
123
|
-
${routes.map((demo, index)
|
124
|
-
return `<Demo_${index} />`;
|
125
|
-
}).join("\n")}
|
120
|
+
${routes.map((demo, index)=>`<Demo_${index} />`).join('\n')}
|
126
121
|
</div>
|
127
122
|
)
|
128
123
|
}
|
129
124
|
render(<App /> , document.getElementById('root'));
|
130
125
|
`;
|
131
|
-
|
126
|
+
const solidContent = `
|
132
127
|
import { render } from 'solid-js/web';
|
133
128
|
import ${JSON.stringify(entryCssPath)};
|
134
|
-
${routes.map((demo, index)
|
135
|
-
return `import Demo_${index} from ${JSON.stringify(demo.path)}`;
|
136
|
-
}).join("\n")}
|
129
|
+
${routes.map((demo, index)=>`import Demo_${index} from ${JSON.stringify(demo.path)}`).join('\n')}
|
137
130
|
function App() {
|
138
131
|
return (
|
139
132
|
<div class="preview-container">
|
140
133
|
<div class="preview-nav">{"${routes[0].title}"}</div>
|
141
|
-
${routes.map((_, index)
|
142
|
-
return `<Demo_${index} />`;
|
143
|
-
}).join("\n")}
|
134
|
+
${routes.map((_, index)=>`<Demo_${index} />`).join('\n')}
|
144
135
|
</div>
|
145
136
|
)
|
146
137
|
}
|
147
138
|
render(() => <App /> , document.getElementById('root'));
|
148
139
|
`;
|
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;
|
140
|
+
const renderContent = 'solid' === framework ? solidContent : reactContent;
|
141
|
+
const id = `_${toValidVarName(key)}`;
|
142
|
+
const entry = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.entry.tsx`);
|
143
|
+
(0, external_node_fs_namespaceObject.writeFileSync)(entry, renderContent);
|
144
|
+
sourceEntry[id] = entry;
|
145
|
+
});
|
146
|
+
return sourceEntry;
|
179
147
|
}
|
180
|
-
|
181
|
-
|
148
|
+
const convert = function(test) {
|
149
|
+
if (null == test) return ok;
|
150
|
+
if ('string' == typeof test) return typeFactory(test);
|
151
|
+
if ('object' == typeof test) return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
|
152
|
+
if ('function' == typeof test) return castFactory(test);
|
153
|
+
throw new Error('Expected function, string, or object as test');
|
154
|
+
};
|
155
|
+
function anyFactory(tests) {
|
156
|
+
const checks = [];
|
157
|
+
let index = -1;
|
158
|
+
while(++index < tests.length)checks[index] = convert(tests[index]);
|
159
|
+
return castFactory(any);
|
160
|
+
function any(...parameters) {
|
161
|
+
let index = -1;
|
162
|
+
while(++index < checks.length)if (checks[index].call(this, ...parameters)) return true;
|
163
|
+
return false;
|
164
|
+
}
|
182
165
|
}
|
183
|
-
|
184
|
-
|
166
|
+
function propsFactory(check) {
|
167
|
+
return castFactory(all);
|
168
|
+
function all(node) {
|
169
|
+
let key;
|
170
|
+
for(key in check)if (node[key] !== check[key]) return false;
|
171
|
+
return true;
|
172
|
+
}
|
185
173
|
}
|
186
|
-
|
187
|
-
|
174
|
+
function typeFactory(check) {
|
175
|
+
return castFactory(type);
|
176
|
+
function type(node) {
|
177
|
+
return node && node.type === check;
|
178
|
+
}
|
188
179
|
}
|
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;
|
180
|
+
function castFactory(check) {
|
181
|
+
return assertion;
|
182
|
+
function assertion(node, ...parameters) {
|
183
|
+
return Boolean(node && 'object' == typeof node && 'type' in node && Boolean(check.call(this, node, ...parameters)));
|
184
|
+
}
|
204
185
|
}
|
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;
|
186
|
+
function ok() {
|
187
|
+
return true;
|
215
188
|
}
|
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;
|
189
|
+
function color(d) {
|
190
|
+
return '\u001B[33m' + d + '\u001B[39m';
|
266
191
|
}
|
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
|
-
}
|
192
|
+
const CONTINUE = true;
|
193
|
+
const EXIT = false;
|
194
|
+
const SKIP = 'skip';
|
195
|
+
const visitParents = function(tree, test, visitor, reverse) {
|
196
|
+
if ('function' == typeof test && 'function' != typeof visitor) {
|
197
|
+
reverse = visitor;
|
198
|
+
visitor = test;
|
199
|
+
test = null;
|
295
200
|
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
if (
|
302
|
-
|
201
|
+
const is = convert(test);
|
202
|
+
const step = reverse ? -1 : 1;
|
203
|
+
factory(tree, void 0, [])();
|
204
|
+
function factory(node, index, parents) {
|
205
|
+
const value = node && 'object' == typeof node ? node : {};
|
206
|
+
if ('string' == typeof value.type) {
|
207
|
+
const name = 'string' == typeof value.tagName ? value.tagName : 'string' == typeof value.name ? value.name : void 0;
|
208
|
+
Object.defineProperty(visit, 'name', {
|
209
|
+
value: 'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'
|
210
|
+
});
|
303
211
|
}
|
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 }
|
212
|
+
return visit;
|
213
|
+
function visit() {
|
214
|
+
let result = [];
|
215
|
+
let subresult;
|
216
|
+
let offset;
|
217
|
+
let grandparents;
|
218
|
+
if (!test || is(node, index, parents[parents.length - 1] || null)) {
|
219
|
+
result = toResult(visitor(node, parents));
|
220
|
+
if (result[0] === EXIT) return result;
|
221
|
+
}
|
222
|
+
if (node.children && result[0] !== SKIP) {
|
223
|
+
offset = (reverse ? node.children.length : -1) + step;
|
224
|
+
grandparents = parents.concat(node);
|
225
|
+
while(offset > -1 && offset < node.children.length){
|
226
|
+
subresult = factory(node.children[offset], offset, grandparents)();
|
227
|
+
if (subresult[0] === EXIT) return subresult;
|
228
|
+
offset = 'number' == typeof subresult[1] ? subresult[1] : offset + step;
|
229
|
+
}
|
230
|
+
}
|
231
|
+
return result;
|
370
232
|
}
|
371
|
-
],
|
372
|
-
source: {
|
373
|
-
type: "Literal",
|
374
|
-
value: from,
|
375
|
-
raw: `${JSON.stringify(from)}`
|
376
|
-
}
|
377
233
|
}
|
378
|
-
|
234
|
+
};
|
235
|
+
function toResult(value) {
|
236
|
+
if (Array.isArray(value)) return value;
|
237
|
+
if ('number' == typeof value) return [
|
238
|
+
CONTINUE,
|
239
|
+
value
|
240
|
+
];
|
241
|
+
return [
|
242
|
+
value
|
243
|
+
];
|
379
244
|
}
|
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",
|
245
|
+
const lib_visit = function(tree, test, visitor, reverse) {
|
246
|
+
if ('function' == typeof test && 'function' != typeof visitor) {
|
247
|
+
reverse = visitor;
|
248
|
+
visitor = test;
|
249
|
+
test = null;
|
250
|
+
}
|
251
|
+
visitParents(tree, test, overload, reverse);
|
252
|
+
function overload(node, parents) {
|
253
|
+
const parent = parents[parents.length - 1];
|
254
|
+
return visitor(node, parent ? parent.children.indexOf(node) : null, parent);
|
255
|
+
}
|
256
|
+
};
|
257
|
+
const getASTNodeImport = (name, from)=>({
|
258
|
+
type: 'mdxjsEsm',
|
259
|
+
value: `import ${name} from ${JSON.stringify(from)}`,
|
260
|
+
data: {
|
261
|
+
estree: {
|
262
|
+
type: 'Program',
|
263
|
+
sourceType: 'module',
|
413
264
|
body: [
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
265
|
+
{
|
266
|
+
type: 'ImportDeclaration',
|
267
|
+
specifiers: [
|
268
|
+
{
|
269
|
+
type: 'ImportDefaultSpecifier',
|
270
|
+
local: {
|
271
|
+
type: 'Identifier',
|
272
|
+
name
|
273
|
+
}
|
274
|
+
}
|
275
|
+
],
|
276
|
+
source: {
|
277
|
+
type: 'Literal',
|
278
|
+
value: from,
|
279
|
+
raw: `${JSON.stringify(from)}`
|
280
|
+
}
|
419
281
|
}
|
420
|
-
}
|
421
282
|
]
|
422
|
-
}
|
423
283
|
}
|
424
|
-
}
|
425
284
|
}
|
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
285
|
});
|
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
|
-
|
286
|
+
const getExternalDemoContent = (tempVar)=>({
|
287
|
+
type: 'mdxJsxFlowElement',
|
288
|
+
name: '',
|
289
|
+
attributes: [],
|
290
|
+
children: [
|
291
|
+
{
|
292
|
+
type: 'mdxJsxFlowElement',
|
293
|
+
name: 'pre',
|
294
|
+
attributes: [],
|
295
|
+
children: [
|
296
|
+
{
|
297
|
+
type: 'mdxJsxFlowElement',
|
298
|
+
name: 'code',
|
299
|
+
attributes: [
|
300
|
+
{
|
301
|
+
type: 'mdxJsxAttribute',
|
302
|
+
name: 'className',
|
303
|
+
value: 'language-tsx'
|
304
|
+
},
|
305
|
+
{
|
306
|
+
type: 'mdxJsxAttribute',
|
307
|
+
name: 'children',
|
308
|
+
value: {
|
309
|
+
type: 'mdxJsxExpressionAttribute',
|
310
|
+
value: tempVar,
|
311
|
+
data: {
|
312
|
+
estree: {
|
313
|
+
type: 'Program',
|
314
|
+
body: [
|
315
|
+
{
|
316
|
+
type: 'ExpressionStatement',
|
317
|
+
expression: {
|
318
|
+
type: 'Identifier',
|
319
|
+
name: tempVar
|
320
|
+
}
|
321
|
+
}
|
322
|
+
]
|
323
|
+
}
|
324
|
+
}
|
325
|
+
}
|
326
|
+
}
|
327
|
+
]
|
328
|
+
}
|
329
|
+
]
|
330
|
+
}
|
331
|
+
]
|
505
332
|
});
|
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
|
-
|
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);
|
333
|
+
const remarkPlugin_demos = {};
|
334
|
+
const remarkCodeToDemo = function({ getRouteMeta, previewMode, defaultRenderMode, position, previewLanguages, previewCodeTransform }) {
|
335
|
+
const routeMeta = getRouteMeta();
|
336
|
+
external_node_fs_default().mkdirSync(virtualDir, {
|
337
|
+
recursive: true
|
338
|
+
});
|
339
|
+
const data = this.data();
|
340
|
+
return (tree, vfile)=>{
|
341
|
+
const demoMdx = [];
|
342
|
+
const route = routeMeta.find((meta)=>(0, shared_namespaceObject.normalizePosixPath)(meta.absolutePath) === (0, shared_namespaceObject.normalizePosixPath)(vfile.path || vfile.history[0]));
|
343
|
+
if (!route) return;
|
344
|
+
const { pageName } = route;
|
345
|
+
remarkPlugin_demos[pageName] = [];
|
346
|
+
let title = pageName;
|
347
|
+
let index = 1;
|
348
|
+
let externalDemoIndex = 0;
|
349
|
+
function constructDemoNode(demoId, demoPath, currentNode, isMobileMode, externalDemoIndex) {
|
350
|
+
if (isMobileMode) {
|
351
|
+
const relativePathReg = new RegExp(/^\.\.?\/.*$/);
|
352
|
+
remarkPlugin_demos[pageName].push({
|
353
|
+
title,
|
354
|
+
id: demoId,
|
355
|
+
path: relativePathReg.test(demoPath) ? (0, external_node_path_namespaceObject.resolve)(vfile.dirname || (0, external_node_path_namespaceObject.dirname)(vfile.path), demoPath) : demoPath
|
356
|
+
});
|
357
|
+
} else demoMdx.push(getASTNodeImport(`Demo${demoId}`, demoPath));
|
358
|
+
const tempVar = `externalDemoContent${externalDemoIndex}`;
|
359
|
+
if (void 0 !== externalDemoIndex) demoMdx.push(getASTNodeImport(tempVar, `!!${demoPath}?raw`));
|
360
|
+
if (isMobileMode && 'fixed' === position) void 0 !== externalDemoIndex && Object.assign(currentNode, getExternalDemoContent(tempVar));
|
361
|
+
else Object.assign(currentNode, {
|
362
|
+
type: 'mdxJsxFlowElement',
|
363
|
+
name: 'Container',
|
364
|
+
attributes: [
|
365
|
+
{
|
366
|
+
type: 'mdxJsxAttribute',
|
367
|
+
name: 'isMobile',
|
368
|
+
value: isMobileMode
|
369
|
+
},
|
370
|
+
{
|
371
|
+
type: 'mdxJsxAttribute',
|
372
|
+
name: 'demoId',
|
373
|
+
value: demoId
|
374
|
+
}
|
375
|
+
],
|
376
|
+
children: [
|
377
|
+
void 0 === externalDemoIndex ? {
|
378
|
+
...currentNode,
|
379
|
+
hasVisited: true
|
380
|
+
} : getExternalDemoContent(tempVar),
|
381
|
+
isMobileMode ? {
|
382
|
+
type: 'mdxJsxFlowElement',
|
383
|
+
name: null
|
384
|
+
} : {
|
385
|
+
type: 'mdxJsxFlowElement',
|
386
|
+
name: `Demo${demoId}`
|
387
|
+
}
|
388
|
+
]
|
389
|
+
});
|
390
|
+
}
|
391
|
+
lib_visit(tree, 'heading', (node)=>{
|
392
|
+
if (1 === node.depth) {
|
393
|
+
if (node.children) {
|
394
|
+
var _node_children_;
|
395
|
+
title = (null === (_node_children_ = node.children[0]) || void 0 === _node_children_ ? void 0 : _node_children_.value) || title;
|
396
|
+
}
|
397
|
+
}
|
621
398
|
});
|
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
|
-
}
|
399
|
+
lib_visit(tree, 'mdxJsxFlowElement', (node)=>{
|
400
|
+
if ('code' === node.name) {
|
401
|
+
var _node_attributes_find, _node_attributes_find1, _node_attributes_find2;
|
402
|
+
const src = null === (_node_attributes_find = node.attributes.find((attr)=>'src' === attr.name)) || void 0 === _node_attributes_find ? void 0 : _node_attributes_find.value;
|
403
|
+
if (!src) return;
|
404
|
+
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;
|
405
|
+
let isMobileMode = null === (_node_attributes_find2 = node.attributes.find((attr)=>'isMobile' === attr.name)) || void 0 === _node_attributes_find2 ? void 0 : _node_attributes_find2.value;
|
406
|
+
isMobileMode = void 0 === isMobileMode ? 'iframe' === currentMode : null === isMobileMode || ('object' == typeof isMobileMode ? 'false' !== isMobileMode.value : 'false' !== isMobileMode);
|
407
|
+
const id = generateId(pageName, index++);
|
408
|
+
constructDemoNode(id, src, node, isMobileMode, externalDemoIndex++);
|
409
|
+
}
|
724
410
|
});
|
725
|
-
|
726
|
-
|
727
|
-
|
411
|
+
lib_visit(tree, 'code', (node)=>{
|
412
|
+
if ('hasVisited' in node) return;
|
413
|
+
if (node.lang && previewLanguages.includes(node.lang)) {
|
414
|
+
var _node_meta, _node_meta1, _node_meta2, _node_meta3, _node_meta4, _node_meta5;
|
415
|
+
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;
|
416
|
+
const value = injectDemoBlockImport(previewCodeTransform({
|
417
|
+
language: node.lang,
|
418
|
+
code: node.value
|
419
|
+
}), demoBlockComponentPath);
|
420
|
+
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;
|
421
|
+
const id = generateId(pageName, index++);
|
422
|
+
const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.tsx`);
|
423
|
+
constructDemoNode(id, virtualModulePath, node, isMobileMode);
|
424
|
+
if (external_node_fs_default().existsSync(virtualModulePath)) {
|
425
|
+
const content = external_node_fs_default().readFileSync(virtualModulePath, 'utf-8');
|
426
|
+
if (content === value) return;
|
427
|
+
}
|
428
|
+
external_node_fs_default().writeFileSync(virtualModulePath, value);
|
429
|
+
}
|
728
430
|
});
|
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
|
-
|
431
|
+
tree.children.unshift(...demoMdx);
|
432
|
+
if (remarkPlugin_demos[pageName].length > 0) data.pageMeta.haveDemos = true;
|
433
|
+
};
|
434
|
+
};
|
435
|
+
let src_rslib_entry_routeMeta;
|
436
|
+
const DEFAULT_PREVIEW_LANGUAGES = [
|
437
|
+
'jsx',
|
438
|
+
'tsx'
|
439
|
+
];
|
440
|
+
function pluginPreview(options) {
|
441
|
+
const { isMobile = false, iframeOptions = {}, iframePosition = 'follow', defaultRenderMode = 'preview', previewLanguages = DEFAULT_PREVIEW_LANGUAGES, previewCodeTransform = ({ code })=>code } = options ?? {};
|
442
|
+
const previewMode = (null == options ? void 0 : options.previewMode) ?? (isMobile ? 'iframe' : 'internal');
|
443
|
+
const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {} } = iframeOptions;
|
444
|
+
const globalUIComponents = 'fixed' === position ? [
|
445
|
+
(0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Device.tsx')
|
446
|
+
] : [];
|
447
|
+
const getRouteMeta = ()=>src_rslib_entry_routeMeta;
|
448
|
+
let lastDemos;
|
449
|
+
let devServer;
|
450
|
+
let clientConfig;
|
451
|
+
const port = devPort;
|
452
|
+
return {
|
453
|
+
name: '@rspress/plugin-preview',
|
454
|
+
config (config) {
|
455
|
+
config.markdown = config.markdown || {};
|
456
|
+
config.markdown.mdxRs = false;
|
457
|
+
return config;
|
458
|
+
},
|
459
|
+
routeGenerated (routes) {
|
460
|
+
src_rslib_entry_routeMeta = routes;
|
461
|
+
},
|
462
|
+
async beforeBuild (_, isProd) {
|
463
|
+
if (!isProd) try {
|
464
|
+
await new Promise((resolve, reject)=>{
|
465
|
+
const server = external_node_net_default().createServer();
|
466
|
+
server.unref();
|
467
|
+
server.on('error', reject);
|
468
|
+
server.listen({
|
469
|
+
port,
|
470
|
+
host: '0.0.0.0'
|
471
|
+
}, ()=>{
|
472
|
+
server.close(resolve);
|
473
|
+
});
|
474
|
+
});
|
475
|
+
} catch (e) {
|
476
|
+
if ('EADDRINUSE' !== e.code) throw e;
|
477
|
+
throw new Error(`Port "${port}" is occupied, please choose another one.`);
|
478
|
+
}
|
479
|
+
},
|
480
|
+
async afterBuild (config, isProd) {
|
481
|
+
var _devServer_server;
|
482
|
+
if ((0, external_lodash_namespaceObject.isEqual)(remarkPlugin_demos, lastDemos)) return;
|
483
|
+
lastDemos = (0, external_lodash_namespaceObject.cloneDeep)(remarkPlugin_demos);
|
484
|
+
await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
|
485
|
+
devServer = void 0;
|
486
|
+
const sourceEntry = generateEntry(remarkPlugin_demos, framework, position);
|
487
|
+
const outDir = (0, external_node_path_namespaceObject.join)(config.outDir ?? 'doc_build', '~demo');
|
488
|
+
if (0 === Object.keys(sourceEntry).length) return;
|
489
|
+
const { html, source, output, performance } = clientConfig ?? {};
|
490
|
+
const rsbuildConfig = (0, core_namespaceObject.mergeRsbuildConfig)({
|
491
|
+
dev: {
|
492
|
+
progressBar: false
|
493
|
+
},
|
494
|
+
server: {
|
495
|
+
port: devPort,
|
496
|
+
printUrls: ()=>void 0,
|
497
|
+
strictPort: true
|
498
|
+
},
|
499
|
+
performance: {
|
500
|
+
...performance,
|
501
|
+
printFileSize: false
|
502
|
+
},
|
503
|
+
html,
|
504
|
+
source: {
|
505
|
+
...source,
|
506
|
+
entry: sourceEntry
|
507
|
+
},
|
508
|
+
output: {
|
509
|
+
...output,
|
510
|
+
assetPrefix: (null == output ? void 0 : output.assetPrefix) ? `${(0, shared_namespaceObject.removeTrailingSlash)(output.assetPrefix)}/~demo` : '/~demo',
|
511
|
+
distPath: {
|
512
|
+
root: outDir
|
513
|
+
},
|
514
|
+
copy: void 0
|
515
|
+
},
|
516
|
+
plugins: null == config ? void 0 : config.builderPlugins
|
517
|
+
}, builderConfig);
|
518
|
+
const rsbuildInstance = await (0, core_namespaceObject.createRsbuild)({
|
519
|
+
rsbuildConfig
|
520
|
+
});
|
521
|
+
const { pluginSass } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@rsbuild/plugin-sass"));
|
522
|
+
const { pluginLess } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@rsbuild/plugin-less"));
|
523
|
+
rsbuildInstance.addPlugins([
|
524
|
+
pluginSass(),
|
525
|
+
pluginLess()
|
526
|
+
]);
|
527
|
+
if ('solid' === framework) rsbuildInstance.addPlugins([
|
528
|
+
(0, plugin_babel_namespaceObject.pluginBabel)({
|
529
|
+
include: /\.(?:jsx|tsx)$/
|
530
|
+
}),
|
531
|
+
(0, plugin_solid_namespaceObject.pluginSolid)()
|
532
|
+
]);
|
533
|
+
if ('react' === framework) rsbuildInstance.addPlugins([
|
534
|
+
(0, plugin_react_namespaceObject.pluginReact)()
|
535
|
+
]);
|
536
|
+
if (isProd) rsbuildInstance.build();
|
537
|
+
else devServer = await rsbuildInstance.startDevServer();
|
538
|
+
},
|
539
|
+
builderConfig: {
|
540
|
+
source: {
|
541
|
+
include: [
|
542
|
+
(0, external_node_path_namespaceObject.join)(__dirname, '..')
|
543
|
+
]
|
544
|
+
},
|
545
|
+
tools: {
|
546
|
+
bundlerChain (chain) {
|
547
|
+
chain.module.rule('Raw').resourceQuery(/raw/).type('asset/source').end();
|
548
|
+
chain.resolve.extensions.prepend('.md').prepend('.mdx');
|
549
|
+
},
|
550
|
+
rspack: {
|
551
|
+
watchOptions: {
|
552
|
+
ignored: /\.git/
|
553
|
+
}
|
554
|
+
}
|
555
|
+
},
|
556
|
+
plugins: [
|
557
|
+
{
|
558
|
+
name: 'close-demo-server',
|
559
|
+
setup: (api)=>{
|
560
|
+
api.modifyRsbuildConfig((config)=>{
|
561
|
+
var _config_output;
|
562
|
+
if ((null === (_config_output = config.output) || void 0 === _config_output ? void 0 : _config_output.target) === 'web') clientConfig = config;
|
563
|
+
});
|
564
|
+
api.onCloseDevServer(async ()=>{
|
565
|
+
var _devServer_server;
|
566
|
+
await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
|
567
|
+
devServer = void 0;
|
568
|
+
});
|
569
|
+
}
|
570
|
+
}
|
571
|
+
]
|
572
|
+
},
|
573
|
+
extendPageData (pageData, isProd) {
|
574
|
+
if (!isProd) pageData.devPort = port;
|
575
|
+
pageData.extraHighlightLanguages = previewLanguages;
|
576
|
+
},
|
577
|
+
markdown: {
|
578
|
+
remarkPlugins: [
|
579
|
+
[
|
580
|
+
remarkCodeToDemo,
|
581
|
+
{
|
582
|
+
getRouteMeta,
|
583
|
+
position,
|
584
|
+
previewMode,
|
585
|
+
defaultRenderMode,
|
586
|
+
previewLanguages,
|
587
|
+
previewCodeTransform
|
588
|
+
}
|
589
|
+
]
|
590
|
+
],
|
591
|
+
globalComponents: [
|
592
|
+
(0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Container.tsx')
|
593
|
+
]
|
594
|
+
},
|
595
|
+
globalUIComponents,
|
596
|
+
globalStyles: (0, external_node_path_namespaceObject.join)(staticPath, 'global-styles', `${previewMode}.css`)
|
597
|
+
};
|
598
|
+
}
|
599
|
+
})();
|
600
|
+
var __webpack_export_target__ = exports;
|
601
|
+
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
602
|
+
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
603
|
+
value: true
|
764
604
|
});
|
765
|
-
|
766
|
-
//# sourceMappingURL=index.js.map
|