fix: import dialog (#10040)

* is renderer check

* 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

* fix: define __IS_RENDERER__ constant in vitest config

The __IS_RENDERER__ constant was undefined in test environments, breaking the
conditional logic in import.ts that determines whether to use the renderer IPC
path or the Node.js path. Define it as false in vitest config so tests run with
the correct code path.

(cherry picked from commit ed388d2fa9)
This commit is contained in:
Jack Kavanagh
2026-06-08 16:17:21 +02:00
committed by Insomnia
parent f365f24232
commit 2992d1d8ea
6 changed files with 14 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) =>
process.type === 'renderer'
__IS_RENDERER__
? window.main.insecureReadFile({ path })
: (await import('../main/secure-read-file')).insecureReadFile(path);
@@ -207,7 +207,9 @@ export async function scanResources(importEntries: ImportEntry[]): Promise<ScanR
};
} else {
const convertProcessFork =
process.type === 'renderer' ? window.main.parseImport : (await import('../main/importers/convert')).convert;
__IS_RENDERER__
? window.main.parseImport
: (await import('../main/importers/convert')).convert;
result = (await convertProcessFork(importEntry)) as unknown as ConvertResult;
}
} catch (err: unknown) {

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).

View File

@@ -3,6 +3,9 @@ import path from 'node:path';
import { defineConfig } from 'vitest/config';
export default defineConfig({
define: {
'__IS_RENDERER__': JSON.stringify(false),
},
test: {
setupFiles: ['./setup-vitest.ts'],
hideSkippedTests: true,