diff --git a/src/__tests__/metro/_expo-js-worker.ts b/src/__tests__/metro/_expo-js-worker.ts new file mode 100644 index 00000000..873a2967 --- /dev/null +++ b/src/__tests__/metro/_expo-js-worker.ts @@ -0,0 +1,20 @@ +import type { JsTransformOptions } from "metro-transform-worker"; + +// eslint-disable-next-line @typescript-eslint/no-require-imports +const expo = require( + jest.requireActual<{ unstable_transformerPath: string }>("@expo/metro-config") + .unstable_transformerPath, +) as typeof import("metro-transform-worker"); + +// Expo's own worker for code; a stylesheet asked for on web comes back as its CSS, since Expo's CSS step needs ESM support that Jest lacks +export function transform( + config: Parameters[0], + projectRoot: string, + filename: string, + data: Buffer, + options: JsTransformOptions, +) { + return options.platform === "web" && filename.endsWith(".css") + ? Promise.resolve({ output: [{ data: { css: { code: data } } }] }) + : expo.transform(config, projectRoot, filename, data, options); +} diff --git a/src/__tests__/metro/tree-shaking.test.ts b/src/__tests__/metro/tree-shaking.test.ts new file mode 100644 index 00000000..b2953442 --- /dev/null +++ b/src/__tests__/metro/tree-shaking.test.ts @@ -0,0 +1,48 @@ +/** + * @jest-environment node + */ +import { join } from "path"; + +import { getDefaultConfig } from "@expo/metro-config"; +import type { JsTransformOptions } from "metro-transform-worker"; + +import { transform } from "../../metro/metro-transformer"; + +type TransformerConfig = Parameters[0]; + +jest.mock("@expo/metro-config", () => ({ + ...jest.requireActual("@expo/metro-config"), + unstable_transformerPath: require.resolve("./_expo-js-worker"), +})); + +const PROJECT_ROOT = process.cwd(); + +// Expo's tree shaking (EXPO_UNSTABLE_TREE_SHAKING) sets `optimize`, and its serializer never finishes a module whose path ends in .css +test("a stylesheet is a wrapped module when Expo tree shaking is on", async () => { + const { transformer } = getDefaultConfig(PROJECT_ROOT); + + const result = await transform( + transformer as TransformerConfig, + PROJECT_ROOT, + join(PROJECT_ROOT, "src", "tree-shaking.css"), + Buffer.from(`.a { color: red; }`), + { + dev: false, + hot: false, + minify: false, + platform: "ios", + type: "module", + experimentalImportSupport: true, + inlinePlatform: true, + inlineRequires: false, + unstable_transformProfile: "default", + customTransformOptions: { optimize: "true" }, + } as JsTransformOptions, + ); + + const code = (result.output[0]?.data as { code: string }).code; + + expect(code).toMatch(/^__d\(function/); + expect(code).not.toMatch(/^\s*(import|export)\b/m); + expect(code).toContain("StyleCollection.inject("); +}); diff --git a/src/metro/metro-transformer.ts b/src/metro/metro-transformer.ts index f9a0afce..ccf1ec24 100644 --- a/src/metro/metro-transformer.ts +++ b/src/metro/metro-transformer.ts @@ -62,12 +62,21 @@ export async function transform( data = Buffer.from(getNativeInjectionCode([], [productionJS])); + // Expo's graph optimizer (tree shaking) leaves modules as untransformed ESM and + // finishes them in a serializer pass that skips every path ending in `.css`, so + // this module would never be wrapped. Transform it fully here instead. const transform = await worker.transform( config, projectRoot, `${filePath}.js`, data, - options, + { + ...options, + customTransformOptions: { + ...options.customTransformOptions, + optimize: false, + }, + }, ); // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access