|
1 | 1 | import type { Plugin } from 'vite'; |
2 | | -import { Principal } from '@dfinity/principal'; |
3 | | -import { inferCanisterIdFromLocation } from '@web3nl/my-canister-dashboard'; |
4 | 2 |
|
5 | 3 | /** |
6 | 4 | * Environment configuration interface for Internet Computer Canister Dapp |
@@ -117,7 +115,7 @@ const DEFAULT_PROD_CONFIG: CanisterDappEnvironmentConfig = { |
117 | 115 | * |
118 | 116 | * Frontend usage with helper functions: |
119 | 117 | * ```typescript |
120 | | - * import { inferEnvironment, isDevMode, inferCanisterId } from '@web3nl/vite-plugin-canister-dapp'; |
| 118 | + * import { inferEnvironment, isDevMode, inferCanisterId } from '@web3nl/vite-plugin-canister-dapp/runtime'; |
121 | 119 | * |
122 | 120 | * const config = inferEnvironment(); // Synchronous! Returns { host, identityProvider } |
123 | 121 | * const canisterId = inferCanisterId(); // Synchronous! Returns Principal |
@@ -270,165 +268,6 @@ export function canisterDappEnvironmentConfig( |
270 | 268 | }; |
271 | 269 | } |
272 | 270 |
|
273 | | -/** |
274 | | - * Detect if the application is running in development mode based on URL origin |
275 | | - * |
276 | | - * Development mode is detected when the origin: |
277 | | - * - Starts with 'http://' (not https) |
278 | | - * - Contains 'localhost' |
279 | | - * - Contains '127.0.0.1' |
280 | | - * |
281 | | - * This function is used internally by `inferEnvironment()` and `isDevMode()`. |
282 | | - * |
283 | | - * @returns true if running in development mode, false otherwise |
284 | | - */ |
285 | | -function detectDevModeFromOrigin(): boolean { |
286 | | - if (typeof window === 'undefined' || !window.location) { |
287 | | - // SSR or non-browser environment, default to production |
288 | | - return false; |
289 | | - } |
290 | | - |
291 | | - const origin = window.location.origin.toLowerCase(); |
292 | | - return ( |
293 | | - origin.startsWith('http://') || |
294 | | - origin.includes('localhost') || |
295 | | - origin.includes('127.0.0.1') |
296 | | - ); |
297 | | -} |
298 | | - |
299 | | -/** |
300 | | - * Infer the canister dapp environment at runtime based on URL origin |
301 | | - * |
302 | | - * This helper function automatically determines the environment configuration by |
303 | | - * analyzing the current URL origin. It returns the development configuration if |
304 | | - * the origin indicates a development environment (http://, localhost, or 127.0.0.1), |
305 | | - * otherwise it returns the production configuration. |
306 | | - * |
307 | | - * Both configurations are embedded in the build by the Vite plugin, so no network |
308 | | - * requests are needed. This enables building a single WASM that works in all environments. |
309 | | - * |
310 | | - * The result is cached for performance, so subsequent calls will return the cached value. |
311 | | - * |
312 | | - * Example usage: |
313 | | - * ```typescript |
314 | | - * import { inferEnvironment } from '@web3nl/vite-plugin-canister-dapp'; |
315 | | - * |
316 | | - * export function getConfig() { |
317 | | - * const config = inferEnvironment(); |
318 | | - * console.log('Running in:', config.host); |
319 | | - * return config; |
320 | | - * } |
321 | | - * ``` |
322 | | - * |
323 | | - * @returns The environment configuration (dev or prod based on URL origin) |
324 | | - */ |
325 | | -export function inferEnvironment(): CanisterDappEnvironmentConfig { |
326 | | - if (configCache !== null) { |
327 | | - return configCache; |
328 | | - } |
329 | | - |
330 | | - // Detect environment based on URL origin |
331 | | - const isDev = detectDevModeFromOrigin(); |
332 | | - |
333 | | - // Get the appropriate config from the injected global constants |
334 | | - if (isDev) { |
335 | | - configCache = |
336 | | - typeof __CANISTER_DAPP_DEV_CONFIG__ !== 'undefined' |
337 | | - ? __CANISTER_DAPP_DEV_CONFIG__ |
338 | | - : DEFAULT_DEV_CONFIG; |
339 | | - } else { |
340 | | - configCache = |
341 | | - typeof __CANISTER_DAPP_PROD_CONFIG__ !== 'undefined' |
342 | | - ? __CANISTER_DAPP_PROD_CONFIG__ |
343 | | - : DEFAULT_PROD_CONFIG; |
344 | | - } |
345 | | - |
346 | | - return configCache; |
347 | | -} |
348 | | - |
349 | | -/** |
350 | | - * Check if the application is running in development mode based on URL origin |
351 | | - * |
352 | | - * This function checks the URL origin to determine if the application is running |
353 | | - * in development mode. Development mode is detected when the origin: |
354 | | - * - Starts with 'http://' (not https) |
355 | | - * - Contains 'localhost' |
356 | | - * - Contains '127.0.0.1' |
357 | | - * |
358 | | - * The result is cached for performance, so subsequent calls will return the cached value. |
359 | | - * |
360 | | - * Example usage: |
361 | | - * ```typescript |
362 | | - * import { isDevMode } from '@web3nl/vite-plugin-canister-dapp'; |
363 | | - * |
364 | | - * if (isDevMode()) { |
365 | | - * console.log('Running in development mode'); |
366 | | - * } else { |
367 | | - * console.log('Running in production mode'); |
368 | | - * } |
369 | | - * ``` |
370 | | - * |
371 | | - * @returns true if running in development mode, false otherwise |
372 | | - */ |
373 | | -export function isDevMode(): boolean { |
374 | | - if (devModeCache !== null) { |
375 | | - return devModeCache; |
376 | | - } |
377 | | - |
378 | | - devModeCache = detectDevModeFromOrigin(); |
379 | | - return devModeCache; |
380 | | -} |
381 | | - |
382 | | -/** |
383 | | - * Infer the canister ID from the current location or fallback to viteDevCanisterId |
384 | | - * |
385 | | - * This function attempts to infer the canister ID from the URL hostname using |
386 | | - * `inferCanisterIdFromLocation` from @web3nl/my-canister-dashboard. This works in: |
387 | | - * - Production: `canister-id.icp0.io` (https only) |
388 | | - * - Local dfx: `canister-id.localhost:port` (http only) |
389 | | - * |
390 | | - * If URL-based inference fails (e.g., when running in a Vite dev server at |
391 | | - * `localhost:5173`), it falls back to the `viteDevCanisterId` configured in the |
392 | | - * Vite plugin. |
393 | | - * |
394 | | - * Example usage: |
395 | | - * ```typescript |
396 | | - * import { inferCanisterId } from '@web3nl/vite-plugin-canister-dapp'; |
397 | | - * |
398 | | - * async function initializeAgent() { |
399 | | - * const canisterId = inferCanisterId(); |
400 | | - * // Use canisterId to create actor... |
401 | | - * } |
402 | | - * ``` |
403 | | - * |
404 | | - * @returns The canister ID as a Principal |
405 | | - * @throws Error if canister ID cannot be inferred from URL and viteDevCanisterId is not configured |
406 | | - */ |
407 | | -export function inferCanisterId(): Principal { |
408 | | - try { |
409 | | - return inferCanisterIdFromLocation(); |
410 | | - } catch { |
411 | | - // URL inference failed - only happens in Vite dev server |
412 | | - if ( |
413 | | - typeof __VITE_DEV_CANISTER_ID__ !== 'undefined' && |
414 | | - __VITE_DEV_CANISTER_ID__ !== null |
415 | | - ) { |
416 | | - return Principal.fromText(__VITE_DEV_CANISTER_ID__); |
417 | | - } |
418 | | - |
419 | | - throw new Error( |
420 | | - 'Could not infer canister ID from URL. ' + |
421 | | - 'When using Vite dev server, set viteDevCanisterId in plugin config.' |
422 | | - ); |
423 | | - } |
424 | | -} |
425 | | - |
426 | | -// Cache for environment configuration |
427 | | -let configCache: CanisterDappEnvironmentConfig | null = null; |
428 | | - |
429 | | -// Cache for dev mode detection |
430 | | -let devModeCache: boolean | null = null; |
431 | | - |
432 | 271 | function notEmptyString(value: unknown): value is string { |
433 | 272 | return typeof value === 'string' && value.length > 0; |
434 | 273 | } |
0 commit comments