-
-
Notifications
You must be signed in to change notification settings - Fork 322
Add metro plugin
#895
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
Merged
Merged
Add metro plugin
#895
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module.exports = { | ||
| projectRoot: './src/app', | ||
| transformer: { | ||
| minifierPath: 'metro-minify-esbuild', | ||
| assetPlugins: ["expo-asset/tools/hashAssetFiles"], | ||
| babelTransformerPath: 'react-native-svg-transformer' | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "name": "@fixtures/metro", | ||
| "version": "*", | ||
| "dependencies": { | ||
| "expo-asset": "*", | ||
| "react": "*", | ||
| "react-native": "*", | ||
| "react-native-svg-transformer": "*" | ||
| }, | ||
| "metro": { | ||
| "transformerPath": "./custom-metro-transformer.js" | ||
| } | ||
| } |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { join } from '../../util/path.js'; | ||
| import type { IsPluginEnabled, Plugin, ResolveConfig, ResolveEntryPaths } from '../../types/config.js'; | ||
| import { toDeferResolve, toProductionEntry } from '../../util/input.js'; | ||
| import { hasDependency } from '../../util/plugin.js'; | ||
| import type { PluginConfig } from './types.js'; | ||
|
|
||
| // https://metrobundler.dev/docs/configuration | ||
|
|
||
| const title = 'Metro'; | ||
|
|
||
| const enablers = [/^metro(-.*)?$/, 'react-native']; | ||
|
|
||
| const isEnabled: IsPluginEnabled = options => hasDependency(options.dependencies, enablers); | ||
|
|
||
| const packageJsonPath = 'metro'; | ||
|
|
||
| const config: string[] = ['metro.config.{js,cjs,mjs,json}', 'package.json']; | ||
jjjjonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const resolveEntryPaths: ResolveEntryPaths<PluginConfig> = async config => { | ||
| if (!config.projectRoot) return []; | ||
|
|
||
| const entryFilePattern = 'index.{js,jsx,ts,tsx}'; | ||
| const entryFilePath = join(config.projectRoot, entryFilePattern); | ||
| return [toProductionEntry(entryFilePath)]; | ||
| }; | ||
|
|
||
| const resolveConfig: ResolveConfig<PluginConfig> = async config => { | ||
| const { transformerPath, transformer } = config; | ||
| const inputs: string[] = []; | ||
|
|
||
| if (transformerPath) inputs.push(transformerPath); | ||
| if (transformer?.assetPlugins) inputs.push(...transformer.assetPlugins); | ||
| if (transformer?.minifierPath) inputs.push(transformer.minifierPath); | ||
| if (transformer?.babelTransformerPath) inputs.push(transformer.babelTransformerPath); | ||
|
|
||
| return [...inputs].map(toDeferResolve); | ||
| }; | ||
|
|
||
| export default { | ||
| title, | ||
| enablers, | ||
| isEnabled, | ||
| packageJsonPath, | ||
| config, | ||
| resolveEntryPaths, | ||
| resolveConfig, | ||
| } satisfies Plugin; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // https://github.com/facebook/metro/blob/main/packages/metro-config/types/configTypes.d.ts | ||
|
|
||
| export type PluginConfig = { | ||
| projectRoot?: string; | ||
| transformerPath?: string; | ||
| transformer?: { | ||
| minifierPath?: string; | ||
| assetPlugins?: string[]; | ||
| babelTransformerPath?: string; | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { test } from 'bun:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { main } from '../../src/index.js'; | ||
| import { resolve } from '../../src/util/path.js'; | ||
| import baseArguments from '../helpers/baseArguments.js'; | ||
| import baseCounters from '../helpers/baseCounters.js'; | ||
|
|
||
| const cwd = resolve('fixtures/plugins/metro'); | ||
|
|
||
| test('Find dependencies with the Metro plugin', async () => { | ||
| const { issues, counters } = await main({ | ||
| ...baseArguments, | ||
| cwd, | ||
| }); | ||
|
|
||
| assert(issues.dependencies['package.json']['react']); | ||
| assert(issues.dependencies['package.json']['react-native']); | ||
|
|
||
| assert(issues.unresolved['metro.config.js']['metro-minify-esbuild']); | ||
| assert(issues.unresolved['package.json']['./custom-metro-transformer.js']); | ||
|
|
||
| assert.deepEqual(counters, { | ||
| ...baseCounters, | ||
| dependencies: 2, | ||
| unresolved: 2, | ||
| processed: 2, | ||
| total: 2, | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if this is the best way to handle this, but typically Metro is not in the project's package.json, as it's a dependency of React Native
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.
In that case, let's just use only
react-native? Is that ever not listed?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.
@webpro It's definitely possible to have a project without
react-nativelisted. Metro can also bundle for web, although it'd be pretty rare to use it for exclusively that purpose over other web bundlers. Or the project could be a custom dev server that uses Metro's bundling API. Again, a rare use case, but possible.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.
We can/should obviously leave some
metrospecific enabler(s) here, but this regex matches anything that starts withmetrowhich is too loose?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.
True, I'll remove the regex in favor of
'metro'.