Skip to content

Commit

Permalink
Unconditionally annotate file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon committed Nov 13, 2024
1 parent 8f5200d commit dd50972
Show file tree
Hide file tree
Showing 35 changed files with 70 additions and 77 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
"docs:deploy": "tsx --no-warnings=ExperimentalWarning ./src/bin/observable.ts deploy",
"build": "rimraf dist && node build.js --outdir=dist --outbase=src \"src/**/*.{ts,js,css}\" --ignore \"**/*.d.ts\"",
"test": "concurrently npm:test:mocha npm:test:tsc npm:test:lint npm:test:prettier",
"test:coverage": "c8 --check-coverage --lines 80 --per-file yarn test:mocha:all",
"test:coverage": "c8 --check-coverage --lines 80 --per-file yarn test:mocha",
"test:build": "rimraf test/build && rimraf --glob test/.observablehq/cache test/input/build/*/.observablehq/cache && cross-env npm_package_version=1.0.0-test node build.js --sourcemap --outdir=test/build \"{src,test}/**/*.{ts,js,css}\" --ignore \"test/input/**\" --ignore \"test/output/**\" --ignore \"test/preview/dashboard/**\" --ignore \"**/*.d.ts\" && cp -r templates test/build",
"test:mocha": "yarn test:mocha:serial -p",
"test:mocha:serial": "yarn test:build && cross-env OBSERVABLE_TELEMETRY_DISABLE=1 TZ=America/Los_Angeles mocha --timeout 30000 \"test/build/test/**/*-test.js\"",
"test:mocha:annotate": "yarn test:build && cross-env OBSERVABLE_TELEMETRY_DISABLE=1 OBSERVABLE_ANNOTATE_FILES=true TZ=America/Los_Angeles mocha --timeout 30000 \"test/build/test/**/annotate.js\"",
"test:mocha:all": "yarn test:mocha && cross-env OBSERVABLE_TELEMETRY_DISABLE=1 OBSERVABLE_ANNOTATE_FILES=true TZ=America/Los_Angeles mocha --timeout 30000 \"test/build/test/**/annotate.js\"",
"test:lint": "eslint src test --max-warnings=0",
"test:prettier": "prettier --check src test",
"test:tsc": "tsc --noEmit",
Expand Down
9 changes: 3 additions & 6 deletions src/javascript/annotate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {isPathImport} from "../path.js";

const annotate = process.env["OBSERVABLE_ANNOTATE_FILES"];
if (annotate && annotate !== "true") throw new Error(`unsupported OBSERVABLE_ANNOTATE_FILES: ${annotate}`);

/** Annotate a path to a local import or file so it can be reworked server-side. */
export const annotatePath = annotate
? (uri: string) => `${JSON.stringify(uri)}${isPathImport(uri) ? "/* observablehq-file */" : ""}`
: JSON.stringify;
export function annotatePath(uri: string) {
return `${JSON.stringify(uri)}${isPathImport(uri) ? "/* observablehq-file */" : ""}`;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// 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";
Expand Down
4 changes: 2 additions & 2 deletions test/javascript/transpile-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ describe("transpileModule(input, root, path)", () => {
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";');
assert.strictEqual(output, 'import "../_npm/[email protected]/_esm.js"/* observablehq-file */;');
});
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";');
assert.strictEqual(output, 'import "../_node/[email protected]/index.js"/* observablehq-file */;');
});
});
30 changes: 15 additions & 15 deletions test/npm-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,37 @@ describe("fromJsDelivrPath(path)", () => {
// prettier-ignore
describe("rewriteNpmImports(input, resolve)", () => {
it("rewrites /npm/ imports to /_npm/", () => {
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";\n');
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("rewrites /npm/…+esm imports to _esm.js", () => {
assert.strictEqual(rewriteNpmImports('export * from "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'export * from "../[email protected]/_esm.js";\n');
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("rewrites /npm/ imports to a relative path", () => {
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";\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";\n');
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("rewrites 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";\n');
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("rewrites empty imports", () => {
assert.strictEqual(rewriteNpmImports('import "/npm/[email protected]/+esm";\n', (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import "../[email protected]/_esm.js";\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');
});
it("rewrites 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";\n');
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("rewrites 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";\n');
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("rewrites 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";\n');
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("rewrites 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";\n');
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("rewrites 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");\n');
assert.strictEqual(rewriteNpmImports("import(`/npm/[email protected]/+esm`);\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js");\n');
assert.strictEqual(rewriteNpmImports("import('/npm/[email protected]/+esm');\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js");\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');
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("ignores dynamic imports with dynamic module specifiers", () => {
assert.strictEqual(rewriteNpmImports("import(`/npm/d3-array@${version}/+esm`);\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), "import(`/npm/d3-array@${version}/+esm`);\n");
Expand All @@ -142,8 +142,8 @@ describe("rewriteNpmImports(input, resolve)", () => {
assert.strictEqual(rewriteNpmImports("import(`/npm/d3-array@${version}/+esm`);\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), "import(`/npm/d3-array@${version}/+esm`);\n");
});
it("strips the sourceMappingURL declaration", () => {
assert.strictEqual(rewriteNpmImports("import(`/npm/[email protected]/+esm`);\n//# sourceMappingURL=index.js.map", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js");\n');
assert.strictEqual(rewriteNpmImports("import(`/npm/[email protected]/+esm`);\n//# sourceMappingURL=index.js.map\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js");\n');
assert.strictEqual(rewriteNpmImports("import(`/npm/[email protected]/+esm`);\n//# sourceMappingURL=index.js.map", (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//# sourceMappingURL=index.js.map\n", (v) => resolve("/_npm/[email protected]/_esm.js", v)), 'import("../[email protected]/_esm.js"/* observablehq-file */);\n');
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {test} from "./test.86a60bc6.js";
export {test} from "./test.86a60bc6.js"/* observablehq-file */;
4 changes: 2 additions & 2 deletions test/output/build/data-loaders/_import/test.86a60bc6.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {FileAttachment} from "../_observablehq/stdlib.00000003.js";
import {FileAttachment} from "../_observablehq/stdlib.00000003.js"/* observablehq-file */;

export const test = FileAttachment({"name":"../test.txt","mimeType":"text/plain","path":"../_file/test.f2ca1bb6.txt","lastModified":/* ts */1706742000000,"size":5}, import.meta.url).text();
export const test = FileAttachment({"name":"../test.txt","mimeType":"text/plain","path":"../_file/test.f2ca1bb6.txt"/* observablehq-file */,"lastModified":/* ts */1706742000000,"size":5}, import.meta.url).text();
2 changes: 1 addition & 1 deletion test/output/build/data-loaders/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
registerFile("./test.txt", {"name":"./test.txt","mimeType":"text/plain","path":"./_file/test.f2ca1bb6.txt","lastModified":/* ts */1706742000000,"size":5});

define({id: "05e74070", inputs: ["display"], outputs: ["test"], body: async (display) => {
const {test} = await import("./_import/import-test.e7269c4e.js");
const {test} = await import("./_import/import-test.e7269c4e.js"/* observablehq-file */);

display(await test);
return {test};
Expand Down
6 changes: 3 additions & 3 deletions test/output/build/embed/_import/chart.2ce91e05.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {FileAttachment} from "../_observablehq/stdlib.00000003.js";
import * as Plot from "../_npm/@observablehq/[email protected]/cd372fb8.js";
import {FileAttachment} from "../_observablehq/stdlib.00000003.js"/* observablehq-file */;
import * as Plot from "../_npm/@observablehq/[email protected]/cd372fb8.js"/* observablehq-file */;

export async function Chart() {
const gistemp = await FileAttachment({"name":"../lib/gistemp.csv","mimeType":"text/csv","path":"../_file/lib/gistemp.1cf298b1.csv","lastModified":/* ts */1706742000000,"size":97}, import.meta.url).csv({typed: true});
const gistemp = await FileAttachment({"name":"../lib/gistemp.csv","mimeType":"text/csv","path":"../_file/lib/gistemp.1cf298b1.csv"/* observablehq-file */,"lastModified":/* ts */1706742000000,"size":97}, import.meta.url).csv({typed: true});
return Plot.plot({
y: {grid: true},
color: {scheme: "burd"},
Expand Down
8 changes: 4 additions & 4 deletions test/output/build/embed/chart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./_observablehq/stdlib.00000003.js";
import "./_npm/@observablehq/[email protected]/cd372fb8.js";
import "./_npm/[email protected]/cd372fb8.js";
export * from "./_import/chart.2ce91e05.js";
import "./_observablehq/stdlib.00000003.js"/* observablehq-file */;
import "./_npm/@observablehq/[email protected]/cd372fb8.js"/* observablehq-file */;
import "./_npm/[email protected]/cd372fb8.js"/* observablehq-file */;
export * from "./_import/chart.2ce91e05.js"/* observablehq-file */;
6 changes: 3 additions & 3 deletions test/output/build/fetches/_import/foo/foo.666599bc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/output/build/fetches/_import/top.c85e149a.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/output/build/fetches/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
registerFile("./foo/foo-data.json", {"name":"./foo/foo-data.json","mimeType":"application/json","path":"./_file/foo/foo-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10});

define({id: "47a695da", inputs: ["display"], outputs: ["fooJsonData","fooCsvData"], body: async (display) => {
const {fooJsonData, fooCsvData} = await import("./_import/foo/foo.666599bc.js");
const {fooJsonData, fooCsvData} = await import("./_import/foo/foo.666599bc.js"/* observablehq-file */);

display(fooJsonData);
display(fooCsvData);
Expand Down
2 changes: 1 addition & 1 deletion test/output/build/fetches/top.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
registerFile("./top-data.json", {"name":"./top-data.json","mimeType":"application/json","path":"./_file/top-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10});

define({id: "cb908c08", inputs: ["display"], outputs: ["fooCsvData","fooJsonData","topCsvData","topJsonData"], body: async (display) => {
const {fooCsvData, fooJsonData, topCsvData, topJsonData} = await import("./_import/top.c85e149a.js");
const {fooCsvData, fooJsonData, topCsvData, topJsonData} = await import("./_import/top.c85e149a.js"/* observablehq-file */);

display(fooJsonData);
display(fooCsvData);
Expand Down
2 changes: 1 addition & 1 deletion test/output/build/imports/_import/bar/bar.4460ccc2.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {bar} from "./baz.2add1dd0.js";
export {bar} from "./baz.2add1dd0.js"/* observablehq-file */;
2 changes: 1 addition & 1 deletion test/output/build/imports/_import/bar/baz.2add1dd0.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {foo} from "../foo/foo.bcd720b2.js";
import {foo} from "../foo/foo.bcd720b2.js"/* observablehq-file */;

export const bar = "bar";
export const foobar = foo + "bar";
8 changes: 4 additions & 4 deletions test/output/build/imports/_import/foo/foo.bcd720b2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "../../_npm/[email protected]/cd372fb8.js";
import "../../_npm/@example/[email protected]/1dd108c5.js";
import {bar} from "../bar/bar.4460ccc2.js";
export {top} from "../top.160847a6.js";
import "../../_npm/[email protected]/cd372fb8.js"/* observablehq-file */;
import "../../_npm/@example/[email protected]/1dd108c5.js"/* observablehq-file */;
import {bar} from "../bar/bar.4460ccc2.js"/* observablehq-file */;
export {top} from "../top.160847a6.js"/* observablehq-file */;

export const foo = "foo";
export const foobar = "foo" + bar;
4 changes: 2 additions & 2 deletions test/output/build/imports/foo/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
registerFile("./hello.txt", {"name":"./hello.txt","mimeType":"text/plain","path":"../_file/foo/hello.5891b5b5.txt","lastModified":/* ts */1706742000000,"size":6});

define({id: "261e010e", inputs: ["display","FileAttachment"], outputs: ["d3","bar","top"], body: async (display,FileAttachment) => {
const [d3, {bar}, {top}] = await Promise.all([import("../_npm/[email protected]/cd372fb8.js"), import("../_import/bar/bar.4460ccc2.js"), import("../_import/top.160847a6.js")]);
const [d3, {bar}, {top}] = await Promise.all([import("../_npm/[email protected]/cd372fb8.js"/* observablehq-file */), import("../_import/bar/bar.4460ccc2.js"/* observablehq-file */), import("../_import/top.160847a6.js"/* observablehq-file */)]);

display(bar);
display(top);
Expand All @@ -38,7 +38,7 @@
}});

define({id: "ec24b17a", inputs: ["display"], outputs: ["foobar"], body: async (display) => {
const {foobar} = await import("./foo%20bar.js");
const {foobar} = await import("./foo%20bar.js"/* observablehq-file */);
display(foobar);
return {foobar};
}});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { randomBetween } from "./between.2ce008c6.js";
import { randomBetween } from "./between.2ce008c6.js"/* observablehq-file */;
/**
* Generates a random integer between the provided minimum and maximum values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/ export * from "./between.2ce008c6.js";
export * from "./integer_between.4528767d.js";
export * from "./sample.6e7cf133.js";
export * from "./seeded.4e59f274.js";
export * from "./shuffle.0ef8dd95.js";
*/ export * from "./between.2ce008c6.js"/* observablehq-file */;
export * from "./integer_between.4528767d.js"/* observablehq-file */;
export * from "./sample.6e7cf133.js"/* observablehq-file */;
export * from "./seeded.4e59f274.js"/* observablehq-file */;
export * from "./shuffle.0ef8dd95.js"/* observablehq-file */;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { randomIntegerBetween } from "./integer_between.4528767d.js";
import { randomIntegerBetween } from "./integer_between.4528767d.js"/* observablehq-file */;
/**
* Returns a random element from the given array.
*
Expand Down
Loading

0 comments on commit dd50972

Please sign in to comment.