babel-preset-expo 9.8.0 → 9.9.0

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/README.md CHANGED
@@ -62,7 +62,7 @@ If the `bundler` is not defined, it will default to checking if a `babel-loader`
62
62
  ];
63
63
  ```
64
64
 
65
- This property is passed down to [`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx). This flag does nothing when `native.useTransformReactJSXExperimental` is set to `true` because `@babel/plugin-transform-react-jsx` is omitted.
65
+ This property is passed down to [`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx). This flag does nothing when `useTransformReactJSXExperimental` is set to `true` because `@babel/plugin-transform-react-jsx` is omitted.
66
66
 
67
67
  ### [`jsxImportSource`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#importsource)
68
68
 
@@ -115,7 +115,7 @@ The value of `lazyImports` has a few possible effects:
115
115
  ],
116
116
  ```
117
117
 
118
- ### `web.disableImportExportTransform`
118
+ ### `disableImportExportTransform`
119
119
 
120
120
  Enabling this option will allow your project to run with older JavaScript syntax (i.e. `module.exports`). This option will break tree shaking and increase your bundle size, but will eliminate the following error when `module.exports` is used:
121
121
 
@@ -127,7 +127,40 @@ Enabling this option will allow your project to run with older JavaScript syntax
127
127
  [
128
128
  'babel-preset-expo',
129
129
  {
130
- web: { disableImportExportTransform: true }
130
+ disableImportExportTransform: true
131
131
  }
132
132
  ],
133
133
  ```
134
+
135
+ ### `unstable_transformProfile`
136
+
137
+ Changes the engine preset in `metro-react-native-babel-preset` based on the JavaScript engine that is being targeted. In Expo SDK 50 and greater, this is automatically set based on the [`jsEngine`](https://docs.expo.dev/versions/latest/config/app/#jsengine) option in your `app.json`.
138
+
139
+ ### `enableBabelRuntime`
140
+
141
+ Passed to `metro-react-native-babel-preset`.
142
+
143
+ ### `disableFlowStripTypesTransform`
144
+
145
+ Passed to `metro-react-native-babel-preset`.
146
+
147
+ ## Platform-specific options
148
+
149
+ All options can be passed in the platform-specific objects `native` and `web` to provide different settings on different platforms. For example, if you'd like to only apply `disableImportExportTransform` on web, use the following:
150
+
151
+ ```js
152
+ [
153
+ 'babel-preset-expo',
154
+ {
155
+ // Default value:
156
+ disableImportExportTransform: false,
157
+
158
+ web: {
159
+ // Web-specific value:
160
+ disableImportExportTransform: true,
161
+ },
162
+ },
163
+ ];
164
+ ```
165
+
166
+ Platform-specific options have higher priority over top-level options.
package/build/common.d.ts CHANGED
@@ -3,3 +3,8 @@ export declare function hasModule(name: string): boolean;
3
3
  export declare function getBundler(caller: any): any;
4
4
  export declare function getPlatform(caller: any): any;
5
5
  export declare function getPossibleProjectRoot(caller: any): any;
6
+ export declare function getIsDev(caller: any): any;
7
+ export declare function getIsProd(caller: any): boolean;
8
+ export declare function getBaseUrl(caller: any): string;
9
+ export declare function getIsServer(caller: any): any;
10
+ export declare function getInlineEnvVarsEnabled(caller: any): boolean;
package/build/common.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPossibleProjectRoot = exports.getPlatform = exports.getBundler = exports.hasModule = void 0;
3
+ exports.getInlineEnvVarsEnabled = exports.getIsServer = exports.getBaseUrl = exports.getIsProd = exports.getIsDev = exports.getPossibleProjectRoot = exports.getPlatform = exports.getBundler = exports.hasModule = void 0;
4
4
  function hasModule(name) {
5
5
  try {
6
6
  return !!require.resolve(name);
@@ -52,3 +52,35 @@ function getPossibleProjectRoot(caller) {
52
52
  return process.env.EXPO_PROJECT_ROOT;
53
53
  }
54
54
  exports.getPossibleProjectRoot = getPossibleProjectRoot;
55
+ function getIsDev(caller) {
56
+ if (caller?.isDev != null)
57
+ return caller.isDev;
58
+ // https://babeljs.io/docs/options#envname
59
+ return process.env.BABEL_ENV === 'development' || process.env.NODE_ENV === 'development';
60
+ }
61
+ exports.getIsDev = getIsDev;
62
+ function getIsProd(caller) {
63
+ if (caller?.isDev != null)
64
+ return caller.isDev === false;
65
+ // https://babeljs.io/docs/options#envname
66
+ return process.env.BABEL_ENV === 'production' || process.env.NODE_ENV === 'production';
67
+ }
68
+ exports.getIsProd = getIsProd;
69
+ function getBaseUrl(caller) {
70
+ return caller?.baseUrl ?? '';
71
+ }
72
+ exports.getBaseUrl = getBaseUrl;
73
+ function getIsServer(caller) {
74
+ return caller?.isServer ?? false;
75
+ }
76
+ exports.getIsServer = getIsServer;
77
+ function getInlineEnvVarsEnabled(caller) {
78
+ const isWebpack = getBundler(caller) === 'webpack';
79
+ const isDev = getIsDev(caller);
80
+ const isServer = getIsServer(caller);
81
+ const preserveEnvVars = caller?.preserveEnvVars;
82
+ // Development env vars are added in the serializer to avoid caching issues in development.
83
+ // Servers have env vars left as-is to read from the environment.
84
+ return !isWebpack && !isDev && !isServer && !preserveEnvVars;
85
+ }
86
+ exports.getInlineEnvVarsEnabled = getInlineEnvVarsEnabled;
package/build/index.d.ts CHANGED
@@ -1,18 +1,23 @@
1
1
  import { ConfigAPI, TransformOptions } from '@babel/core';
2
2
  type BabelPresetExpoPlatformOptions = {
3
+ /** Enable or disable adding the Reanimated plugin by default. @default `true` */
4
+ reanimated?: boolean;
5
+ /** @deprecated Set `jsxRuntime: 'classic'` to disable automatic JSX handling. */
3
6
  useTransformReactJSXExperimental?: boolean;
7
+ /** Change the policy for handling JSX in a file. Passed to `plugin-transform-react-jsx`. @default `'automatic'` */
8
+ jsxRuntime?: 'classic' | 'automatic';
9
+ /** Change the source module ID to use when importing an automatic JSX import. Only applied when `jsxRuntime` is `'automatic'` (default). Passed to `plugin-transform-react-jsx`. @default `'react'` */
10
+ jsxImportSource?: string;
11
+ lazyImports?: boolean;
4
12
  disableImportExportTransform?: boolean;
5
- withDevTools?: boolean;
6
13
  disableFlowStripTypesTransform?: boolean;
7
14
  enableBabelRuntime?: boolean;
8
15
  unstable_transformProfile?: 'default' | 'hermes-stable' | 'hermes-canary';
9
16
  };
10
- export type BabelPresetExpoOptions = {
11
- lazyImports?: boolean;
12
- reanimated?: boolean;
13
- jsxRuntime?: 'classic' | 'automatic';
14
- jsxImportSource?: string;
17
+ export type BabelPresetExpoOptions = BabelPresetExpoPlatformOptions & {
18
+ /** Web-specific settings. */
15
19
  web?: BabelPresetExpoPlatformOptions;
20
+ /** Native-specific settings. */
16
21
  native?: BabelPresetExpoPlatformOptions;
17
22
  };
18
23
  declare function babelPresetExpo(api: ConfigAPI, options?: BabelPresetExpoOptions): TransformOptions;
package/build/index.js CHANGED
@@ -3,34 +3,48 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const common_1 = require("./common");
4
4
  const expo_inline_manifest_plugin_1 = require("./expo-inline-manifest-plugin");
5
5
  const expo_router_plugin_1 = require("./expo-router-plugin");
6
+ const inline_env_vars_1 = require("./inline-env-vars");
6
7
  const lazyImports_1 = require("./lazyImports");
8
+ function getOptions(options, platform) {
9
+ const tag = platform === 'web' ? 'web' : 'native';
10
+ return {
11
+ ...options,
12
+ ...options[tag],
13
+ };
14
+ }
7
15
  function babelPresetExpo(api, options = {}) {
8
- const { web = {}, native = {}, reanimated } = options;
9
16
  const bundler = api.caller(common_1.getBundler);
10
17
  const isWebpack = bundler === 'webpack';
11
18
  let platform = api.caller((caller) => caller?.platform);
12
19
  const engine = api.caller((caller) => caller?.engine) ?? 'default';
20
+ const isDev = api.caller(common_1.getIsDev);
21
+ const baseUrl = api.caller(common_1.getBaseUrl);
22
+ // Unlike `isDev`, this will be `true` when the bundler is explicitly set to `production`,
23
+ // i.e. `false` when testing, development, or used with a bundler that doesn't specify the correct inputs.
24
+ const isProduction = api.caller(common_1.getIsProd);
25
+ const inlineEnvironmentVariables = api.caller(common_1.getInlineEnvVarsEnabled);
13
26
  // If the `platform` prop is not defined then this must be a custom config that isn't
14
27
  // defining a platform in the babel-loader. Currently this may happen with Next.js + Expo web.
15
28
  if (!platform && isWebpack) {
16
29
  platform = 'web';
17
30
  }
18
- const platformOptions = platform === 'web'
19
- ? {
31
+ const platformOptions = getOptions(options, platform);
32
+ if (platformOptions.disableImportExportTransform == null) {
33
+ if (platform === 'web') {
20
34
  // Only disable import/export transform when Webpack is used because
21
35
  // Metro does not support tree-shaking.
22
- disableImportExportTransform: isWebpack,
23
- unstable_transformProfile: engine === 'hermes' ? 'hermes-stable' : 'default',
24
- ...web,
36
+ platformOptions.disableImportExportTransform = isWebpack;
25
37
  }
26
- : {
27
- disableImportExportTransform: false,
28
- unstable_transformProfile: engine === 'hermes' ? 'hermes-stable' : 'default',
29
- ...native,
30
- };
38
+ else {
39
+ platformOptions.disableImportExportTransform = false;
40
+ }
41
+ }
42
+ if (platformOptions.unstable_transformProfile == null) {
43
+ platformOptions.unstable_transformProfile = engine === 'hermes' ? 'hermes-stable' : 'default';
44
+ }
31
45
  // Note that if `options.lazyImports` is not set (i.e., `null` or `undefined`),
32
46
  // `metro-react-native-babel-preset` will handle it.
33
- const lazyImportsOption = options?.lazyImports;
47
+ const lazyImportsOption = platformOptions?.lazyImports;
34
48
  const extraPlugins = [];
35
49
  if (engine !== 'hermes') {
36
50
  // `metro-react-native-babel-preset` configures this plugin with `{ loose: true }`, which breaks all
@@ -41,31 +55,52 @@ function babelPresetExpo(api, options = {}) {
41
55
  { loose: false },
42
56
  ]);
43
57
  }
44
- // Set true to disable `@babel/plugin-transform-react-jsx`
45
- // we override this logic outside of the metro preset so we can add support for
46
- // React 17 automatic JSX transformations.
47
- // If the logic for `useTransformReactJSXExperimental` ever changes in `metro-react-native-babel-preset`
48
- // then this block should be updated to reflect those changes.
49
- if (!platformOptions.useTransformReactJSXExperimental) {
58
+ else {
59
+ // This is added back on hermes to ensure the react-jsx-dev plugin (`@babel/preset-react`) works as expected when
60
+ // JSX is used in a function body. This is technically not required in production, but we
61
+ // should retain the same behavior since it's hard to debug the differences.
62
+ extraPlugins.push(require('@babel/plugin-transform-parameters'));
63
+ }
64
+ if (isProduction && (0, common_1.hasModule)('metro-transform-plugins')) {
65
+ // Metro applies this plugin too but it does it after the imports have been transformed which breaks
66
+ // the plugin. Here, we'll apply it before the commonjs transform, in production, to ensure `Platform.OS`
67
+ // is replaced with a string literal and `__DEV__` is converted to a boolean.
68
+ // Applying early also means that web can be transformed before the `react-native-web` transform mutates the import.
50
69
  extraPlugins.push([
51
- require('@babel/plugin-transform-react-jsx'),
70
+ require('metro-transform-plugins/src/inline-plugin.js'),
52
71
  {
53
- // Defaults to `automatic`, pass in `classic` to disable auto JSX transformations.
54
- runtime: (options && options.jsxRuntime) || 'automatic',
55
- ...(options &&
56
- options.jsxRuntime !== 'classic' && {
57
- importSource: (options && options.jsxImportSource) || 'react',
58
- }),
72
+ dev: isDev,
73
+ inlinePlatform: true,
74
+ platform,
59
75
  },
60
76
  ]);
61
- // Purposefully not adding the deprecated packages:
62
- // `@babel/plugin-transform-react-jsx-self` and `@babel/plugin-transform-react-jsx-source`
63
- // back to the preset.
77
+ }
78
+ if (platformOptions.useTransformReactJSXExperimental != null) {
79
+ throw new Error(`babel-preset-expo: The option 'useTransformReactJSXExperimental' has been removed in favor of { jsxRuntime: 'classic' }.`);
64
80
  }
65
81
  const aliasPlugin = getAliasPlugin();
66
82
  if (aliasPlugin) {
67
83
  extraPlugins.push(aliasPlugin);
68
84
  }
85
+ // Allow jest tests to redefine the environment variables.
86
+ if (process.env.NODE_ENV !== 'test') {
87
+ extraPlugins.push([
88
+ inline_env_vars_1.expoInlineTransformEnvVars,
89
+ {
90
+ // These values should not be prefixed with `EXPO_PUBLIC_`, so we don't
91
+ // squat user-defined environment variables.
92
+ EXPO_BASE_URL: baseUrl,
93
+ },
94
+ ]);
95
+ }
96
+ // Only apply in non-server, for metro-only, in production environments, when the user hasn't disabled the feature.
97
+ // Webpack uses DefinePlugin for environment variables.
98
+ // Development uses an uncached serializer.
99
+ // Servers read from the environment.
100
+ // Users who disable the feature may be using a different babel plugin.
101
+ if (inlineEnvironmentVariables) {
102
+ extraPlugins.push(inline_env_vars_1.expoInlineEnvVars);
103
+ }
69
104
  if (platform === 'web') {
70
105
  extraPlugins.push(require.resolve('babel-plugin-react-native-web'));
71
106
  // Webpack uses the DefinePlugin to provide the manifest to `expo-constants`.
@@ -85,8 +120,6 @@ function babelPresetExpo(api, options = {}) {
85
120
  // Reference: https://github.com/expo/expo/pull/4685#discussion_r307143920
86
121
  require('metro-react-native-babel-preset'),
87
122
  {
88
- // Defaults to undefined, set to something truthy to disable `@babel/plugin-transform-react-jsx-self` and `@babel/plugin-transform-react-jsx-source`.
89
- withDevTools: platformOptions.withDevTools,
90
123
  // Defaults to undefined, set to `true` to disable `@babel/plugin-transform-flow-strip-types`
91
124
  disableFlowStripTypesTransform: platformOptions.disableFlowStripTypesTransform,
92
125
  // Defaults to undefined, set to `false` to disable `@babel/plugin-transform-runtime`
@@ -101,6 +134,9 @@ function babelPresetExpo(api, options = {}) {
101
134
  // TransformError App.js: /path/to/App.js: Duplicate __self prop found. You are most likely using the deprecated transform-react-jsx-self Babel plugin.
102
135
  // Both __source and __self are automatically set when using the automatic jsxRuntime. Please remove transform-react-jsx-source and transform-react-jsx-self from your Babel config.
103
136
  useTransformReactJSXExperimental: true,
137
+ // This will never be used regardless because `useTransformReactJSXExperimental` is set to `true`.
138
+ // https://github.com/facebook/react-native/blob/a4a8695cec640e5cf12be36a0c871115fbce9c87/packages/react-native-babel-preset/src/configs/main.js#L151
139
+ withDevTools: false,
104
140
  disableImportExportTransform: platformOptions.disableImportExportTransform,
105
141
  lazyImportExportTransform: lazyImportsOption === true
106
142
  ? (importModuleSpecifier) => {
@@ -113,16 +149,40 @@ function babelPresetExpo(api, options = {}) {
113
149
  lazyImportsOption,
114
150
  },
115
151
  ],
152
+ // React support with similar options to Metro.
153
+ // We override this logic outside of the metro preset so we can add support for
154
+ // React 17 automatic JSX transformations.
155
+ // The only known issue is the plugin `@babel/plugin-transform-react-display-name` will be run twice,
156
+ // once in the Metro plugin, and another time here.
157
+ [
158
+ require('@babel/preset-react'),
159
+ {
160
+ development: isDev,
161
+ // Defaults to `automatic`, pass in `classic` to disable auto JSX transformations.
162
+ runtime: platformOptions?.jsxRuntime || 'automatic',
163
+ ...(platformOptions &&
164
+ platformOptions.jsxRuntime !== 'classic' && {
165
+ importSource: (platformOptions && platformOptions.jsxImportSource) || 'react',
166
+ }),
167
+ // NOTE: Unexposed props:
168
+ // pragma?: string;
169
+ // pragmaFrag?: string;
170
+ // pure?: string;
171
+ // throwIfNamespace?: boolean;
172
+ // useBuiltIns?: boolean;
173
+ // useSpread?: boolean;
174
+ },
175
+ ],
116
176
  ],
117
177
  plugins: [
118
178
  ...extraPlugins,
119
179
  // TODO: Remove
120
180
  [require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
121
- require.resolve('@babel/plugin-proposal-export-namespace-from'),
181
+ require.resolve('@babel/plugin-transform-export-namespace-from'),
122
182
  // Automatically add `react-native-reanimated/plugin` when the package is installed.
123
183
  // TODO: Move to be a customTransformOption.
124
184
  (0, common_1.hasModule)('react-native-reanimated') &&
125
- reanimated !== false && [require.resolve('react-native-reanimated/plugin')],
185
+ platformOptions.reanimated !== false && [require.resolve('react-native-reanimated/plugin')],
126
186
  ].filter(Boolean),
127
187
  };
128
188
  }
@@ -0,0 +1,13 @@
1
+ import { ConfigAPI, PluginObj, types } from '@babel/core';
2
+ export declare function expoInlineEnvVars(api: ConfigAPI & {
3
+ types: typeof types;
4
+ }): PluginObj;
5
+ /**
6
+ * Given a set of options like `{ EXPO_BASE_URL: '/' }`, inline the values into the bundle.
7
+ * This is used for build settings that are always available and not configurable at runtime.
8
+ *
9
+ * Webpack uses DefinePlugin for similar functionality.
10
+ */
11
+ export declare function expoInlineTransformEnvVars(api: ConfigAPI & {
12
+ types: typeof types;
13
+ }): PluginObj;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expoInlineTransformEnvVars = exports.expoInlineEnvVars = void 0;
4
+ const debug = require('debug')('expo:babel:env-vars');
5
+ function expoInlineEnvVars(api) {
6
+ const { types: t } = api;
7
+ function isFirstInAssign(path) {
8
+ return t.isAssignmentExpression(path.parent) && path.parent.left === path.node;
9
+ }
10
+ return {
11
+ name: 'expo-inline-production-environment-variables',
12
+ visitor: {
13
+ MemberExpression(path, state) {
14
+ const filename = state.filename;
15
+ // If the filename is not defined, skip to prevent a node module from intercepting
16
+ // environment variables.
17
+ if (!filename) {
18
+ debug('No filename found in state, skipping to be safe:', state.file.opts);
19
+ return;
20
+ }
21
+ // Do nothing in node modules
22
+ // Skip node_modules, the feature is a bit too sensitive to allow in arbitrary code.
23
+ if (/node_modules/.test(filename)) {
24
+ return;
25
+ }
26
+ if (path.get('object').matchesPattern('process.env')) {
27
+ // @ts-expect-error: missing types
28
+ const key = path.toComputedKey();
29
+ if (t.isStringLiteral(key) &&
30
+ !isFirstInAssign(path) &&
31
+ key.value.startsWith('EXPO_PUBLIC_')) {
32
+ debug('Inlining environment variable in %s: %s', filename, key.value);
33
+ path.replaceWith(t.valueToNode(process.env[key.value]));
34
+ }
35
+ }
36
+ },
37
+ },
38
+ };
39
+ }
40
+ exports.expoInlineEnvVars = expoInlineEnvVars;
41
+ /**
42
+ * Given a set of options like `{ EXPO_BASE_URL: '/' }`, inline the values into the bundle.
43
+ * This is used for build settings that are always available and not configurable at runtime.
44
+ *
45
+ * Webpack uses DefinePlugin for similar functionality.
46
+ */
47
+ function expoInlineTransformEnvVars(api) {
48
+ const { types: t } = api;
49
+ function isFirstInAssign(path) {
50
+ return t.isAssignmentExpression(path.parent) && path.parent.left === path.node;
51
+ }
52
+ return {
53
+ name: 'expo-inline-transform-environment-variables',
54
+ visitor: {
55
+ MemberExpression(path, state) {
56
+ const options = state.opts;
57
+ if (path.get('object').matchesPattern('process.env')) {
58
+ // @ts-expect-error: missing types
59
+ const key = path.toComputedKey();
60
+ if (t.isStringLiteral(key) &&
61
+ !isFirstInAssign(path) &&
62
+ options[key.value] !== undefined) {
63
+ debug('Inlining transform setting in %s: %s', state.filename || '[unknown file]', key.value);
64
+ path.replaceWith(t.valueToNode(options[key.value]));
65
+ }
66
+ }
67
+ },
68
+ },
69
+ };
70
+ }
71
+ exports.expoInlineTransformEnvVars = expoInlineTransformEnvVars;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-preset-expo",
3
- "version": "9.8.0",
3
+ "version": "9.9.0",
4
4
  "description": "The Babel preset for Expo projects",
5
5
  "main": "build/index.js",
6
6
  "files": [
@@ -42,10 +42,11 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@babel/plugin-proposal-decorators": "^7.12.9",
45
- "@babel/plugin-proposal-export-namespace-from": "^7.18.9",
46
45
  "@babel/plugin-proposal-object-rest-spread": "^7.12.13",
47
- "@babel/plugin-transform-react-jsx": "^7.12.17",
46
+ "@babel/plugin-transform-export-namespace-from": "^7.22.11",
48
47
  "@babel/preset-env": "^7.20.0",
48
+ "@babel/preset-react": "^7.22.15",
49
+ "@babel/plugin-transform-parameters": "^7.22.15",
49
50
  "babel-plugin-module-resolver": "^5.0.0",
50
51
  "babel-plugin-react-native-web": "~0.18.10",
51
52
  "metro-react-native-babel-preset": "0.76.8"
@@ -54,5 +55,5 @@
54
55
  "@babel/core": "^7.20.0",
55
56
  "jest": "^29.2.1"
56
57
  },
57
- "gitHead": "ee7897097f5f946ad7fcb94447eed789b984dd02"
58
+ "gitHead": "3142a086578deffd8704a8f1b6f0f661527d836c"
58
59
  }