-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
find fetches in locally imported files (#286)
* wip * working for both build and dev * delint * fix tests * fix typescript errors * redelint * fix type import * add local fetch test * some more tests * better describe test * fix wording * properly handle fetches directly from markdown * delint and fix typescript * fix tests * fix missing fetch test output * rename rewriteFetch -> rewriteIfLocalFetch * improve typescript typing * extract referenceds in imports.ts and adjust fetch logic to accept references directly * move reference search into findFetches to better findImports pattern * remove blank line * update output * change maybeExtractFetch -> maybeAddFetch and pass in target array
- Loading branch information
Showing
24 changed files
with
295 additions
and
47 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
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 |
---|---|---|
@@ -1,18 +1,71 @@ | ||
import type {CallExpression, Identifier, Node} from "acorn"; | ||
import {simple} from "acorn-walk"; | ||
import {type JavaScriptNode} from "../javascript.js"; | ||
import {type Feature, type JavaScriptNode} from "../javascript.js"; | ||
import {type Sourcemap} from "../sourcemap.js"; | ||
import {relativeUrl, resolvePath} from "../url.js"; | ||
import {getStringLiteralValue, isLocalFetch} from "./features.js"; | ||
import {getStringLiteralValue, isStringLiteral} from "./features.js"; | ||
import {defaultGlobals} from "./globals.js"; | ||
import {isLocalImport} from "./imports.js"; | ||
import {findReferences} from "./references.js"; | ||
|
||
export function rewriteFetches(output: Sourcemap, rootNode: JavaScriptNode, sourcePath: string): void { | ||
simple(rootNode.body, { | ||
CallExpression(node) { | ||
if (isLocalFetch(node, rootNode.references, sourcePath)) { | ||
const arg = node.arguments[0]; | ||
const value = getStringLiteralValue(arg); | ||
const path = resolvePath("_file", sourcePath, value); | ||
output.replaceLeft(arg.start, arg.end, JSON.stringify(relativeUrl(sourcePath, path))); | ||
} | ||
rewriteIfLocalFetch(node, output, rootNode.references, sourcePath); | ||
} | ||
}); | ||
} | ||
|
||
export function rewriteIfLocalFetch( | ||
node: CallExpression, | ||
output: Sourcemap, | ||
references: Identifier[], | ||
sourcePath: string | ||
) { | ||
if (isLocalFetch(node, references, sourcePath)) { | ||
const arg = node.arguments[0]; | ||
const value = getStringLiteralValue(arg); | ||
const path = resolvePath("_file", sourcePath, value); | ||
output.replaceLeft(arg.start, arg.end, JSON.stringify(relativeUrl(sourcePath, path))); | ||
} | ||
} | ||
|
||
export function findFetches(body: Node, path: string) { | ||
const references: Identifier[] = findReferences(body, defaultGlobals); | ||
const fetches: Feature[] = []; | ||
|
||
simple(body, {CallExpression: findFetch}, undefined, path); | ||
|
||
// Promote fetches with static literals to file attachment references. | ||
|
||
function findFetch(node: CallExpression, sourcePath: string) { | ||
maybeAddFetch(fetches, node, references, sourcePath); | ||
} | ||
|
||
return fetches; | ||
} | ||
|
||
export function maybeAddFetch( | ||
features: Feature[], | ||
node: CallExpression, | ||
references: Identifier[], | ||
sourcePath: string | ||
): void { | ||
if (isLocalFetch(node, references, sourcePath)) { | ||
features.push({type: "FileAttachment", name: getStringLiteralValue(node.arguments[0])}); | ||
} | ||
} | ||
|
||
function isLocalFetch(node: CallExpression, references: Identifier[], sourcePath: string): boolean { | ||
const { | ||
callee, | ||
arguments: [arg] | ||
} = node; | ||
return ( | ||
callee.type === "Identifier" && | ||
callee.name === "fetch" && | ||
!references.includes(callee) && | ||
isStringLiteral(arg) && | ||
isLocalImport(getStringLiteralValue(arg), sourcePath) | ||
); | ||
} |
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,8 @@ | ||
# Top | ||
|
||
```js | ||
import {fooJsonData, fooCsvData} from "/foo/foo.js"; | ||
|
||
display(fooJsonData); | ||
display(fooCsvData); | ||
``` |
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,3 @@ | ||
eruid,description | ||
batman,uses technology | ||
superman,flies through the air |
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 @@ | ||
[1, 2, 3] |
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,2 @@ | ||
export const fooJsonData = await fetch("./foo-data.json").then(d => d.json()); | ||
export const fooCsvData = await fetch("./foo-data.csv").then(d => d.csv({ typed: true })); |
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,3 @@ | ||
eruid,description | ||
batman,uses technology | ||
superman,flies through the air |
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 @@ | ||
[1, 2, 3] |
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,3 @@ | ||
export {fooCsvData, fooJsonData} from "./foo/foo.js"; | ||
export const topJsonData = await fetch("./top-data.json").then(d => d.json()); | ||
export const topCsvData = await fetch("./top-data.csv").then(d => d.csv({ typed: true })); |
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,10 @@ | ||
# Top | ||
|
||
```js | ||
import {fooCsvData, fooJsonData, topCsvData, topjsondata} from "/top.js"; | ||
|
||
display(fooJsonData); | ||
display(fooCsvData); | ||
display(topJsonData); | ||
display(topCsvData); | ||
``` |
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 @@ | ||
export const data = fetch("./fetch-local-data.json").then(d => d.json()); |
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 @@ | ||
[1, 2, 3] |
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,3 @@ | ||
import {data} from "./baz.js"; | ||
|
||
display(data); |
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,25 @@ | ||
import assert from "node:assert"; | ||
import {ascending} from "d3-array"; | ||
import {parseLocalImports} from "../../src/javascript/imports.js"; | ||
import {type Feature} from "../../src/javascript.js"; | ||
|
||
describe("parseLocalFetches(root, paths)", () => { | ||
it("find all local fetches in one file", () => { | ||
assert.deepStrictEqual(parseLocalImports("test/input/build/fetches", ["foo/foo.js"]).fetches.sort(compareImport), [ | ||
{name: "./foo-data.csv", type: "FileAttachment"}, | ||
{name: "./foo-data.json", type: "FileAttachment"} | ||
]); | ||
}); | ||
it("find all local fetches via transivite import", () => { | ||
assert.deepStrictEqual(parseLocalImports("test/input/build/fetches", ["top.js"]).fetches.sort(compareImport), [ | ||
{name: "./foo-data.csv", type: "FileAttachment"}, | ||
{name: "./foo-data.json", type: "FileAttachment"}, | ||
{name: "./top-data.csv", type: "FileAttachment"}, | ||
{name: "./top-data.json", type: "FileAttachment"} | ||
]); | ||
}); | ||
}); | ||
|
||
function compareImport(a: Feature, b: Feature): number { | ||
return ascending(a.type, b.type) || ascending(a.name, b.name); | ||
} |
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,3 @@ | ||
eruid,description | ||
batman,uses technology | ||
superman,flies through the air |
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 @@ | ||
[1, 2, 3] |
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,2 @@ | ||
export const fooJsonData = await fetch("../_file/foo/foo-data.json").then(d => d.json()); | ||
export const fooCsvData = await fetch("../_file/foo/foo-data.csv").then(d => d.csv({ typed: true })); |
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,3 @@ | ||
export {fooCsvData, fooJsonData} from "./foo/foo.js?sha=7a93b271ec78dd07db6d9265e7b82eacc1a1bb6682cd665f0f86ea2c0fbc7350"; | ||
export const topJsonData = await fetch("./_file/top-data.json").then(d => d.json()); | ||
export const topCsvData = await fetch("./_file/top-data.csv").then(d => d.csv({ typed: true })); |
Oops, something went wrong.