Skip to content

Commit

Permalink
chore: Added installed-check linter and Knip (#199)
Browse files Browse the repository at this point in the history
* Added installed-check linter

* Added a changeset file

* Added knip
  • Loading branch information
patricklafrance authored Apr 8, 2024
1 parent 0a3337a commit 7805dbe
Show file tree
Hide file tree
Showing 19 changed files with 1,521 additions and 278 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-cougars-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workleap/tsup-configs": patch
---

Add an export for the tsup configs default entry paths.
186 changes: 186 additions & 0 deletions knip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import type { KnipConfig } from "knip";

type KnipWorkspaceConfig = NonNullable<KnipConfig["workspaces"]>[string];

type KnipTransformer = (config: KnipWorkspaceConfig) => KnipWorkspaceConfig;

function defineWorkspace({ ignore, ...config }: KnipWorkspaceConfig, transformers?: KnipTransformer[]): KnipWorkspaceConfig {
let transformedConfig: KnipWorkspaceConfig = {
...config,
ignore: [
...(ignore as string[] ?? []),
"node_modules/**",
"dist/**"
]
};

if (transformers) {
transformedConfig = transformers.reduce((acc, transformer) => transformer(acc), transformedConfig);
}

return transformedConfig;
}

const ignoreBrowserslist: KnipTransformer = ({ ignoreDependencies, ...config }) => {
return {
...config,
ignoreDependencies: [
...(ignoreDependencies as string[] ?? []),
// Browserlist isn't supported by plugins.
"@workleap/browserslist-config"
]
};
};

const configurePostcss: KnipTransformer = config => {
return {
...config,
postcss: {
config: ["postcss.config.ts"]
}
};
};

const configureMsw: KnipTransformer = ({ entry, ignore, ...config }) => {
return {
...config,
entry: [
...(entry as string[] ?? []),
"src/mocks/browser.ts",
"src/mocks/handlers.ts"
],
ignore: [
...(ignore as string[] ?? []),
// MSW isn't supported by plugins.
"public/mockServiceWorker.js"
]
};
};

const configureWebpack: KnipTransformer = ({ ignoreDependencies, ...config }) => {
return {
...config,
webpack: {
config: ["webpack.*.js"]
},
ignoreDependencies: [
...(ignoreDependencies as string[] ?? []),
"@svgr/webpack",
"swc-loader",
"css-loader",
"postcss-loader",
"style-loader",
"mini-css-extract-plugin"
].filter(Boolean) as string[]
};
};

const configureTsup: KnipTransformer = config => {
return {
...config,
tsup: {
config: ["tsup.*.ts"]
}
};
};

const configurePackage: KnipTransformer = config => {
return {
...config,
eslint: true
};
};

const configureSample: KnipTransformer = ({ entry, ...config }) => {
return {
...config,
entry: [
...(entry as string[] ?? []),
"src/index.ts",
"src/index.tsx"
],
eslint: true,
stylelint: true
};
};

const rootConfig = defineWorkspace({
ignoreDependencies: [
// Required for Stylelint (seems like a Knip bug)
"prettier",
// Installed once for all the workspace's projects
"ts-node"
]
});

const packagesConfig: KnipWorkspaceConfig = defineWorkspace({}, [
configurePackage,
configureTsup
]);

const swcConfig: KnipWorkspaceConfig = defineWorkspace({
ignoreDependencies: [
// Omitting the optional peer dependencies from the peerDependencies emits
// runtime errors like "[ERROR] Could not resolve "browserslist"".
"@swc/jest",
"browserslist"
]
}, [
configurePackage,
configureTsup
]);

const webpackConfig: KnipWorkspaceConfig = defineWorkspace({
ignoreDependencies: [
// Emits an referenced optionap peerDependencies warning but according to PNPM
// documentation, to specify a version constraint, the optional dependency must be define.
// See: https://pnpm.io/package_json#peerdependenciesmetaoptional.
"webpack-dev-server"
]
}, [
configurePackage,
configureTsup
]);

const sampleAppConfig = defineWorkspace({}, [
configureSample,
ignoreBrowserslist,
configurePostcss,
configureWebpack,
configureMsw
]);

const sampleComponentsConfig = defineWorkspace({}, [
configureSample,
configureTsup
]);

const sampleUtilsConfig = defineWorkspace({}, [
configureSample,
configureTsup
]);

const config: KnipConfig = {
workspaces: {
".": rootConfig,
"packages/*": packagesConfig,
"packages/swc-configs": swcConfig,
"packages/webpack-configs": webpackConfig,
"sample/app": sampleAppConfig,
"sample/components": sampleComponentsConfig,
"sample/utils": sampleUtilsConfig
},
ignoreWorkspaces: [
// Until it's migrated to ESLint 9.
"packages/eslint-plugin",
// Until it supports ESM.
"packages/stylelint-configs"
],
exclude: [
// It cause issues with config like Jest "projects".
"unresolved"
],
ignoreExportsUsedInFile: true
};

export default config;
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"author": "Workleap",
"description": "Common configurations for building web apps and libraries at Workleap",
"private": true,
"type": "module",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand All @@ -19,6 +20,8 @@
"lint": "pnpm run \"/^lint:.*/\"",
"lint:eslint": "eslint . --max-warnings=0 --cache --cache-location node_modules/.cache/eslint",
"lint:stylelint": "stylelint \"**/*.css\" --cache --cache-location node_modules/.cache/stylelint",
"lint:knip": "knip",
"lint:installed-check": "installed-check",
"lint:types": "pnpm -r --parallel --filter \"!@workleap/typescript-configs\" exec tsc",
"changeset": "changeset",
"ci-release": "pnpm build && changeset publish",
Expand All @@ -39,14 +42,16 @@
"@workleap/eslint-plugin": "workspace:*",
"@workleap/typescript-configs": "workspace:*",
"eslint": "8.57.0",
"installed-check": "9.3.0",
"jest": "29.7.0",
"knip": "5.9.0",
"prettier": "3.2.5",
"retypeapp": "3.5.0",
"stylelint": "16.2.1",
"ts-node": "10.9.2",
"typescript": "5.4.2"
},
"engines": {
"node": ">=16.0.0"
"node": ">=18.12.0"
}
}
9 changes: 3 additions & 6 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,19 @@
}
},
"devDependencies": {
"@swc/core": "1.4.6",
"@swc/helpers": "0.5.6",
"@swc/core": "1.4.12",
"@swc/helpers": "0.5.8",
"@swc/jest": "0.2.36",
"@types/eslint": "8.56.5",
"@types/estree": "1.0.5",
"@types/jest": "29.5.12",
"@types/node": "20.11.25",
"@typescript-eslint/parser": "7.1.1",
"@workleap/swc-configs": "workspace:*",
"@workleap/tsup-configs": "workspace:*",
"@workleap/typescript-configs": "workspace:*",
"eslint": "8.57.0",
"jest": "29.7.0",
"ts-node": "10.9.2",
"tsup": "8.0.2",
"typescript": "5.4.2"
"tsup": "8.0.2"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/postcss-configs/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Config } from "jest";
import { config as swcConfig } from "./swc.jest.ts";
import { swcConfig } from "./swc.jest.ts";

const config: Config = {
testEnvironment: "node",
Expand Down
4 changes: 1 addition & 3 deletions packages/postcss-configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"postcss": ">=8.4.6"
},
"devDependencies": {
"@swc/core": "1.4.6",
"@swc/core": "1.4.12",
"@swc/jest": "0.2.36",
"@types/jest": "29.5.12",
"@types/node": "20.11.25",
Expand All @@ -43,8 +43,6 @@
"@workleap/tsup-configs": "workspace:*",
"@workleap/typescript-configs": "workspace:*",
"jest": "29.7.0",
"postcss": "8.4.35",
"ts-node": "10.9.2",
"tsup": "8.0.2",
"typescript": "5.4.2"
},
Expand Down
23 changes: 3 additions & 20 deletions packages/postcss-configs/swc.jest.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
import type { Config } from "@swc/core";
import { defineJestConfig } from "@workleap/swc-configs";

export const swcConfig = defineJestConfig();

export const config: Config = {
jsc: {
parser: {
syntax: "typescript",
tsx: true
},
// The output environment that the code will be compiled for.
target: "es2022",
// Import shims from an external module rather than inlining them in bundle files to greatly reduce the bundles size.
// Requires to add "@swc/helpers" as a project dependency
externalHelpers: true
},
module: {
// The output module resolution system that the code will be compiled for.
type: "es6",
// Prevent SWC from exporting the `__esModule` property.
strict: true
}
};
2 changes: 0 additions & 2 deletions packages/stylelint-configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
"@workleap/eslint-plugin": "workspace:*",
"@workleap/tsup-configs": "workspace:*",
"@workleap/typescript-configs": "workspace:*",
"prettier": "3.2.5",
"stylelint": "16.2.1",
"tsup": "8.0.2",
"typescript": "5.4.2"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/swc-configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@
}
},
"devDependencies": {
"@swc/core": "1.4.6",
"@swc/core": "1.4.12",
"@swc/helpers": "0.5.8",
"@swc/jest": "0.2.36",
"@types/jest": "29.5.12",
"@workleap/eslint-plugin": "workspace:*",
"@workleap/tsup-configs": "workspace:*",
"@workleap/typescript-configs": "workspace:*",
"browserslist": "4.23.0",
"jest": "29.7.0",
"ts-node": "10.9.2",
"tsup": "8.0.2",
"typescript": "5.4.2"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/tsup-configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@
"typescript": "*"
},
"devDependencies": {
"@swc/core": "1.4.6",
"@swc/core": "1.4.12",
"@swc/jest": "0.2.36",
"@types/jest": "29.5.12",
"@workleap/eslint-plugin": "workspace:*",
"@workleap/swc-configs": "workspace:*",
"@workleap/typescript-configs": "workspace:*",
"jest": "29.7.0",
"ts-node": "10.9.2",
"tsup": "8.0.2",
"typescript": "5.4.2"
"typescript": "5.4.4"
},
"publishConfig": {
"access": "public",
Expand Down
14 changes: 8 additions & 6 deletions packages/tsup-configs/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export interface DefineBuildConfigOptions extends TsupConfig {
transformers?: TsupConfigTransformer[];
}

export const TsupBuildEntryPaths = [
"./src",
"!src/**/*.css",
"!src/**/*.stories.ts(x)",
"!src/**/*.test.ts(x)"
];

export function defineBuildConfig(options: DefineBuildConfigOptions = {}) {
const {
transformers = [],
Expand All @@ -15,12 +22,7 @@ export function defineBuildConfig(options: DefineBuildConfigOptions = {}) {
clean: true,
dts: true,
treeshake: true,
entry: [
"./src",
"!src/**/*.css",
"!src/**/*.stories.ts(x)",
"!src/**/*.test.ts(x)"
],
entry: TsupBuildEntryPaths,
outDir: "./dist",
format: "esm",
target: "esnext",
Expand Down
Loading

0 comments on commit 7805dbe

Please sign in to comment.