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

Add unbuild plugin #435

Merged
merged 2 commits into from
Jan 8, 2024
Merged
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
5 changes: 5 additions & 0 deletions packages/knip/fixtures/plugins/unbuild/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
entries: ['./src/index'],
});
7 changes: 7 additions & 0 deletions packages/knip/fixtures/plugins/unbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@fixtures/unbuild",
"version": "*",
"devDependencies": {
"unbuild": "^2.0.0"
}
}
76 changes: 58 additions & 18 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"ignoreExportsUsedInFile": {
"title": "Ignore exports used in file",
"examples": [
{ "ignoreExportsUsedInFile": true },
{
"ignoreExportsUsedInFile": true
},
{
"ignoreExportsUsedInFile": {
"interface": true,
Expand All @@ -64,12 +66,24 @@
{
"type": "object",
"properties": {
"class": { "type": "boolean" },
"enum": { "type": "boolean" },
"function": { "type": "boolean" },
"interface": { "type": "boolean" },
"member": { "type": "boolean" },
"type": { "type": "boolean" }
"class": {
"type": "boolean"
},
"enum": {
"type": "boolean"
},
"function": {
"type": "boolean"
},
"interface": {
"type": "boolean"
},
"member": {
"type": "boolean"
},
"type": {
"type": "boolean"
}
}
}
]
Expand All @@ -96,17 +110,39 @@
"rules": {
"type": "object",
"properties": {
"classMembers": { "$ref": "#/definitions/ruleValue" },
"dependencies": { "$ref": "#/definitions/ruleValue" },
"duplicates": { "$ref": "#/definitions/ruleValue" },
"enumMembers": { "$ref": "#/definitions/ruleValue" },
"exports": { "$ref": "#/definitions/ruleValue" },
"files": { "$ref": "#/definitions/ruleValue" },
"nsExports": { "$ref": "#/definitions/ruleValue" },
"nsTypes": { "$ref": "#/definitions/ruleValue" },
"types": { "$ref": "#/definitions/ruleValue" },
"unlisted": { "$ref": "#/definitions/ruleValue" },
"unresolved": { "$ref": "#/definitions/ruleValue" }
"classMembers": {
"$ref": "#/definitions/ruleValue"
},
"dependencies": {
"$ref": "#/definitions/ruleValue"
},
"duplicates": {
"$ref": "#/definitions/ruleValue"
},
"enumMembers": {
"$ref": "#/definitions/ruleValue"
},
"exports": {
"$ref": "#/definitions/ruleValue"
},
"files": {
"$ref": "#/definitions/ruleValue"
},
"nsExports": {
"$ref": "#/definitions/ruleValue"
},
"nsTypes": {
"$ref": "#/definitions/ruleValue"
},
"types": {
"$ref": "#/definitions/ruleValue"
},
"unlisted": {
"$ref": "#/definitions/ruleValue"
},
"unresolved": {
"$ref": "#/definitions/ruleValue"
}
}
}
},
Expand Down Expand Up @@ -414,6 +450,10 @@
"title": "TypeScript plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/typescript/README.md)",
"$ref": "#/definitions/plugin"
},
"unbuild": {
"title": "unbuild plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/unbuild/README.md)",
"$ref": "#/definitions/plugin"
},
"vite": {
"title": "vite plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/vite/README.md)",
"$ref": "#/definitions/plugin"
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const pluginsSchema = z.object({
tsup: pluginSchema,
typedoc: pluginSchema,
typescript: pluginSchema,
unbuild: pluginSchema,
vite: pluginSchema,
vitest: pluginSchema,
webpack: pluginSchema,
Expand Down
3 changes: 2 additions & 1 deletion packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export * as tailwind from './tailwind/index.js';
export * as tsup from './tsup/index.js';
export * as typedoc from './typedoc/index.js';
export * as typescript from './typescript/index.js';
export * as unbuild from './unbuild/index.js';
export * as vite from './vite/index.js';
export * as vitest from './vitest/index.js';
export * as webpack from './webpack/index.js';
export * as wireit from './wireit/index.js';
export * as vue from './vue/index.js';
export * as vue from './vue/index.js';
36 changes: 36 additions & 0 deletions packages/knip/src/plugins/unbuild/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { toEntryPattern } from 'src/util/protocols.js';
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import type { UnbuildConfig } from './types.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

// https://github.com/unjs/unbuild#unbuild

export const NAME = 'unbuild';

/** @public */
export const ENABLERS = ['unbuild'];

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = ['build.config.{js,cjs,mjs,ts,mts,cts,json}'];

const findUnbuildDependencies: GenericPluginCallback = async configFilePath => {
const localConfig: UnbuildConfig | undefined = await load(configFilePath);
if (!localConfig) return [];

const entries = [];
if (Array.isArray(localConfig)) {
for (const obj of localConfig) {
entries.push(...(obj.entries || []));
}
} else {
entries.push(...(localConfig.entries || []));
}

const entryPatterns = entries.map(entry => toEntryPattern(typeof entry === 'string' ? entry : entry.input));

return [...entryPatterns];
};

export const findDependencies = timerify(findUnbuildDependencies);
15 changes: 15 additions & 0 deletions packages/knip/src/plugins/unbuild/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type UnbuildConfigObject = {
name: string;
entries:
| string[]
| {
builder: string;
input: string;
outDir: string;
}[];
outDir: string;
declaration: boolean;
rollup: Record<string, unknown>;
};

export type UnbuildConfig = Partial<UnbuildConfigObject> | Partial<UnbuildConfigObject>[];
19 changes: 19 additions & 0 deletions packages/knip/test/plugins/unbuild.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import * as unbuild from '../../src/plugins/unbuild/index.js';
import { resolve, join } from '../../src/util/path.js';
import { getManifest, pluginConfig as config } from '../helpers/index.js';
import type { GenericPluginCallbackOptions } from '../../src/types/plugins.js';

const cwd = resolve('fixtures/plugins/unbuild');
const manifest = getManifest(cwd);

test('Find dependencies in unbuild configuration', async () => {
const configFilePath = join(cwd, 'build.config.ts');
const dependencies = await unbuild.findDependencies(configFilePath, {
manifest,
config,
} as GenericPluginCallbackOptions);

assert.deepEqual(dependencies, []);
});
Loading