Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions packages/knip/fixtures/plugins/wxt/entrypoints/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default defineBackground(() => {
console.log('Background script started');
});

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.

9 changes: 9 additions & 0 deletions packages/knip/fixtures/plugins/wxt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@plugins/wxt",
"dependencies": {
"wxt": "*",
"@wxt-dev/module-react": "*",
"wxt-module-console-forward": "*",
"unused-module": "*"
}
}
5 changes: 5 additions & 0 deletions packages/knip/fixtures/plugins/wxt/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}
5 changes: 5 additions & 0 deletions packages/knip/fixtures/plugins/wxt/wxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'wxt';

export default defineConfig({
modules: ['@wxt-dev/module-react', 'wxt-module-console-forward'],
});
2 changes: 2 additions & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ import { default as webdriverIo } from './webdriver-io/index.ts';
import { default as webpack } from './webpack/index.ts';
import { default as wireit } from './wireit/index.ts';
import { default as wrangler } from './wrangler/index.ts';
import { default as wxt } from './wxt/index.ts';
import { default as xo } from './xo/index.ts';
import { default as yarn } from './yarn/index.ts';
import { default as yorkie } from './yorkie/index.ts';
Expand Down Expand Up @@ -271,6 +272,7 @@ export const Plugins = {
webpack,
wireit,
wrangler,
wxt,
xo,
yarn,
yorkie,
Expand Down
52 changes: 52 additions & 0 deletions packages/knip/src/plugins/wxt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.ts';
import type { Input } from '../../util/input.ts';
import { toDependency } from '../../util/input.ts';
import { hasDependency } from '../../util/plugin.ts';
import type { WxtConfig } from './types.ts';

// https://wxt.dev/guide/configuration.html

const title = 'WXT';

const enablers = ['wxt'];

const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);

const config = ['wxt.config.{js,mjs,ts}'];

const entry = ['entrypoints/**/*.{ts,tsx,js,jsx,html}'];

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

const setup = async () => {
if (globalThis && !('defineConfig' in globalThis)) {
Object.defineProperty(globalThis, 'defineConfig', {
value: (id: any) => id,
writable: true,
configurable: true,
});
}
};

const resolveConfig: ResolveConfig<WxtConfig> = async localConfig => {
const deps =
localConfig?.modules?.reduce<Input[]>((acc, id) => {
if (typeof id === 'string') acc.push(toDependency(id));
return acc;
}, []) ?? [];

return deps;
};

const plugin: Plugin = {
title,
enablers,
isEnabled,
config,
entry,
production,
setup,
resolveConfig,
};

export default plugin;
3 changes: 3 additions & 0 deletions packages/knip/src/plugins/wxt/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface WxtConfig {
modules?: 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 @@ -146,6 +146,7 @@ export const pluginsSchema = z.object({
webpack: pluginSchema,
wireit: pluginSchema,
wrangler: pluginSchema,
wxt: pluginSchema,
xo: pluginSchema,
yarn: pluginSchema,
yorkie: 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 @@ -133,6 +133,7 @@ export type PluginName =
| 'webpack'
| 'wireit'
| 'wrangler'
| 'wxt'
| 'xo'
| 'yarn'
| 'yorkie'
Expand Down Expand Up @@ -272,6 +273,7 @@ export const pluginNames = [
'webpack',
'wireit',
'wrangler',
'wxt',
'xo',
'yarn',
'yorkie',
Expand Down
23 changes: 23 additions & 0 deletions packages/knip/test/plugins/wxt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../../src/index.ts';
import baseCounters from '../helpers/baseCounters.ts';
import { createOptions } from '../helpers/create-options.ts';
import { resolve } from '../helpers/resolve.ts';

const cwd = resolve('fixtures/plugins/wxt');

test('Find dependencies with the wxt plugin', async () => {
const options = await createOptions({ cwd });
const { issues, counters } = await main(options);

assert(issues.dependencies['package.json']['unused-module']);

assert.deepEqual(counters, {
...baseCounters,
files: 1,
dependencies: 1,
processed: 2,
total: 2,
});
});