1 | # expo-codemod
|
2 |
|
3 | `expo-codemod` is collection of codemods (transforms) for projects using the Expo SDK and a small command-line interface (CLI) for running the codemods.
|
4 |
|
5 | The codemods can be used to transform JavaScript (.js, .jsx) and TypeScript (.ts, .tsx) files.
|
6 |
|
7 | ## Installation
|
8 |
|
9 | You can run `expo-codemod` using `npx` without having to install it (Node.js 8.x, 10.x or 12.x is required):
|
10 |
|
11 | ```
|
12 | npx expo-codemod
|
13 | ```
|
14 |
|
15 | Alternatively, you can install it globally:
|
16 |
|
17 | ```
|
18 | npm install --global expo-codemod
|
19 | ```
|
20 |
|
21 | (Installing with `yarn global add` works too.)
|
22 |
|
23 | ## Usage
|
24 |
|
25 | ```sh
|
26 | npx expo-codemod <transform> <paths...>
|
27 | ```
|
28 |
|
29 | ```
|
30 | Options:
|
31 | <transform> (required) name of transform to apply to files
|
32 | (see a list of transforms available below)
|
33 | <paths...> one or more paths or globs (e.g. src/**/*.js) of sources to transform
|
34 | files in .gitignore are ignored by default
|
35 | -h, --help print this help message
|
36 | -p, --parser <babel|flow|ts|tsx> parser to use to parse the source files
|
37 | (default: babel, ts or tsx based on the file extension)
|
38 |
|
39 | Transforms available:
|
40 | sdk33-imports
|
41 | sdk37-imports
|
42 | ```
|
43 |
|
44 | For example, to apply the `sdk33-imports` transform to all source files in the `src` folder, run:
|
45 |
|
46 | ```
|
47 | npx expo-codemod sdk33-imports src
|
48 | ```
|
49 |
|
50 | You can also use glob patterns (make sure to wrap the patterns in quotes):
|
51 |
|
52 | ```
|
53 | npx expo-codemod sdk33-imports '**/*.js' '**/*.{ts,tsx}'
|
54 | ```
|
55 |
|
56 | ### Advanced usage with jscodeshift
|
57 |
|
58 | The CLI is a wrapper over [jscodeshift](https://github.com/facebook/jscodeshift). If you need more fine grained control of jscodeshift or parser options, you can also use the jscodeshift CLI directly. First install `expo-codemod` and `jscodeshift`:
|
59 |
|
60 | ```sh
|
61 | npm install --save-dev expo-codemod
|
62 | npm install --global jscodeshift
|
63 | ```
|
64 |
|
65 | You can pass the transform filename to jscodeshift using the `--transform` option, for example:
|
66 |
|
67 | ```sh
|
68 | jscodeshift --transform ./node_modules/expo-codemod/build/transforms/sdk33-imports.js --no-babel --ignore-config .gitignore .
|
69 | ```
|
70 |
|
71 | Read more about jscodeshift options [here](https://github.com/facebook/jscodeshift#usage-cli).
|
72 |
|
73 | ## Troubleshooting
|
74 |
|
75 | ### Custom babel.config.js not being used
|
76 |
|
77 | As per [#676](https://github.com/expo/expo-cli/issues/676#issuecomment-505793327) you can pass in the `--parser=flow` option.
|
78 |
|
79 | ## Transforms
|
80 |
|
81 | ### `sdk33-imports`
|
82 |
|
83 | _Used to migrate a project from SDK 32 to SDK 33._
|
84 |
|
85 | Transforms imports from the `expo` package that were deprecated in Expo SDK 33 to imports from the individual packages.
|
86 |
|
87 | #### Example
|
88 |
|
89 | Input:
|
90 |
|
91 | ```js
|
92 | import { Accelerometer, GestureHandler, MapView } from 'expo';
|
93 | ```
|
94 |
|
95 | Output:
|
96 |
|
97 | ```js
|
98 | import { Accelerometer } from 'expo-sensors';
|
99 | import * as GestureHandler from 'react-native-gesture-handler';
|
100 | import MapView from 'react-native-maps';
|
101 | ```
|
102 |
|
103 | ### `sdk37-imports`
|
104 |
|
105 | _Used to migrate a project from SDK 36 to SDK 37._
|
106 |
|
107 | Transforms imports of `AuthSession` and `ScreenOrientation` that were extracted out from the `expo` package to `expo-auth-session` and `expo-screen-orientation` packages for SDK 37.
|
108 |
|
109 | #### Example
|
110 |
|
111 | Input:
|
112 |
|
113 | ```js
|
114 | import { AuthSession, ScreenOrientation } from 'expo';
|
115 | ```
|
116 |
|
117 | Output:
|
118 |
|
119 | ```js
|
120 | import * as AuthSession from 'expo-auth-session';
|
121 | import * as ScreenOrientation from 'expo-screen-orientation';
|
122 | ```
|