Skip to content

Commit

Permalink
Merge pull request #3360 from opral/expose-runtime-type
Browse files Browse the repository at this point in the history
expose runtime type
  • Loading branch information
samuelstroschein authored Jan 18, 2025
2 parents 43d81f8 + 88884c0 commit 3b975f6
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 28 deletions.
14 changes: 1 addition & 13 deletions inlang/packages/paraglide-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions inlang/packages/paraglide-js/src/cli/commands/init/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions inlang/packages/paraglide-js/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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";
9 changes: 9 additions & 0 deletions inlang/packages/paraglide-js/src/cli/steps/index.ts
Original file line number Diff line number Diff line change
@@ -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";
10 changes: 5 additions & 5 deletions inlang/packages/paraglide-js/src/cli/steps/update-ts-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 2 additions & 0 deletions inlang/packages/paraglide-js/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
54 changes: 54 additions & 0 deletions inlang/packages/paraglide-js/src/compiler/runtime/type.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
17 changes: 17 additions & 0 deletions inlang/packages/paraglide-js/src/compiler/runtime/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* The Paraglide runtime API.
*/
export type Runtime = {
baseLocale: UnknownLocale;
locales: Readonly<UnknownLocale[]>;
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;

0 comments on commit 3b975f6

Please sign in to comment.