Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
[xdl] Switch to in-process Metro JS bundling starting from SDK 40 (#2921
Browse files Browse the repository at this point in the history
)

Always use in-process Metro for SDK 40 and newer.

For older SDK versions, keep the old behavior (in-process Metro can be
enabled by setting `EXPO_USE_DEV_SERVER=true` in the environment).
  • Loading branch information
fson authored Nov 19, 2020
1 parent f0715d4 commit 4ece18c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This is the log of notable changes to Expo CLI and related packages.

### 🎉 New features

- [xdl] Switch to in-process Metro JS bundling through `@expo/dev-server` starting from SDK 40 ([#2921](https://github.com/expo/expo-cli/pull/2921))

### 🐛 Bug fixes

## [Mon, 9 Nov 2020 13:44:59 -0800](https://github.com/expo/expo-cli/commit/d5b8759b32d5a7747067a969728d9ba732926824)
Expand Down
27 changes: 19 additions & 8 deletions packages/xdl/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ export async function runHook(hook: LoadedHook, hookOptions: Omit<HookArguments,
}
}

/**
* Returns true if we should use Metro using its JS APIs via @expo/dev-server (the modern and fast
* way), false if we should fall back to spawning it as a subprocess (supported for backwards
* compatibility with SDK39 and older).
*/
function shouldUseDevServer(exp: ExpoConfig) {
return Versions.gteSdkVersion(exp, '40.0.0') || getenv.boolish('EXPO_USE_DEV_SERVER', false);
}

/**
* Apps exporting for self hosting will have the files created in the project directory the following way:
.
Expand Down Expand Up @@ -366,8 +375,12 @@ export async function exportForAppHosting(
const bundlesPathToWrite = path.resolve(projectRoot, path.join(outputDir, 'bundles'));
await fs.ensureDir(bundlesPathToWrite);

const publishOptions = options.publishOptions || {};
const { exp, pkg, hooks } = await _getPublishExpConfigAsync(projectRoot, publishOptions);

const bundles = await buildPublishBundlesAsync(projectRoot, options.publishOptions, {
dev: options.isDev,
useDevServer: shouldUseDevServer(exp),
});
const iosBundle = bundles.ios.code;
const androidBundle = bundles.android.code;
Expand All @@ -385,10 +398,6 @@ export async function exportForAppHosting(

logger.global.info('Finished saving JS Bundles.');

// save the assets
// Get project config
const publishOptions = options.publishOptions || {};
const { exp, pkg, hooks } = await _getPublishExpConfigAsync(projectRoot, publishOptions);
const { assets } = await exportAssetsAsync({
projectRoot,
exp,
Expand Down Expand Up @@ -627,7 +636,9 @@ export async function publishAsync(

// TODO: refactor this out to a function, throw error if length doesn't match
const validPostPublishHooks: LoadedHook[] = prepareHooks(hooks, 'postPublish', projectRoot, exp);
const bundles = await buildPublishBundlesAsync(projectRoot, options);
const bundles = await buildPublishBundlesAsync(projectRoot, options, {
useDevServer: shouldUseDevServer(exp),
});
const androidBundle = bundles.android.code;
const iosBundle = bundles.ios.code;

Expand Down Expand Up @@ -835,9 +846,9 @@ async function _getPublishExpConfigAsync(
async function buildPublishBundlesAsync(
projectRoot: string,
publishOptions: PublishOptions = {},
bundleOptions: { dev?: boolean } = {}
bundleOptions: { dev?: boolean; useDevServer: boolean }
) {
if (!getenv.boolish('EXPO_USE_DEV_SERVER', false)) {
if (!bundleOptions.useDevServer) {
try {
await startReactNativeServerAsync({
projectRoot,
Expand Down Expand Up @@ -1968,7 +1979,7 @@ export async function startAsync(
await Webpack.restartAsync(projectRoot, options);
DevSession.startSession(projectRoot, exp, 'web');
return exp;
} else if (getenv.boolish('EXPO_USE_DEV_SERVER', false)) {
} else if (shouldUseDevServer(exp)) {
await startDevServerAsync(projectRoot, options);
DevSession.startSession(projectRoot, exp, 'native');
} else {
Expand Down

0 comments on commit 4ece18c

Please sign in to comment.