Skip to content

Commit

Permalink
chore: stricter type checks (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jul 2, 2024
1 parent af26e40 commit 027fdf6
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ jobs:
cache: "pnpm"
- run: pnpm install
- run: pnpm lint
- run: pnpm test:types
- run: pnpm build
- run: pnpm vitest run --coverage
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A unified javascript build system",
"repository": "unjs/unbuild",
"license": "MIT",
"type": "module",
"exports": {
".": {
"import": "./dist/index.mjs"
Expand All @@ -24,7 +25,8 @@
"prepack": "pnpm unbuild",
"release": "pnpm test && changelogen --release --prerelease --publish && git push --follow-tags",
"stub": "pnpm unbuild --stub",
"test": "pnpm lint && vitest run --coverage",
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
"test:types": "tsc --noEmit",
"unbuild": "jiti ./src/cli"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/builders/mkdist/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relative } from "pathe";
import { mkdist, MkdistOptions } from "mkdist";
import { mkdist, type MkdistOptions } from "mkdist";
import { symlink, rmdir } from "../../utils";
import type { MkdistBuildEntry, BuildContext } from "../../types";
import consola from "consola";
Expand Down
8 changes: 4 additions & 4 deletions src/builders/rollup/plugins/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Based on https://github.com/egoist/rollup-plugin-esbuild and nitropack fork (MIT)

import { extname, relative } from "pathe";
import type { Plugin, PluginContext } from "rollup";
import { Loader, TransformResult, CommonOptions, transform } from "esbuild";
import { createFilter } from "@rollup/pluginutils";
import type { FilterPattern } from "@rollup/pluginutils";
import type { Loader, TransformResult, CommonOptions } from "esbuild";
import { transform } from "esbuild";
import { extname, relative } from "pathe";
import { createFilter } from "@rollup/pluginutils";

const DefaultLoaders: { [ext: string]: Loader } = {
".js": "js",
Expand Down
4 changes: 2 additions & 2 deletions src/builders/rollup/plugins/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin } from "rollup";
import type { Plugin, TransformHook } from "rollup";
import type { RollupJsonOptions } from "@rollup/plugin-json";
import rollupJSONPlugin from "@rollup/plugin-json";

Expand All @@ -10,7 +10,7 @@ export function JSONPlugin(options: RollupJsonOptions): Plugin {
...plugin,
name: "unbuild-json",
transform(code, id) {
const res = plugin.transform!.call(this, code, id);
const res = (plugin.transform as TransformHook)!.call(this, code, id);
if (
res &&
typeof res !== "string" &&
Expand Down
4 changes: 2 additions & 2 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PackageJson } from "pkg-types";
import type { BuildContext } from "./types";
import { existsSync } from "node:fs";
import chalk from "chalk";
import { resolve } from "pathe";
import { PackageJson } from "pkg-types";
import { arrayIncludes, extractExportFilenames, getpkg, warn } from "./utils";
import { BuildContext } from "./types";

export function validateDependencies(ctx: BuildContext): void {
const usedDependencies = new Set<string>();
Expand Down
5 changes: 5 additions & 0 deletions test/auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe("inferEntries", () => {
it("handles types within exports`", () => {
const result = inferEntries(
{
// @ts-expect-error - fix pkg-types
exports: {
import: {
types: "dist/test.d.mts",
Expand Down Expand Up @@ -166,6 +167,7 @@ describe("inferEntries", () => {

it("ignores top-level exports", () => {
expect(
// @ts-expect-error - fix pkg-types
inferEntries({ exports: { "./*": "./*" } }, [
"src/",
"src/",
Expand All @@ -183,6 +185,7 @@ describe("inferEntries", () => {
expect(
inferEntries(
{
// @ts-expect-error - fix pkg-types
exports: {
".": "./dist/index.cjs",
"first-test": "./dist/first-test.cjs",
Expand Down Expand Up @@ -219,6 +222,7 @@ describe("inferEntries", () => {
warnings: [],
});
expect(
// @ts-expect-error - fix pkg-types
inferEntries({ exports: { "./runtime/*": "./dist/runtime/*.mjs," } }, [
"src/",
"src/runtime/",
Expand All @@ -233,6 +237,7 @@ describe("inferEntries", () => {
});
expect(
inferEntries(
// @ts-expect-error - fix pkg-types
{ exports: { "./runtime/*": { require: "./dist/runtime/*" } } },
["src/", "src/runtime/"],
),
Expand Down
1 change: 1 addition & 0 deletions test/fixture/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default defineBuildConfig([
jiti: {
transformOptions: {
babel: {
// @ts-expect-error - type complexity
plugins: [["@babel/plugin-transform-class-properties"]],
},
},
Expand Down
3 changes: 2 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ describe("extractExportFilenames", () => {
]);
});
it("handles nested objects", () => {
// @ts-expect-error - fix pkg-types
expect(extractExportFilenames({ require: "test" })).to.deep.equal([
{ file: "test", type: "cjs" },
]);
// @ts-ignore TODO: fix pkg-types
expect(
// @ts-expect-error - fix pkg-types
extractExportFilenames({
require: { node: "test", other: { import: "this", require: "that" } },
}),
Expand Down
2 changes: 1 addition & 1 deletion test/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { BuildEntry } from "../src/types.ts";
import { fileURLToPath } from "node:url";
import { consola } from "consola";
import { join } from "pathe";
import { describe, it, expect } from "vitest";
import { validateDependencies, validatePackage } from "../src/validate";
import { BuildEntry } from "../src/types";

describe("validatePackage", () => {
it("detects missing files", () => {
Expand Down
23 changes: 14 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"module": "Preserve",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"outDir": "dist",
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"allowJs": true,
"checkJs": true,
"strict": true,
"declaration": true
"verbatimModuleSyntax": true,
"isolatedModules": true,
"forceConsistentCasingInFileNames": true,
"allowImportingTsExtensions": true,
"noImplicitOverride": true,
"noEmit": true
},
"include": [
"src",
"test"
]
"include": ["src", "test"]
}

0 comments on commit 027fdf6

Please sign in to comment.