| // Copyright 2025 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import * as fs from 'node:fs'; | |
| import {registerHooks} from 'node:module'; | |
| import * as path from 'node:path'; | |
| import {pathToFileURL} from 'node:url'; | |
| const devtoolsRoot = path.dirname(import.meta.dirname); | |
| const packageJsonURL = pathToFileURL(path.join(devtoolsRoot, 'package.json')).href; | |
| const forAgentsPackageJsonPath = path.join(devtoolsRoot, 'for_agents', 'package.json'); | |
| const forAgentsPackageJsonURL = | |
| fs.existsSync(forAgentsPackageJsonPath) ? pathToFileURL(forAgentsPackageJsonPath).href : undefined; | |
| function isBareSpecifier(specifier) { | |
| return ( | |
| !specifier.startsWith('.') && !specifier.startsWith('/') && !specifier.startsWith('node:') && | |
| !specifier.startsWith('file:')); | |
| } | |
| registerHooks({ | |
| resolve(specifier, context, nextResolve) { | |
| const bare = isBareSpecifier(specifier); | |
| if (bare && forAgentsPackageJsonURL && context.parentURL?.includes('/for_agents/')) { | |
| try { | |
| return nextResolve(specifier, {...context, parentURL: forAgentsPackageJsonURL}); | |
| } catch { | |
| // Fall through to standard resolution and devtools-frontend root. | |
| } | |
| } | |
| try { | |
| return nextResolve(specifier, context); | |
| } catch (err) { | |
| if (bare) { | |
| return nextResolve(specifier, {...context, parentURL: packageJsonURL}); | |
| } | |
| throw err; | |
| } | |
| }, | |
| }); |