-
Notifications
You must be signed in to change notification settings - Fork 43
Add an internal Expo-project marker to the Uniwind Metro config and use it to lazily choose the correct transform worker. #593
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,29 @@ import { nativeResolver, webResolver } from './resolvers' | |
|
|
||
| const isUniwindRequest = (moduleName: string) => moduleName === 'uniwind' || moduleName.startsWith('uniwind/') | ||
|
|
||
| type ExpoTransformerConfig = NonNullable<MetroConfig['transformer']> & { | ||
| _expoRelativeProjectRoot?: string | ||
| _expoRouterPath?: string | ||
| expo_customTransformerPath?: string | false | ||
| postcssHash?: string | null | ||
| } | ||
|
|
||
| const isExpoMetroConfig = (config: MetroConfig) => { | ||
| const transformerPath = config.transformerPath | ||
| const transformer = config.transformer as ExpoTransformerConfig | undefined | ||
| const hasExpoTransformerField = transformer | ||
| ? '_expoRelativeProjectRoot' in transformer | ||
| || '_expoRouterPath' in transformer | ||
| || 'expo_customTransformerPath' in transformer | ||
| || 'postcssHash' in transformer | ||
| : false | ||
|
|
||
| return Boolean( | ||
| transformerPath?.includes('@expo/metro-config') | ||
| || hasExpoTransformerField, | ||
| ) | ||
| } | ||
|
|
||
| export const withUniwindConfig = <T extends MetroConfig>( | ||
| config: T, | ||
| uniwindConfig: UniwindConfig, | ||
|
|
@@ -24,7 +47,10 @@ export const withUniwindConfig = <T extends MetroConfig>( | |
| transformerPath: require.resolve('./transformer.cjs'), | ||
| transformer: { | ||
| ...config.transformer, | ||
| uniwind: bundlerConfig.toMetroConfig(), | ||
| uniwind: { | ||
| ...bundlerConfig.toMetroConfig(), | ||
| isExpoProject: isExpoMetroConfig(config), | ||
| }, | ||
|
Comment on lines
-27
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extend |
||
| }, | ||
| resolver: { | ||
| ...config.resolver, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,18 +9,31 @@ import path from 'path' | |
|
|
||
| const cssArtifactPath = path.resolve(__dirname, '../../uniwind.css') | ||
|
|
||
| let worker: typeof MetroTransformWorker | ||
| const workerCache = new Map<boolean, typeof MetroTransformWorker>() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| try { | ||
| try { | ||
| const { unstable_transformerPath } = require('@expo/metro-config') as typeof ExpoMetroConfig | ||
| const getTransformWorker = (isExpoProject?: boolean): typeof MetroTransformWorker => { | ||
| const cacheKey = Boolean(isExpoProject) | ||
| const cachedWorker = workerCache.get(cacheKey) | ||
|
|
||
| worker = require(unstable_transformerPath) | ||
| } catch { | ||
| worker = require('@expo/metro-config/build/transform-worker/transform-worker.js') | ||
| if (cachedWorker) { | ||
| return cachedWorker | ||
| } | ||
| } catch { | ||
| worker = require('metro-transform-worker') | ||
|
|
||
| const resolvedWorker: typeof MetroTransformWorker = cacheKey | ||
| ? (() => { | ||
| try { | ||
| const { unstable_transformerPath } = require('@expo/metro-config') as typeof ExpoMetroConfig | ||
|
|
||
| return require(unstable_transformerPath) | ||
| } catch { | ||
| return require('@expo/metro-config/build/transform-worker/transform-worker.js') | ||
| } | ||
| })() | ||
| : require('metro-transform-worker') | ||
|
|
||
| workerCache.set(cacheKey, resolvedWorker) | ||
|
|
||
| return resolvedWorker | ||
| } | ||
|
|
||
| export const transform = async ( | ||
|
|
@@ -32,6 +45,7 @@ export const transform = async ( | |
| data: Buffer, | ||
| options: JsTransformOptions, | ||
| ) => { | ||
| const worker = getTransformWorker(config.uniwind.isExpoProject) | ||
| const isCss = options.type !== 'asset' && path.join(process.cwd(), config.uniwind.cssEntryFile) === path.join(projectRoot, filePath) | ||
|
|
||
| if (filePath.endsWith('/components/web/metro-injected.js')) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we could just scan all transformer keys to start with
expoor_expo? Having those manually defined doesn't seem easy to maintain, especially because it looks like some expo's internal stuff