Skip to content
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

add lastModified to a module’s hash when it references a file attachment #1837

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function readPages(root: string, md: MarkdownIt): Page[] {
return pages;
}

let currentDate: Date | null = null;
export let currentDate: Date | null = null;

/** For testing only! */
export function setCurrentDate(date: Date | null): void {
Expand Down
3 changes: 2 additions & 1 deletion src/javascript/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {extname, join} from "node:path/posix";
import type {Program} from "acorn";
import type {TransformOptions} from "esbuild";
import {transform, transformSync} from "esbuild";
import {currentDate} from "../config.js";
import {resolveJsrImport} from "../jsr.js";
import {resolveNodeImport} from "../node.js";
import {resolveNpmImport} from "../npm.js";
Expand Down Expand Up @@ -199,7 +200,7 @@ export function getFileInfo(root: string, path: string): FileInfo | undefined {
const stat = statSync(key);
if (!stat.isFile()) return; // ignore non-files
accessSync(key, constants.R_OK); // verify that file is readable
mtimeMs = Math.floor(stat.mtimeMs);
mtimeMs = Math.floor((currentDate ?? stat.mtimeMs) as number);
size = stat.size;
} catch {
fileInfoCache.delete(key); // delete stale entry
Expand Down
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class LoaderResolver {
getOutputFileHash(name: string): string {
const info = this.getOutputInfo(name);
if (!info) throw new Error(`output file not found: ${name}`);
return info.hash;
return createHash("sha256").update(info.hash).update(String(info.mtimeMs)).digest("hex");
}

getSourceInfo(name: string): FileInfo | undefined {
Expand Down
4 changes: 1 addition & 3 deletions test/build-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const failureTests = ["missing-file", "missing-import"];

describe("build", () => {
before(() => setCurrentDate(new Date("2024-01-10T16:00:00")));
after(() => setCurrentDate(null));
mockJsDelivr();
mockJsr();
mockDuckDB();
Expand Down Expand Up @@ -233,9 +234,6 @@ class TestEffects extends FileBuildEffects {
contents = contents.replace(/^(\s*<script>\{).*(\}<\/script>)$/gm, "$1/* redacted init script */$2");
contents = contents.replace(/(registerFile\(.*,"lastModified":)\d+(,"size":\d+.*\))/gm, "$1/* ts */1706742000000$2"); // prettier-ignore
}
if (typeof contents === "string" && outputPath.endsWith(".js")) {
contents = contents.replace(/(FileAttachment\(.*,"lastModified":)\d+(,"size":\d+.*\))/gm, "$1/* ts */1706742000000$2"); // prettier-ignore
}
return super.writeFile(outputPath, contents);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const DUCKDB_DEFAULTS: DuckDBConfig = {

describe("readConfig(undefined, root)", () => {
before(() => setCurrentDate(new Date("2024-01-10T16:00:00")));
after(() => setCurrentDate(null));
it("imports the config file at the specified root", async () => {
const {md, loaders, paths, normalizePath, ...config} = await readConfig(undefined, "test/input/build/config");
assert(md instanceof MarkdownIt);
Expand Down
1 change: 1 addition & 0 deletions test/deploy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ const DEPLOY_CONFIG: DeployConfig & {projectId: string; projectSlug: string; wor

describe("deploy", () => {
before(() => setCurrentDate(new Date("2024-01-10T16:00:00")));
after(() => setCurrentDate(null));
mockObservableApi();
mockJsDelivr();

Expand Down
3 changes: 3 additions & 0 deletions test/input/build/params2/[code]/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {FileAttachment} from "npm:@observablehq/stdlib";

FileAttachment("data.json");
1 change: 1 addition & 0 deletions test/input/build/params2/[code]/data.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.stdout.write(JSON.stringify({a: 1}));
5 changes: 5 additions & 0 deletions test/input/build/params2/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test

```js
import "/code/analytics.js"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {test} from "./test.a9a4ef0e.js";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {FileAttachment} from "../_observablehq/stdlib.00000003.js";

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","lastModified":1704931200000,"size":5}, import.meta.url).text();
6 changes: 3 additions & 3 deletions test/output/build/data-loaders/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/import-test.e7269c4e.js">
<link rel="modulepreload" href="./_import/test.86a60bc6.js">
<link rel="modulepreload" href="./_import/import-test.3349a02d.js">
<link rel="modulepreload" href="./_import/test.a9a4ef0e.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
Expand All @@ -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.3349a02d.js");

display(await test);
return {test};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {FileAttachment} from "../_observablehq/stdlib.00000003.js";
import * as Plot from "../_npm/@observablehq/[email protected]/cd372fb8.js";

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","lastModified":1704931200000,"size":97}, import.meta.url).csv({typed: true});
return Plot.plot({
y: {grid: true},
color: {scheme: "burd"},
Expand Down
2 changes: 1 addition & 1 deletion 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";
export * from "./_import/chart.4140747c.js";

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FileAttachment} from "../_observablehq/stdlib.00000003.js";
export {fooCsvData, fooJsonData} from "./foo/foo.666599bc.js";
export const topJsonData = await FileAttachment({"name":"../top-data.json","mimeType":"application/json","path":"../_file/top-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10}, import.meta.url).json();
export const topCsvData = await FileAttachment({"name":"../top-data.csv","mimeType":"text/csv","path":"../_file/top-data.24ef4634.csv","lastModified":/* ts */1706742000000,"size":72}, import.meta.url).text();
export {fooCsvData, fooJsonData} from "./foo/foo.0cc12e18.js";
export const topJsonData = await FileAttachment({"name":"../top-data.json","mimeType":"application/json","path":"../_file/top-data.67358ed8.json","lastModified":1704931200000,"size":10}, import.meta.url).json();
export const topCsvData = await FileAttachment({"name":"../top-data.csv","mimeType":"text/csv","path":"../_file/top-data.24ef4634.csv","lastModified":1704931200000,"size":72}, import.meta.url).text();
4 changes: 2 additions & 2 deletions test/output/build/fetches/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/foo/foo.666599bc.js">
<link rel="modulepreload" href="./_import/foo/foo.0cc12e18.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
Expand All @@ -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.0cc12e18.js");

display(fooJsonData);
display(fooCsvData);
Expand Down
6 changes: 3 additions & 3 deletions test/output/build/fetches/top.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/top.c85e149a.js">
<link rel="modulepreload" href="./_import/foo/foo.666599bc.js">
<link rel="modulepreload" href="./_import/top.bacf54cc.js">
<link rel="modulepreload" href="./_import/foo/foo.0cc12e18.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
Expand All @@ -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.bacf54cc.js");

display(fooJsonData);
display(fooCsvData);
Expand Down
1 change: 1 addition & 0 deletions test/output/build/params2/_file/code/data.015abd7f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":1}
3 changes: 3 additions & 0 deletions test/output/build/params2/_import/code/analytics.0d826e00.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {FileAttachment} from "../../_observablehq/stdlib.00000003.js";

FileAttachment({"name":"../../code/data.json","mimeType":"application/json","path":"../../_file/code/data.015abd7f.json","lastModified":1704931200000,"size":7}, import.meta.url);
Empty file.
Empty file.
Empty file.
46 changes: 46 additions & 0 deletions test/output/build/params2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="generator" content="Observable Framework v1.0.0-test">
<title>test</title>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
<link rel="preload" as="style" href="./_observablehq/theme-air,near-midnight.00000004.css">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
<link rel="stylesheet" type="text/css" href="./_observablehq/theme-air,near-midnight.00000004.css">
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/code/analytics.0d826e00.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
import {registerFile} from "./_observablehq/stdlib.00000003.js";

registerFile("./code/data.json", {"name":"./code/data.json","mimeType":"application/json","path":"./_file/code/data.015abd7f.json","lastModified":/* ts */1706742000000,"size":7});

define({id: "d2a99c55", body: async () => {
const {} = await import("./_import/code/analytics.0d826e00.js");

}});

</script>
</head>
<body>
<div id="observablehq-center">
<aside id="observablehq-toc" data-selector="h1:not(:first-of-type)[id], h2:first-child[id], :not(h1) + h2[id]">
<nav>
</nav>
</aside>
<main id="observablehq-main" class="observablehq">
<h1 id="test" tabindex="-1"><a class="observablehq-header-anchor" href="#test">test</a></h1>
<div class="observablehq observablehq--block"><!--:d2a99c55:--></div>
</main>
<footer id="observablehq-footer">
<div>Built with <a href="https://observablehq.com/" target="_blank" rel="noopener noreferrer">Observable</a> on <a title="2024-01-10T16:00:00">Jan 10, 2024</a>.</div>
</footer>
</div>
</body>
</html>