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

feat: add hook before prepare:types #2505

Draft
wants to merge 1 commit into
base: v2
Choose a base branch
from
Draft
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
72 changes: 43 additions & 29 deletions src/core/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,45 +165,27 @@ declare module "nitropack/types" {
"export {}",
];

function pathToReference(path: string) {
return `/// <reference path="${path}" />`;
}

const declarations = [
// local nitropack augmentations
'/// <reference path="./nitro-routes.d.ts" />',
'/// <reference path="./nitro-config.d.ts" />',
"./nitro-routes.d.ts",
"./nitro-config.d.ts",
// global server auto-imports
'/// <reference path="./nitro-imports.d.ts" />',
"./nitro-imports.d.ts",
];

const buildFiles: { path: string; contents: string }[] = [];

buildFiles.push({
path: join(typesDir, "nitro-routes.d.ts"),
contents: routes.join("\n"),
});

buildFiles.push({
path: join(typesDir, "nitro-config.d.ts"),
contents: config.join("\n"),
});

buildFiles.push({
path: join(typesDir, "nitro-imports.d.ts"),
contents: [...autoImportedTypes, autoImportExports || "export {}"].join(
"\n"
),
});

buildFiles.push({
path: join(typesDir, "nitro.d.ts"),
contents: declarations.join("\n"),
});

let tsConfigPath: string = "";
let tsConfig: TSConfig | undefined = undefined;
if (nitro.options.typescript.generateTsConfig) {
const tsConfigPath = resolve(
tsConfigPath = resolve(
nitro.options.buildDir,
nitro.options.typescript.tsconfigPath
);
const tsconfigDir = dirname(tsConfigPath);
const tsConfig: TSConfig = defu(nitro.options.typescript.tsConfig, {
tsConfig = defu(nitro.options.typescript.tsConfig, {
compilerOptions: {
forceConsistentCasingInFileNames: true,
strict: nitro.options.typescript.strict,
Expand Down Expand Up @@ -314,7 +296,39 @@ declare module "nitropack/types" {
),
];
}
}

// Allow modules to modify the types before writing them
await nitro.hooks.callHook("prepare:types", {
declarations: declarations,
tsConfig: tsConfig,
});

const buildFiles: { path: string; contents: string }[] = [];

buildFiles.push({
path: join(typesDir, "nitro-routes.d.ts"),
contents: routes.join("\n"),
});

buildFiles.push({
path: join(typesDir, "nitro-config.d.ts"),
contents: config.join("\n"),
});

buildFiles.push({
path: join(typesDir, "nitro-imports.d.ts"),
contents: [...autoImportedTypes, autoImportExports || "export {}"].join(
"\n"
),
});

buildFiles.push({
path: join(typesDir, "nitro.d.ts"),
contents: declarations.map((file) => pathToReference(file)).join("\n"),
});

if (tsConfig) {
buildFiles.push({
path: tsConfigPath,
contents: JSON.stringify(tsConfig, null, 2),
Expand Down
5 changes: 5 additions & 0 deletions src/types/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TSConfig } from "pkg-types";
import type { NitroConfig } from "./config";
import type { NitroTypes, Nitro } from "./nitro";
import type { PrerenderRoute } from "./prerender";
Expand All @@ -6,6 +7,10 @@ import type { RollupConfig } from "./rollup";
type HookResult = void | Promise<void>;

export interface NitroHooks {
"prepare:types": (options: {
declarations: string[];
tsConfig: TSConfig | undefined;
}) => HookResult;
"types:extend": (types: NitroTypes) => HookResult;
"rollup:before": (nitro: Nitro, config: RollupConfig) => HookResult;
compiled: (nitro: Nitro) => HookResult;
Expand Down