diff --git a/inlang/packages/paraglide-js/package.json b/inlang/packages/paraglide-js/package.json index f6ce560457..1304613d5a 100644 --- a/inlang/packages/paraglide-js/package.json +++ b/inlang/packages/paraglide-js/package.json @@ -60,19 +60,7 @@ "exports": { ".": "./dist/index.js", "./cli": "./dist/cli/index.js", - "./compiler": "./dist/compiler/index.js", - "./internal": { - "import": "./dist/index.js", - "types": "./dist/index.d.ts" - }, - "./internal/cli": { - "import": "./dist/cli/index.js", - "types": "./dist/cli/index.d.ts" - }, - "./internal/adapter-utils": { - "import": "./dist/adapter-utils/index.js", - "types": "./dist/adapter-utils/index.d.ts" - } + "./compiler": "./dist/compiler/index.js" }, "keywords": [ "inlang", diff --git a/inlang/packages/paraglide-js/src/cli/commands/init/command.ts b/inlang/packages/paraglide-js/src/cli/commands/init/command.ts index aa858374df..138e4c3630 100644 --- a/inlang/packages/paraglide-js/src/cli/commands/init/command.ts +++ b/inlang/packages/paraglide-js/src/cli/commands/init/command.ts @@ -6,7 +6,7 @@ import { findPackageJson } from "../../../services/environment/package.js"; import { checkForUncommittedChanges } from "../../steps/check-for-uncomitted-changes.js"; import { initializeInlangProject } from "../../steps/initialize-inlang-project.js"; import { maybeAddSherlock } from "../../steps/maybe-add-sherlock.js"; -import { maybeChangeTsConfig } from "../../steps/update-ts-config.js"; +import { maybeUpdateTsConfig } from "../../steps/update-ts-config.js"; import { promptForOutdir } from "../../steps/prompt-for-outdir.js"; import { updatePackageJson } from "../../steps/update-package-json.js"; import { runCompiler } from "../../steps/run-compiler.js"; @@ -44,7 +44,7 @@ export const initCommand = new Command() await addCompileStepToPackageJSON(ctx6); } - const ctx7 = await maybeChangeTsConfig(ctx6); + const ctx7 = await maybeUpdateTsConfig(ctx6); const ctx8 = await maybeAddSherlock(ctx7); try { diff --git a/inlang/packages/paraglide-js/src/cli/index.ts b/inlang/packages/paraglide-js/src/cli/index.ts index 74bd2d2b2b..003f9cc7dd 100644 --- a/inlang/packages/paraglide-js/src/cli/index.ts +++ b/inlang/packages/paraglide-js/src/cli/index.ts @@ -3,14 +3,6 @@ import { compileCommand } from "./commands/compile/command.js"; import { ENV_VARIABLES } from "../services/env-variables/index.js"; import { initCommand } from "./commands/init/command.js"; -export { checkForUncommittedChanges } from "./steps/check-for-uncomitted-changes.js"; -export { initializeInlangProject } from "./steps/initialize-inlang-project.js"; -export { maybeAddSherlock } from "./steps/maybe-add-sherlock.js"; -export { maybeChangeTsConfig } from "./steps/update-ts-config.js"; -export { promptForOutdir } from "./steps/prompt-for-outdir.js"; -export { updatePackageJson } from "./steps/update-package-json.js"; -export { runCompiler } from "./steps/run-compiler.js"; - export const cli = new Command() .name("paraglide-js") .addCommand(compileCommand) @@ -20,3 +12,5 @@ export const cli = new Command() export * as Utils from "./utils.js"; export * as Defaults from "./defaults.js"; +export * as Steps from "./steps/index.js"; +export { Logger } from "../services/logger/index.js"; diff --git a/inlang/packages/paraglide-js/src/cli/steps/index.ts b/inlang/packages/paraglide-js/src/cli/steps/index.ts new file mode 100644 index 0000000000..2522ad7130 --- /dev/null +++ b/inlang/packages/paraglide-js/src/cli/steps/index.ts @@ -0,0 +1,9 @@ +export { addVitePlugin } from "./add-vite-plugin.js"; +export { checkForUncommittedChanges } from "./check-for-uncomitted-changes.js"; +export { detectBundler } from "./detect-bundler.js"; +export { initializeInlangProject } from "./initialize-inlang-project.js"; +export { maybeAddSherlock } from "./maybe-add-sherlock.js"; +export { promptForOutdir } from "./prompt-for-outdir.js"; +export { runCompiler } from "./run-compiler.js"; +export { updatePackageJson } from "./update-package-json.js"; +export { maybeUpdateTsConfig } from "./update-ts-config.js"; diff --git a/inlang/packages/paraglide-js/src/cli/steps/update-ts-config.ts b/inlang/packages/paraglide-js/src/cli/steps/update-ts-config.ts index 7195bc4d12..05bf3c58e6 100644 --- a/inlang/packages/paraglide-js/src/cli/steps/update-ts-config.ts +++ b/inlang/packages/paraglide-js/src/cli/steps/update-ts-config.ts @@ -5,18 +5,18 @@ import JSON5 from "json5"; import { pathExists } from "../../services/file-handling/exists.js"; import nodePath from "node:path"; -export const maybeChangeTsConfig: CliStep< +export const maybeUpdateTsConfig: CliStep< { fs: typeof import("node:fs/promises"); logger: Logger }, unknown > = async (ctx) => { - const ctx1 = await maybeChangeTsConfigAllowJs(ctx); - return await maybeChangeTsConfigModuleResolution(ctx1); + const ctx1 = await maybeUpdateTsConfigAllowJs(ctx); + return await maybeUpdateTsConfigModuleResolution(ctx1); }; /** * Paraligde JS compiles to JS with JSDoc comments. TypeScript doesn't allow JS files by default. */ -export const maybeChangeTsConfigAllowJs: CliStep< +export const maybeUpdateTsConfigAllowJs: CliStep< { fs: typeof import("node:fs/promises"); logger: Logger }, unknown > = async (ctx) => { @@ -86,7 +86,7 @@ export const maybeChangeTsConfigAllowJs: CliStep< * Otherwise, types defined in `package.exports` are not resolved by TypeScript. Leading to type * errors with Paraglide-JS. */ -const maybeChangeTsConfigModuleResolution: CliStep< +const maybeUpdateTsConfigModuleResolution: CliStep< { fs: typeof import("node:fs/promises"); logger: Logger }, unknown > = async (ctx) => { diff --git a/inlang/packages/paraglide-js/src/compiler/index.ts b/inlang/packages/paraglide-js/src/compiler/index.ts index a3149de109..9081c51fbe 100644 --- a/inlang/packages/paraglide-js/src/compiler/index.ts +++ b/inlang/packages/paraglide-js/src/compiler/index.ts @@ -1,5 +1,7 @@ export type { CompilerArgs } from "./compile.js"; export type { CompilerOptions } from "./compile-project.js"; +export type { MessageBundleFunction, MessageFunction } from "./types.js"; +export type { Runtime } from "./runtime/type.js"; export { compile } from "./compile.js"; export { compileProject } from "./compile-project.js"; export { compileBundle } from "./compile-bundle.js"; diff --git a/inlang/packages/paraglide-js/src/compiler/runtime/type.test.ts b/inlang/packages/paraglide-js/src/compiler/runtime/type.test.ts new file mode 100644 index 0000000000..f9b8b17999 --- /dev/null +++ b/inlang/packages/paraglide-js/src/compiler/runtime/type.test.ts @@ -0,0 +1,54 @@ +import { expect, test } from "vitest"; +import { createProject as typescriptProject, ts } from "@ts-morph/bootstrap"; +import { createRuntime } from "./create-runtime.js"; +import fs from "node:fs/promises"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +test("runtime type", async () => { + const project = await typescriptProject({ + useInMemoryFileSystem: true, + compilerOptions: { + outDir: "./dist", + declaration: true, + allowJs: true, + checkJs: true, + module: ts.ModuleKind.Node16, + strict: true, + }, + }); + + const jsdocRuntime = createRuntime( + { baseLocale: "en", locales: ["en"] }, + false + ); + + const runtimeType = await fs.readFile( + resolve(__dirname, "./type.ts"), + "utf-8" + ); + + project.createSourceFile("./runtime.js", jsdocRuntime); + + project.createSourceFile("./runtime-type.ts", runtimeType); + + project.createSourceFile( + "./test.ts", + ` + import * as runtime from "./runtime.js" + import type { Runtime as RuntimeType } from "./runtime-type.js" + + const runtimeType: RuntimeType = runtime + ` + ); + + const program = project.createProgram(); + const diagnostics = ts.getPreEmitDiagnostics(program); + for (const diagnostic of diagnostics) { + console.error(diagnostic.messageText, diagnostic.file?.fileName); + } + expect(diagnostics.length).toEqual(0); +}); diff --git a/inlang/packages/paraglide-js/src/compiler/runtime/type.ts b/inlang/packages/paraglide-js/src/compiler/runtime/type.ts new file mode 100644 index 0000000000..b2a25604c7 --- /dev/null +++ b/inlang/packages/paraglide-js/src/compiler/runtime/type.ts @@ -0,0 +1,17 @@ +/** + * The Paraglide runtime API. + */ +export type Runtime = { + baseLocale: UnknownLocale; + locales: Readonly; + getLocale: () => string; + setLocale: (newLocale: UnknownLocale) => void; + defineGetLocale: (fn: () => UnknownLocale) => void; + defineSetLocale: (fn: (newLocale: UnknownLocale) => void) => void; + isLocale: (locale: UnknownLocale) => locale is UnknownLocale; +}; + +/** + * A locale that is unknown before compilation. + */ +export type UnknownLocale = any;