Skip to content
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
20 changes: 20 additions & 0 deletions src/__tests__/metro/_expo-js-worker.ts
Original file line number Diff line number Diff line change
@@ -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<typeof expo.transform>[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);
}
48 changes: 48 additions & 0 deletions src/__tests__/metro/tree-shaking.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof transform>[0];

jest.mock("@expo/metro-config", () => ({
...jest.requireActual<object>("@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(");
});
11 changes: 10 additions & 1 deletion src/metro/metro-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down