Skip to content

Commit dfcc016

Browse files
authored
disable preload during preview (#599)
1 parent 636db46 commit dfcc016

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

bin/observable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {type ParseArgsConfig, parseArgs} from "node:util";
22
import {readConfig} from "../src/config.js";
33
import {CliError} from "../src/error.js";
4+
import {enableNpmVersionResolution, enableRemoteModulePreload} from "../src/javascript/imports.js";
45
import {red} from "../src/tty.js";
56

67
const args = process.argv.slice(2);
@@ -129,6 +130,8 @@ try {
129130
}
130131
}
131132
});
133+
enableNpmVersionResolution(false);
134+
enableRemoteModulePreload(false);
132135
await import("../src/preview.js").then(async (preview) =>
133136
preview.preview({
134137
config: await readConfig(config, root),

src/javascript/imports.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ import {getFeature, getFeatureReferenceMap, getStringLiteralValue, isStringLiter
1515
type ImportNode = ImportDeclaration | ImportExpression;
1616
type ExportNode = ExportAllDeclaration | ExportNamedDeclaration;
1717

18+
let npmVersionResolutionEnabled = true;
19+
let remoteModulePreloadEnabled = true;
20+
21+
export function enableNpmVersionResolution(enabled = true) {
22+
npmVersionResolutionEnabled = enabled;
23+
}
24+
25+
export function enableRemoteModulePreload(enabled = true) {
26+
remoteModulePreloadEnabled = enabled;
27+
}
28+
1829
export interface ImportsAndFeatures {
1930
imports: ImportReference[];
2031
features: Feature[];
@@ -305,6 +316,7 @@ function formatNpmSpecifier({name, range, path}: {name: string; range?: string;
305316
const fetchCache = new Map<string, Promise<{headers: Headers; body: any}>>();
306317

307318
async function cachedFetch(href: string): Promise<{headers: Headers; body: any}> {
319+
if (!remoteModulePreloadEnabled) throw new Error("remote module preload is not enabled");
308320
let promise = fetchCache.get(href);
309321
if (promise) return promise;
310322
promise = (async () => {
@@ -320,6 +332,7 @@ async function cachedFetch(href: string): Promise<{headers: Headers; body: any}>
320332
}
321333

322334
async function resolveNpmVersion(specifier: string): Promise<string> {
335+
if (!npmVersionResolutionEnabled) throw new Error("npm version resolution is not enabled");
323336
const {name, range} = parseNpmSpecifier(specifier); // ignore path
324337
specifier = formatNpmSpecifier({name, range});
325338
const search = range ? `?specifier=${range}` : "";
@@ -396,6 +409,7 @@ const integrityCache = new Map<string, string>();
396409
* precomputes the subresource integrity hash for each fetched module.
397410
*/
398411
export async function resolveModulePreloads(hrefs: Set<string>): Promise<void> {
412+
if (!remoteModulePreloadEnabled) return;
399413
let resolve: () => void;
400414
const visited = new Set<string>();
401415
const queue = new Set<Promise<void>>();

0 commit comments

Comments
 (0)