@vxrn/vite-plugin-metro 1.2.62 → 1.2.63-1768593796425

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/cjs/babel-plugins/import-meta-env-plugin.cjs +34 -24
  2. package/dist/cjs/babel-plugins/import-meta-env-plugin.js +31 -24
  3. package/dist/cjs/babel-plugins/import-meta-env-plugin.js.map +1 -1
  4. package/dist/cjs/babel-plugins/import-meta-env-plugin.native.js +17 -9
  5. package/dist/cjs/babel-plugins/import-meta-env-plugin.native.js.map +1 -1
  6. package/dist/cjs/env/platformEnv.cjs +65 -0
  7. package/dist/cjs/env/platformEnv.js +60 -0
  8. package/dist/cjs/env/platformEnv.js.map +6 -0
  9. package/dist/cjs/env/platformEnv.native.js +84 -0
  10. package/dist/cjs/env/platformEnv.native.js.map +1 -0
  11. package/dist/cjs/index.cjs +5 -1
  12. package/dist/cjs/index.js +4 -1
  13. package/dist/cjs/index.js.map +1 -1
  14. package/dist/cjs/index.native.js +5 -1
  15. package/dist/cjs/index.native.js.map +1 -1
  16. package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.cjs +6 -11
  17. package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.js +5 -9
  18. package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.js.map +1 -1
  19. package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.native.js +24 -14
  20. package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.native.js.map +1 -1
  21. package/dist/esm/babel-plugins/import-meta-env-plugin.js +31 -23
  22. package/dist/esm/babel-plugins/import-meta-env-plugin.js.map +1 -1
  23. package/dist/esm/babel-plugins/import-meta-env-plugin.mjs +33 -23
  24. package/dist/esm/babel-plugins/import-meta-env-plugin.mjs.map +1 -1
  25. package/dist/esm/babel-plugins/import-meta-env-plugin.native.js +17 -9
  26. package/dist/esm/babel-plugins/import-meta-env-plugin.native.js.map +1 -1
  27. package/dist/esm/env/platformEnv.js +44 -0
  28. package/dist/esm/env/platformEnv.js.map +6 -0
  29. package/dist/esm/env/platformEnv.mjs +40 -0
  30. package/dist/esm/env/platformEnv.mjs.map +1 -0
  31. package/dist/esm/env/platformEnv.native.js +56 -0
  32. package/dist/esm/env/platformEnv.native.js.map +1 -0
  33. package/dist/esm/index.js +8 -0
  34. package/dist/esm/index.js.map +1 -1
  35. package/dist/esm/index.mjs +2 -1
  36. package/dist/esm/index.mjs.map +1 -1
  37. package/dist/esm/index.native.js +2 -1
  38. package/dist/esm/index.native.js.map +1 -1
  39. package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.js +5 -9
  40. package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.js.map +1 -1
  41. package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.mjs +6 -11
  42. package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.mjs.map +1 -1
  43. package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.native.js +24 -14
  44. package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.native.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/babel-plugins/import-meta-env-plugin.ts +22 -5
  47. package/src/env/platformEnv.ts +66 -0
  48. package/src/index.ts +8 -0
  49. package/src/metro-config/getMetroBabelConfigFromViteConfig.ts +13 -24
  50. package/types/babel-plugins/import-meta-env-plugin.d.ts +7 -3
  51. package/types/babel-plugins/import-meta-env-plugin.d.ts.map +1 -1
  52. package/types/env/platformEnv.d.ts +16 -0
  53. package/types/env/platformEnv.d.ts.map +1 -0
  54. package/types/index.d.ts +1 -0
  55. package/types/index.d.ts.map +1 -1
  56. package/types/metro-config/getMetroBabelConfigFromViteConfig.d.ts +6 -0
  57. package/types/metro-config/getMetroBabelConfigFromViteConfig.d.ts.map +1 -1
@@ -1,19 +1,36 @@
1
1
  import { declare } from '@babel/helper-plugin-utils'
2
2
  import * as t from '@babel/types'
3
3
  import type { PluginObj } from '@babel/core'
4
+ import { getPlatformEnv, metroPlatformToViteEnvironment } from '../env/platformEnv'
4
5
 
5
6
  type PluginOptions = {
6
- env?: Record<string, string | undefined>
7
+ env?: Record<string, string | boolean | undefined>
7
8
  }
8
9
 
9
10
  /**
10
- * A Babel plugin to replace `import.meta.env` and `import.meta.env.*` with the provided env variables.
11
+ * Babel plugin to replace `import.meta.env` and `import.meta.env.*` with env values.
11
12
  *
12
- * This plugin may not be referenced directly since metro runs transformers in separate workers, search for `@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin` to see how it's used.
13
+ * Platform-specific env vars (VITE_ENVIRONMENT, VITE_PLATFORM, EXPO_OS, TAMAGUI_ENVIRONMENT)
14
+ * are automatically injected based on the babel caller's platform.
15
+ *
16
+ * This plugin is referenced by name since Metro runs transformers in separate workers:
17
+ * '@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin'
13
18
  */
14
19
  export const importMetaEnvPlugin = declare<PluginOptions>((api, options): PluginObj => {
15
20
  api.assertVersion(7)
16
21
 
22
+ const platform = api.caller((caller) => (caller as any)?.platform) as
23
+ | string
24
+ | null
25
+ | undefined
26
+ const platformEnv = getPlatformEnv(metroPlatformToViteEnvironment(platform))
27
+
28
+ // Platform env is authoritative and cannot be overridden
29
+ const env: Record<string, string | boolean | undefined> = {
30
+ ...options.env,
31
+ ...platformEnv,
32
+ }
33
+
17
34
  return {
18
35
  name: 'import-meta-env',
19
36
  visitor: {
@@ -27,7 +44,7 @@ export const importMetaEnvPlugin = declare<PluginOptions>((api, options): Plugin
27
44
 
28
45
  // Replace import.meta.env
29
46
  if (isImportMeta && t.isIdentifier(node.property, { name: 'env' })) {
30
- const envEntries = Object.entries(options.env ?? {}).map(([key, value]) =>
47
+ const envEntries = Object.entries(env).map(([key, value]) =>
31
48
  t.objectProperty(
32
49
  t.identifier(key),
33
50
  value === undefined ? t.identifier('undefined') : t.valueToNode(value)
@@ -53,7 +70,7 @@ export const importMetaEnvPlugin = declare<PluginOptions>((api, options): Plugin
53
70
 
54
71
  if (!envKey) return
55
72
 
56
- const value = options.env?.[envKey]
73
+ const value = env[envKey]
57
74
  path.replaceWith(
58
75
  value === undefined ? t.identifier('undefined') : t.valueToNode(value)
59
76
  )
@@ -0,0 +1,66 @@
1
+ export type Platform = 'ios' | 'android' | 'web'
2
+ export type ViteEnvironment = 'client' | 'ssr' | 'ios' | 'android'
3
+
4
+ export type PlatformEnv = {
5
+ VITE_ENVIRONMENT: ViteEnvironment
6
+ VITE_PLATFORM: 'web' | 'native'
7
+ EXPO_OS: 'web' | 'ios' | 'android'
8
+ TAMAGUI_ENVIRONMENT: ViteEnvironment
9
+ }
10
+
11
+ const platformEnvMap: Record<ViteEnvironment, PlatformEnv> = {
12
+ client: {
13
+ VITE_ENVIRONMENT: 'client',
14
+ VITE_PLATFORM: 'web',
15
+ EXPO_OS: 'web',
16
+ TAMAGUI_ENVIRONMENT: 'client',
17
+ },
18
+ ssr: {
19
+ VITE_ENVIRONMENT: 'ssr',
20
+ VITE_PLATFORM: 'web',
21
+ EXPO_OS: 'web',
22
+ TAMAGUI_ENVIRONMENT: 'ssr',
23
+ },
24
+ ios: {
25
+ VITE_ENVIRONMENT: 'ios',
26
+ VITE_PLATFORM: 'native',
27
+ EXPO_OS: 'ios',
28
+ TAMAGUI_ENVIRONMENT: 'ios',
29
+ },
30
+ android: {
31
+ VITE_ENVIRONMENT: 'android',
32
+ VITE_PLATFORM: 'native',
33
+ EXPO_OS: 'android',
34
+ TAMAGUI_ENVIRONMENT: 'android',
35
+ },
36
+ }
37
+
38
+ export function getPlatformEnv(environment: ViteEnvironment): PlatformEnv {
39
+ return platformEnvMap[environment]
40
+ }
41
+
42
+ export function metroPlatformToViteEnvironment(
43
+ platform: Platform | string | null | undefined
44
+ ): ViteEnvironment {
45
+ if (platform === 'ios') return 'ios'
46
+ if (platform === 'android') return 'android'
47
+ return 'client'
48
+ }
49
+
50
+ /**
51
+ * Format platform env for Vite's define config.
52
+ * Returns both process.env.* and import.meta.env.* definitions.
53
+ */
54
+ export function getPlatformEnvDefine(
55
+ environment: ViteEnvironment
56
+ ): Record<string, string> {
57
+ const env = getPlatformEnv(environment)
58
+ const define: Record<string, string> = {}
59
+
60
+ for (const [key, value] of Object.entries(env)) {
61
+ define[`process.env.${key}`] = JSON.stringify(value)
62
+ define[`import.meta.env.${key}`] = JSON.stringify(value)
63
+ }
64
+
65
+ return define
66
+ }
package/src/index.ts CHANGED
@@ -2,3 +2,11 @@ export { metroPlugin } from './plugins/metroPlugin'
2
2
  export type { MetroPluginOptions } from './plugins/metroPlugin'
3
3
  export { expoManifestRequestHandlerPlugin } from './plugins/expoManifestRequestHandlerPlugin'
4
4
  export type { ExpoManifestRequestHandlerPluginPluginOptions } from './plugins/expoManifestRequestHandlerPlugin'
5
+ export {
6
+ getPlatformEnv,
7
+ getPlatformEnvDefine,
8
+ metroPlatformToViteEnvironment,
9
+ type Platform,
10
+ type PlatformEnv,
11
+ type ViteEnvironment,
12
+ } from './env/platformEnv'
@@ -1,45 +1,34 @@
1
1
  import type { ResolvedConfig } from 'vite'
2
-
3
- // For Metro and Expo, we only import types here.
4
- // We use `projectImport` to dynamically import the actual modules
5
- // at runtime to ensure they are loaded from the user's project root.
6
- import type { loadConfig as loadConfigT } from 'metro'
7
-
8
2
  import type { TransformOptions } from '@babel/core'
9
3
 
10
- type MetroInputConfig = Parameters<typeof loadConfigT>[1]
11
-
4
+ /**
5
+ * Creates babel config for Metro transforms from Vite config.
6
+ *
7
+ * Platform-specific env vars (VITE_ENVIRONMENT, VITE_PLATFORM, EXPO_OS, TAMAGUI_ENVIRONMENT)
8
+ * are handled by the import-meta-env-plugin based on caller.platform and always take precedence.
9
+ */
12
10
  export function getMetroBabelConfigFromViteConfig(
13
11
  config: ResolvedConfig
14
12
  ): TransformOptions {
15
- const importMetaEnv = {
16
- // https://vite.dev/guide/env-and-mode.html#built-in-constants
13
+ const importMetaEnv: Record<string, string | boolean | undefined> = {
17
14
  MODE: config.mode,
18
- BASE_URL: 'https://vxrn.not.supported.yet',
15
+ BASE_URL: config.base,
19
16
  PROD: config.mode === 'production',
20
17
  DEV: config.mode === 'development',
21
18
  SSR: false,
22
19
  }
23
20
 
24
- // Collect from process.env
25
21
  const envPrefix = config.envPrefix || 'VITE_'
26
- if (envPrefix) {
27
- Object.keys(config.env).forEach((key) => {
28
- const shouldInclude = Array.isArray(envPrefix)
29
- ? envPrefix.some((p) => key.startsWith(p))
30
- : key.startsWith(envPrefix)
22
+ const prefixes = Array.isArray(envPrefix) ? envPrefix : [envPrefix]
31
23
 
32
- if (shouldInclude) {
33
- importMetaEnv[key] = process.env[key]
34
- }
35
- })
24
+ for (const key of Object.keys(config.env)) {
25
+ if (prefixes.some((p) => key.startsWith(p))) {
26
+ importMetaEnv[key] = process.env[key]
27
+ }
36
28
  }
37
29
 
38
30
  return {
39
31
  plugins: [
40
- // Note that we can only pass Babel plugins by name here,
41
- // since Metro will run Babel transformers in different threads
42
- // and plugins are not serializable, it seems.
43
32
  [
44
33
  '@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin',
45
34
  { env: importMetaEnv },
@@ -1,11 +1,15 @@
1
1
  import type { PluginObj } from '@babel/core';
2
2
  type PluginOptions = {
3
- env?: Record<string, string | undefined>;
3
+ env?: Record<string, string | boolean | undefined>;
4
4
  };
5
5
  /**
6
- * A Babel plugin to replace `import.meta.env` and `import.meta.env.*` with the provided env variables.
6
+ * Babel plugin to replace `import.meta.env` and `import.meta.env.*` with env values.
7
7
  *
8
- * This plugin may not be referenced directly since metro runs transformers in separate workers, search for `@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin` to see how it's used.
8
+ * Platform-specific env vars (VITE_ENVIRONMENT, VITE_PLATFORM, EXPO_OS, TAMAGUI_ENVIRONMENT)
9
+ * are automatically injected based on the babel caller's platform.
10
+ *
11
+ * This plugin is referenced by name since Metro runs transformers in separate workers:
12
+ * '@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin'
9
13
  */
10
14
  export declare const importMetaEnvPlugin: (api: object, options: PluginOptions | null | undefined, dirname: string) => PluginObj<import("@babel/core").PluginPass>;
11
15
  export default importMetaEnvPlugin;
@@ -1 +1 @@
1
- {"version":3,"file":"import-meta-env-plugin.d.ts","sourceRoot":"","sources":["../../src/babel-plugins/import-meta-env-plugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE5C,KAAK,aAAa,GAAG;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;CACzC,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,0HAkD9B,CAAA;AAEF,eAAe,mBAAmB,CAAA"}
1
+ {"version":3,"file":"import-meta-env-plugin.d.ts","sourceRoot":"","sources":["../../src/babel-plugins/import-meta-env-plugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAG5C,KAAK,aAAa,GAAG;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC,CAAA;CACnD,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,0HA8D9B,CAAA;AAEF,eAAe,mBAAmB,CAAA"}
@@ -0,0 +1,16 @@
1
+ export type Platform = 'ios' | 'android' | 'web';
2
+ export type ViteEnvironment = 'client' | 'ssr' | 'ios' | 'android';
3
+ export type PlatformEnv = {
4
+ VITE_ENVIRONMENT: ViteEnvironment;
5
+ VITE_PLATFORM: 'web' | 'native';
6
+ EXPO_OS: 'web' | 'ios' | 'android';
7
+ TAMAGUI_ENVIRONMENT: ViteEnvironment;
8
+ };
9
+ export declare function getPlatformEnv(environment: ViteEnvironment): PlatformEnv;
10
+ export declare function metroPlatformToViteEnvironment(platform: Platform | string | null | undefined): ViteEnvironment;
11
+ /**
12
+ * Format platform env for Vite's define config.
13
+ * Returns both process.env.* and import.meta.env.* definitions.
14
+ */
15
+ export declare function getPlatformEnvDefine(environment: ViteEnvironment): Record<string, string>;
16
+ //# sourceMappingURL=platformEnv.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platformEnv.d.ts","sourceRoot":"","sources":["../../src/env/platformEnv.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,KAAK,GAAG,SAAS,GAAG,KAAK,CAAA;AAChD,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,KAAK,GAAG,KAAK,GAAG,SAAS,CAAA;AAElE,MAAM,MAAM,WAAW,GAAG;IACxB,gBAAgB,EAAE,eAAe,CAAA;IACjC,aAAa,EAAE,KAAK,GAAG,QAAQ,CAAA;IAC/B,OAAO,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAA;IAClC,mBAAmB,EAAE,eAAe,CAAA;CACrC,CAAA;AA6BD,wBAAgB,cAAc,CAAC,WAAW,EAAE,eAAe,GAAG,WAAW,CAExE;AAED,wBAAgB,8BAA8B,CAC5C,QAAQ,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAC7C,eAAe,CAIjB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,eAAe,GAC3B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAUxB"}
package/types/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export { metroPlugin } from './plugins/metroPlugin';
2
2
  export type { MetroPluginOptions } from './plugins/metroPlugin';
3
3
  export { expoManifestRequestHandlerPlugin } from './plugins/expoManifestRequestHandlerPlugin';
4
4
  export type { ExpoManifestRequestHandlerPluginPluginOptions } from './plugins/expoManifestRequestHandlerPlugin';
5
+ export { getPlatformEnv, getPlatformEnvDefine, metroPlatformToViteEnvironment, type Platform, type PlatformEnv, type ViteEnvironment, } from './env/platformEnv';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,YAAY,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC/D,OAAO,EAAE,gCAAgC,EAAE,MAAM,4CAA4C,CAAA;AAC7F,YAAY,EAAE,6CAA6C,EAAE,MAAM,4CAA4C,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,YAAY,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC/D,OAAO,EAAE,gCAAgC,EAAE,MAAM,4CAA4C,CAAA;AAC7F,YAAY,EAAE,6CAA6C,EAAE,MAAM,4CAA4C,CAAA;AAC/G,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,8BAA8B,EAC9B,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAA"}
@@ -1,4 +1,10 @@
1
1
  import type { ResolvedConfig } from 'vite';
2
2
  import type { TransformOptions } from '@babel/core';
3
+ /**
4
+ * Creates babel config for Metro transforms from Vite config.
5
+ *
6
+ * Platform-specific env vars (VITE_ENVIRONMENT, VITE_PLATFORM, EXPO_OS, TAMAGUI_ENVIRONMENT)
7
+ * are handled by the import-meta-env-plugin based on caller.platform and always take precedence.
8
+ */
3
9
  export declare function getMetroBabelConfigFromViteConfig(config: ResolvedConfig): TransformOptions;
4
10
  //# sourceMappingURL=getMetroBabelConfigFromViteConfig.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getMetroBabelConfigFromViteConfig.d.ts","sourceRoot":"","sources":["../../src/metro-config/getMetroBabelConfigFromViteConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAO1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAInD,wBAAgB,iCAAiC,CAC/C,MAAM,EAAE,cAAc,GACrB,gBAAgB,CAmClB"}
1
+ {"version":3,"file":"getMetroBabelConfigFromViteConfig.d.ts","sourceRoot":"","sources":["../../src/metro-config/getMetroBabelConfigFromViteConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAC1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD;;;;;GAKG;AACH,wBAAgB,iCAAiC,CAC/C,MAAM,EAAE,cAAc,GACrB,gBAAgB,CA0BlB"}