Skip to content
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

(PostCSS plugin): Add postcss to used dependencies when using PostCSS with Tailwind CSS #764

Merged
merged 3 commits into from
Aug 21, 2024
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@fixtures/postcss-tailwindcss",
"devDependencies": {
"postcss": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
plugins: {
tailwindcss: {},
},
};
16 changes: 10 additions & 6 deletions packages/knip/src/WorkspaceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ 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 @@ -299,7 +303,7 @@ export class WorkspaceWorker {
if (fd.meta.data.resolveConfig)
for (const id of fd.meta.data.resolveConfig) addDependency(id, configFilePath);
} else {
const config = await loadConfigForPlugin(configFilePath, plugin, opts, pluginName);
config = await loadConfigForPlugin(configFilePath, plugin, opts, pluginName);
const data: CacheItem = {};
if (config) {
if (hasResolveEntryPaths) {
Expand All @@ -316,16 +320,16 @@ 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: 10 additions & 1 deletion 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, ResolveConfig } from '#p/types/plugins.js';
import type { IsPluginEnabled, Plugin, Resolve, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency, toLilconfig } from '#p/util/plugin.js';
import type { PostCSSConfig } from './types.js';

Expand Down Expand Up @@ -27,10 +27,19 @@ const resolveConfig: ResolveConfig<PostCSSConfig> = config => {
: [];
};

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for not adding a check for tailwindcss in config.plugins to the resolveConfig function directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood the implementation policy🙏

FIxed!
03a7583

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, there was a mistake.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry taro. Is there anything in the documentation or code that could have prevented this? Maybe we can improve it for the next contributor.

Thanks for the fix!

Copy link
Contributor Author

@taro-28 taro-28 Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, there was a mistake.

Re-fixed!

Sorry taro. Is there anything in the documentation or code that could have prevented this? Maybe we can improve it for the next contributor.

I re-read the documentation again, and it succinctly describes how to use resolveConfig, so I thought it was fine as it is.

My misunderstanding was due to overcomplicating the issue, as it was a slightly exceptional modification, such as adding the plugin's dependency itself (postcss).

return [];
};

export default {
title,
enablers,
isEnabled,
config,
resolveConfig,
resolve,
} satisfies Plugin;
3 changes: 2 additions & 1 deletion packages/knip/src/types/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ 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[];

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

export interface Plugin {
title: string;
Expand Down
24 changes: 24 additions & 0 deletions packages/knip/test/plugins/postcss-tailwindcss.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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/postcss-tailwindcss');

test('Find dependencies with the PostCSS plugin (with tailwindcss)', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

assert(issues.unlisted['postcss.config.js']['tailwindcss']);

assert.deepEqual(counters, {
...baseCounters,
unlisted: 1,
processed: 1,
total: 1,
});
});
Loading