Skip to content

Commit 2307d36

Browse files
committed
fix: cross framework imports when bundling
1 parent b08ec08 commit 2307d36

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

packages/nativescript-inspector/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@
4545
}
4646
},
4747
"peerDependencies": {
48+
"@nativescript/angular": ">=18.0.0",
4849
"@nativescript/core": ">=8.0.0"
4950
},
51+
"peerDependenciesMeta": {
52+
"@nativescript/angular": {
53+
"optional": true
54+
}
55+
},
5056
"devDependencies": {
5157
"@nativescript/core": "~9.0.0",
5258
"typescript": "~5.8.0"

packages/nativescript-inspector/src/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ declare const CGRectMake: any;
2424
declare const CGPointMake: any;
2525
declare const CGSizeMake: any;
2626
declare const UIEdgeInsetsMake: any;
27-
declare const require: any;
2827
declare const WebSocket: any | undefined;
2928

3029
type JSONObject = Record<string, unknown>;
@@ -195,11 +194,26 @@ function installAngularSourceLocationCaptureShim(): void {
195194
return;
196195
}
197196

197+
// Resolve `@nativescript/angular` through an indirect, runtime-only require.
198+
// The literal `require("@nativescript/angular")` call would otherwise be
199+
// statically detected by web bundlers (Vite/esbuild/webpack/Rollup) running
200+
// in non-Angular host apps (Vue, Solid, Svelte, React, vanilla), causing
201+
// them to fail with "Could not resolve '@nativescript/angular'". Using a
202+
// global-scope require lookup with a non-literal module id keeps the
203+
// integration purely opt-in at runtime — present only when Angular is.
198204
const angular = safeCall(() => {
199-
if (typeof require !== "function") {
205+
const scope: any =
206+
typeof globalThis !== "undefined"
207+
? globalThis
208+
: typeof global !== "undefined"
209+
? (global as any)
210+
: null;
211+
const runtimeRequire: any = scope?.require;
212+
if (typeof runtimeRequire !== "function") {
200213
return null;
201214
}
202-
return require("@nativescript/angular");
215+
const moduleId = ["@nativescript", "angular"].join("/");
216+
return runtimeRequire(moduleId);
203217
}, null) as any;
204218
const viewUtilPrototype = angular?.ɵViewUtil?.ViewUtil?.prototype;
205219
if (

0 commit comments

Comments
 (0)