Skip to content

Commit

Permalink
make emitTs experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Jan 14, 2025
1 parent 718d2c1 commit c3d55a6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
39 changes: 39 additions & 0 deletions inlang/packages/paraglide-js/docs/compiler-options.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
imports:
- https://cdn.jsdelivr.net/npm/@opral/markdown-wc-doc-elements/dist/doc-callout.js
---

# Compiler options

## `emitGitIgnore`
Expand Down Expand Up @@ -64,4 +69,38 @@ Messages are bundled in a per locale module. Bundlers sometimes struggle tree-sh
- ...
- messages.js
- runtime.js
```

## `experimentalEmitTs`

Emits TypeScript files instead of JSDoc annotated JavaScript files. Defaults to `false`.

<doc-callout type="warning">
This feature is experimental and may change or be removed in the future.
</doc-callout>

```diff
- outdir/
- - messages.js
+ - messages.ts
- - runtime.js
+ - runtime.ts
...
```

## `experimentalUseTsImports`

Imports emitted files with `.ts` extension instead of `.js`. Defaults to `false`.

Only works in combination with `experimentalEmitTs`. The feature is useful in some
codebases which need to resolve TypeScript files with a `.ts` ending. Node's
strip-types flag is an example.

<doc-callout type="warning">
This feature is experimental and may change or be removed in the future.
</doc-callout>

```diff
-import { getLocale } from "./runtime.js";
+import { getLocale } from "./runtime.ts";
```
26 changes: 21 additions & 5 deletions inlang/packages/paraglide-js/src/compiler/compileProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,31 @@ test("emitPrettierIgnore", async () => {

describe.each([
// useTsImports must be true to test emitTs. Otherwise, rolldown can't resolve the imports
{ outputStructure: "locale-modules", emitTs: false, useTsImports: false },
{ outputStructure: "locale-modules", emitTs: true, useTsImports: true },
{ outputStructure: "message-modules", emitTs: false, useTsImports: false },
{ outputStructure: "message-modules", emitTs: true, useTsImports: true },
{
outputStructure: "locale-modules",
experimentalEmitTs: false,
experimentalUseTsImports: false,
},
{
outputStructure: "locale-modules",
experimentalEmitTs: true,
experimentalUseTsImports: true,
},
{
outputStructure: "message-modules",
experimentalEmitTs: false,
experimentalUseTsImports: false,
},
{
outputStructure: "message-modules",
experimentalEmitTs: true,
experimentalUseTsImports: true,
},
] satisfies Array<ParaglideCompilerOptions>)(
"options",
async (compilerOptions) => {
const output = await compileProject({ project, compilerOptions });
const importExt = compilerOptions.useTsImports ? "ts" : "js";
const importExt = compilerOptions.experimentalEmitTs ? "ts" : "js";
describe("tree-shaking", () => {
test("should tree-shake unused messages", async () => {
const code = await bundleCode(
Expand Down
18 changes: 9 additions & 9 deletions inlang/packages/paraglide-js/src/compiler/compileProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ParaglideCompilerOptions = {
*
* @default false
*/
emitTs?: boolean;
experimentalEmitTs?: boolean;
/**
* Whether to import files as TypeScript in the emitted code.
*
Expand All @@ -29,7 +29,7 @@ export type ParaglideCompilerOptions = {
* // true
* import { getLocale } from "./runtime.ts";
*/
useTsImports?: boolean;
experimentalUseTsImports?: boolean;
/**
* Whether to emit a .prettierignore file.
*
Expand All @@ -52,8 +52,8 @@ export type ParaglideCompilerOptions = {

const defaultCompilerOptions = {
outputStructure: "message-modules",
emitTs: false,
useTsImports: false,
experimentalEmitTs: false,
experimentalUseTsImports: false,
emitGitIgnore: true,
emitPrettierIgnore: true,
} as const satisfies ParaglideCompilerOptions;
Expand Down Expand Up @@ -88,7 +88,7 @@ export const compileProject = async (args: {
bundle,
fallbackMap,
registry: DEFAULT_REGISTRY,
emitTs: optionsWithDefaults.emitTs,
emitTs: optionsWithDefaults.experimentalEmitTs,
})
);

Expand All @@ -99,8 +99,8 @@ export const compileProject = async (args: {
compiledBundles,
settings,
fallbackMap,
optionsWithDefaults.emitTs,
optionsWithDefaults.useTsImports
optionsWithDefaults.experimentalEmitTs,
optionsWithDefaults.experimentalUseTsImports
);
Object.assign(output, regularOutput);
}
Expand All @@ -110,8 +110,8 @@ export const compileProject = async (args: {
compiledBundles,
settings,
fallbackMap,
optionsWithDefaults.emitTs,
optionsWithDefaults.useTsImports
optionsWithDefaults.experimentalEmitTs,
optionsWithDefaults.experimentalUseTsImports
);
Object.assign(output, messageModuleOutput);
}
Expand Down

0 comments on commit c3d55a6

Please sign in to comment.