|
| 1 | +/** |
| 2 | + * This file is not suffixed with '-test'; it expects to run with an extra |
| 3 | + * OBSERVABLE_ANNOTATE_FILES=true environment variable. |
| 4 | + */ |
| 5 | +import assert from "node:assert"; |
| 6 | +import type {TranspileModuleOptions} from "../../src/javascript/transpile.js"; |
| 7 | +import {transpileModule} from "../../src/javascript/transpile.js"; |
| 8 | +import {fromJsDelivrPath, rewriteNpmImports} from "../../src/npm.js"; |
| 9 | +import {relativePath} from "../../src/path.js"; |
| 10 | + |
| 11 | +// prettier-ignore |
| 12 | +describe("annotates", () => { |
| 13 | + const options: TranspileModuleOptions = {root: "src", path: "test.js"}; |
| 14 | + it("npm imports", async () => { |
| 15 | + const input = 'import "npm:d3-array";'; |
| 16 | + const output = (await transpileModule(input, options)).split("\n").pop()!; |
| 17 | + assert.strictEqual(output, 'import "../_npm/[email protected]/_esm.js"/* observablehq-file */;'); |
| 18 | + }); |
| 19 | + it("node imports", async () => { |
| 20 | + const input = 'import "d3-array";'; |
| 21 | + const output = (await transpileModule(input, options)).split("\n").pop()!; |
| 22 | + assert.strictEqual(output, 'import "../_node/[email protected]/index.js"/* observablehq-file */;'); |
| 23 | + }); |
| 24 | + it("dynamic imports", async () => { |
| 25 | + const input = 'import("d3-array");'; |
| 26 | + const output = (await transpileModule(input, options)).split("\n").pop()!; |
| 27 | + assert.strictEqual(output, 'import("../_node/[email protected]/index.js"/* observablehq-file */);'); |
| 28 | + }); |
| 29 | + it("/npm/ exports", () => { |
| 30 | + assert.strictEqual(rewriteNpmImports('export * from "/npm/[email protected]/dist/d3-array.js";\n', (v) => resolve("/_npm/[email protected]/dist/d3.js", v)), 'export * from "../../[email protected]/dist/d3-array.js"/* observablehq-file */;\n'); |
| 31 | + }); |
| 32 | + it("/npm/ imports", () => { |
| 33 | + assert.strictEqual(rewriteNpmImports('import "/npm/[email protected]/dist/d3-array.js";\n', (v) => resolve("/_npm/[email protected]/dist/d3.js", v)), 'import "../../[email protected]/dist/d3-array.js"/* observablehq-file */;\n'); |
| 34 | + assert.strictEqual(rewriteNpmImports('import "/npm/[email protected]/dist/d3-array.js";\n', (v) => resolve("/_npm/[email protected]/d3.js", v)), 'import "../[email protected]/dist/d3-array.js"/* observablehq-file */;\n'); |
| 35 | + }); |
| 36 | + it("named imports", () => { |
| 37 | + assert.strictEqual(rewriteNpmImports('import {sort} from "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import {sort} from "../[email protected]/_esm.js"/* observablehq-file */;\n'); |
| 38 | + }); |
| 39 | + it("empty imports", () => { |
| 40 | + assert.strictEqual(rewriteNpmImports('import "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import "../[email protected]/_esm.js"/* observablehq-file */;\n'); |
| 41 | + }); |
| 42 | + it("default imports", () => { |
| 43 | + assert.strictEqual(rewriteNpmImports('import d3 from "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import d3 from "../[email protected]/_esm.js"/* observablehq-file */;\n'); |
| 44 | + }); |
| 45 | + it("namespace imports", () => { |
| 46 | + assert.strictEqual(rewriteNpmImports('import * as d3 from "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import * as d3 from "../[email protected]/_esm.js"/* observablehq-file */;\n'); |
| 47 | + }); |
| 48 | + it("named exports", () => { |
| 49 | + assert.strictEqual(rewriteNpmImports('export {sort} from "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'export {sort} from "../[email protected]/_esm.js"/* observablehq-file */;\n'); |
| 50 | + }); |
| 51 | + it("namespace exports", () => { |
| 52 | + assert.strictEqual(rewriteNpmImports('export * from "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'export * from "../[email protected]/_esm.js"/* observablehq-file */;\n'); |
| 53 | + }); |
| 54 | + it("dynamic imports with static module specifiers", () => { |
| 55 | + assert.strictEqual(rewriteNpmImports('import("/npm/[email protected]/+esm");\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js"/* observablehq-file */);\n'); |
| 56 | + assert.strictEqual(rewriteNpmImports("import(`/npm/[email protected]/+esm`);\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js"/* observablehq-file */);\n'); |
| 57 | + assert.strictEqual(rewriteNpmImports("import('/npm/[email protected]/+esm');\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js"/* observablehq-file */);\n'); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +function resolve(path: string, specifier: string): string { |
| 62 | + return specifier.startsWith("/npm/") ? relativePath(path, fromJsDelivrPath(specifier)) : specifier; |
| 63 | +} |
0 commit comments