mdx-artifacts 0.1.0 → 0.1.1
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.
|
@@ -3,6 +3,7 @@ import tailwindcss from "@tailwindcss/vite";
|
|
|
3
3
|
import react from "@vitejs/plugin-react";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
6
7
|
import path from "node:path";
|
|
7
8
|
import { fileURLToPath } from "node:url";
|
|
8
9
|
import { createServer, build as viteBuild } from "vite";
|
|
@@ -17,7 +18,9 @@ export async function createArtifactProject(projectRoot, mdxPath, config) {
|
|
|
17
18
|
const entryPath = path.join(srcDir, "entry.tsx");
|
|
18
19
|
const mdxImport = toRelativeImport(entryPath, mdxPath);
|
|
19
20
|
const styleImports = createStyleImports(projectRoot, entryPath, config);
|
|
20
|
-
const
|
|
21
|
+
const reactEntryPath = await resolveReactEntryPath();
|
|
22
|
+
const reactEntryImport = toRelativeImport(entryPath, reactEntryPath);
|
|
23
|
+
const reactAliases = resolveReactAliases(projectRoot);
|
|
21
24
|
await mkdir(srcDir, { recursive: true });
|
|
22
25
|
await writeFile(path.join(tmpDir, "index.html"), `<!doctype html>
|
|
23
26
|
<html lang="en">
|
|
@@ -58,6 +61,17 @@ createRoot(document.getElementById("root")!).render(<App />);
|
|
|
58
61
|
root: tmpDir,
|
|
59
62
|
logLevel: "warn",
|
|
60
63
|
plugins: [artifactStatePlugin(projectRoot, artifact), react(), mdx(), tailwindcss()],
|
|
64
|
+
resolve: {
|
|
65
|
+
alias: [
|
|
66
|
+
...reactAliases,
|
|
67
|
+
{ find: /^mdx-artifacts\/react$/, replacement: reactEntryPath },
|
|
68
|
+
{ find: /^mdx-artifacts$/, replacement: reactEntryPath }
|
|
69
|
+
],
|
|
70
|
+
dedupe: ["react", "react-dom"]
|
|
71
|
+
},
|
|
72
|
+
optimizeDeps: {
|
|
73
|
+
exclude: ["mdx-artifacts", "mdx-artifacts/react"]
|
|
74
|
+
},
|
|
61
75
|
server: {
|
|
62
76
|
port: config.port,
|
|
63
77
|
fs: {
|
|
@@ -163,6 +177,16 @@ async function resolveReactEntryPath() {
|
|
|
163
177
|
return sourceEntryPath;
|
|
164
178
|
}
|
|
165
179
|
}
|
|
180
|
+
function resolveReactAliases(projectRoot) {
|
|
181
|
+
const projectRequire = createRequire(path.join(projectRoot, "package.json"));
|
|
182
|
+
return [
|
|
183
|
+
{ find: /^react$/, replacement: projectRequire.resolve("react") },
|
|
184
|
+
{ find: /^react\/jsx-runtime$/, replacement: projectRequire.resolve("react/jsx-runtime") },
|
|
185
|
+
{ find: /^react\/jsx-dev-runtime$/, replacement: projectRequire.resolve("react/jsx-dev-runtime") },
|
|
186
|
+
{ find: /^react-dom$/, replacement: projectRequire.resolve("react-dom") },
|
|
187
|
+
{ find: /^react-dom\/client$/, replacement: projectRequire.resolve("react-dom/client") }
|
|
188
|
+
];
|
|
189
|
+
}
|
|
166
190
|
export async function startDevServer(project) {
|
|
167
191
|
const server = await createServer(project.config);
|
|
168
192
|
await server.listen();
|