Skip to content

Commit

Permalink
fix: expose atlas file recreation logic to expo cli (#44)
Browse files Browse the repository at this point in the history
* fix: reuse the Atlas file when exporting static web bundles

* refactor(cli): move atlas file recreation logic to cli

* docs: add docblock for `resetExpoAtlasFile` method
  • Loading branch information
byCedric authored Apr 28, 2024
1 parent 467466b commit a42f8cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
27 changes: 24 additions & 3 deletions src/data/AtlasFileSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export async function readAtlasEntry(filePath: string, id: number): Promise<Atla
let writeQueue: Promise<any> = Promise.resolve();

/**
* Add a new entry to the file.
* This is appended on a new line, so we can load the selectively.
* Add a new entry to the Atlas file.
* This function also ensures the Atlas file is ready to be written to, due to complications with Expo CLI.
* Eventually, the entry is appended on a new line, so we can load them selectively.
*/
export function writeAtlasEntry(filePath: string, entry: AtlasBundle) {
const line = [
Expand All @@ -98,7 +99,9 @@ export function writeAtlasEntry(filePath: string, entry: AtlasBundle) {
entry.serializeOptions,
];

return (writeQueue = writeQueue.then(() => appendJsonLine(filePath, line)));
writeQueue = writeQueue.then(() => appendJsonLine(filePath, line));

return writeQueue;
}

/** The default location of the metro file */
Expand Down Expand Up @@ -137,3 +140,21 @@ export async function createAtlasFile(filePath: string) {
await fs.promises.rm(filePath, { force: true });
await appendJsonLine(filePath, getAtlasMetdata());
}

/**
* Create the Atlas file if it doesn't exist, or recreate it if it's incompatible.
*/
export async function ensureAtlasFileExist(filePath: string) {
try {
await validateAtlasFile(filePath);
} catch (error: any) {
if (error.code === 'ATLAS_FILE_NOT_FOUND' || error.code === 'ATLAS_FILE_INCOMPATIBLE') {
await createAtlasFile(filePath);
return false;
}

throw error;
}

return true;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { MetroGraphSource } from './data/MetroGraphSource';
export {
AtlasFileSource,
createAtlasFile,
ensureAtlasFileExist,
validateAtlasFile,
getAtlasMetdata,
getAtlasPath,
Expand Down
19 changes: 17 additions & 2 deletions src/metro.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { type MetroConfig } from 'metro-config';

import { createAtlasFile, getAtlasPath, writeAtlasEntry } from './data/AtlasFileSource';
import {
createAtlasFile,
ensureAtlasFileExist,
getAtlasPath,
writeAtlasEntry,
} from './data/AtlasFileSource';
import { convertGraph, convertMetroConfig } from './data/MetroGraphSource';

type ExpoAtlasOptions = Partial<{
Expand Down Expand Up @@ -36,7 +41,7 @@ export function withExpoAtlas(config: MetroConfig, options: ExpoAtlasOptions = {
const metroConfig = convertMetroConfig(config);

// Note(cedric): we don't have to await this, Metro would never bundle before this is finishes
createAtlasFile(atlasFile);
ensureAtlasFileExist(atlasFile);

// @ts-expect-error
config.serializer.customSerializer = (entryPoint, preModules, graph, serializeOptions) => {
Expand All @@ -51,3 +56,13 @@ export function withExpoAtlas(config: MetroConfig, options: ExpoAtlasOptions = {

return config;
}

/**
* Fully reset, or recreate, the Expo Atlas file containing all Metro information.
* This method should only be called once per exporting session, to avoid overwriting data with mutliple Metro instances.
*/
export async function resetExpoAtlasFile(projectRoot: string) {
const filePath = getAtlasPath(projectRoot);
await createAtlasFile(filePath);
return filePath;
}

0 comments on commit a42f8cb

Please sign in to comment.