Skip to content
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

browser entry point #1160

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,25 @@ const bundlePromises = new Map<string, Promise<void>>();
async function resolveNodeImportInternal(root: string, packageRoot: string, spec: string): Promise<string> {
const specifier = parseNpmSpecifier(spec);
const require = createRequire(pathToFileURL(op.join(packageRoot, "/")));
const pathResolution = require.resolve(spec);
let 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"));
const {version, browser} = JSON.parse(await readFile(op.join(packageResolution, "package.json"), "utf-8"));
if (browser) {
if (typeof browser === "string") pathResolution = op.join(packageResolution, browser);
else if (typeof browser === "object") {
for (const [key, value] of Object.entries(browser)) {
if (typeof value === "string" && op.join(packageResolution, key) === pathResolution) {
pathResolution = op.join(packageResolution, value);
break;
}
}
}
}
const resolution = `${specifier.name}@${version}/${fromOsPath(op.relative(packageResolution, pathResolution))}`;
const outputPath = op.join(root, ".observablehq", "cache", "_node", toOsPath(resolution));
if (!existsSync(outputPath)) {
Expand Down
14 changes: 14 additions & 0 deletions test/node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ describe("extractNodeSpecifier(path)", () => {
assert.strictEqual(extractNodeSpecifier("/_node/d3-array@3.2.4/src/index.js"), "d3-array@3.2.4/src/index.js");
});
});

describe("browser condition", () => {
before(async () => {
if (existsSync("docs/.observablehq/cache/_node")) {
await rm("docs/.observablehq/cache/_node", {recursive: true});
}
});
it("bundles the file specified as browser condition (string)", async () => {
assert.deepStrictEqual(await resolveNodeImport("docs", "supports-color"), "/_node/supports-color@7.2.0/browser.js");
});
it("bundles the file specified as browser condition (map)", async () => {
assert.deepStrictEqual(await resolveNodeImport("docs", "asap"), "/_node/asap@2.0.6/browser-asap.js");
});
});