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

fix: compile should generate a TS file #1851

Merged
merged 4 commits into from Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/api/__snapshots__/compile.test.ts.snap
Expand Up @@ -10,7 +10,7 @@ exports[`createCompiledCatalog options.namespace should compile with global 1`]

exports[`createCompiledCatalog options.namespace should compile with json 1`] = `{"messages":{"key":["Hello ",["name"]]}}`;

exports[`createCompiledCatalog options.namespace should compile with ts 1`] = `/*eslint-disable*/export const messages=JSON.parse("{\\"key\\":[\\"Hello \\",[\\"name\\"]]}");`;
exports[`createCompiledCatalog options.namespace should compile with ts 1`] = `/*eslint-disable*/import type{Messages}from"@lingui/core";export const messages:Messages=JSON.parse("{\\"key\\":[\\"Hello \\",[\\"name\\"]]}");`;

exports[`createCompiledCatalog options.namespace should compile with window 1`] = `/*eslint-disable*/window.test={messages:JSON.parse("{\\"key\\":[\\"Hello \\",[\\"name\\"]]}")};`;

Expand Down
24 changes: 23 additions & 1 deletion packages/cli/src/api/compile.ts
Expand Up @@ -70,7 +70,29 @@ function buildExportStatement(
expression: t.Expression,
namespace: CompiledCatalogNamespace
) {
if (namespace === "es" || namespace === "ts") {
if (namespace === "ts") {
// import type { Messages } from "@lingui/core";
const importMessagesTypeDeclaration = t.importDeclaration(
[t.importSpecifier(t.identifier("Messages"), t.identifier("Messages"))],
t.stringLiteral("@lingui/core")
)
importMessagesTypeDeclaration.importKind = "type"

// Create the exported `messages` identifier with a `Messages` TS type annotation
const messagesIdentifier = t.identifier("messages")
messagesIdentifier.typeAnnotation = t.tsTypeAnnotation(
t.tsTypeReference(t.identifier("Messages"))
)

// export const messages:Messages = { message: "Translation" }
const exportDeclaration = t.exportNamedDeclaration(
t.variableDeclaration("const", [
t.variableDeclarator(messagesIdentifier, expression),
])
)

return t.program([importMessagesTypeDeclaration, exportDeclaration])
} else if (namespace === "es") {
// export const messages = { message: "Translation" }
return t.exportNamedDeclaration(
t.variableDeclaration("const", [
Expand Down
12 changes: 0 additions & 12 deletions packages/cli/src/lingui-compile.ts
@@ -1,6 +1,5 @@
import chalk from "chalk"
import chokidar from "chokidar"
import fs from "fs"
import { program } from "commander"

import { getConfig, LinguiConfigNormalized } from "@lingui/conf"
Expand Down Expand Up @@ -90,17 +89,6 @@ export async function command(
namespace
)

if (options.typescript) {
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
const typescriptPath = compiledPath.replace(/\.ts?$/, "") + ".d.ts"
fs.writeFileSync(
typescriptPath,
`import type { Messages } from '@lingui/core';
declare const messages: Messages;
export { messages };
`
)
}

compiledPath = normalizeSlashes(
nodepath.relative(config.rootDir, compiledPath)
)
Expand Down
18 changes: 16 additions & 2 deletions website/docs/ref/cli.md
Expand Up @@ -21,6 +21,19 @@
}
```

:::tip
If you use TypeScript, you can add `--typescript` flag to `compile` script to produce compiled message catalogs with TypeScript types.

```json title="package.json"
{
"scripts": {
"compile": "lingui compile --typescript"
}
}
```

:::

## Global options

### `--config <config>`
Expand Down Expand Up @@ -58,7 +71,7 @@ lingui extract src/components

Will extract only messages from `src/components/**/*` files, you can also pass multiple paths.

It's useful if you want to run extract command on files that are staged, using for example `husky`, before commiting will extract messages from staged files:
It's useful if you want to run extract command on files that are staged, using for example `husky`, before committing will extract messages from staged files:

```json title="package.json"
{
Expand Down Expand Up @@ -125,6 +138,7 @@ lingui compile
[--strict]
[--format <format>]
[--verbose]
[--typescript]
[--namespace <namespace>]
[--watch [--debounce <delay>]]
```
Expand Down Expand Up @@ -161,7 +175,7 @@ Specify namespace for compiled message catalogs (also see [`compileNamespace`](/

#### `--typescript` {#compile-typescript}

Is the same as using [`compileNamespace`](/docs/ref/conf.md#compilenamespace) with the value "ts". Generates a `{compiledFile}.d.ts` and the compiled file is generated using the extension .ts
Is the same as using [`compileNamespace`](/docs/ref/conf.md#compilenamespace) with the value "ts". Generates a `{compiledFile}.ts` file and the exported object is typed using TS.

#### `--watch` {#compile-watch}

Expand Down
4 changes: 2 additions & 2 deletions website/docs/tutorials/react.md
Expand Up @@ -245,13 +245,13 @@ Catalog statistics:
That's great! So, how we're going to load it into your app? [LinguiJS](https://github.com/lingui/js-lingui) introduces concept of compiled message catalogs. Before we load messages into our app, we need to compile them. As you see in the help in command output, we use [`compile`](/docs/ref/cli.md#compile) for that:

```bash
> lingui compile
> lingui compile --typescript
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved

Compiling message catalogs…
Done!
```

What just happened? If you look inside `locales/<locale>` directory, you'll see there's a new file for each locale: `messages.js`. This file contains compiled message catalog.
What just happened? If you look inside `locales/<locale>` directory, you'll see there's a new file for each locale: `messages.ts`. This file contains compiled message catalog.

Let's load this file into our app and set active language to `cs`:

Expand Down
2 changes: 1 addition & 1 deletion website/docs/tutorials/setup-react.md
Expand Up @@ -60,7 +60,7 @@ This setup guide is for any project which uses React.
{
"scripts": {
"extract": "lingui extract",
"compile": "lingui compile"
"compile": "lingui compile --typescript"
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
}
}
```
Expand Down