fix: use __IS_RENDERER__ constant for treeshaking node-only imports

The recent change to guard Node-only code with `typeof window !== 'undefined' && window.main != null` broke Vite's treeshaking because the expression is not statically resolvable at build time. This caused Rollup to include both the renderer (IPC) and Node-only code paths, pulling transitive imports of `node:url` and `node:crypto` into the renderer bundle.

Introduced `__IS_RENDERER__` constant in the Vite `define` block (set to `true` for the renderer build) which is statically resolvable, allowing Rollup to eliminate the Node-only branch via dead code elimination.

Also added the constant declarations to a new `types/vite.d.ts` file so TypeScript recognizes `__IS_RENDERER__` as a valid global constant.

Fixes: MISSING_EXPORT error for node:url and node:crypto in curl.ts, openapi-3.ts, and swagger-2.ts
This commit is contained in:
jackkav
2026-06-08 15:16:56 +02:00
parent 6e21dc0e28
commit c33ea973c4
5 changed files with 9 additions and 3 deletions

View File

@@ -92,7 +92,7 @@ export async function fetchImportContentFromURI({ uri }: { uri: string }) {
} else if (uri.match(/^(file):\/\//)) {
const path = uri.replace(/^(file):\/\//, '');
const readFileProcessFork = async (path: string) =>
typeof window !== 'undefined' && window.main != null
__IS_RENDERER__
? window.main.insecureReadFile({ path })
: (await import('../main/secure-read-file')).insecureReadFile(path);
@@ -207,7 +207,7 @@ export async function scanResources(importEntries: ImportEntry[]): Promise<ScanR
};
} else {
const convertProcessFork =
typeof window !== 'undefined' && window.main != null
__IS_RENDERER__
? window.main.parseImport
: (await import('../main/importers/convert')).convert;
result = (await convertProcessFork(importEntry)) as unknown as ConvertResult;

View File

@@ -31,7 +31,7 @@
"verbatimModuleSyntax": true,
"forceConsistentCasingInFileNames": true
},
"include": ["**/*", ".react-router/types/**/*"],
"include": ["types/global.d.ts", "**/*", ".react-router/types/**/*"],
"exclude": [
"./src/__mocks__/@getinsomnia/node-libcurl.ts",
"**/__mocks__",

View File

@@ -63,6 +63,7 @@ declare global {
}
declare const __DEV__: boolean;
declare const __IS_RENDERER__: boolean;
declare namespace NodeJS {
interface Global {

4
packages/insomnia/types/vite.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
/// <reference types="vite/client" />
declare const __DEV__: boolean;
declare const __IS_RENDERER__: boolean;

View File

@@ -12,6 +12,7 @@ export default defineConfig(({ mode }) => {
return {
define: {
'__DEV__': JSON.stringify(__DEV__),
'__IS_RENDERER__': JSON.stringify(true),
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.INSOMNIA_ENV': JSON.stringify(mode),
// Only apply in production builds: Rollup does text substitution (safe).