6c3d4a4af7
runtime.ts captured the SDK namespaces (the plugin SDK, React and the two jsx runtimes) in a module-scope object literal, and that module sits in an import cycle: sdk/index -> @/contrib/* -> contrib/runtime-loader -> sdk/runtime -> sdk/index. In an unbundled (dev) graph the namespace object is live, so the capture works. In a production bundle the bundler emits the SDK namespace as a hoisted `var` whose assignment lands AFTER the literal that reads it, so the captured value is `undefined` (no TDZ error) and `Object.keys(GLOBALS[globalKey])` in `shimUrl()` throws "Cannot convert undefined or null to object". That throw happens inside `loadRuntimePlugin()` -- via `unsupportedImports()` -> `sdkImportMap()` -> `shimUrl()`, which run for every source before it is evaluated -- so it is content-independent: EVERY plugin loaded from $HERMES_HOME/desktop-plugins/<id>/plugin.js (and the desktop/plugin.js half of a unified package) fails to load, showing status "failed" in Capabilities -> Plugins. Resolve the namespaces at call time instead: `installPluginSdk()` and the shim builder only ever run once the app is up, so reading them there is always safe and statement ordering can no longer matter. Repro (production build only): build apps/desktop for production, drop any plugin.js into ~/.hermes/desktop-plugins/<id>/, start the app -> [plugins] runtime load failed (<id>) TypeError: Cannot convert undefined or null to object (.../assets/sdk-<hash>.js:5). Note: a vitest unit test cannot catch this (dev module graph keeps the namespace live); the faithful guard is a production-build smoke test that loads a fixture plugin through loadRuntimePlugin().
71 lines
2.8 KiB
TypeScript
71 lines
2.8 KiB
TypeScript
/**
|
|
* Runtime SDK injection — the other half of the vscode-module model. Bundled
|
|
* plugins resolve `@hermes/plugin-sdk` through the vite alias; RUNTIME-loaded
|
|
* plugins (disk / fetched) import the same specifier and get the same object:
|
|
* the loader rewrites bare specifiers to shim modules that re-export the
|
|
* live namespaces installed here. React ships as the app's singletons —
|
|
* a second React instance would break hooks.
|
|
*/
|
|
|
|
import * as React from 'react'
|
|
import * as jsxDevRuntime from 'react/jsx-dev-runtime'
|
|
import * as jsxRuntime from 'react/jsx-runtime'
|
|
|
|
import * as sdk from './index'
|
|
|
|
// Resolved LAZILY, never as a module-scope literal. This module sits in an
|
|
// import cycle — `sdk/index` → `contrib/*` → `contrib/runtime-loader` →
|
|
// `sdk/runtime` — so a module-scope `{ __HERMES_PLUGIN_SDK__: sdk, … }` is
|
|
// evaluated BEFORE `sdk/index`'s own body runs. In the bundled app that read
|
|
// yields `undefined` (the bundler emits the namespace as a hoisted `var`), so
|
|
// `Object.keys(GLOBALS.__HERMES_PLUGIN_SDK__)` threw
|
|
// "Cannot convert undefined or null to object" and EVERY runtime (disk)
|
|
// plugin failed to load. Reading them at call time — installPluginSdk() and
|
|
// the shim builder only ever run once the app is up — gets the live
|
|
// namespaces.
|
|
function pluginNamespaces() {
|
|
return {
|
|
__HERMES_PLUGIN_SDK__: sdk,
|
|
__HERMES_REACT__: React,
|
|
__HERMES_REACT_JSX__: jsxRuntime,
|
|
__HERMES_REACT_JSX_DEV__: jsxDevRuntime
|
|
}
|
|
}
|
|
|
|
type PluginGlobalKey = keyof ReturnType<typeof pluginNamespaces>
|
|
|
|
export function installPluginSdk(): void {
|
|
Object.assign(globalThis, pluginNamespaces())
|
|
}
|
|
|
|
/** Build a shim ESM blob that re-exports a global namespace's live members.
|
|
* Export names come from the namespace itself, so the list can't drift. */
|
|
function shimUrl(globalKey: PluginGlobalKey): string {
|
|
const names = Object.keys(pluginNamespaces()[globalKey]).filter(
|
|
name => name !== 'default' && /^[A-Za-z_$][\w$]*$/.test(name)
|
|
)
|
|
|
|
const source =
|
|
`const m = globalThis.${globalKey};\n` +
|
|
`export default m.default ?? m;\n` +
|
|
// Guard the destructuring: `export const { } = m` is a syntax error, so
|
|
// only emit it when the namespace actually has named exports.
|
|
(names.length ? `export const { ${names.join(', ')} } = m;\n` : '')
|
|
|
|
return URL.createObjectURL(new Blob([source], { type: 'text/javascript' }))
|
|
}
|
|
|
|
let cached: Record<string, string> | null = null
|
|
|
|
/** Specifier -> shim URL map for the runtime loader (longest keys first). */
|
|
export function sdkImportMap(): Record<string, string> {
|
|
cached ??= {
|
|
'@hermes/plugin-sdk': shimUrl('__HERMES_PLUGIN_SDK__'),
|
|
'react/jsx-dev-runtime': shimUrl('__HERMES_REACT_JSX_DEV__'),
|
|
'react/jsx-runtime': shimUrl('__HERMES_REACT_JSX__'),
|
|
react: shimUrl('__HERMES_REACT__')
|
|
}
|
|
|
|
return cached
|
|
}
|