babel-preset-expo 8.4.0 → 9.0.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.
Files changed (3) hide show
  1. package/README.md +36 -0
  2. package/index.js +59 -5
  3. package/package.json +10 -9
package/README.md CHANGED
@@ -42,6 +42,42 @@ If the `bundler` is not defined, it will default to checking if a `babel-loader`
42
42
 
43
43
  ## Options
44
44
 
45
+ ### [`jsxRuntime`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#runtime)
46
+
47
+ `classic | automatic`, defaults to `automatic`
48
+
49
+ - `automatic` automatically convert JSX to JS without the need to `import React from 'react'` in every file. Be sure to follow the rest of the [setup guide](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#how-to-upgrade-to-the-new-jsx-transform) after enabling this, otherwise ESLint and other tools will throw warnings.
50
+ - `classic` does not automatically import anything, React must imported into every file that uses JSX syntax.
51
+
52
+ ```js
53
+ [
54
+ 'babel-preset-expo',
55
+ {
56
+ jsxRuntime: 'classic',
57
+ },
58
+ ];
59
+ ```
60
+
61
+ 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.
62
+
63
+ ### [`jsxImportSource`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#importsource)
64
+
65
+ `string`, defaults to `react`
66
+
67
+ This option allows specifying a custom import source for importing functions.
68
+
69
+ ```js
70
+ [
71
+ 'babel-preset-expo',
72
+ {
73
+ jsxRuntime: 'automatic',
74
+ importSource: 'react',
75
+ },
76
+ ];
77
+ ```
78
+
79
+ This property is passed down to [`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx). This options does nothing when `jsxRuntime` is not set to `automatic`.
80
+
45
81
  ### [`lazyImports`](https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#lazy)
46
82
 
47
83
  Changes Babel's compiled `import` statements to be lazily evaluated when their imported bindings are used for the first time.
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  const lazyImportsBlacklist = require('./lazy-imports-blacklist');
2
2
 
3
- module.exports = function(api, options = {}) {
3
+ let hasWarnedJsxRename = false;
4
+
5
+ module.exports = function (api, options = {}) {
4
6
  const { web = {}, native = {} } = options;
5
7
 
6
8
  const bundler = api.caller(getBundler);
@@ -22,6 +24,37 @@ module.exports = function(api, options = {}) {
22
24
  // `metro-react-native-babel-preset` will handle it.
23
25
  const lazyImportsOption = options && options.lazyImports;
24
26
 
27
+ const extraPlugins = [];
28
+
29
+ if ('useTransformReactJsxExperimental' in platformOptions && !hasWarnedJsxRename) {
30
+ // https://github.com/expo/expo/pull/13945#pullrequestreview-724327024
31
+ hasWarnedJsxRename = true;
32
+ console.warn(
33
+ 'Warning: useTransformReactJsxExperimental has been renamed to useTransformReactJSXExperimental (capitalized JSX) in react-native@0.64.0'
34
+ );
35
+ }
36
+
37
+ // Set true to disable `@babel/plugin-transform-react-jsx`
38
+ // we override this logic outside of the metro preset so we can add support for
39
+ // React 17 automatic JSX transformations.
40
+ // If the logic for `useTransformReactJSXExperimental` ever changes in `metro-react-native-babel-preset`
41
+ // then this block should be updated to reflect those changes.
42
+ if (!platformOptions.useTransformReactJSXExperimental) {
43
+ extraPlugins.push([
44
+ require('@babel/plugin-transform-react-jsx'),
45
+ {
46
+ // Defaults to `automatic`, pass in `classic` to disable auto JSX transformations.
47
+ runtime: (options && options.jsxRuntime) || 'automatic',
48
+ ...(options?.jsxRuntime !== 'classic' && {
49
+ importSource: (options && options.jsxImportSource) || 'react',
50
+ }),
51
+ },
52
+ ]);
53
+ // Purposefully not adding the deprecated packages:
54
+ // `@babel/plugin-transform-react-jsx-self` and `@babel/plugin-transform-react-jsx-source`
55
+ // back to the preset.
56
+ }
57
+
25
58
  return {
26
59
  presets: [
27
60
  [
@@ -39,12 +72,19 @@ module.exports = function(api, options = {}) {
39
72
  enableBabelRuntime: platformOptions.enableBabelRuntime,
40
73
  // Defaults to `'default'`, can also use `'hermes-canary'`
41
74
  unstable_transformProfile: platformOptions.unstable_transformProfile,
42
- // Set true to disable `@babel/plugin-transform-react-jsx`
43
- useTransformReactJsxExperimental: platformOptions.useTransformReactJsxExperimental,
75
+ // Set true to disable `@babel/plugin-transform-react-jsx` and
76
+ // the deprecated packages `@babel/plugin-transform-react-jsx-self`, and `@babel/plugin-transform-react-jsx-source`.
77
+ //
78
+ // Otherwise, you'll sometime get errors like the following (starting in Expo SDK 43, React Native 64, React 17):
79
+ //
80
+ // TransformError App.js: /path/to/App.js: Duplicate __self prop found. You are most likely using the deprecated transform-react-jsx-self Babel plugin.
81
+ // 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.
82
+ useTransformReactJSXExperimental: true,
83
+
44
84
  disableImportExportTransform: platformOptions.disableImportExportTransform,
45
85
  lazyImportExportTransform:
46
86
  lazyImportsOption === true
47
- ? importModuleSpecifier => {
87
+ ? (importModuleSpecifier) => {
48
88
  // Do not lazy-initialize packages that are local imports (similar to `lazy: true`
49
89
  // behavior) or are in the blacklist.
50
90
  return !(
@@ -59,6 +99,8 @@ module.exports = function(api, options = {}) {
59
99
  ],
60
100
  ],
61
101
  plugins: [
102
+ getObjectRestSpreadPlugin(),
103
+ ...extraPlugins,
62
104
  getAliasPlugin(),
63
105
  [require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
64
106
  platform === 'web' && [require.resolve('babel-plugin-react-native-web')],
@@ -85,6 +127,15 @@ function getAliasPlugin() {
85
127
  return null;
86
128
  }
87
129
 
130
+ /**
131
+ * metro-react-native-babel-preset configures this plugin with `{ loose: true }`, which breaks all
132
+ * getters and setters in spread objects. We need to add this plugin ourself without that option.
133
+ * @see https://github.com/expo/expo/pull/11960#issuecomment-887796455
134
+ */
135
+ function getObjectRestSpreadPlugin() {
136
+ return [require.resolve('@babel/plugin-proposal-object-rest-spread'), { loose: false }];
137
+ }
138
+
88
139
  function hasModule(name) {
89
140
  try {
90
141
  return !!require.resolve(name);
@@ -114,8 +165,11 @@ function getBundler(caller) {
114
165
  if (name === 'metro') {
115
166
  // This is a hack to determine if metro is being used.
116
167
  return 'metro';
168
+ } else if (name === 'next-babel-turbo-loader') {
169
+ // NextJS 11
170
+ return 'webpack';
117
171
  } else if (name === 'babel-loader') {
118
- // This won't work in all cases as tools like Next.js could change the name of their loader.
172
+ // expo/webpack-config, gatsby, storybook, and next.js <10
119
173
  return 'webpack';
120
174
  }
121
175
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-preset-expo",
3
- "version": "8.4.0",
3
+ "version": "9.0.0",
4
4
  "description": "The Babel preset for Expo projects",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -45,15 +45,16 @@
45
45
  ]
46
46
  },
47
47
  "dependencies": {
48
- "@babel/plugin-proposal-decorators": "^7.6.0",
49
- "@babel/preset-env": "^7.6.3",
50
- "babel-plugin-module-resolver": "^3.2.0",
51
- "babel-plugin-react-native-web": "~0.13.6",
52
- "metro-react-native-babel-preset": "~0.59.0"
48
+ "@babel/plugin-proposal-decorators": "^7.12.9",
49
+ "@babel/plugin-transform-react-jsx": "^7.12.17",
50
+ "@babel/preset-env": "^7.12.9",
51
+ "babel-plugin-module-resolver": "^4.1.0",
52
+ "babel-plugin-react-native-web": "~0.17.1",
53
+ "metro-react-native-babel-preset": "~0.64.0"
53
54
  },
54
55
  "devDependencies": {
55
- "@babel/core": "^7.6.4",
56
- "jest": "^25.2.0"
56
+ "@babel/core": "^7.12.9",
57
+ "jest": "^26.6.3"
57
58
  },
58
- "gitHead": "03d9022d47660e60ffd99562b28a9854939e79e6"
59
+ "gitHead": "2e5c6983b86d5ecfca028ba64002897d8adc2cc4"
59
60
  }