Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 12, 2024
1 parent e9cbadc commit a3f838c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
30 changes: 26 additions & 4 deletions docs/lib/react.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
# React

```js echo
import {createElement} from "react";
import {createRoot} from "react-dom/client";

display(createElement);
const root = createRoot(display(document.createElement("DIV")));
```

```js echo
import {createRoot} from "react-dom/client";
root.render(createContent());
```

```js echo
import {Fragment, jsx, jsxs} from "react/jsx-runtime";

display(createRoot);
function createContent() {
return jsxs(Fragment, {
children: [jsx("h1", {
children: "Hello, world!"
}), "\n", jsx("p", {
children: "Below is an example of markdown in JSX."
}), "\n", jsx("div", {
style: {
backgroundColor: 'violet',
padding: '1rem'
},
children: jsxs("p", {
children: ["Try and change the background color to ", jsx("code", {
children: "tomato"
}), "."]
})
})]
});
}
```
8 changes: 5 additions & 3 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {existsSync} from "node:fs";
import {copyFile, readFile, writeFile} from "node:fs/promises";
import {createRequire} from "node:module";
import op from "node:path";
import {dirname, extname, join, relative} from "node:path/posix";
import {extname, join} from "node:path/posix";
import {pathToFileURL} from "node:url";
import commonjs from "@rollup/plugin-commonjs";
import {nodeResolve} from "@rollup/plugin-node-resolve";
Expand All @@ -14,7 +14,7 @@ import {prepareOutput, toOsPath} from "./files.js";
import type {ImportReference} from "./javascript/imports.js";
import {isJavaScript, parseImports} from "./javascript/imports.js";
import {parseNpmSpecifier, rewriteNpmImports} from "./npm.js";
import {isPathImport} from "./path.js";
import {isPathImport, relativePath} from "./path.js";
import {faint} from "./tty.js";

export async function resolveNodeImport(root: string, spec: string): Promise<string> {
Expand Down Expand Up @@ -72,6 +72,8 @@ async function resolveNodeImportInternal(cacheRoot: string, packageRoot: string,
function overrideNodeResolution(specifier: string, packageResolution: string): string {
return specifier === "react"
? op.join(packageResolution, "cjs", "react.production.min.js")
: specifier === "react/jsx-runtime"
? op.join(packageResolution, "cjs", "react-jsx-runtime.production.min.js")
: specifier === "react-dom" || specifier === "react-dom/client"
? op.join(packageResolution, "cjs", "react-dom.production.min.js")
: specifier === "scheduler"
Expand Down Expand Up @@ -124,7 +126,7 @@ async function bundle(path: string, input: string, cacheRoot: string, packageRoo
try {
const output = await bundle.generate({format: "es", exports: "named"});
const code = output.output.find((o): o is OutputChunk => o.type === "chunk")!.code;
return rewriteNpmImports(code, (i) => (i.startsWith("/_node/") ? relative(dirname(path), i) : i));
return rewriteNpmImports(code, (i) => (i.startsWith("/_node/") ? relativePath(path, i) : i));
} finally {
await bundle.close();
}
Expand Down

0 comments on commit a3f838c

Please sign in to comment.