-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OBSERVABLE_ANNOTATE_FILES
- Loading branch information
Showing
7 changed files
with
98 additions
and
18 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
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,14 @@ | ||
import {isPathImport} from "../path.js"; | ||
|
||
/** | ||
* Annotate a path to a local import or file so it can be reworked server-side. | ||
*/ | ||
|
||
const annotate = process.env["OBSERVABLE_ANNOTATE_FILES"]; | ||
if (typeof annotate === "string" && annotate !== "true") | ||
throw new Error(`unsupported OBSERVABLE_ANNOTATE_FILES value: ${annotate}`); | ||
export default annotate | ||
? function (uri: string): string { | ||
return `${JSON.stringify(uri)}${isPathImport(uri) ? "/* observablehq-file */" : ""}`; | ||
} | ||
: JSON.stringify; |
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,63 @@ | ||
/** | ||
* This file is not suffixed with '-test'; it expects to run with an extra | ||
* OBSERVABLE_ANNOTATE_FILES=true environment variable. | ||
*/ | ||
import assert from "node:assert"; | ||
import type {TranspileModuleOptions} from "../../src/javascript/transpile.js"; | ||
import {transpileModule} from "../../src/javascript/transpile.js"; | ||
import {fromJsDelivrPath, rewriteNpmImports} from "../../src/npm.js"; | ||
import {relativePath} from "../../src/path.js"; | ||
|
||
// prettier-ignore | ||
describe("annotates", () => { | ||
const options: TranspileModuleOptions = {root: "src", path: "test.js"}; | ||
it("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"/* observablehq-file */;'); | ||
}); | ||
it("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"/* observablehq-file */;'); | ||
}); | ||
it("dynamic 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"/* observablehq-file */);'); | ||
}); | ||
it("/npm/ exports", () => { | ||
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'); | ||
}); | ||
it("/npm/ imports", () => { | ||
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'); | ||
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'); | ||
}); | ||
it("named imports", () => { | ||
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'); | ||
}); | ||
it("empty imports", () => { | ||
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'); | ||
}); | ||
it("default imports", () => { | ||
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'); | ||
}); | ||
it("namespace imports", () => { | ||
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'); | ||
}); | ||
it("named exports", () => { | ||
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'); | ||
}); | ||
it("namespace exports", () => { | ||
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'); | ||
}); | ||
it("dynamic imports with static module specifiers", () => { | ||
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'); | ||
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'); | ||
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'); | ||
}); | ||
}); | ||
|
||
function resolve(path: string, specifier: string): string { | ||
return specifier.startsWith("/npm/") ? relativePath(path, fromJsDelivrPath(specifier)) : specifier; | ||
} |