vite-plugin-react-deck 1.0.4 → 1.0.6

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/codegen.d.ts CHANGED
@@ -1 +1 @@
1
- export declare function extractMainCodeAsChildren(source: string): string;
1
+ export declare function extractMainCodeAsChildren(source: string): Promise<string>;
package/index.cjs CHANGED
@@ -29,28 +29,34 @@ var import_gray_matter = __toESM(require("gray-matter"), 1);
29
29
  var import_mdx = require("@mdx-js/mdx");
30
30
 
31
31
  // src/codegen.ts
32
- function extractMainCodeAsChildren(source) {
32
+ var import_core = __toESM(require("@swc/core"), 1);
33
+ function findMainFunction(program) {
34
+ for (const statement of program.body) {
35
+ if (statement.type === "FunctionDeclaration") {
36
+ return statement;
37
+ }
38
+ }
39
+ }
40
+ async function extractMainCodeAsChildren(source) {
33
41
  const start = source.indexOf("const _components = {");
34
42
  const end = source.indexOf("\n}", start);
35
43
  const components = source.slice(start, end);
44
+ const code = await import_core.default.parse(source);
45
+ const mainFunction = findMainFunction(code);
46
+ const extractedFunction = await import_core.default.print({ type: "Module", body: [mainFunction], span: { start: 0, end: 1e3, ctxt: 0 }, interpreter: null });
47
+ const body = extractedFunction.code.split("\n").slice(1, -2).join("\n");
36
48
  if (!components) {
37
49
  return "";
38
50
  }
39
- const withWrapper = components.replace(
51
+ const withWrapper = body.replace(
40
52
  /};\s*return/gm,
41
- "};\n const {wrapper: MDXLayout} = _components;\nreturn"
53
+ "};\n const {wrapper: MDXLayout} = _components;\nconst _content ="
42
54
  );
43
- const hasLayout = withWrapper.match(/\n\s*return (<>|_jsxs\(_Fragment)/gm);
44
- if (hasLayout) {
45
- return withWrapper.replace(/\n\s*return <>/gm, "\nreturn <MDXLayout {...props}>").replace(/<\/>;\s*$/gm, "</MDXLayout>;\n").replace(
46
- /return _jsxs?\(_Fragment, \{/,
47
- " return _jsx(MDXLayout, {...props,"
48
- );
49
- }
50
- const result = withWrapper.replace(/\n\s*return\s*</gm, "\nreturn <MDXLayout {...props}><").replace(/>;\s*$/gm, "></MDXLayout>;\n").replace(
51
- /return _jsxs?\(_components/gm,
52
- " return _jsx(MDXLayout, {...props, children: _jsx(_components"
53
- ).replace(/}\);$/gm, "})});");
55
+ const result = `${withWrapper}
56
+ return MDXLayout ? _jsx(MDXLayout, {
57
+ ...props,
58
+ children: _content
59
+ }) : _content;`;
54
60
  return result;
55
61
  }
56
62
 
@@ -82,7 +88,7 @@ ${slide}
82
88
  frontmatterForNextSlide = null;
83
89
  }
84
90
  const compiledSlides = await Promise.all(
85
- enrichedSlides.map(async (slide) => {
91
+ enrichedSlides.map(async (slide, index) => {
86
92
  const code = addInlineModules(slide.content, inlineModules);
87
93
  const normalizedCode = code.replace("process.env", "process\u200B.env");
88
94
  const result = await (0, import_mdx.compile)(normalizedCode, {
@@ -92,7 +98,7 @@ ${slide}
92
98
  remarkPlugins: [import_remark_gfm.default],
93
99
  ...options
94
100
  });
95
- const mainCode = extractMainCodeAsChildren(result.value.toString());
101
+ const mainCode = await extractMainCodeAsChildren(result.value.toString());
96
102
  return {
97
103
  ...slide,
98
104
  mdxContent: mainCode
@@ -272,7 +278,6 @@ var src_default = (options) => {
272
278
  return contentIndex;
273
279
  }
274
280
  if (!id.endsWith("deck.mdx")) {
275
- console.log("passing");
276
281
  return;
277
282
  }
278
283
  const content = await fs.readFile(id, "utf-8");
package/index.mjs CHANGED
@@ -6,28 +6,34 @@ import matter from "gray-matter";
6
6
  import { compile } from "@mdx-js/mdx";
7
7
 
8
8
  // src/codegen.ts
9
- function extractMainCodeAsChildren(source) {
9
+ import swc from "@swc/core";
10
+ function findMainFunction(program) {
11
+ for (const statement of program.body) {
12
+ if (statement.type === "FunctionDeclaration") {
13
+ return statement;
14
+ }
15
+ }
16
+ }
17
+ async function extractMainCodeAsChildren(source) {
10
18
  const start = source.indexOf("const _components = {");
11
19
  const end = source.indexOf("\n}", start);
12
20
  const components = source.slice(start, end);
21
+ const code = await swc.parse(source);
22
+ const mainFunction = findMainFunction(code);
23
+ const extractedFunction = await swc.print({ type: "Module", body: [mainFunction], span: { start: 0, end: 1e3, ctxt: 0 }, interpreter: null });
24
+ const body = extractedFunction.code.split("\n").slice(1, -2).join("\n");
13
25
  if (!components) {
14
26
  return "";
15
27
  }
16
- const withWrapper = components.replace(
28
+ const withWrapper = body.replace(
17
29
  /};\s*return/gm,
18
- "};\n const {wrapper: MDXLayout} = _components;\nreturn"
30
+ "};\n const {wrapper: MDXLayout} = _components;\nconst _content ="
19
31
  );
20
- const hasLayout = withWrapper.match(/\n\s*return (<>|_jsxs\(_Fragment)/gm);
21
- if (hasLayout) {
22
- return withWrapper.replace(/\n\s*return <>/gm, "\nreturn <MDXLayout {...props}>").replace(/<\/>;\s*$/gm, "</MDXLayout>;\n").replace(
23
- /return _jsxs?\(_Fragment, \{/,
24
- " return _jsx(MDXLayout, {...props,"
25
- );
26
- }
27
- const result = withWrapper.replace(/\n\s*return\s*</gm, "\nreturn <MDXLayout {...props}><").replace(/>;\s*$/gm, "></MDXLayout>;\n").replace(
28
- /return _jsxs?\(_components/gm,
29
- " return _jsx(MDXLayout, {...props, children: _jsx(_components"
30
- ).replace(/}\);$/gm, "})});");
32
+ const result = `${withWrapper}
33
+ return MDXLayout ? _jsx(MDXLayout, {
34
+ ...props,
35
+ children: _content
36
+ }) : _content;`;
31
37
  return result;
32
38
  }
33
39
 
@@ -59,7 +65,7 @@ ${slide}
59
65
  frontmatterForNextSlide = null;
60
66
  }
61
67
  const compiledSlides = await Promise.all(
62
- enrichedSlides.map(async (slide) => {
68
+ enrichedSlides.map(async (slide, index) => {
63
69
  const code = addInlineModules(slide.content, inlineModules);
64
70
  const normalizedCode = code.replace("process.env", "process\u200B.env");
65
71
  const result = await compile(normalizedCode, {
@@ -69,7 +75,7 @@ ${slide}
69
75
  remarkPlugins: [remarkGfm],
70
76
  ...options
71
77
  });
72
- const mainCode = extractMainCodeAsChildren(result.value.toString());
78
+ const mainCode = await extractMainCodeAsChildren(result.value.toString());
73
79
  return {
74
80
  ...slide,
75
81
  mdxContent: mainCode
@@ -249,7 +255,6 @@ var src_default = (options) => {
249
255
  return contentIndex;
250
256
  }
251
257
  if (!id.endsWith("deck.mdx")) {
252
- console.log("passing");
253
258
  return;
254
259
  }
255
260
  const content = await fs.readFile(id, "utf-8");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-react-deck",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "index.cjs",
@@ -24,6 +24,7 @@
24
24
  "dependencies": {
25
25
  "@mdx-js/mdx": "^3.0.0",
26
26
  "@mdx-js/react": "^3.0.0",
27
+ "@swc/core": "^1.4.1",
27
28
  "glob": "^10.3.10",
28
29
  "gray-matter": "^4.0.3",
29
30
  "remark-gfm": "^4.0.0"