Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions packages/knip/fixtures/plugins/metro/metro.config.js
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'
},
};
13 changes: 13 additions & 0 deletions packages/knip/fixtures/plugins/metro/package.json
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.
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@
"title": "markdownlint plugin configuration (https://knip.dev/reference/plugins/markdownlint)",
"$ref": "#/definitions/plugin"
},
"metro": {
"title": "metro plugin configuration (https://knip.dev/reference/plugins/metro)",
"$ref": "#/definitions/plugin"
},
"mocha": {
"title": "Mocha plugin configuration (https://knip.dev/reference/plugins/mocha)",
"$ref": "#/definitions/plugin"
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { default as linthtml } from './linthtml/index.js';
import { default as lockfileLint } from './lockfile-lint/index.js';
import { default as lostPixel } from './lost-pixel/index.js';
import { default as markdownlint } from './markdownlint/index.js';
import { default as metro } from './metro/index.js';
import { default as mocha } from './mocha/index.js';
import { default as moonrepo } from './moonrepo/index.js';
import { default as msw } from './msw/index.js';
Expand Down Expand Up @@ -121,6 +122,7 @@ export const Plugins = {
'lockfile-lint': lockfileLint,
'lost-pixel': lostPixel,
markdownlint,
metro,
mocha,
moonrepo,
msw,
Expand Down
47 changes: 47 additions & 0 deletions packages/knip/src/plugins/metro/index.ts
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'];
Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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-native listed. 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.

Copy link
Member

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 metro specific enabler(s) here, but this regex matches anything that starts with metro which is too loose?

Copy link
Contributor Author

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'.


const isEnabled: IsPluginEnabled = options => hasDependency(options.dependencies, enablers);

const packageJsonPath = 'metro';

const config: string[] = ['metro.config.{js,cjs,mjs,json}', 'package.json'];

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;
11 changes: 11 additions & 0 deletions packages/knip/src/plugins/metro/types.ts
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;
};
};
1 change: 1 addition & 0 deletions packages/knip/src/schema/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const pluginsSchema = z.object({
'lockfile-lint': pluginSchema,
'lost-pixel': pluginSchema,
markdownlint: pluginSchema,
metro: pluginSchema,
mocha: pluginSchema,
moonrepo: pluginSchema,
msw: pluginSchema,
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/types/PluginNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type PluginName =
| 'lockfile-lint'
| 'lost-pixel'
| 'markdownlint'
| 'metro'
| 'mocha'
| 'moonrepo'
| 'msw'
Expand Down Expand Up @@ -122,6 +123,7 @@ export const pluginNames = [
'lockfile-lint',
'lost-pixel',
'markdownlint',
'metro',
'mocha',
'moonrepo',
'msw',
Expand Down
29 changes: 29 additions & 0 deletions packages/knip/test/plugins/metro.test.ts
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,
});
});
Loading