-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix node imports * prettier * add missing mockJsDelivr * log resolveNodeImport error * fix react import * more fixes * more fixes * fix input; tsc * more consistent promising? * more test isolation
- Loading branch information
Showing
13 changed files
with
255 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# React | ||
|
||
```js echo | ||
import {createRoot} from "react-dom/client"; | ||
|
||
const root = createRoot(display(document.createElement("DIV"))); | ||
``` | ||
|
||
The code above creates a root; a place for React content to live. We render some content into the root below. | ||
|
||
```js echo | ||
root.render(jsxs(createContent, {})); | ||
``` | ||
|
||
The content is defined as a component, but hand-authored using the JSX runtime. You wouldn’t normally write this code by hand, but Framework doesn’t support JSX yet. We’re working on it. | ||
|
||
```js echo | ||
import {useState} from "react"; | ||
import {Fragment, jsx, jsxs} from "react/jsx-runtime"; | ||
|
||
function createContent() { | ||
const [counter, setCounter] = useState(0); | ||
return jsxs(Fragment, { | ||
children: [ | ||
jsx("p", { | ||
children: ["Hello, world! ", counter] | ||
}), | ||
"\n", | ||
jsx("p", { | ||
children: "This content is rendered by React." | ||
}), | ||
"\n", | ||
jsx("div", { | ||
style: { | ||
backgroundColor: "indigo", | ||
padding: "1rem" | ||
}, | ||
onClick: () => setCounter(counter + 1), | ||
children: jsxs("p", { | ||
children: [ | ||
"Try changing the background color to ", | ||
jsx("code", { | ||
children: "tomato" | ||
}), | ||
"." | ||
] | ||
}) | ||
}) | ||
] | ||
}); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file shouldn’t be found by visitFiles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file shouldn’t be found by visitFiles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file should be found by visitFiles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file should be found by visitFiles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import assert from "node:assert"; | ||
import {existsSync} from "node:fs"; | ||
import {rm} from "node:fs/promises"; | ||
import {join} from "node:path/posix"; | ||
import {extractNodeSpecifier, isNodeBuiltin, resolveNodeImport, resolveNodeImports} from "../src/node.js"; | ||
|
||
describe("isNodeBuiltin(specifier)", () => { | ||
|
@@ -29,64 +29,63 @@ describe("isNodeBuiltin(specifier)", () => { | |
}); | ||
}); | ||
|
||
describe("resolveNodeImport(root, spec)", () => { | ||
const importRoot = "../../input/packages/.observablehq/cache"; | ||
before(async () => { | ||
await rm("docs/.observablehq/cache/_node", {recursive: true, force: true}); | ||
await rm("test/input/packages/.observablehq/cache", {recursive: true, force: true}); | ||
}); | ||
describe("resolveNodeImport(root, spec) with top-level node_modules", () => { | ||
const testRoot = "test/output/node-import"; // unique to this test | ||
before(() => rm(join(testRoot, ".observablehq/cache/_node"), {recursive: true, force: true})); | ||
it("resolves the version of a direct dependency", async () => { | ||
assert.deepStrictEqual(await resolveNodeImport("docs", "d3-array"), "/_node/[email protected]/index.js"); | ||
assert.deepStrictEqual(await resolveNodeImport("docs", "mime"), "/_node/[email protected]/index.js"); | ||
assert.deepStrictEqual(await resolveNodeImport(testRoot, "d3-array"), "/_node/[email protected]/index.js"); | ||
assert.deepStrictEqual(await resolveNodeImport(testRoot, "mime"), "/_node/[email protected]/index.js"); | ||
}); | ||
it("allows entry points", async () => { | ||
assert.deepStrictEqual(await resolveNodeImport("docs", "mime/lite"), "/_node/[email protected]/lite.js"); | ||
assert.deepStrictEqual(await resolveNodeImport(testRoot, "mime/lite"), "/_node/[email protected]/lite.js"); | ||
}); | ||
it("allows non-javascript entry points", async () => { | ||
assert.deepStrictEqual(await resolveNodeImport("docs", "glob/package.json"), "/_node/[email protected]/package.json"); | ||
assert.deepStrictEqual(await resolveNodeImport(testRoot, "glob/package.json"), "/_node/[email protected]/package.json"); | ||
}); | ||
it("does not allow version ranges", async () => { | ||
await assert.rejects(() => resolveNodeImport("docs", "mime@4"), /Cannot find module/); | ||
await assert.rejects(() => resolveNodeImport(testRoot, "mime@4"), /Cannot find module/); | ||
}); | ||
}); | ||
|
||
describe("resolveNodeImport(root, spec) with test node_modules", () => { | ||
const testRoot = "test/input/packages"; // unique to this test | ||
const importRoot = "../../input/packages/.observablehq/cache"; | ||
before(() => rm(join(testRoot, ".observablehq/cache/_node"), {recursive: true, force: true})); | ||
it("bundles a package with a shorthand export", async () => { | ||
assert.strictEqual(await resolveNodeImport("test/input/packages", "test-shorthand-export"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual(await resolveNodeImport(testRoot, "test-shorthand-export"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual((await import(`${importRoot}/_node/[email protected]/index.js`)).name, "test-shorthand-export"); // prettier-ignore | ||
}); | ||
it("bundles a package with an import conditional export", async () => { | ||
assert.strictEqual(await resolveNodeImport("test/input/packages", "test-import-condition"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual(await resolveNodeImport(testRoot, "test-import-condition"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual((await import(`${importRoot}/_node/[email protected]/index.js`)).name, "test-import-condition:import"); // prettier-ignore | ||
}); | ||
it("bundles a package with a browser field", async () => { | ||
assert.strictEqual(await resolveNodeImport("test/input/packages", "test-browser-field"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual(await resolveNodeImport(testRoot, "test-browser-field"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual((await import(`${importRoot}/_node/[email protected]/index.js`)).name, "test-browser-field:browser"); // prettier-ignore | ||
}); | ||
it("bundles a package with a browser map", async () => { | ||
assert.strictEqual(await resolveNodeImport("test/input/packages", "test-browser-map"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual(await resolveNodeImport(testRoot, "test-browser-map"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual((await import(`${importRoot}/_node/[email protected]/index.js`)).name, "test-browser-map:browser"); // prettier-ignore | ||
}); | ||
it("bundles a package with a browser conditional export", async () => { | ||
assert.strictEqual(await resolveNodeImport("test/input/packages", "test-browser-condition"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual(await resolveNodeImport(testRoot, "test-browser-condition"), "/_node/[email protected]/index.js"); // prettier-ignore | ||
assert.strictEqual((await import(`${importRoot}/_node/[email protected]/index.js`)).name, "test-browser-condition:browser"); // prettier-ignore | ||
}); | ||
}); | ||
|
||
describe("resolveNodeImports(root, path)", () => { | ||
before(async () => { | ||
if (existsSync("docs/.observablehq/cache/_node")) { | ||
await rm("docs/.observablehq/cache/_node", {recursive: true}); | ||
} | ||
}); | ||
const testRoot = "test/output/node-imports"; // unique to this test | ||
before(() => rm(join(testRoot, ".observablehq/cache/_node"), {recursive: true, force: true})); | ||
it("resolves the imports of a dependency", async () => { | ||
assert.deepStrictEqual(await resolveNodeImports("docs", await resolveNodeImport("docs", "d3-array")), [ | ||
{ | ||
method: "static", | ||
name: "../[email protected]/index.js", | ||
type: "local" | ||
} | ||
assert.deepStrictEqual(await resolveNodeImports(testRoot, await resolveNodeImport(testRoot, "d3-array")), [ | ||
{method: "static", name: "../[email protected]/index.js", type: "local"} | ||
]); | ||
}); | ||
it("ignores non-JavaScript paths", async () => { | ||
assert.deepStrictEqual(await resolveNodeImports("docs", await resolveNodeImport("docs", "glob/package.json")), []); | ||
assert.deepStrictEqual( | ||
await resolveNodeImports(testRoot, await resolveNodeImport(testRoot, "glob/package.json")), | ||
[] | ||
); | ||
}); | ||
}); | ||
|
||
|
Oops, something went wrong.