vite-plugin-react-deck 1.0.5 → 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,41 +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
- return `${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
- children: content
59
- }) : content;`;
64
+ children: _content
65
+ }) : _content;
66
+ `.trim();
60
67
  }
61
68
 
62
69
  // src/slides.ts
@@ -97,7 +104,9 @@ ${slide}
97
104
  remarkPlugins: [import_remark_gfm.default],
98
105
  ...options
99
106
  });
100
- const mainCode = await extractMainCodeAsChildren(result.value.toString());
107
+ const mainCode = extractMainCodeAsChildren(result.value.toString(), {
108
+ isJsx: !isProd
109
+ });
101
110
  return {
102
111
  ...slide,
103
112
  mdxContent: mainCode
@@ -132,6 +141,7 @@ Deck.slides = [
132
141
  `,
133
142
  inlineModules
134
143
  );
144
+ import_fs.default.writeFileSync("slides.js", output);
135
145
  return output;
136
146
  }
137
147
  var MOD_REG = /\\`|`(?:\\`|[^`])*`|(^(?:import|export).*$)/gm;
@@ -205,7 +215,7 @@ var glob = __toESM(require("glob"), 1);
205
215
  var import_node_path = __toESM(require("path"), 1);
206
216
  async function checkIfDirectoryExists(path2) {
207
217
  try {
208
- await fs.access(path2);
218
+ await fs2.access(path2);
209
219
  return true;
210
220
  } catch {
211
221
  return false;
@@ -279,7 +289,7 @@ var src_default = (options) => {
279
289
  if (!id.endsWith("deck.mdx")) {
280
290
  return;
281
291
  }
282
- const content = await fs.readFile(id, "utf-8");
292
+ const content = await fs2.readFile(id, "utf-8");
283
293
  const data = await transformSlidesMdxToReact(content, {
284
294
  production: isProd,
285
295
  ...options
package/index.mjs CHANGED
@@ -1,39 +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
- return `${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
- children: content
36
- }) : content;`;
41
+ children: _content
42
+ }) : _content;
43
+ `.trim();
37
44
  }
38
45
 
39
46
  // src/slides.ts
@@ -74,7 +81,9 @@ ${slide}
74
81
  remarkPlugins: [remarkGfm],
75
82
  ...options
76
83
  });
77
- const mainCode = await extractMainCodeAsChildren(result.value.toString());
84
+ const mainCode = extractMainCodeAsChildren(result.value.toString(), {
85
+ isJsx: !isProd
86
+ });
78
87
  return {
79
88
  ...slide,
80
89
  mdxContent: mainCode
@@ -109,6 +118,7 @@ Deck.slides = [
109
118
  `,
110
119
  inlineModules
111
120
  );
121
+ fs.writeFileSync("slides.js", output);
112
122
  return output;
113
123
  }
114
124
  var MOD_REG = /\\`|`(?:\\`|[^`])*`|(^(?:import|export).*$)/gm;
@@ -182,7 +192,7 @@ import * as glob from "glob";
182
192
  import path from "path";
183
193
  async function checkIfDirectoryExists(path2) {
184
194
  try {
185
- await fs.access(path2);
195
+ await fs2.access(path2);
186
196
  return true;
187
197
  } catch {
188
198
  return false;
@@ -256,7 +266,7 @@ var src_default = (options) => {
256
266
  if (!id.endsWith("deck.mdx")) {
257
267
  return;
258
268
  }
259
- const content = await fs.readFile(id, "utf-8");
269
+ const content = await fs2.readFile(id, "utf-8");
260
270
  const data = await transformSlidesMdxToReact(content, {
261
271
  production: isProd,
262
272
  ...options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-react-deck",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "index.cjs",