-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
node imports #1156
node imports #1156
Conversation
Probably the big thing missing here is that we need to set the “browser” condition instead of the “node” condition for conditional exports. Unfortunately I don’t think Node exposes any functionality for resolving a require with custom conditions; |
Another case where things don't work:
results in the .cjs extension is a first blocker; the console says: |
Yup, that’s the node entry point rather than the browser entry point. |
Okay, now using @rollup/plugin-node-resolve! 🚀 |
Argh, it doesn’t work on windows for some reason. |
Okay, it was my mistake. 😌 Fixed now! |
yoohoo! |
I can't find how to load the DuckDB wasm this way. import * as duckdb from "@duckdb/duckdb-wasm"; // works
import duckdb_wasm from '@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm'; // doesn't work The file is correctly copied from Console: "Expected a JavaScript module script but the server responded with a MIME type of "application/wasm"." |
You can’t |
Yes I was just getting to the point where this seems to work:
I'll approve the PR and continue tinkering with DuckDB :) |
Seems to work! I tested this: ```js echo
const connection = await db.connect();
try {
display(await connection.query(`SELECT 1 + 2`));
} finally {
await connection.close();
}
``` |
Confirmed that this works with Yarn’s resolutions, so you can for example tell @duckdb/duckdb-wasm to use the latest version of apache-arrow like so in "resolutions": {
"@duckdb/duckdb-wasm/apache-arrow": "15.0.2"
} |
src/node.ts
Outdated
const require = createRequire(pathToFileURL(op.join(packageRoot, "/"))); | ||
const pathResolution = require.resolve(spec); | ||
let packageResolution = pathResolution; | ||
do { | ||
const p = op.dirname(packageResolution); | ||
if (p === packageResolution) throw new Error(`unable to resolve package.json: ${spec}`); | ||
packageResolution = p; | ||
} while (!existsSync(op.join(packageResolution, "package.json"))); | ||
const {version} = JSON.parse(await readFile(op.join(packageResolution, "package.json"), "utf-8")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For posterity, here is an alternative approach to finding the package.json
that I discovered:
I guess there are some cases of a “nested” package.json
that our approach doesn’t handle; the linked approach is to trim the relative path to the entry point from the fully-resolved path. I think our approach is fine for now, though. There’s also this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve switched this branch to use pkg-dir.
we need to document! it's on https://observablehq.com/framework/getting-started |
Fixes #360.