Skip to content
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
13 changes: 6 additions & 7 deletions packages/extension/tests/harness/hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@
// the agent (`tests/harness/dom.mjs`), rather than this file inventing a module
// graph the real build never has.

import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";
import { createRequire } from "node:module";

// Required rather than imported: esbuild is CJS, and the sync transform is what
// keeps this hook simple — no service to start on a loader thread.
// Required rather than imported: esbuild is CJS, and its sync transform is what
// lets these be synchronous `registerHooks` hooks — no service to start.
const esbuild = createRequire(import.meta.url)("esbuild");

/** `./x.js` → `./x.tsx` → `./x.ts`, when the `.js` is not on disk. */
export async function resolve(specifier, context, nextResolve) {
export function resolve(specifier, context, nextResolve) {
if (specifier.endsWith(".js") && (specifier.startsWith(".") || specifier.startsWith("/"))) {
const base = context.parentURL ?? pathToFileURL(`${process.cwd()}/`).href;
const asJs = new URL(specifier, base);
Expand All @@ -69,12 +68,12 @@ export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context);
}

export async function load(url, context, nextLoad) {
export function load(url, context, nextLoad) {
const isTsx = url.endsWith(".tsx");
const isTs = url.endsWith(".ts") && !url.includes("/node_modules/");
if (!isTsx && !isTs) return nextLoad(url, context);
const path = fileURLToPath(url);
const source = await readFile(path, "utf8");
const source = readFileSync(path, "utf8");
const { code } = esbuild.transformSync(source, {
loader: isTsx ? "tsx" : "ts",
// The automatic runtime, matching `tsconfig.base.json`'s `jsx: react-jsx`,
Expand Down
8 changes: 6 additions & 2 deletions packages/extension/tests/harness/register.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Installs the `.tsx` hooks. Passed to `node --import` by the test script.
import { register } from "node:module";
register("./hooks.mjs", import.meta.url);
// `registerHooks` runs the hooks synchronously on this thread; the older
// `module.register` (off-thread, async hooks) is deprecated as of Node 26
// (DEP0205).
import { registerHooks } from "node:module";
import { resolve, load } from "./hooks.mjs";
registerHooks({ resolve, load });
2 changes: 1 addition & 1 deletion packages/extension/vite.config.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default defineConfig({
// top-level-await plugin's destructuring to vite's default targets.
target: "es2022",
rollupOptions: {
input: { background: resolve(__dirname, "src/background.ts") },
input: { background: resolve(import.meta.dirname, "src/background.ts") },
output: {
entryFileNames: "background.js",
codeSplitting: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/vite.config.content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default defineConfig({
emptyOutDir: false,
target: "es2022",
rollupOptions: {
input: { content: resolve(__dirname, "src/content.ts") },
input: { content: resolve(import.meta.dirname, "src/content.ts") },
output: {
entryFileNames: "content.js",
format: "iife",
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/vite.config.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default defineConfig({
// top-level-await plugin's destructuring to vite's default targets.
target: "es2022",
rollupOptions: {
input: { manager: resolve(__dirname, "manager.html") },
input: { manager: resolve(import.meta.dirname, "manager.html") },
output: {
entryFileNames: "manager.js",
// Named so the guard can address it, and so a stray second chunk is
Expand Down
12 changes: 6 additions & 6 deletions packages/extension/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function emitManifest() {
writeBundle() {
const manifest = buildManifest({ includeKey: true });
writeFileSync(
resolve(__dirname, "dist/manifest.json"),
resolve(import.meta.dirname, "dist/manifest.json"),
`${JSON.stringify(manifest, null, 2)}\n`,
);
},
Expand Down Expand Up @@ -62,11 +62,11 @@ export default defineConfig({
modulePreload: false,
rollupOptions: {
input: {
popup: resolve(__dirname, "popup.html"),
options: resolve(__dirname, "options.html"),
confirm: resolve(__dirname, "confirm.html"),
offscreen: resolve(__dirname, "offscreen.html"),
provider: resolve(__dirname, "src/provider.ts"),
popup: resolve(import.meta.dirname, "popup.html"),
options: resolve(import.meta.dirname, "options.html"),
confirm: resolve(import.meta.dirname, "confirm.html"),
offscreen: resolve(import.meta.dirname, "offscreen.html"),
provider: resolve(import.meta.dirname, "src/provider.ts"),
},
output: {
// Fixed names for the files the manifest references by path.
Expand Down