knip
Version:
Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects
45 lines (44 loc) • 1.61 kB
JavaScript
import ts from 'typescript';
import { findDescendants, stripQuotes } from "../../typescript/ast-helpers.js";
import { toAlias, toIgnore, toProductionEntry } from "../../util/input.js";
import { hasDependency } from "../../util/plugin.js";
const title = 'SvelteKit';
const enablers = ['@sveltejs/kit'];
const isEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
const config = ['svelte.config.js'];
const production = [
'src/routes/**/+{page,server,page.server,error,layout,layout.server}{,@*}.{js,ts,svelte}',
'src/hooks.{server,client}.{js,ts}',
'src/params/*.{js,ts}',
'src/service-worker.{js,ts}',
'src/service-worker/index.{js,ts}',
'src/instrumentation.server.{js,ts}',
];
const getLibPath = (sourceFile) => {
const assignments = findDescendants(sourceFile, node => ts.isPropertyAssignment(node));
for (const assignment of assignments) {
if (assignment.name.getText() === 'lib' && ts.isStringLiteral(assignment.initializer)) {
return stripQuotes(assignment.initializer.getText());
}
}
return 'src/lib';
};
const resolveFromAST = sourceFile => {
const lib = getLibPath(sourceFile);
return [
...production.map(pattern => toProductionEntry(pattern)),
toAlias('$lib', [`./${lib}`]),
toAlias('$lib/*', [`./${lib}/*`]),
toIgnore('\\$app/.+', 'unresolved'),
toIgnore('\\$env/.+', 'unresolved'),
toIgnore('\\$service-worker', 'unresolved'),
];
};
const plugin = {
title,
enablers,
isEnabled,
config,
resolveFromAST,
};
export default plugin;