UNPKG

3.38 kBMarkdownView Raw
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
5The codemods can be used to transform JavaScript (.js, .jsx) and TypeScript (.ts, .tsx) files.
6
7## Installation
8
9You can run `expo-codemod` using `npx` without having to install it (Node.js 8.x, 10.x or 12.x is required):
10
11```
12npx expo-codemod
13```
14
15Alternatively, you can install it globally:
16
17```
18npm install --global expo-codemod
19```
20
21(Installing with `yarn global add` works too.)
22
23## Usage
24
25```sh
26npx expo-codemod <transform> <paths...>
27```
28
29```
30Options:
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
39Transforms available:
40 sdk33-imports
41 sdk37-imports
42```
43
44For example, to apply the `sdk33-imports` transform to all source files in the `src` folder, run:
45
46```
47npx expo-codemod sdk33-imports src
48```
49
50You can also use glob patterns (make sure to wrap the patterns in quotes):
51
52```
53npx expo-codemod sdk33-imports '**/*.js' '**/*.{ts,tsx}'
54```
55
56### Advanced usage with jscodeshift
57
58The 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
61npm install --save-dev expo-codemod
62npm install --global jscodeshift
63```
64
65You can pass the transform filename to jscodeshift using the `--transform` option, for example:
66
67```sh
68jscodeshift --transform ./node_modules/expo-codemod/build/transforms/sdk33-imports.js --no-babel --ignore-config .gitignore .
69```
70
71Read 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
77As 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
85Transforms imports from the `expo` package that were deprecated in Expo SDK 33 to imports from the individual packages.
86
87#### Example
88
89Input:
90
91```js
92import { Accelerometer, GestureHandler, MapView } from 'expo';
93```
94
95Output:
96
97```js
98import { Accelerometer } from 'expo-sensors';
99import * as GestureHandler from 'react-native-gesture-handler';
100import MapView from 'react-native-maps';
101```
102
103### `sdk37-imports`
104
105_Used to migrate a project from SDK 36 to SDK 37._
106
107Transforms 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
111Input:
112
113```js
114import { AuthSession, ScreenOrientation } from 'expo';
115```
116
117Output:
118
119```js
120import * as AuthSession from 'expo-auth-session';
121import * as ScreenOrientation from 'expo-screen-orientation';
122```