vite-plugin-react-deck 1.0.4 → 1.0.5

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,29 +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, "})});");
54
- return result;
55
+ return `${withWrapper}
56
+ return MDXLayout ? _jsx(MDXLayout, {
57
+ ...props,
58
+ children: content
59
+ }) : content;`;
55
60
  }
56
61
 
57
62
  // src/slides.ts
@@ -82,7 +87,7 @@ ${slide}
82
87
  frontmatterForNextSlide = null;
83
88
  }
84
89
  const compiledSlides = await Promise.all(
85
- enrichedSlides.map(async (slide) => {
90
+ enrichedSlides.map(async (slide, index) => {
86
91
  const code = addInlineModules(slide.content, inlineModules);
87
92
  const normalizedCode = code.replace("process.env", "process\u200B.env");
88
93
  const result = await (0, import_mdx.compile)(normalizedCode, {
@@ -92,7 +97,7 @@ ${slide}
92
97
  remarkPlugins: [import_remark_gfm.default],
93
98
  ...options
94
99
  });
95
- const mainCode = extractMainCodeAsChildren(result.value.toString());
100
+ const mainCode = await extractMainCodeAsChildren(result.value.toString());
96
101
  return {
97
102
  ...slide,
98
103
  mdxContent: mainCode
@@ -272,7 +277,6 @@ var src_default = (options) => {
272
277
  return contentIndex;
273
278
  }
274
279
  if (!id.endsWith("deck.mdx")) {
275
- console.log("passing");
276
280
  return;
277
281
  }
278
282
  const content = await fs.readFile(id, "utf-8");
package/index.mjs CHANGED
@@ -6,29 +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, "})});");
31
- return result;
32
+ return `${withWrapper}
33
+ return MDXLayout ? _jsx(MDXLayout, {
34
+ ...props,
35
+ children: content
36
+ }) : content;`;
32
37
  }
33
38
 
34
39
  // src/slides.ts
@@ -59,7 +64,7 @@ ${slide}
59
64
  frontmatterForNextSlide = null;
60
65
  }
61
66
  const compiledSlides = await Promise.all(
62
- enrichedSlides.map(async (slide) => {
67
+ enrichedSlides.map(async (slide, index) => {
63
68
  const code = addInlineModules(slide.content, inlineModules);
64
69
  const normalizedCode = code.replace("process.env", "process\u200B.env");
65
70
  const result = await compile(normalizedCode, {
@@ -69,7 +74,7 @@ ${slide}
69
74
  remarkPlugins: [remarkGfm],
70
75
  ...options
71
76
  });
72
- const mainCode = extractMainCodeAsChildren(result.value.toString());
77
+ const mainCode = await extractMainCodeAsChildren(result.value.toString());
73
78
  return {
74
79
  ...slide,
75
80
  mdxContent: mainCode
@@ -249,7 +254,6 @@ var src_default = (options) => {
249
254
  return contentIndex;
250
255
  }
251
256
  if (!id.endsWith("deck.mdx")) {
252
- console.log("passing");
253
257
  return;
254
258
  }
255
259
  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.5",
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"