vite-plugin-react-deck 1.0.6 → 1.0.7

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