babel-preset-expo 13.1.5 → 13.1.6

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.
package/build/common.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { type NodePath } from '@babel/core';
2
+ import * as t from '@babel/types';
1
3
  export declare function hasModule(name: string): boolean;
2
4
  /** Determine which bundler is being used. */
3
5
  export declare function getBundler(caller?: any): "metro" | "webpack" | null;
@@ -15,3 +17,4 @@ export declare function getIsServer(caller?: any): boolean;
15
17
  export declare function getExpoRouterAbsoluteAppRoot(caller?: any): string;
16
18
  export declare function getInlineEnvVarsEnabled(caller?: any): boolean;
17
19
  export declare function getAsyncRoutes(caller?: any): boolean;
20
+ export declare function createAddNamedImportOnce(t: typeof import('@babel/types')): (path: NodePath<t.Node>, name: string, source: string) => any;
package/build/common.js CHANGED
@@ -18,6 +18,9 @@ exports.getIsServer = getIsServer;
18
18
  exports.getExpoRouterAbsoluteAppRoot = getExpoRouterAbsoluteAppRoot;
19
19
  exports.getInlineEnvVarsEnabled = getInlineEnvVarsEnabled;
20
20
  exports.getAsyncRoutes = getAsyncRoutes;
21
+ exports.createAddNamedImportOnce = createAddNamedImportOnce;
22
+ // @ts-expect-error: missing types
23
+ const helper_module_imports_1 = require("@babel/helper-module-imports");
21
24
  const node_path_1 = __importDefault(require("node:path"));
22
25
  function hasModule(name) {
23
26
  try {
@@ -122,13 +125,12 @@ function getExpoRouterAbsoluteAppRoot(caller) {
122
125
  function getInlineEnvVarsEnabled(caller) {
123
126
  assertExpoBabelCaller(caller);
124
127
  const isWebpack = getBundler(caller) === 'webpack';
125
- const isDev = getIsDev(caller);
126
128
  const isServer = getIsServer(caller);
127
129
  const isNodeModule = getIsNodeModule(caller);
128
130
  const preserveEnvVars = caller?.preserveEnvVars;
129
- // Development env vars are added in the serializer to avoid caching issues in development.
131
+ // Development env vars are added using references to enable HMR in development.
130
132
  // Servers have env vars left as-is to read from the environment.
131
- return !isNodeModule && !isWebpack && !isDev && !isServer && !preserveEnvVars;
133
+ return !isNodeModule && !isWebpack && !isServer && !preserveEnvVars;
132
134
  }
133
135
  function getAsyncRoutes(caller) {
134
136
  assertExpoBabelCaller(caller);
@@ -143,3 +145,22 @@ function getAsyncRoutes(caller) {
143
145
  }
144
146
  return caller?.asyncRoutes ?? false;
145
147
  }
148
+ const getOrCreateInMap = (map, key, create) => {
149
+ if (!map.has(key)) {
150
+ const result = create();
151
+ map.set(key, result);
152
+ return [result, true];
153
+ }
154
+ return [map.get(key), false];
155
+ };
156
+ function createAddNamedImportOnce(t) {
157
+ const addedImportsCache = new Map();
158
+ return function addNamedImportOnce(path, name, source) {
159
+ const [sourceCache] = getOrCreateInMap(addedImportsCache, source, () => new Map());
160
+ const [identifier, didCreate] = getOrCreateInMap(sourceCache, name, () => (0, helper_module_imports_1.addNamed)(path, name, source));
161
+ // for cached imports, we need to clone the resulting identifier, because otherwise
162
+ // '@babel/plugin-transform-modules-commonjs' won't replace the references to the import for some reason.
163
+ // this is a helper for that.
164
+ return didCreate ? identifier : t.cloneNode(identifier);
165
+ };
166
+ }
@@ -1,4 +1,4 @@
1
- import { ConfigAPI, PluginObj, types } from '@babel/core';
1
+ import { ConfigAPI, PluginObj, types as t } from '@babel/core';
2
2
  export declare function expoInlineEnvVars(api: ConfigAPI & {
3
- types: typeof types;
3
+ types: typeof t;
4
4
  }): PluginObj;
@@ -1,28 +1,54 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.expoInlineEnvVars = expoInlineEnvVars;
4
+ const core_1 = require("@babel/core");
5
+ const common_1 = require("./common");
4
6
  const debug = require('debug')('expo:babel:env-vars');
5
7
  function expoInlineEnvVars(api) {
6
- const { types: t } = api;
8
+ const isProduction = api.caller(common_1.getIsProd);
7
9
  function isFirstInAssign(path) {
8
- return t.isAssignmentExpression(path.parent) && path.parent.left === path.node;
10
+ return core_1.types.isAssignmentExpression(path.parent) && path.parent.left === path.node;
9
11
  }
12
+ let addEnvImport;
13
+ const publicEnvVars = new Set();
10
14
  return {
11
- name: 'expo-inline-production-environment-variables',
15
+ name: 'expo-inline-or-reference-env-vars',
16
+ pre(file) {
17
+ const addNamedImportOnce = (0, common_1.createAddNamedImportOnce)(core_1.types);
18
+ addEnvImport = () => {
19
+ return addNamedImportOnce(file.path, 'env', 'expo/virtual/env');
20
+ };
21
+ },
12
22
  visitor: {
13
23
  MemberExpression(path, state) {
14
24
  const filename = state.filename;
15
25
  if (path.get('object').matchesPattern('process.env')) {
16
- // @ts-expect-error: missing types
26
+ // @ts-expect-error
17
27
  const key = path.toComputedKey();
18
- if (t.isStringLiteral(key) &&
28
+ if (core_1.types.isStringLiteral(key) &&
19
29
  !isFirstInAssign(path) &&
20
30
  key.value.startsWith('EXPO_PUBLIC_')) {
21
- debug('Inlining environment variable in %s: %s', filename, key.value);
22
- path.replaceWith(t.valueToNode(process.env[key.value]));
31
+ const envVar = key.value;
32
+ debug(`${isProduction ? 'Inlining' : 'Referencing'} environment variable in %s: %s`, filename, envVar);
33
+ publicEnvVars.add(envVar);
34
+ if (isProduction) {
35
+ path.replaceWith(core_1.types.valueToNode(process.env[envVar]));
36
+ }
37
+ else {
38
+ path.replaceWith(core_1.types.memberExpression(addEnvImport(), core_1.types.identifier(envVar)));
39
+ }
23
40
  }
24
41
  }
25
42
  },
26
43
  },
44
+ post(file) {
45
+ assertExpoMetadata(file.metadata);
46
+ file.metadata.publicEnvVars = Array.from(publicEnvVars);
47
+ },
27
48
  };
28
49
  }
50
+ function assertExpoMetadata(metadata) {
51
+ if (!metadata || typeof metadata !== 'object') {
52
+ throw new Error('Expected Babel state.file.metadata to be an object');
53
+ }
54
+ }
@@ -44,8 +44,6 @@ var __importStar = (this && this.__importStar) || (function () {
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.reactServerActionsPlugin = reactServerActionsPlugin;
46
46
  const core_1 = require("@babel/core");
47
- // @ts-expect-error: missing types
48
- const helper_module_imports_1 = require("@babel/helper-module-imports");
49
47
  const t = __importStar(require("@babel/types"));
50
48
  const node_path_1 = require("node:path");
51
49
  const node_url_1 = __importStar(require("node:url"));
@@ -189,7 +187,7 @@ function reactServerActionsPlugin(api) {
189
187
  assertExpoMetadata(file.metadata);
190
188
  file.metadata.extractedActions = [];
191
189
  file.metadata.isModuleMarkedWithUseServerDirective = false;
192
- const addNamedImportOnce = createAddNamedImportOnce(t);
190
+ const addNamedImportOnce = (0, common_1.createAddNamedImportOnce)(t);
193
191
  addReactImport = () => {
194
192
  return addNamedImportOnce(file.path, 'registerServerReference', 'react-server-dom-webpack/server');
195
193
  };
@@ -634,26 +632,7 @@ function assertExpoMetadata(metadata) {
634
632
  throw new Error('Expected Babel state.file.metadata to be an object');
635
633
  }
636
634
  }
637
- const getOrCreateInMap = (map, key, create) => {
638
- if (!map.has(key)) {
639
- const result = create();
640
- map.set(key, result);
641
- return [result, true];
642
- }
643
- return [map.get(key), false];
644
- };
645
635
  function hasUseServerDirective(path) {
646
636
  const { body } = path.node;
647
637
  return t.isBlockStatement(body) && body.directives.some((d) => d.value.value === 'use server');
648
638
  }
649
- const createAddNamedImportOnce = (t) => {
650
- const addedImportsCache = new Map();
651
- return function addNamedImportOnce(path, name, source) {
652
- const [sourceCache] = getOrCreateInMap(addedImportsCache, source, () => new Map());
653
- const [identifier, didCreate] = getOrCreateInMap(sourceCache, name, () => (0, helper_module_imports_1.addNamed)(path, name, source));
654
- // for cached imports, we need to clone the resulting identifier, because otherwise
655
- // '@babel/plugin-transform-modules-commonjs' won't replace the references to the import for some reason.
656
- // this is a helper for that.
657
- return didCreate ? identifier : t.cloneNode(identifier);
658
- };
659
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-preset-expo",
3
- "version": "13.1.5",
3
+ "version": "13.1.6",
4
4
  "description": "The Babel preset for Expo projects",
5
5
  "main": "build/index.js",
6
6
  "files": [
@@ -76,8 +76,8 @@
76
76
  "@babel/traverse": "^7.9.0",
77
77
  "@babel/types": "^7.9.0",
78
78
  "babel-plugin-react-compiler": "^19.0.0-beta-e993439-20250405",
79
- "expo-module-scripts": "^4.1.2",
79
+ "expo-module-scripts": "^4.1.4",
80
80
  "jest": "^29.2.1"
81
81
  },
82
- "gitHead": "f1394f21ff2719a9a3037d7511db170704e5c492"
82
+ "gitHead": "dd4ea2ac865a2b0c1f4ea3fc5879d90679099862"
83
83
  }