-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli, metro-config): environment variable support (#21983)
# Why - It's nice to be able to use uncommitted values in your app, based on the environment. This feels very familiar to web developers. - Values that are prefixed with `EXPO_PUBLIC_` will be inlined in the bundle when bundling normally (e.g. not for Node.js). - `.env` files are loaded into memory and applied to the process during a run. This also means that they're available in `app.config.js`. - During development-only, environment variables are exposed on the `process.env` object (non-enumerable) to ensure they're available between fast refresh updates. <!-- Please describe the motivation for this PR, and link to relevant GitHub issues, forums posts, or feature requests. --> # How - Create new package `@expo/env` which is used to hydrate env variables in a unified way. I plan to open another PR in `eas-cli` which uses this package to fill in environment variables before uploading. NOTE: environment variables that are defined in eas.json are not available in Expo CLI when building locally, but are available in the cloud since they'll be on the process, this means they effectively emulate `.env.production`. - Update templates to gitignore local env files. - Add basic documentation to the versioned metro guide (more to come). <!-- How did you build this feature or fix this bug and why? --> # Test Plan - [ ] E2E rendering test - [ ] E2E Node.js rendering test - [x] Unit tests for serializer - [x] Tests for env package <!-- Please describe how you tested this change and how a reviewer could reproduce your test, especially if this PR does not include automated tests! If possible, please also provide terminal output and/or screenshots demonstrating your test/reproduction. --> # Checklist <!-- Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules. --> - [ ] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) - [ ] This diff will work correctly for `expo prebuild` & EAS Build (eg: updated a module plugin).
- Loading branch information
Showing
66 changed files
with
1,517 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
EXPO_PUBLIC_DEMO_KEY=DEMO_KEY | ||
INVALID_DEMO_KEY=INVALID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
EXPO_PUBLIC_DEMO_KEY=DEMO_KEY_PROD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/@expo/cli/src/start/server/metro/waitForMetroToObserveTypeScriptFile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import path from 'path'; | ||
|
||
import type { ServerLike } from '../BundlerDevServer'; | ||
|
||
const debug = require('debug')('expo:start:server:metro:waitForTypescript') as typeof console.log; | ||
|
||
/** | ||
* Use the native file watcher / Metro ruleset to detect if a | ||
* TypeScript file is added to the project during development. | ||
*/ | ||
export function waitForMetroToObserveTypeScriptFile( | ||
projectRoot: string, | ||
runner: { | ||
metro: import('metro').Server; | ||
server: ServerLike; | ||
}, | ||
callback: () => Promise<void> | ||
): () => void { | ||
const watcher = runner.metro.getBundler().getBundler().getWatcher(); | ||
|
||
const tsconfigPath = path.join(projectRoot, 'tsconfig.json'); | ||
|
||
const listener = ({ | ||
eventsQueue, | ||
}: { | ||
eventsQueue: { | ||
filePath: string; | ||
metadata?: { | ||
type: 'f' | 'd' | 'l'; // Regular file / Directory / Symlink | ||
} | null; | ||
type: string; | ||
}[]; | ||
}) => { | ||
for (const event of eventsQueue) { | ||
if ( | ||
event.type === 'add' && | ||
event.metadata?.type !== 'd' && | ||
// We need to ignore node_modules because Metro will add all of the files in node_modules to the watcher. | ||
!/node_modules/.test(event.filePath) | ||
) { | ||
const { filePath } = event; | ||
// Is TypeScript? | ||
if ( | ||
// If the user adds a TypeScript file to the observable files in their project. | ||
/\.tsx?$/.test(filePath) || | ||
// Or if the user adds a tsconfig.json file to the project root. | ||
filePath === tsconfigPath | ||
) { | ||
debug('Detected TypeScript file added to the project: ', filePath); | ||
callback(); | ||
off(); | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
debug('Waiting for TypeScript files to be added to the project...'); | ||
watcher.addListener('change', listener); | ||
|
||
const off = () => { | ||
watcher.removeListener('change', listener); | ||
}; | ||
|
||
runner.server.addListener?.('close', off); | ||
return off; | ||
} | ||
|
||
export function observeFileChanges( | ||
runner: { | ||
metro: import('metro').Server; | ||
server: ServerLike; | ||
}, | ||
files: string[], | ||
callback: () => void | Promise<void> | ||
): () => void { | ||
const watcher = runner.metro.getBundler().getBundler().getWatcher(); | ||
|
||
const listener = ({ | ||
eventsQueue, | ||
}: { | ||
eventsQueue: { | ||
filePath: string; | ||
metadata?: { | ||
type: 'f' | 'd' | 'l'; // Regular file / Directory / Symlink | ||
} | null; | ||
type: string; | ||
}[]; | ||
}) => { | ||
for (const event of eventsQueue) { | ||
if ( | ||
// event.type === 'add' && | ||
event.metadata?.type !== 'd' && | ||
// We need to ignore node_modules because Metro will add all of the files in node_modules to the watcher. | ||
!/node_modules/.test(event.filePath) | ||
) { | ||
const { filePath } = event; | ||
// Is TypeScript? | ||
if (files.includes(filePath)) { | ||
debug('Observed change:', filePath); | ||
callback(); | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
debug('Watching file changes:', files); | ||
watcher.addListener('change', listener); | ||
|
||
const off = () => { | ||
watcher.removeListener('change', listener); | ||
}; | ||
|
||
runner.server.addListener?.('close', off); | ||
return off; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/@expo/cli/src/start/server/middleware/CreateFileMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.