vite-plugin-react-deck 1.0.6 → 1.0.8

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,6 @@
1
- export declare function extractMainCodeAsChildren(source: string): Promise<string>;
1
+ /**
2
+ * Extracts the main code from the MDX file and returns it as a string.
3
+ */
4
+ export declare function extractMainCodeAsChildren(source: string, { isJsx }: {
5
+ isJsx: boolean;
6
+ }): string;
package/index.cjs CHANGED
@@ -22,42 +22,46 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  ));
23
23
 
24
24
  // src/index.ts
25
- var fs = __toESM(require("fs/promises"), 1);
25
+ var fs2 = __toESM(require("fs/promises"), 1);
26
26
 
27
27
  // src/slides.ts
28
28
  var import_gray_matter = __toESM(require("gray-matter"), 1);
29
+ var import_fs = __toESM(require("fs"), 1);
29
30
  var import_mdx = require("@mdx-js/mdx");
30
31
 
31
32
  // src/codegen.ts
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
+ var Patterns = {
34
+ FragmentProd: /^\s*_jsxs\(_Fragment, {\s*children:|\s*\}\);/g,
35
+ FragmentDev: /^\s*<>\s*|\s*<\/>;\s*$/g
36
+ };
37
+ function extractMainCodeAsChildren(source, { isJsx }) {
41
38
  const start = source.indexOf("const _components = {");
42
39
  const end = source.indexOf("\n}", start);
43
40
  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");
48
41
  if (!components) {
49
42
  return "";
50
43
  }
51
- const withWrapper = body.replace(
52
- /};\s*return/gm,
53
- "};\n const {wrapper: MDXLayout} = _components;\nconst _content ="
54
- );
55
- const result = `${withWrapper}
56
- return MDXLayout ? _jsx(MDXLayout, {
44
+ const lines = components.split("\n");
45
+ const returnPos = lines.findIndex((line) => line.includes("return"));
46
+ const header = lines.slice(0, returnPos).join("\n");
47
+ const footer = lines.slice(returnPos).join("\n").replace("return", "");
48
+ if (isJsx) {
49
+ const finalFooter2 = footer.replace(Patterns.FragmentDev, " ").replace(/;$/, "");
50
+ return `${header}
51
+ const {wrapper: MDXLayout} = _components;
52
+ return <MDXLayout {...props}>${finalFooter2}</MDXLayout>;
53
+ `.trim();
54
+ }
55
+ const finalFooter = footer.match(/^\s*_jsxs?\(_Fragment,/gm) ? footer.replace(Patterns.FragmentProd, " ") : footer;
56
+ return `
57
+ ${header}
58
+ const _content = ${finalFooter};
59
+ const {wrapper: MDXLayout} = _components;
60
+ return MDXLayout ? _jsx(MDXLayout, {
57
61
  ...props,
58
62
  children: _content
59
- }) : _content;`;
60
- return result;
63
+ }) : _content;
64
+ `.trim();
61
65
  }
62
66
 
63
67
  // src/slides.ts
@@ -98,7 +102,9 @@ ${slide}
98
102
  remarkPlugins: [import_remark_gfm.default],
99
103
  ...options
100
104
  });
101
- const mainCode = await extractMainCodeAsChildren(result.value.toString());
105
+ const mainCode = extractMainCodeAsChildren(result.value.toString(), {
106
+ isJsx: !isProd
107
+ });
102
108
  return {
103
109
  ...slide,
104
110
  mdxContent: mainCode
@@ -133,6 +139,7 @@ Deck.slides = [
133
139
  `,
134
140
  inlineModules
135
141
  );
142
+ import_fs.default.writeFileSync("slides.js", output);
136
143
  return output;
137
144
  }
138
145
  var MOD_REG = /\\`|`(?:\\`|[^`])*`|(^(?:import|export).*$)/gm;
@@ -206,7 +213,7 @@ var glob = __toESM(require("glob"), 1);
206
213
  var import_node_path = __toESM(require("path"), 1);
207
214
  async function checkIfDirectoryExists(path2) {
208
215
  try {
209
- await fs.access(path2);
216
+ await fs2.access(path2);
210
217
  return true;
211
218
  } catch {
212
219
  return false;
@@ -280,7 +287,7 @@ var src_default = (options) => {
280
287
  if (!id.endsWith("deck.mdx")) {
281
288
  return;
282
289
  }
283
- const content = await fs.readFile(id, "utf-8");
290
+ const content = await fs2.readFile(id, "utf-8");
284
291
  const data = await transformSlidesMdxToReact(content, {
285
292
  production: isProd,
286
293
  ...options
package/index.mjs CHANGED
@@ -1,40 +1,44 @@
1
1
  // src/index.ts
2
- import * as fs from "fs/promises";
2
+ import * as fs2 from "fs/promises";
3
3
 
4
4
  // src/slides.ts
5
5
  import matter from "gray-matter";
6
+ import fs from "fs";
6
7
  import { compile } from "@mdx-js/mdx";
7
8
 
8
9
  // src/codegen.ts
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
+ var Patterns = {
11
+ FragmentProd: /^\s*_jsxs\(_Fragment, {\s*children:|\s*\}\);/g,
12
+ FragmentDev: /^\s*<>\s*|\s*<\/>;\s*$/g
13
+ };
14
+ function extractMainCodeAsChildren(source, { isJsx }) {
18
15
  const start = source.indexOf("const _components = {");
19
16
  const end = source.indexOf("\n}", start);
20
17
  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");
25
18
  if (!components) {
26
19
  return "";
27
20
  }
28
- const withWrapper = body.replace(
29
- /};\s*return/gm,
30
- "};\n const {wrapper: MDXLayout} = _components;\nconst _content ="
31
- );
32
- const result = `${withWrapper}
33
- return MDXLayout ? _jsx(MDXLayout, {
21
+ const lines = components.split("\n");
22
+ const returnPos = lines.findIndex((line) => line.includes("return"));
23
+ const header = lines.slice(0, returnPos).join("\n");
24
+ const footer = lines.slice(returnPos).join("\n").replace("return", "");
25
+ if (isJsx) {
26
+ const finalFooter2 = footer.replace(Patterns.FragmentDev, " ").replace(/;$/, "");
27
+ return `${header}
28
+ const {wrapper: MDXLayout} = _components;
29
+ return <MDXLayout {...props}>${finalFooter2}</MDXLayout>;
30
+ `.trim();
31
+ }
32
+ const finalFooter = footer.match(/^\s*_jsxs?\(_Fragment,/gm) ? footer.replace(Patterns.FragmentProd, " ") : footer;
33
+ return `
34
+ ${header}
35
+ const _content = ${finalFooter};
36
+ const {wrapper: MDXLayout} = _components;
37
+ return MDXLayout ? _jsx(MDXLayout, {
34
38
  ...props,
35
39
  children: _content
36
- }) : _content;`;
37
- return result;
40
+ }) : _content;
41
+ `.trim();
38
42
  }
39
43
 
40
44
  // src/slides.ts
@@ -75,7 +79,9 @@ ${slide}
75
79
  remarkPlugins: [remarkGfm],
76
80
  ...options
77
81
  });
78
- const mainCode = await extractMainCodeAsChildren(result.value.toString());
82
+ const mainCode = extractMainCodeAsChildren(result.value.toString(), {
83
+ isJsx: !isProd
84
+ });
79
85
  return {
80
86
  ...slide,
81
87
  mdxContent: mainCode
@@ -110,6 +116,7 @@ Deck.slides = [
110
116
  `,
111
117
  inlineModules
112
118
  );
119
+ fs.writeFileSync("slides.js", output);
113
120
  return output;
114
121
  }
115
122
  var MOD_REG = /\\`|`(?:\\`|[^`])*`|(^(?:import|export).*$)/gm;
@@ -183,7 +190,7 @@ import * as glob from "glob";
183
190
  import path from "path";
184
191
  async function checkIfDirectoryExists(path2) {
185
192
  try {
186
- await fs.access(path2);
193
+ await fs2.access(path2);
187
194
  return true;
188
195
  } catch {
189
196
  return false;
@@ -257,7 +264,7 @@ var src_default = (options) => {
257
264
  if (!id.endsWith("deck.mdx")) {
258
265
  return;
259
266
  }
260
- const content = await fs.readFile(id, "utf-8");
267
+ const content = await fs2.readFile(id, "utf-8");
261
268
  const data = await transformSlidesMdxToReact(content, {
262
269
  production: isProd,
263
270
  ...options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-react-deck",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "index.cjs",