unplugin-docubook 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -2
- package/dist/astro.cjs +152 -0
- package/dist/astro.cjs.map +1 -0
- package/dist/astro.js +125 -12
- package/dist/astro.js.map +1 -1
- package/dist/bun.cjs +140 -0
- package/dist/bun.cjs.map +1 -0
- package/dist/bun.js +115 -5
- package/dist/bun.js.map +1 -1
- package/dist/components/index.cjs +829 -0
- package/dist/components/index.cjs.map +1 -0
- package/dist/components/index.js +720 -512
- package/dist/components/index.js.map +1 -1
- package/dist/esbuild.cjs +140 -0
- package/dist/esbuild.cjs.map +1 -0
- package/dist/esbuild.js +115 -5
- package/dist/esbuild.js.map +1 -1
- package/dist/farm.cjs +140 -0
- package/dist/farm.cjs.map +1 -0
- package/dist/farm.js +115 -5
- package/dist/farm.js.map +1 -1
- package/dist/index.cjs +143 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +116 -2
- package/dist/index.js.map +1 -0
- package/dist/next.cjs +140 -0
- package/dist/next.cjs.map +1 -0
- package/dist/next.js +119 -0
- package/dist/next.js.map +1 -0
- package/dist/nuxt.cjs +160 -0
- package/dist/nuxt.cjs.map +1 -0
- package/dist/nuxt.js +134 -16
- package/dist/nuxt.js.map +1 -1
- package/dist/rolldown.cjs +140 -0
- package/dist/rolldown.cjs.map +1 -0
- package/dist/rolldown.js +115 -5
- package/dist/rolldown.js.map +1 -1
- package/dist/rollup.cjs +140 -0
- package/dist/rollup.cjs.map +1 -0
- package/dist/rollup.js +115 -5
- package/dist/rollup.js.map +1 -1
- package/dist/rspack.cjs +140 -0
- package/dist/rspack.cjs.map +1 -0
- package/dist/rspack.js +115 -5
- package/dist/rspack.js.map +1 -1
- package/dist/types.cjs +19 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -0
- package/dist/vite.cjs +140 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.js +115 -5
- package/dist/vite.js.map +1 -1
- package/dist/webpack.cjs +140 -0
- package/dist/webpack.cjs.map +1 -0
- package/dist/webpack.js +115 -5
- package/dist/webpack.js.map +1 -1
- package/package.json +92 -33
- package/dist/adapters/next-DNQX7_ln.d.ts +0 -24
- package/dist/adapters/next-DNQX7_ln.d.ts.map +0 -1
- package/dist/adapters/next.js +0 -38
- package/dist/adapters/next.js.map +0 -1
- package/dist/adapters/react-C9Vk6aTD.d.ts +0 -24
- package/dist/adapters/react-C9Vk6aTD.d.ts.map +0 -1
- package/dist/adapters/react.js +0 -39
- package/dist/adapters/react.js.map +0 -1
- package/dist/adapters/svelte-C-dhzdmU.d.ts +0 -19
- package/dist/adapters/svelte-C-dhzdmU.d.ts.map +0 -1
- package/dist/adapters/svelte.js +0 -43
- package/dist/adapters/svelte.js.map +0 -1
- package/dist/adapters/vue-BWv1EZit.d.ts +0 -22
- package/dist/adapters/vue-BWv1EZit.d.ts.map +0 -1
- package/dist/adapters/vue.js +0 -41
- package/dist/adapters/vue.js.map +0 -1
- package/dist/astro-BcckVQ-2.d.ts +0 -19
- package/dist/astro-BcckVQ-2.d.ts.map +0 -1
- package/dist/bun-DYPhytPs.d.ts +0 -7
- package/dist/bun-DYPhytPs.d.ts.map +0 -1
- package/dist/components/index-LWFtIGRd.d.ts +0 -265
- package/dist/components/index-LWFtIGRd.d.ts.map +0 -1
- package/dist/context-BKx0jIwX.js +0 -49
- package/dist/context-BKx0jIwX.js.map +0 -1
- package/dist/esbuild-BD42nX0_.d.ts +0 -8
- package/dist/esbuild-BD42nX0_.d.ts.map +0 -1
- package/dist/farm-C3XYk4nf.d.ts +0 -7
- package/dist/farm-C3XYk4nf.d.ts.map +0 -1
- package/dist/framework/index-98dEoo0g.d.ts +0 -11
- package/dist/framework/index-98dEoo0g.d.ts.map +0 -1
- package/dist/framework/index.js +0 -29
- package/dist/framework/index.js.map +0 -1
- package/dist/index-2f48-Fxc.d.ts +0 -10
- package/dist/index-2f48-Fxc.d.ts.map +0 -1
- package/dist/nuxt-BidHgMAG.d.ts +0 -5
- package/dist/nuxt-BidHgMAG.d.ts.map +0 -1
- package/dist/rolldown-CrcgSKP1.d.ts +0 -8
- package/dist/rolldown-CrcgSKP1.d.ts.map +0 -1
- package/dist/rollup-DDEc1C_K.d.ts +0 -8
- package/dist/rollup-DDEc1C_K.d.ts.map +0 -1
- package/dist/rspack-QbbymFlY.d.ts +0 -7
- package/dist/rspack-QbbymFlY.d.ts.map +0 -1
- package/dist/src-KZV_M01u.js +0 -165
- package/dist/src-KZV_M01u.js.map +0 -1
- package/dist/types-BQhWL5Qs.d.ts +0 -34
- package/dist/types-BQhWL5Qs.d.ts.map +0 -1
- package/dist/types-uxY1cMXO.d.ts +0 -28
- package/dist/types-uxY1cMXO.d.ts.map +0 -1
- package/dist/vite-9CqZQPO-.d.ts +0 -8
- package/dist/vite-9CqZQPO-.d.ts.map +0 -1
- package/dist/webpack-CBuuXltg.d.ts +0 -7
- package/dist/webpack-CBuuXltg.d.ts.map +0 -1
package/dist/rspack.cjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/rspack.ts
|
|
21
|
+
var rspack_exports = {};
|
|
22
|
+
__export(rspack_exports, {
|
|
23
|
+
default: () => rspack_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(rspack_exports);
|
|
26
|
+
var import_unplugin2 = require("unplugin");
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var import_unplugin = require("unplugin");
|
|
30
|
+
|
|
31
|
+
// src/core/registry.ts
|
|
32
|
+
var FRAMEWORK_PATHS = {
|
|
33
|
+
react: "/react",
|
|
34
|
+
vue: "/vue",
|
|
35
|
+
svelte: "/svelte"
|
|
36
|
+
};
|
|
37
|
+
function createRegistry(framework) {
|
|
38
|
+
const basePath = FRAMEWORK_PATHS[framework];
|
|
39
|
+
return {
|
|
40
|
+
Note: { exports: ["Note"], path: basePath },
|
|
41
|
+
Card: { exports: ["Card"], path: basePath },
|
|
42
|
+
CardGroup: { exports: ["CardGroup"], path: basePath },
|
|
43
|
+
Accordion: { exports: ["Accordion"], path: basePath },
|
|
44
|
+
AccordionGroup: { exports: ["AccordionGroup"], path: basePath },
|
|
45
|
+
Stepper: { exports: ["Stepper", "StepperItem"], path: basePath },
|
|
46
|
+
Kbd: { exports: ["Kbd"], path: basePath },
|
|
47
|
+
Tooltip: { exports: ["Tooltip"], path: basePath },
|
|
48
|
+
Youtube: { exports: ["Youtube"], path: basePath },
|
|
49
|
+
Button: { exports: ["Button"], path: basePath },
|
|
50
|
+
DocuLink: { exports: ["DocuLink"], path: basePath },
|
|
51
|
+
DocuImage: { exports: ["DocuImage"], path: basePath },
|
|
52
|
+
Files: { exports: ["Files"], path: basePath },
|
|
53
|
+
Folder: { exports: ["Folder"], path: basePath },
|
|
54
|
+
File: { exports: ["File"], path: basePath },
|
|
55
|
+
Release: { exports: ["Release"], path: basePath },
|
|
56
|
+
Changes: { exports: ["Changes"], path: basePath },
|
|
57
|
+
Pre: { exports: ["Pre"], path: basePath },
|
|
58
|
+
Copy: { exports: ["Copy"], path: basePath },
|
|
59
|
+
DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function getComponentRegistry(framework = "react") {
|
|
63
|
+
return createRegistry(framework);
|
|
64
|
+
}
|
|
65
|
+
function buildComponentDetectionRegex(framework = "react") {
|
|
66
|
+
const registry = getComponentRegistry(framework);
|
|
67
|
+
const names = Object.keys(registry);
|
|
68
|
+
return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/core/transform.ts
|
|
72
|
+
var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
|
|
73
|
+
var DEFAULT_FRAMEWORK = "react";
|
|
74
|
+
function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
|
|
75
|
+
const regex = buildComponentDetectionRegex(framework);
|
|
76
|
+
const registry = getComponentRegistry(framework);
|
|
77
|
+
const found = /* @__PURE__ */ new Set();
|
|
78
|
+
let match;
|
|
79
|
+
while ((match = regex.exec(code)) !== null) {
|
|
80
|
+
const name = match[1];
|
|
81
|
+
if (name && registry[name]) {
|
|
82
|
+
found.add(name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return Array.from(found);
|
|
86
|
+
}
|
|
87
|
+
function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
|
|
88
|
+
const registry = getComponentRegistry(framework);
|
|
89
|
+
const pathToExports = /* @__PURE__ */ new Map();
|
|
90
|
+
for (const name of componentNames) {
|
|
91
|
+
const meta = registry[name];
|
|
92
|
+
if (!meta) continue;
|
|
93
|
+
const fullPath = `${importSource}${meta.path}`;
|
|
94
|
+
if (!pathToExports.has(fullPath)) {
|
|
95
|
+
pathToExports.set(fullPath, /* @__PURE__ */ new Set());
|
|
96
|
+
}
|
|
97
|
+
for (const exp of meta.exports) {
|
|
98
|
+
pathToExports.get(fullPath).add(exp);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const lines = [];
|
|
102
|
+
for (const [path, exports2] of pathToExports) {
|
|
103
|
+
const names = Array.from(exports2).sort().join(", ");
|
|
104
|
+
lines.push(`import { ${names} } from '${path}'`);
|
|
105
|
+
}
|
|
106
|
+
return lines.join("\n");
|
|
107
|
+
}
|
|
108
|
+
function transformMdx(code, options = {}) {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
|
|
111
|
+
const usedComponents = detectComponents(code, framework);
|
|
112
|
+
if (usedComponents.length === 0) {
|
|
113
|
+
return { code, hasTransformed: false };
|
|
114
|
+
}
|
|
115
|
+
const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
|
|
116
|
+
const importStatements = generateImports(usedComponents, importSource, framework);
|
|
117
|
+
return {
|
|
118
|
+
code: `${importStatements}
|
|
119
|
+
|
|
120
|
+
${code}`,
|
|
121
|
+
hasTransformed: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/index.ts
|
|
126
|
+
var unpluginFactory = (options) => ({
|
|
127
|
+
name: "unplugin-docubook",
|
|
128
|
+
transformInclude(id) {
|
|
129
|
+
return /\.mdx?$/.test(id);
|
|
130
|
+
},
|
|
131
|
+
transform(code, _id) {
|
|
132
|
+
const result = transformMdx(code, options);
|
|
133
|
+
if (!result.hasTransformed) return;
|
|
134
|
+
return result.code;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// src/rspack.ts
|
|
139
|
+
var rspack_default = (0, import_unplugin2.createRspackPlugin)(unpluginFactory);
|
|
140
|
+
//# sourceMappingURL=rspack.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rspack.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts"],"sourcesContent":["import { createRspackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createRspackPlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importS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|
|
1
|
+
// src/rspack.ts
|
|
2
2
|
import { createRspackPlugin } from "unplugin";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import { createUnplugin } from "unplugin";
|
|
6
|
+
|
|
7
|
+
// src/core/registry.ts
|
|
8
|
+
var FRAMEWORK_PATHS = {
|
|
9
|
+
react: "/react",
|
|
10
|
+
vue: "/vue",
|
|
11
|
+
svelte: "/svelte"
|
|
12
|
+
};
|
|
13
|
+
function createRegistry(framework) {
|
|
14
|
+
const basePath = FRAMEWORK_PATHS[framework];
|
|
15
|
+
return {
|
|
16
|
+
Note: { exports: ["Note"], path: basePath },
|
|
17
|
+
Card: { exports: ["Card"], path: basePath },
|
|
18
|
+
CardGroup: { exports: ["CardGroup"], path: basePath },
|
|
19
|
+
Accordion: { exports: ["Accordion"], path: basePath },
|
|
20
|
+
AccordionGroup: { exports: ["AccordionGroup"], path: basePath },
|
|
21
|
+
Stepper: { exports: ["Stepper", "StepperItem"], path: basePath },
|
|
22
|
+
Kbd: { exports: ["Kbd"], path: basePath },
|
|
23
|
+
Tooltip: { exports: ["Tooltip"], path: basePath },
|
|
24
|
+
Youtube: { exports: ["Youtube"], path: basePath },
|
|
25
|
+
Button: { exports: ["Button"], path: basePath },
|
|
26
|
+
DocuLink: { exports: ["DocuLink"], path: basePath },
|
|
27
|
+
DocuImage: { exports: ["DocuImage"], path: basePath },
|
|
28
|
+
Files: { exports: ["Files"], path: basePath },
|
|
29
|
+
Folder: { exports: ["Folder"], path: basePath },
|
|
30
|
+
File: { exports: ["File"], path: basePath },
|
|
31
|
+
Release: { exports: ["Release"], path: basePath },
|
|
32
|
+
Changes: { exports: ["Changes"], path: basePath },
|
|
33
|
+
Pre: { exports: ["Pre"], path: basePath },
|
|
34
|
+
Copy: { exports: ["Copy"], path: basePath },
|
|
35
|
+
DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function getComponentRegistry(framework = "react") {
|
|
39
|
+
return createRegistry(framework);
|
|
40
|
+
}
|
|
41
|
+
function buildComponentDetectionRegex(framework = "react") {
|
|
42
|
+
const registry = getComponentRegistry(framework);
|
|
43
|
+
const names = Object.keys(registry);
|
|
44
|
+
return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/core/transform.ts
|
|
48
|
+
var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
|
|
49
|
+
var DEFAULT_FRAMEWORK = "react";
|
|
50
|
+
function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
|
|
51
|
+
const regex = buildComponentDetectionRegex(framework);
|
|
52
|
+
const registry = getComponentRegistry(framework);
|
|
53
|
+
const found = /* @__PURE__ */ new Set();
|
|
54
|
+
let match;
|
|
55
|
+
while ((match = regex.exec(code)) !== null) {
|
|
56
|
+
const name = match[1];
|
|
57
|
+
if (name && registry[name]) {
|
|
58
|
+
found.add(name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return Array.from(found);
|
|
62
|
+
}
|
|
63
|
+
function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
|
|
64
|
+
const registry = getComponentRegistry(framework);
|
|
65
|
+
const pathToExports = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const name of componentNames) {
|
|
67
|
+
const meta = registry[name];
|
|
68
|
+
if (!meta) continue;
|
|
69
|
+
const fullPath = `${importSource}${meta.path}`;
|
|
70
|
+
if (!pathToExports.has(fullPath)) {
|
|
71
|
+
pathToExports.set(fullPath, /* @__PURE__ */ new Set());
|
|
72
|
+
}
|
|
73
|
+
for (const exp of meta.exports) {
|
|
74
|
+
pathToExports.get(fullPath).add(exp);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const lines = [];
|
|
78
|
+
for (const [path, exports] of pathToExports) {
|
|
79
|
+
const names = Array.from(exports).sort().join(", ");
|
|
80
|
+
lines.push(`import { ${names} } from '${path}'`);
|
|
81
|
+
}
|
|
82
|
+
return lines.join("\n");
|
|
83
|
+
}
|
|
84
|
+
function transformMdx(code, options = {}) {
|
|
85
|
+
var _a, _b;
|
|
86
|
+
const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
|
|
87
|
+
const usedComponents = detectComponents(code, framework);
|
|
88
|
+
if (usedComponents.length === 0) {
|
|
89
|
+
return { code, hasTransformed: false };
|
|
90
|
+
}
|
|
91
|
+
const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
|
|
92
|
+
const importStatements = generateImports(usedComponents, importSource, framework);
|
|
93
|
+
return {
|
|
94
|
+
code: `${importStatements}
|
|
6
95
|
|
|
7
|
-
|
|
8
|
-
|
|
96
|
+
${code}`,
|
|
97
|
+
hasTransformed: true
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/index.ts
|
|
102
|
+
var unpluginFactory = (options) => ({
|
|
103
|
+
name: "unplugin-docubook",
|
|
104
|
+
transformInclude(id) {
|
|
105
|
+
return /\.mdx?$/.test(id);
|
|
106
|
+
},
|
|
107
|
+
transform(code, _id) {
|
|
108
|
+
const result = transformMdx(code, options);
|
|
109
|
+
if (!result.hasTransformed) return;
|
|
110
|
+
return result.code;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// src/rspack.ts
|
|
115
|
+
var rspack_default = createRspackPlugin(unpluginFactory);
|
|
116
|
+
export {
|
|
117
|
+
rspack_default as default
|
|
118
|
+
};
|
|
9
119
|
//# sourceMappingURL=rspack.js.map
|
package/dist/rspack.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/rspack.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts"],"sourcesContent":["import { createRspackPlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createRspackPlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n"],"mappings":";AAAA,SAAS,0BAA0B;;;ACEnC,SAAS,sBAA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AAQ,EAAG,IAAI,GAAG;AAAA,IACxC;AAAA,EACJ;AAEA,QAAM,QAAkB,CAAC;AACzB,aAAW,CAAC,MAAM,OAAO,KAAK,eAAe;AACzC,UAAM,QAAQ,MAAM,KAAK,OAAO,EAAE,KAAK,EAAE,KAAK,IAAI;AAClD,UAAM,KAAK,YAAY,KAAK,YAAY,IAAI,GAAG;AAAA,EACnD;AAEA,SAAO,MAAM,KAAK,IAAI;AAC1B;AAEO,SAAS,aACZ,MACA,UAAmB,CAAC,GACqB;AAvD7C;AAwDI,QAAM,aAAY,aAAQ,cAAR,YAAqB;AACvC,QAAM,iBAAiB,iBAAiB,MAAM,SAAS;AAEvD,MAAI,eAAe,WAAW,GAAG;AAC7B,WAAO,EAAE,MAAM,gBAAgB,MAAM;AAAA,EACzC;AAEA,QAAM,gBAAe,aAAQ,iBAAR,YAAwB;AAC7C,QAAM,mBAAmB,gBAAgB,gBAAgB,cAAc,SAAS;AAEhF,SAAO;AAAA,IACH,MAAM,GAAG,gBAAgB;AAAA;AAAA,EAAO,IAAI;AAAA,IACpC,gBAAgB;AAAA,EACpB;AACJ;;;AFjEO,IAAM,kBAAwD,CACjE,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,iBAAQ,mBAAmB,eAAe;","names":[]}
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/types.ts
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
|
19
|
+
//# sourceMappingURL=types.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["export type Framework = 'react' | 'vue' | 'svelte'\n\nexport interface Options {\n /**\n * Target framework for auto-imports.\n * Determines which component path to use in generated imports.\n * @default 'react'\n */\n framework?: Framework\n\n /**\n * Glob patterns to include for MDX transformation.\n * @default ['**\\/*.mdx', '**\\/*.md']\n */\n include?: string[]\n\n /**\n * Glob patterns to exclude from transformation.\n */\n exclude?: string[]\n\n /**\n * Custom import source for DocuBook components.\n * @default 'unplugin-docubook/components'\n */\n importSource?: string\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
package/dist/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/vite.cjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/vite.ts
|
|
21
|
+
var vite_exports = {};
|
|
22
|
+
__export(vite_exports, {
|
|
23
|
+
default: () => vite_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(vite_exports);
|
|
26
|
+
var import_unplugin2 = require("unplugin");
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var import_unplugin = require("unplugin");
|
|
30
|
+
|
|
31
|
+
// src/core/registry.ts
|
|
32
|
+
var FRAMEWORK_PATHS = {
|
|
33
|
+
react: "/react",
|
|
34
|
+
vue: "/vue",
|
|
35
|
+
svelte: "/svelte"
|
|
36
|
+
};
|
|
37
|
+
function createRegistry(framework) {
|
|
38
|
+
const basePath = FRAMEWORK_PATHS[framework];
|
|
39
|
+
return {
|
|
40
|
+
Note: { exports: ["Note"], path: basePath },
|
|
41
|
+
Card: { exports: ["Card"], path: basePath },
|
|
42
|
+
CardGroup: { exports: ["CardGroup"], path: basePath },
|
|
43
|
+
Accordion: { exports: ["Accordion"], path: basePath },
|
|
44
|
+
AccordionGroup: { exports: ["AccordionGroup"], path: basePath },
|
|
45
|
+
Stepper: { exports: ["Stepper", "StepperItem"], path: basePath },
|
|
46
|
+
Kbd: { exports: ["Kbd"], path: basePath },
|
|
47
|
+
Tooltip: { exports: ["Tooltip"], path: basePath },
|
|
48
|
+
Youtube: { exports: ["Youtube"], path: basePath },
|
|
49
|
+
Button: { exports: ["Button"], path: basePath },
|
|
50
|
+
DocuLink: { exports: ["DocuLink"], path: basePath },
|
|
51
|
+
DocuImage: { exports: ["DocuImage"], path: basePath },
|
|
52
|
+
Files: { exports: ["Files"], path: basePath },
|
|
53
|
+
Folder: { exports: ["Folder"], path: basePath },
|
|
54
|
+
File: { exports: ["File"], path: basePath },
|
|
55
|
+
Release: { exports: ["Release"], path: basePath },
|
|
56
|
+
Changes: { exports: ["Changes"], path: basePath },
|
|
57
|
+
Pre: { exports: ["Pre"], path: basePath },
|
|
58
|
+
Copy: { exports: ["Copy"], path: basePath },
|
|
59
|
+
DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function getComponentRegistry(framework = "react") {
|
|
63
|
+
return createRegistry(framework);
|
|
64
|
+
}
|
|
65
|
+
function buildComponentDetectionRegex(framework = "react") {
|
|
66
|
+
const registry = getComponentRegistry(framework);
|
|
67
|
+
const names = Object.keys(registry);
|
|
68
|
+
return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/core/transform.ts
|
|
72
|
+
var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
|
|
73
|
+
var DEFAULT_FRAMEWORK = "react";
|
|
74
|
+
function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
|
|
75
|
+
const regex = buildComponentDetectionRegex(framework);
|
|
76
|
+
const registry = getComponentRegistry(framework);
|
|
77
|
+
const found = /* @__PURE__ */ new Set();
|
|
78
|
+
let match;
|
|
79
|
+
while ((match = regex.exec(code)) !== null) {
|
|
80
|
+
const name = match[1];
|
|
81
|
+
if (name && registry[name]) {
|
|
82
|
+
found.add(name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return Array.from(found);
|
|
86
|
+
}
|
|
87
|
+
function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
|
|
88
|
+
const registry = getComponentRegistry(framework);
|
|
89
|
+
const pathToExports = /* @__PURE__ */ new Map();
|
|
90
|
+
for (const name of componentNames) {
|
|
91
|
+
const meta = registry[name];
|
|
92
|
+
if (!meta) continue;
|
|
93
|
+
const fullPath = `${importSource}${meta.path}`;
|
|
94
|
+
if (!pathToExports.has(fullPath)) {
|
|
95
|
+
pathToExports.set(fullPath, /* @__PURE__ */ new Set());
|
|
96
|
+
}
|
|
97
|
+
for (const exp of meta.exports) {
|
|
98
|
+
pathToExports.get(fullPath).add(exp);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const lines = [];
|
|
102
|
+
for (const [path, exports2] of pathToExports) {
|
|
103
|
+
const names = Array.from(exports2).sort().join(", ");
|
|
104
|
+
lines.push(`import { ${names} } from '${path}'`);
|
|
105
|
+
}
|
|
106
|
+
return lines.join("\n");
|
|
107
|
+
}
|
|
108
|
+
function transformMdx(code, options = {}) {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
|
|
111
|
+
const usedComponents = detectComponents(code, framework);
|
|
112
|
+
if (usedComponents.length === 0) {
|
|
113
|
+
return { code, hasTransformed: false };
|
|
114
|
+
}
|
|
115
|
+
const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
|
|
116
|
+
const importStatements = generateImports(usedComponents, importSource, framework);
|
|
117
|
+
return {
|
|
118
|
+
code: `${importStatements}
|
|
119
|
+
|
|
120
|
+
${code}`,
|
|
121
|
+
hasTransformed: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/index.ts
|
|
126
|
+
var unpluginFactory = (options) => ({
|
|
127
|
+
name: "unplugin-docubook",
|
|
128
|
+
transformInclude(id) {
|
|
129
|
+
return /\.mdx?$/.test(id);
|
|
130
|
+
},
|
|
131
|
+
transform(code, _id) {
|
|
132
|
+
const result = transformMdx(code, options);
|
|
133
|
+
if (!result.hasTransformed) return;
|
|
134
|
+
return result.code;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// src/vite.ts
|
|
139
|
+
var vite_default = (0, import_unplugin2.createVitePlugin)(unpluginFactory);
|
|
140
|
+
//# sourceMappingURL=vite.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vite.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts"],"sourcesContent":["import { createVitePlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createVitePlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,mBAAiC;;;ACEjC,sBAA+B;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,gBAAgB,EAAE,SAAS,CAAC,gBAAgB,GAAG,MAAM,SAAS;AAAA,IAC9D,SAAS,EAAE,SAAS,CAAC,WAAW,aAAa,GAAG,MAAM,SAAS;AAAA,IAC/D,KAAK,EAAE,SAAS,CAAC,KAAK,GAAG,MAAM,SAAS;AAAA,IACxC,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IAChD,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,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O,IAAM,kBAAwD,CACjE,aACE;AAAA,EACF,MAAM;AAAA,EAEN,iBAAiB,IAAI;AAEjB,WAAO,UAAU,KAAK,EAAE;AAAA,EAC5B;AAAA,EAEA,UAAU,MAAM,KAAK;AACjB,UAAM,SAAS,aAAa,MAAM,OAAO;AACzC,QAAI,CAAC,OAAO,eAAgB;AAC5B,WAAO,OAAO;AAAA,EAClB;AACJ;;;ADjBA,IAAO,mBAAQ,mCAAiB,eAAe;","names":["import_unplugin","exports"]}
|
package/dist/vite.js
CHANGED
|
@@ -1,9 +1,119 @@
|
|
|
1
|
-
|
|
1
|
+
// src/vite.ts
|
|
2
2
|
import { createVitePlugin } from "unplugin";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import { createUnplugin } from "unplugin";
|
|
6
|
+
|
|
7
|
+
// src/core/registry.ts
|
|
8
|
+
var FRAMEWORK_PATHS = {
|
|
9
|
+
react: "/react",
|
|
10
|
+
vue: "/vue",
|
|
11
|
+
svelte: "/svelte"
|
|
12
|
+
};
|
|
13
|
+
function createRegistry(framework) {
|
|
14
|
+
const basePath = FRAMEWORK_PATHS[framework];
|
|
15
|
+
return {
|
|
16
|
+
Note: { exports: ["Note"], path: basePath },
|
|
17
|
+
Card: { exports: ["Card"], path: basePath },
|
|
18
|
+
CardGroup: { exports: ["CardGroup"], path: basePath },
|
|
19
|
+
Accordion: { exports: ["Accordion"], path: basePath },
|
|
20
|
+
AccordionGroup: { exports: ["AccordionGroup"], path: basePath },
|
|
21
|
+
Stepper: { exports: ["Stepper", "StepperItem"], path: basePath },
|
|
22
|
+
Kbd: { exports: ["Kbd"], path: basePath },
|
|
23
|
+
Tooltip: { exports: ["Tooltip"], path: basePath },
|
|
24
|
+
Youtube: { exports: ["Youtube"], path: basePath },
|
|
25
|
+
Button: { exports: ["Button"], path: basePath },
|
|
26
|
+
DocuLink: { exports: ["DocuLink"], path: basePath },
|
|
27
|
+
DocuImage: { exports: ["DocuImage"], path: basePath },
|
|
28
|
+
Files: { exports: ["Files"], path: basePath },
|
|
29
|
+
Folder: { exports: ["Folder"], path: basePath },
|
|
30
|
+
File: { exports: ["File"], path: basePath },
|
|
31
|
+
Release: { exports: ["Release"], path: basePath },
|
|
32
|
+
Changes: { exports: ["Changes"], path: basePath },
|
|
33
|
+
Pre: { exports: ["Pre"], path: basePath },
|
|
34
|
+
Copy: { exports: ["Copy"], path: basePath },
|
|
35
|
+
DocuBookProvider: { exports: ["DocuBookProvider"], path: basePath }
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function getComponentRegistry(framework = "react") {
|
|
39
|
+
return createRegistry(framework);
|
|
40
|
+
}
|
|
41
|
+
function buildComponentDetectionRegex(framework = "react") {
|
|
42
|
+
const registry = getComponentRegistry(framework);
|
|
43
|
+
const names = Object.keys(registry);
|
|
44
|
+
return new RegExp(`<(${names.join("|")})(?=[\\s/>])`, "g");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/core/transform.ts
|
|
48
|
+
var DEFAULT_IMPORT_SOURCE = "unplugin-docubook/components";
|
|
49
|
+
var DEFAULT_FRAMEWORK = "react";
|
|
50
|
+
function detectComponents(code, framework = DEFAULT_FRAMEWORK) {
|
|
51
|
+
const regex = buildComponentDetectionRegex(framework);
|
|
52
|
+
const registry = getComponentRegistry(framework);
|
|
53
|
+
const found = /* @__PURE__ */ new Set();
|
|
54
|
+
let match;
|
|
55
|
+
while ((match = regex.exec(code)) !== null) {
|
|
56
|
+
const name = match[1];
|
|
57
|
+
if (name && registry[name]) {
|
|
58
|
+
found.add(name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return Array.from(found);
|
|
62
|
+
}
|
|
63
|
+
function generateImports(componentNames, importSource, framework = DEFAULT_FRAMEWORK) {
|
|
64
|
+
const registry = getComponentRegistry(framework);
|
|
65
|
+
const pathToExports = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const name of componentNames) {
|
|
67
|
+
const meta = registry[name];
|
|
68
|
+
if (!meta) continue;
|
|
69
|
+
const fullPath = `${importSource}${meta.path}`;
|
|
70
|
+
if (!pathToExports.has(fullPath)) {
|
|
71
|
+
pathToExports.set(fullPath, /* @__PURE__ */ new Set());
|
|
72
|
+
}
|
|
73
|
+
for (const exp of meta.exports) {
|
|
74
|
+
pathToExports.get(fullPath).add(exp);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const lines = [];
|
|
78
|
+
for (const [path, exports] of pathToExports) {
|
|
79
|
+
const names = Array.from(exports).sort().join(", ");
|
|
80
|
+
lines.push(`import { ${names} } from '${path}'`);
|
|
81
|
+
}
|
|
82
|
+
return lines.join("\n");
|
|
83
|
+
}
|
|
84
|
+
function transformMdx(code, options = {}) {
|
|
85
|
+
var _a, _b;
|
|
86
|
+
const framework = (_a = options.framework) != null ? _a : DEFAULT_FRAMEWORK;
|
|
87
|
+
const usedComponents = detectComponents(code, framework);
|
|
88
|
+
if (usedComponents.length === 0) {
|
|
89
|
+
return { code, hasTransformed: false };
|
|
90
|
+
}
|
|
91
|
+
const importSource = (_b = options.importSource) != null ? _b : DEFAULT_IMPORT_SOURCE;
|
|
92
|
+
const importStatements = generateImports(usedComponents, importSource, framework);
|
|
93
|
+
return {
|
|
94
|
+
code: `${importStatements}
|
|
6
95
|
|
|
7
|
-
|
|
8
|
-
|
|
96
|
+
${code}`,
|
|
97
|
+
hasTransformed: true
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/index.ts
|
|
102
|
+
var unpluginFactory = (options) => ({
|
|
103
|
+
name: "unplugin-docubook",
|
|
104
|
+
transformInclude(id) {
|
|
105
|
+
return /\.mdx?$/.test(id);
|
|
106
|
+
},
|
|
107
|
+
transform(code, _id) {
|
|
108
|
+
const result = transformMdx(code, options);
|
|
109
|
+
if (!result.hasTransformed) return;
|
|
110
|
+
return result.code;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// src/vite.ts
|
|
115
|
+
var vite_default = createVitePlugin(unpluginFactory);
|
|
116
|
+
export {
|
|
117
|
+
vite_default as default
|
|
118
|
+
};
|
|
9
119
|
//# sourceMappingURL=vite.js.map
|
package/dist/vite.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/vite.ts","../src/index.ts","../src/core/registry.ts","../src/core/transform.ts"],"sourcesContent":["import { createVitePlugin } from 'unplugin'\nimport { unpluginFactory } from '.'\n\nexport default createVitePlugin(unpluginFactory)\n","import type { UnpluginFactory } from 'unplugin'\nimport type { Options } from './types'\nimport { createUnplugin } from 'unplugin'\nimport { transformMdx } from './core/transform'\n\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (\n options,\n) => ({\n name: 'unplugin-docubook',\n\n transformInclude(id) {\n // Only process .mdx and .md files\n return /\\.mdx?$/.test(id)\n },\n\n transform(code, _id) {\n const result = transformMdx(code, options)\n if (!result.hasTransformed) return\n return result.code\n },\n})\n\nexport const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { Framework } from '../types'\n\nexport interface ComponentMeta {\n exports: string[]\n path: string\n}\n\nconst FRAMEWORK_PATHS: Record<Framework, string> = {\n react: '/react',\n vue: '/vue',\n svelte: '/svelte',\n}\n\nfunction createRegistry(framework: Framework): Record<string, ComponentMeta> {\n const basePath = FRAMEWORK_PATHS[framework]\n\n return {\n Note: { exports: ['Note'], path: basePath },\n Card: { exports: ['Card'], path: basePath },\n CardGroup: { exports: ['CardGroup'], path: basePath },\n Accordion: { exports: ['Accordion'], path: basePath },\n AccordionGroup: { exports: ['AccordionGroup'], path: basePath },\n Stepper: { exports: ['Stepper', 'StepperItem'], path: basePath },\n Kbd: { exports: ['Kbd'], path: basePath },\n Tooltip: { exports: ['Tooltip'], path: basePath },\n Youtube: { exports: ['Youtube'], path: basePath },\n Button: { exports: ['Button'], path: basePath },\n DocuLink: { exports: ['DocuLink'], path: basePath },\n DocuImage: { exports: ['DocuImage'], path: basePath },\n Files: { exports: ['Files'], path: basePath },\n Folder: { exports: ['Folder'], path: basePath },\n File: { exports: ['File'], path: basePath },\n Release: { exports: ['Release'], path: basePath },\n Changes: { exports: ['Changes'], path: basePath },\n Pre: { exports: ['Pre'], path: basePath },\n Copy: { exports: ['Copy'], path: basePath },\n DocuBookProvider: { exports: ['DocuBookProvider'], path: basePath },\n }\n}\n\nexport function getComponentRegistry(framework: Framework = 'react'): Record<string, ComponentMeta> {\n return createRegistry(framework)\n}\n\nexport function buildComponentDetectionRegex(framework: Framework = 'react'): RegExp {\n const registry = getComponentRegistry(framework)\n const names = Object.keys(registry)\n return new RegExp(`<(${names.join('|')})(?=[\\\\s/>])`, 'g')\n}\n","import type { Options, Framework } from '../types'\nimport { getComponentRegistry, buildComponentDetectionRegex } from './registry'\n\nconst DEFAULT_IMPORT_SOURCE = 'unplugin-docubook/components'\nconst DEFAULT_FRAMEWORK: Framework = 'react'\n\nexport function detectComponents(code: string, framework: Framework = DEFAULT_FRAMEWORK): string[] {\n const regex = buildComponentDetectionRegex(framework)\n const registry = getComponentRegistry(framework)\n const found = new Set<string>()\n let match: RegExpExecArray | null\n\n while ((match = regex.exec(code)) !== null) {\n const name = match[1]\n if (name && registry[name]) {\n found.add(name)\n }\n }\n\n return Array.from(found)\n}\n\nexport function generateImports(\n componentNames: string[],\n importSource: string,\n framework: Framework = DEFAULT_FRAMEWORK,\n): string {\n const registry = getComponentRegistry(framework)\n const pathToExports = new Map<string, Set<string>>()\n\n for (const name of componentNames) {\n const meta = registry[name]\n if (!meta) continue\n\n const fullPath = `${importSource}${meta.path}`\n if (!pathToExports.has(fullPath)) {\n pathToExports.set(fullPath, new Set())\n }\n for (const exp of meta.exports) {\n pathToExports.get(fullPath)!.add(exp)\n }\n }\n\n const lines: string[] = []\n for (const [path, exports] of pathToExports) {\n const names = Array.from(exports).sort().join(', ')\n lines.push(`import { ${names} } from '${path}'`)\n }\n\n return lines.join('\\n')\n}\n\nexport function transformMdx(\n code: string,\n options: Options = {},\n): { code: string; hasTransformed: boolean } {\n const framework = options.framework ?? DEFAULT_FRAMEWORK\n const usedComponents = detectComponents(code, framework)\n\n if (usedComponents.length === 0) {\n return { code, hasTransformed: false }\n }\n\n const importSource = options.importSource ?? DEFAULT_IMPORT_SOURCE\n const importStatements = generateImports(usedComponents, importSource, framework)\n\n return {\n code: `${importStatements}\\n\\n${code}`,\n hasTransformed: true,\n }\n}\n"],"mappings":";AAAA,SAAS,wBAAwB;;;ACEjC,SAAS,sBAAsB;;;ACK/B,IAAM,kBAA6C;AAAA,EAC/C,OAAO;AAAA,EACP,KAAK;AAAA,EACL,QAAQ;AACZ;AAEA,SAAS,eAAe,WAAqD;AACzE,QAAM,WAAW,gBAAgB,SAAS;AAE1C,SAAO;AAAA,IACH,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,SAAS;AAAA,IAC1C,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,WAAW,EAAE,SAAS,CAAC,WAAW,GAAG,MAAM,SAAS;AAAA,IACpD,gBAAgB,EAAE,SAAS,CAAC,gBAAgB,GAAG,MAAM,SAAS;AAAA,IAC9D,SAAS,EAAE,SAAS,CAAC,WAAW,aAAa,GAAG,MAAM,SAAS;AAAA,IAC/D,KAAK,EAAE,SAAS,CAAC,KAAK,GAAG,MAAM,SAAS;AAAA,IACxC,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IAChD,SAAS,EAAE,SAAS,CAAC,SAAS,GAAG,MAAM,SAAS;AAAA,IAChD,QAAQ,EAAE,SAAS,CAAC,QAAQ,GAAG,MAAM,SAAS;AAAA,IAC9C,UAAU,EAAE,SAAS,CAAC,UAAU,GAAG,MAAM,SAAS;AAAA,IAClD,WAAW,EAAE,SAAS,CAAC,WAA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