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 runtime type generation to wrangler types command #6295

Merged
merged 12 commits into from
Jul 23, 2024
52 changes: 37 additions & 15 deletions packages/wrangler/src/deployment-bundle/node-compat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserError } from "../errors";
import { logger } from "../logger";
import type { Config } from "../config";

/**
* Wrangler can provide Node.js compatibility in a number of different modes:
Expand Down Expand Up @@ -42,20 +43,27 @@ export function validateNodeCompat({
);
}

const nodejsCompatV2 = compatibilityFlags.includes(
"experimental:nodejs_compat_v2"
);
const nodejsCompatV2NotExperimental =
compatibilityFlags.includes("nodejs_compat_v2");

if (nodejsCompatV2NotExperimental) {
throw new UserError(
`The \`nodejs_compat_v2\` compatibility flag is experimental and must be prefixed with \`experimental:\`. Use \`experimental:nodejs_compat_v2\` flag instead.`
);
}

const { mode, nodejsCompat, nodejsCompatV2 } = getNodeCompatMode({
compatibility_flags: compatibilityFlags,
node_compat: legacyNodeCompat,
});

if (nodejsCompatV2) {
// strip the "experimental:" prefix because workerd doesn't understand it yet.
compatibilityFlags[
compatibilityFlags.indexOf("experimental:nodejs_compat_v2")
] = "nodejs_compat_v2";
}

const nodejsCompat = compatibilityFlags.includes("nodejs_compat");
if (nodejsCompat && nodejsCompatV2) {
throw new UserError(
"The `nodejs_compat` and `nodejs_compat_v2` compatibility flags cannot be used in together. Please select just one."
Expand All @@ -68,12 +76,6 @@ export function validateNodeCompat({
);
}

if (nodejsCompatV2NotExperimental) {
throw new UserError(
`The \`nodejs_compat_v2\` compatibility flag is experimental and must be prefixed with \`experimental:\`. Use \`experimental:nodejs_compat_v2\` flag instead.`
);
}

if (nodejsCompatV2) {
logger.warn(
"Enabling experimental Node.js compatibility mode v2. This feature is still in development and not ready for production use."
Expand All @@ -92,14 +94,34 @@ export function validateNodeCompat({
);
}

return mode;
}

export function getNodeCompatMode({
compatibility_flags,
node_compat,
}: Pick<Config, "compatibility_flags" | "node_compat">) {
const nodejsCompat = compatibility_flags.includes("nodejs_compat");
const nodejsCompatV2 = compatibility_flags.includes(
"experimental:nodejs_compat_v2"
);

let mode;
if (nodejsCompatV2) {
return "v2";
mode = "v2";
}
if (nodejsCompat) {
return "v1";
mode = "v1";
}
if (legacyNodeCompat) {
return "legacy";
if (node_compat) {
mode = "legacy";
}
return null;
mode = null;
andyjessop marked this conversation as resolved.
Show resolved Hide resolved

return {
legacy: node_compat === true,
mode,
nodejsCompat,
nodejsCompatV2,
};
}
9 changes: 3 additions & 6 deletions packages/wrangler/src/type-generation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as esmLexer from "es-module-lexer";
import { findUpSync } from "find-up";
import { findWranglerToml, readConfig } from "../config";
import { getEntry } from "../deployment-bundle/entry";
import { getNodeCompatMode } from "../deployment-bundle/node-compat";
import { getVarsForDev } from "../dev/dev-vars";
import { UserError } from "../errors";
import { CommandLineArgsError } from "../index";
Expand Down Expand Up @@ -90,13 +91,9 @@ export async function typesHandler(
const tsconfigPath =
config.tsconfig ?? join(dirname(configPath), "tsconfig.json");
const tsconfigTypes = readTsconfigTypes(tsconfigPath);
const hasNodeCompatFlag = config.compatibility_flags.some((flag) =>
flag.includes("nodejs_")
);

const isNodeCompat = config.node_compat || hasNodeCompatFlag;
const { mode } = getNodeCompatMode(config);

logRuntimeTypesMessage(outFile, tsconfigTypes, isNodeCompat);
logRuntimeTypesMessage(outFile, tsconfigTypes, mode !== null);
}

const secrets = getVarsForDev(
Expand Down
Loading