Skip to content

Commit a6570a5

Browse files
authored
fix(types): improve type safety (#516)
1 parent dda85ce commit a6570a5

File tree

7 files changed

+25
-23
lines changed

7 files changed

+25
-23
lines changed

src/build.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async function _build(
100100
pkg.unbuild || pkg.build,
101101
inputConfig,
102102
preset,
103-
<BuildOptions>{
103+
{
104104
name: (pkg?.name || "").split("/").pop() || "default",
105105
rootDir,
106106
entries: [],
@@ -171,7 +171,7 @@ async function _build(
171171
},
172172
},
173173
parallel: false,
174-
},
174+
} satisfies BuildOptions,
175175
) as BuildOptions;
176176

177177
// Resolve dirs relative to rootDir
@@ -265,8 +265,8 @@ async function _build(
265265
for (const dir of new Set(
266266
options.entries
267267
.map((e) => e.outDir)
268-
.filter(Boolean)
269-
.sort() as unknown as Set<string>,
268+
.filter((p): p is NonNullable<typeof p> => !!p)
269+
.sort(),
270270
)) {
271271
if (
272272
dir === options.rootDir ||

src/builders/rollup/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ export async function rollupBuild(ctx: BuildContext): Promise<void> {
8484
removeShebangPlugin(),
8585
ctx.options.rollup.emitCJS && fixCJSExportTypePlugin(ctx),
8686
].filter(
87-
(plugin) =>
87+
(plugin): plugin is NonNullable<Exclude<typeof plugin, false>> =>
8888
/**
8989
* Issue: #396
9090
* rollup-plugin-dts conflicts with rollup-plugin-commonjs:
9191
* https://github.com/Swatinem/rollup-plugin-dts?tab=readme-ov-file#what-to-expect
9292
*/
93-
plugin && (!("name" in plugin) || plugin.name !== "commonjs"),
93+
!!plugin && (!("name" in plugin) || plugin.name !== "commonjs"),
9494
);
9595

9696
await ctx.hooks.callHook("rollup:dts:options", ctx, rollupOptions);

src/builders/rollup/config.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { DEFAULT_EXTENSIONS, getChunkFilename, resolveAliases } from "./utils";
1717

1818
export function getRollupOptions(ctx: BuildContext): RollupOptions {
1919
const _aliases = resolveAliases(ctx);
20-
return (<RollupOptions>{
20+
return {
2121
input: Object.fromEntries(
2222
ctx.options.entries
2323
.filter((entry) => entry.builder === "rollup")
@@ -29,7 +29,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
2929

3030
output: [
3131
ctx.options.rollup.emitCJS &&
32-
<OutputOptions>{
32+
({
3333
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
3434
entryFileNames: "[name].cjs",
3535
chunkFileNames: (chunk: PreRenderedChunk) =>
@@ -42,8 +42,8 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
4242
freeze: false,
4343
sourcemap: ctx.options.sourcemap,
4444
...ctx.options.rollup.output,
45-
},
46-
<OutputOptions>{
45+
} satisfies OutputOptions),
46+
{
4747
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
4848
entryFileNames: "[name].mjs",
4949
chunkFileNames: (chunk: PreRenderedChunk) =>
@@ -55,10 +55,10 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
5555
freeze: false,
5656
sourcemap: ctx.options.sourcemap,
5757
...ctx.options.rollup.output,
58-
},
59-
].filter(Boolean),
58+
} satisfies OutputOptions,
59+
].filter(Boolean) as OutputOptions[],
6060

61-
external(originalId) {
61+
external(originalId): boolean {
6262
// Resolve aliases
6363
const resolvedId = resolveAlias(originalId, _aliases);
6464

@@ -105,7 +105,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
105105
return false;
106106
},
107107

108-
onwarn(warning, rollupWarn) {
108+
onwarn(warning, rollupWarn): void {
109109
if (!warning.code || !["CIRCULAR_DEPENDENCY"].includes(warning.code)) {
110110
rollupWarn(warning);
111111
}
@@ -154,6 +154,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
154154
}),
155155

156156
ctx.options.rollup.preserveDynamicImports && {
157+
name: "unbuild=preserve-dynamic-imports",
157158
renderDynamicImport(): { left: string; right: string } {
158159
return { left: "import(", right: ")" };
159160
},
@@ -162,6 +163,6 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
162163
ctx.options.rollup.cjsBridge && cjsPlugin({}),
163164

164165
rawPlugin(),
165-
].filter(Boolean),
166-
}) as RollupOptions;
166+
].filter((p): p is NonNullable<Exclude<typeof p, false>> => !!p),
167+
} satisfies RollupOptions;
167168
}

src/builders/rollup/plugins/cjs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function fixCJSExportTypePlugin(ctx: BuildContext): Plugin {
3232
info.isEntry
3333
);
3434
},
35-
}) as Plugin;
35+
});
3636
}
3737

3838
const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/;

src/builders/rollup/plugins/json.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type { Plugin, TransformHook } from "rollup";
1+
import type { Plugin, TransformHook, TransformResult } from "rollup";
22
import type { RollupJsonOptions } from "@rollup/plugin-json";
33
import rollupJSONPlugin from "@rollup/plugin-json";
44

55
const EXPORT_DEFAULT = "export default ";
66

77
export function JSONPlugin(options: RollupJsonOptions): Plugin {
88
const plugin = rollupJSONPlugin(options);
9-
return <Plugin>{
9+
return {
1010
...plugin,
1111
name: "unbuild-json",
12-
transform(code, id) {
12+
transform(code, id): TransformResult {
1313
const res = (plugin.transform as TransformHook)!.call(this, code, id);
1414
if (
1515
res &&
@@ -22,5 +22,5 @@ export function JSONPlugin(options: RollupJsonOptions): Plugin {
2222
}
2323
return res;
2424
},
25-
};
25+
} satisfies Plugin;
2626
}

src/builders/rollup/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
RollupBuild,
44
OutputOptions,
55
InputPluginOption,
6+
Plugin,
67
} from "rollup";
78
import type { RollupReplaceOptions } from "@rollup/plugin-replace";
89
import type { RollupAliasOptions } from "@rollup/plugin-alias";
@@ -107,7 +108,7 @@ export interface RollupBuildOptions {
107108
}
108109

109110
export interface RollupOptions extends _RollupOptions {
110-
plugins: InputPluginOption[];
111+
plugins: Plugin[];
111112
}
112113

113114
export interface RollupHooks {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export interface BuildOptions {
9898
/**
9999
* Watch mode options.
100100
*/
101-
watchOptions: WatcherOptions;
101+
watchOptions: WatcherOptions | undefined;
102102

103103
/**
104104
* Stub options, where [jiti](https://github.com/unjs/jiti)

0 commit comments

Comments
 (0)