Skip to content

Commit

Permalink
Use resolveConfig instead of resolve for PostCSS plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
taro-28 committed Aug 21, 2024
1 parent 3cd136f commit 7d649cc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
14 changes: 5 additions & 9 deletions packages/knip/src/WorkspaceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,6 @@ export class WorkspaceWorker {
for (const configFilePath of configFilePaths) {
const opts = { ...options, configFileDir: dirname(configFilePath), configFileName: basename(configFilePath) };

// biome-ignore lint/suspicious/noExplicitAny: raw incoming user data
let config: any;

if (hasResolveEntryPaths || shouldRunConfigResolver) {
const isManifest = basename(configFilePath) === 'package.json';
const fd = isManifest ? undefined : this.cache.getFileDescriptor(configFilePath);
Expand All @@ -303,7 +300,7 @@ export class WorkspaceWorker {
if (fd.meta.data.resolveConfig)
for (const id of fd.meta.data.resolveConfig) addDependency(id, configFilePath);
} else {
config = await loadConfigForPlugin(configFilePath, plugin, opts, pluginName);
const config = await loadConfigForPlugin(configFilePath, plugin, opts, pluginName);
const data: CacheItem = {};
if (config) {
if (hasResolveEntryPaths) {
Expand All @@ -320,16 +317,15 @@ export class WorkspaceWorker {
}
}
}

if (hasResolve) {
const dependencies = (await plugin.resolve?.(config, options)) ?? [];
for (const id of dependencies) addDependency(id, join(cwd, 'package.json'));
}
}

const finalEntryPaths = getFinalEntryPaths(plugin, options, configEntryPaths);
for (const id of finalEntryPaths) addDependency(id);

if (hasResolve) {
const dependencies = (await plugin.resolve?.(options)) ?? [];
for (const id of dependencies) addDependency(id, join(cwd, 'package.json'));
}
debugLogArray([name, plugin.title], 'dependencies', pluginDependencies);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/knip/src/plugins/astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const entry = ['astro.config.{js,cjs,mjs,ts}', 'src/content/config.ts'];

const production = ['src/pages/**/*.{astro,mdx,js,ts}', 'src/content/**/*.mdx'];

const resolve: Resolve = (_, options) => {
const resolve: Resolve = options => {
const { isProduction, manifest } = options;
const dependencies = [];

Expand Down
11 changes: 4 additions & 7 deletions packages/knip/src/plugins/postcss/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IsPluginEnabled, Plugin, Resolve, ResolveConfig } from '#p/types/plugins.js';
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency, toLilconfig } from '#p/util/plugin.js';
import type { PostCSSConfig } from './types.js';

Expand All @@ -18,21 +18,19 @@ const config = [
];

const resolveConfig: ResolveConfig<PostCSSConfig> = config => {
return config.plugins
const plugins = config.plugins
? (Array.isArray(config.plugins) ? config.plugins : Object.keys(config.plugins)).flatMap(plugin => {
if (typeof plugin === 'string') return plugin;
if (Array.isArray(plugin) && typeof plugin[0] === 'string') return plugin[0];
return [];
})
: [];
};

const resolve: Resolve<PostCSSConfig> = async (options, config) => {
for (const plugin of await resolveConfig(options, config)) {
for (const plugin of plugins) {
// Because postcss is not included in peerDependencies of tailwindcss
if (plugin === 'tailwindcss') return ['postcss'];
}
return [];
return plugins;
};

export default {
Expand All @@ -41,5 +39,4 @@ export default {
isEnabled,
config,
resolveConfig,
resolve,
} satisfies Plugin;
3 changes: 1 addition & 2 deletions packages/knip/src/types/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export type ResolveEntryPaths<T = any> = (config: T, options: PluginOptions) =>
// biome-ignore lint/suspicious/noExplicitAny: TODO
export type ResolveConfig<T = any> = (config: T, options: PluginOptions) => Promise<string[]> | string[];

// biome-ignore lint/suspicious/noExplicitAny: TODO
export type Resolve<T = any> = (config: T, options: PluginOptions) => Promise<string[]> | string[];
export type Resolve = (options: PluginOptions) => Promise<string[]> | string[];

export interface Plugin {
title: string;
Expand Down

0 comments on commit 7d649cc

Please sign in to comment.