Skip to content

Commit

Permalink
Plugin docs tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 8, 2025
1 parent 2bd03e0 commit f008158
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
10 changes: 8 additions & 2 deletions packages/docs/scripts/generate-plugin-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ for await (const dir of directories) {
if (dir.isDirectory() && dir.name !== '_template') {
const pluginName = dir.name;
const pluginDir = path.join(pluginsDir, pluginName);
const plugin: Plugin = (await import(path.join(pluginDir, 'index.ts'))).default;
const mod = await import(path.join(pluginDir, 'index.ts'));
const plugin: Plugin = mod.default;
const docs: undefined | { entry?: string[]; production?: string[] } = mod.docs;

const { title, enablers, note, args, config, entry, production, project } = plugin;

Expand All @@ -57,7 +59,10 @@ for await (const dir of directories) {
const defaults: Record<string, string[]> = {};
if (config && config.length > 0) defaults.config = config;
if (entry && entry.length > 0) defaults.entry = entry;
if (docs?.entry && docs.entry.length > 0) defaults.entry = [...(defaults.entry ?? []), ...docs.entry];
if (production && production.length > 0) defaults.entry = [...(defaults.entry ?? []), ...production];
if (docs?.production && docs.production.length > 0)
defaults.entry = [...(defaults.entry ?? []), ...docs.production];
if (project && project.length > 0) defaults.project = project;

const hasDefaultConfig = Object.values(defaults).some(v => v.length > 0);
Expand All @@ -84,11 +89,12 @@ for await (const dir of directories) {
const defaultConfig = hasDefaultConfig
? [
u('heading', { depth: 2 }, [u('text', 'Default configuration')]),
...parseFragment('If enabled, this configuration is added automatically:'),
...parseFragment('If this plugin is enabled, the following configuration is added automatically:'),
u('code', {
lang: 'json', // TODO How to set attributes/properties/props properly?
value: JSON.stringify({ [pluginName]: defaults }, null, 2),
}),
...parseFragment('Depending on local configuration, plugins may modify the defaults as shown.'),
...parseFragment('Custom `config` or `entry` options override default values, they are not merged.'),
...parseFragment(
'See [Plugins](../../explanations/plugins) for more details about plugins and their `entry` and `config` options.'
Expand Down
13 changes: 9 additions & 4 deletions packages/knip/src/plugins/expo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc

const config = ['app.json', 'app.config.{ts,js}'];

const production = ['app/**/*.{js,jsx,ts,tsx}', 'src/app/**/*.{js,jsx,ts,tsx}'];

/** @internal */
export const docs = { production };

const resolveEntryPaths: ResolveEntryPaths<ExpoConfig> = async (expoConfig, { manifest }) => {
const config = 'expo' in expoConfig ? expoConfig.expo : expoConfig;

let production: string[] = [];
let patterns: string[] = [];

// https://docs.expo.dev/router/installation/#setup-entry-point
if (manifest.main === 'expo-router/entry') {
production = ['app/**/*.{js,jsx,ts,tsx}', 'src/app/**/*.{js,jsx,ts,tsx}'];
patterns = [...production];

const normalizedPlugins =
config.plugins?.map(plugin => (Array.isArray(plugin) ? plugin : ([plugin] as const))) ?? [];
Expand All @@ -32,12 +37,12 @@ const resolveEntryPaths: ResolveEntryPaths<ExpoConfig> = async (expoConfig, { ma
const [, options] = expoRouterPlugin;

if (typeof options?.root === 'string') {
production = [join(options.root, '**/*.{js,jsx,ts,tsx}')];
patterns = [join(options.root, '**/*.{js,jsx,ts,tsx}')];
}
}
}

return production.map(entry => toProductionEntry(entry));
return patterns.map(entry => toProductionEntry(entry));
};

const resolveConfig: ResolveConfig<ExpoConfig> = async (expoConfig, options) => getDependencies(expoConfig, options);
Expand Down
4 changes: 3 additions & 1 deletion packages/knip/src/plugins/metro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const DEFAULT_EXTENSIONS = ['js', 'jsx', 'json', 'ts', 'tsx'];

const production = [`src/**/*.{${PLATFORMS.join(',')}}.{${DEFAULT_EXTENSIONS.join(',')}}`];

/** @internal */
export const docs = { production };

const resolveEntryPaths: ResolveEntryPaths<MetroConfig> = async config => {
const platformEntryPatterns = compact(PLATFORMS.concat(config.resolver?.platforms ?? []));
const sourceExts = config.resolver?.sourceExts ?? DEFAULT_EXTENSIONS;
Expand Down Expand Up @@ -56,7 +59,6 @@ export default {
enablers,
isEnabled,
config,
production,
resolveEntryPaths,
resolveConfig,
} satisfies Plugin;

0 comments on commit f008158

Please sign in to comment.