Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid chaining multiple atlas serializers together #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { AtlasError, AtlasValidationError } from './utils/errors';
export { createAtlasMiddleware } from './utils/middleware';
export { fuzzyFilterModules } from './utils/search';
export { createStatsFile, validateStatsFile, getStatsMetdata, getStatsPath } from './utils/stats';
export { attachMetroSerializer } from './utils/metro';
9 changes: 3 additions & 6 deletions src/metro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type MetroConfig } from 'metro-config';

import { convertGraph } from './data/MetroGraphSource';
import { writeStatsEntry } from './data/StatsFileSource';
import { attachMetroSerializer } from './utils/metro';
import { createStatsFile, getStatsPath } from './utils/stats';

type ExpoAtlasOptions = Partial<{
Expand All @@ -27,7 +28,6 @@ type ExpoAtlasOptions = Partial<{
*/
export function withExpoAtlas(config: MetroConfig, options: ExpoAtlasOptions = {}) {
const projectRoot = config.projectRoot;
const originalSerializer = config.serializer?.customSerializer ?? (() => {});

if (!projectRoot) {
throw new Error('No "projectRoot" configured in Metro config.');
Expand All @@ -42,16 +42,13 @@ export function withExpoAtlas(config: MetroConfig, options: ExpoAtlasOptions = {
// Note(cedric): we don't have to await this, Metro would never bundle before this is finisheds
createStatsFile(statsFile);

// @ts-expect-error
config.serializer.customSerializer = (entryPoint, preModules, graph, options) => {
attachMetroSerializer(config, (entryPoint, preModules, graph, options) => {
// Note(cedric): we don't have to await this, it has a built-in write queue
writeStatsEntry(
statsFile,
convertGraph({ projectRoot, entryPoint, preModules, graph, options, extensions })
);

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

return config;
}
36 changes: 36 additions & 0 deletions src/utils/metro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { ConfigT as MetroConfig, InputConfigT as MetroInputConfig } from 'metro-config';

const atlasSerializerSymbol = Symbol('expo-atlas-serializer');
const originalSerializerSymbol = Symbol('expo-atlas-original-serializer');

type MetroSerializerParams = Parameters<NonNullable<MetroConfig['serializer']['customSerializer']>>

Check warning on line 6 in src/utils/metro.ts

View workflow job for this annotation

GitHub Actions / core

Insert `;`
type AtlasSerializer = (...params: MetroSerializerParams) => void;

/**
* Attach a custom serializer, marked as being an Expo Atlas serializer.
* If there was a previous serializer, it will be overwritten to avoid conflicts.
*/
export function attachMetroSerializer(
config: MetroConfig | MetroInputConfig,
serializer: AtlasSerializer
) {
// @ts-expect-error
if (!config.serializer) config.serializer = {};

let prevSerializer = config.serializer?.customSerializer;

// Already attached, overwrite the serializer
if (prevSerializer?.[atlasSerializerSymbol]) {
prevSerializer = prevSerializer[originalSerializerSymbol];
}

// @ts-expect-error
config.serializer.customSerializer = (entryPoint, preModules, graph, options) => {
serializer(entryPoint, preModules, graph, options);
return prevSerializer?.(entryPoint, preModules, graph, options);
};

// Mark this serializer as being atlas
config.serializer.customSerializer![atlasSerializerSymbol] = true;
config.serializer.customSerializer![originalSerializerSymbol] = prevSerializer;
}
Loading