lingui / lingui/js-lingui

`compileNamespace` should take package.json "type" into account

Open
#1,704 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
TypeScript
Stars
5.9k
Forks
457
Avg merge
1d 23h
Merged PRs (30d)
16

Description

When you set `"type": "module"` in your JSON both `.js` and `.ts` files (if you are using TypeScript) are considered modules. You only need to use the `.m[jt]s` or `.c[jt]s` extensions as an override.

I bring this up because it also applies to TypeScript, not just vanilla JavaScript. And both can have `module.exports =` or `export const` depending on the combination of package type and extension used.

I'm running into issues with babel when compiling code with a mix of `.ts` and `.cts` extensions. Babel just compiles everything to `.js`. There doesn't seem to be a way to solve this.

Also, newer node versions unfortunately are extremely strict about this, so if you don't use the right syntax it just fails.

I would like to propose reading the package.json type and use the following logic:

- If module and `compileNamespace` is `"cjs"`, write `module.exports =` to a `.cjs` file
- If module and `compileNamespace` is `"es"`, write `export const messages =` to a `.js` file
- If module and `compileNamespace` is `"ts"`, write `export const messages: AllMessages =` to a `.ts` file
- If commonjs and `compileNamespace` is `"cjs"`, write `module.exports =` to a `.js` file
- If commonjs and `compileNamespace` is `"es"`, write `export const messages =` to a `.mjs` file
- If commonjs and `compileNamespace` is `"ts"`, write `export const messages: AllMessages =` to a `.mts` file

If this is too complicated I understand. Ideally Lingui would just have a "typescript" option and just detect the package.json type. And optionally be able to provide a custom compilation function.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the compileNamespace implementation and any tests covering generated JavaScript or TypeScript output. Read how package.json is currently handled, then verify the module/commonjs and cjs, es, and ts combinations described in the issue, including the expected output extensions and syntax.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript, nodejs, typescript
Domain
internationalization, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.