Skip to content

Commit

Permalink
fix node imports (#1225)
Browse files Browse the repository at this point in the history
* fix node imports

* prettier

* add missing mockJsDelivr

* log resolveNodeImport error
  • Loading branch information
mbostock authored Apr 12, 2024
1 parent fc919c2 commit 46e7203
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function resolveNodeImportInternal(cacheRoot: string, packageRoot: string,
}
})();
bundlePromises.set(outputPath, promise);
promise.catch(() => {}).then(() => bundlePromises.delete(outputPath));
promise.catch(console.error).then(() => bundlePromises.delete(outputPath));
}
await promise;
}
Expand Down
2 changes: 1 addition & 1 deletion src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async function resolveNpmVersion(root: string, specifier: NpmSpecifier): Promise
mkdir(join(root, ".observablehq", "cache", "_npm", spec), {recursive: true}); // disk cache
return version;
})();
promise.catch(() => {}).then(() => npmVersionRequests.delete(href));
promise.catch(console.error).then(() => npmVersionRequests.delete(href));
npmVersionRequests.set(href, promise);
return promise;
}
Expand Down
2 changes: 2 additions & 0 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ export function getModuleResolver(root: string, path: string): (specifier: strin
? relativePath(servePath, `/_observablehq/${specifier.slice("observablehq:".length)}${extname(specifier) ? "" : ".js"}`) // prettier-ignore
: specifier.startsWith("npm:")
? relativePath(servePath, await resolveNpmImport(root, specifier.slice("npm:".length)))
: !/^\w+:/.test(specifier)
? relativePath(servePath, await resolveNodeImport(root, specifier))
: specifier;
};
}
Expand Down
14 changes: 13 additions & 1 deletion test/javascript/transpile-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {isEnoent} from "../../src/error.js";
import {parseJavaScript} from "../../src/javascript/parse.js";
import type {TranspileModuleOptions} from "../../src/javascript/transpile.js";
import {transpileJavaScript, transpileModule} from "../../src/javascript/transpile.js";
import {mockJsDelivr} from "../mocks/jsdelivr.js";

function isJsFile(inputRoot: string, fileName: string) {
if (!fileName.endsWith(".js")) return false;
Expand Down Expand Up @@ -106,6 +107,7 @@ describe("transpileModule(input, root, path, sourcePath)", () => {
});

describe("transpileModule(input, root, path)", () => {
mockJsDelivr();
const options: TranspileModuleOptions = {root: "docs", path: "test.js"};
it("rewrites relative files with import.meta.resolve", async () => {
assert.strictEqual(await testFile("./test.txt", "test.js"), 'FileAttachment("../test.txt", import.meta.url)'); // prettier-ignore
Expand Down Expand Up @@ -139,7 +141,7 @@ describe("transpileModule(input, root, path)", () => {
assert.strictEqual(output, '(1, FileAttachment)("./test.txt")');
});
it("ignores FileAttachment if not imported from @observablehq/stdlib", async () => {
const input = 'import {FileAttachment} from "@observablehq/not-stdlib";\nFileAttachment("./test.txt")';
const input = 'import {FileAttachment} from "npm:@observablehq/inputs";\nFileAttachment("./test.txt")';
const output = (await transpileModule(input, options)).split("\n").pop()!;
assert.strictEqual(output, 'FileAttachment("./test.txt")');
});
Expand Down Expand Up @@ -191,4 +193,14 @@ describe("transpileModule(input, root, path)", () => {
await assert.rejects(() => transpileModule(input3, {...options, path: "sub/test.js"}), /non-local file path/); // prettier-ignore
await assert.rejects(() => transpileModule(input4, {...options, path: "sub/test.js"}), /non-local file path/); // prettier-ignore
});
it("rewrites npm imports", async () => {
const input = 'import "npm:d3-array";';
const output = (await transpileModule(input, options)).split("\n").pop()!;
assert.strictEqual(output, 'import "../_npm/[email protected]/_esm.js";');
});
it("rewrites node imports", async () => {
const input = 'import "d3-array";';
const output = (await transpileModule(input, options)).split("\n").pop()!;
assert.strictEqual(output, 'import "../_node/[email protected]/index.js";');
});
});

0 comments on commit 46e7203

Please sign in to comment.