Skip to content

Commit

Permalink
feature: add dedicated Expo CLI entrypoint (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored Mar 27, 2024
1 parent d891cde commit 37ccfcf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/src/cli';
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/src/cli');
45 changes: 45 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { ConfigT as MetroConfig } from 'metro-config';

import { MetroGraphSource } from './data/MetroGraphSource';
import { createAtlasMiddleware } from './utils/middleware';

/**
* Iniitalize Expo Atlas to gather statistics from Metro when exporting bundles.
* This function adds the required Metro config, and should be used inside the Expo CLI.
*
* @example ```js
* const atlasFromProject = requireFrom(projectRoot, 'expo-atlas/cli');
*
* if (atlasFromProject) {
* middleware.use('/_expo/atlas', atlasFromProject.middleware);
* }
* ```
*/
export function createExpoAtlasMiddleware(config: MetroConfig) {
const projectRoot = config.projectRoot;

const source = new MetroGraphSource();
const middleware = createAtlasMiddleware(source);

const metroCustomSerializer = config.serializer?.customSerializer ?? (() => {});
const metroExtensions = {
source: config.resolver?.sourceExts,
asset: config.resolver?.assetExts,
};

// @ts-expect-error Should still be writable at this stage
config.serializer.customSerializer = (entryPoint, preModules, graph, options) => {
source.onSerializeGraph({
projectRoot,
entryPoint,
preModules,
graph,
options,
extensions: metroExtensions,
});

return metroCustomSerializer(entryPoint, preModules, graph, options);
};

return { source, middleware };
}

0 comments on commit 37ccfcf

Please sign in to comment.