Skip to content

Commit 0a18310

Browse files
committed
warn instead of error on missing assets
1 parent c50b5c6 commit 0a18310

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/runtime/stdlib/assets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ function isImportPath(specifier: string): boolean {
8484

8585
/**
8686
* Returns true if the specified source (such as an img element src attribute)
87-
* is a path; this is anything that doesn’t start with a protocol or a hash.
87+
* is a path; this is anything that is not empty and does not start with a
88+
* protocol or a hash.
8889
*/
8990
function isSourcePath(specifier: string): boolean {
90-
return !/^(\w+:|#)/.test(specifier);
91+
return asPath(specifier) ? !/^(\w+:|#)/.test(specifier) : false;
9192
}
9293

9394
/** Parses the specified srcset attribute, returning the array of sources. */

src/vite/observable.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {existsSync} from "node:fs";
12
import {readFile} from "node:fs/promises";
2-
import {resolve} from "node:path";
3+
import {dirname, join, resolve} from "node:path";
34
import {fileURLToPath} from "node:url";
45
import type {TemplateLiteral} from "acorn";
56
import {JSDOM} from "jsdom";
@@ -42,7 +43,7 @@ export function observable({
4243
},
4344
transformIndexHtml: {
4445
order: "pre",
45-
async handler(input) {
46+
async handler(input, context) {
4647
const notebook = deserialize(input, {parser});
4748
const tsource = await readFile(template, "utf-8");
4849
const document = parser.parseFromString(tsource, "text/html");
@@ -89,6 +90,9 @@ export function observable({
8990
}
9091
}
9192

93+
// Don’t error if assets are missing (matching Vite’s behavior).
94+
filterMissingAssets(assets, dirname(context.filename));
95+
9296
const output = serializer.serializeToString(document);
9397
const i = output.indexOf("</body>");
9498
if (!(i >= 0)) throw new Error("body not found");
@@ -103,16 +107,16 @@ import {define} from "observable:runtime/define";${Array.from(assets)
103107
import asset${i + 1} from ${JSON.stringify(`${asset}?url`)};`
104108
)
105109
.join("")}${
106-
assets.size > 0
107-
? `
110+
assets.size > 0
111+
? `
108112
109113
const assets = new Map([
110114
${Array.from(assets)
111115
.map((asset, i) => ` [${JSON.stringify(asset)}, asset${i + 1}]`)
112116
.join(",\n")}
113117
]);`
114-
: ""
115-
}
118+
: ""
119+
}
116120
${notebook.cells
117121
.filter((cell) => !statics.has(cell))
118122
.map((cell) => {
@@ -146,11 +150,20 @@ define(
146150
};
147151
}
148152

153+
function filterMissingAssets(assets: Set<string>, dir: string): void {
154+
for (const asset of assets) {
155+
if (!existsSync(join(dir, asset))) {
156+
console.warn(`warning: asset not found: ${asset}`);
157+
assets.delete(asset);
158+
}
159+
}
160+
}
161+
149162
function stripExpressions(template: TemplateLiteral, input: string): string {
150163
const source = new Sourcemap(input);
151164
let index = template.start;
152165
for (const q of template.quasis) {
153-
if (q.start > index) source.replaceLeft(index, q.start, "…");
166+
if (q.start > index) source.delete(index, q.start);
154167
index = q.end;
155168
}
156169
return String(source);

0 commit comments

Comments
 (0)