-
Notifications
You must be signed in to change notification settings - Fork 131
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
annotate local paths #1731
Merged
Merged
annotate local paths #1731
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6cfb74d
annotate local paths (_import and _file)
Fil 06b5fee
test
Fil 5b7ad8d
transpile
Fil 1136735
more transpile
Fil 4224b3a
transpiled modules only
Fil d706f6c
dynamicPath annotate.js
Fil 498d37b
this part is about pages, do not annotate
Fil b69ad62
flatten file attachments json
Fil aec5051
isPathImport
Fil 829e087
tests
Fil fabeb4f
remove early test
Fil 963aeb8
Merge branch 'main' into fil/annotate
Fil 7c96c68
fix tests
Fil e0bc373
Annotate scripts in _node, _npm, and _jsr
Fil 568a4bc
Merge branch 'main' into fil/annotate
Fil c81145e
annotate only if env.ANNOTATE_FILES
Fil 1001dde
test annotations
Fil 49acd88
OBSERVABLE_ANNOTATE_FILES
Fil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don’t use default exports (because there’s no real reason to, and it encourages more inconsistency in how people name an imported symbol). This should be called |
||
? 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern breaks
yarn test:mocha:serial
withit.only
ordescribe.only
because it always runs thetest:annotate
in addition to your only’d tests.