Describe the bug
Create typescript config file lingui.config.ts with any import or require
import { ALL_LANGUAGES_ARRAY, DEFAULT_LANGUAGE } from './languages';
module.exports = {
locales: ALL_LANGUAGES_ARRAY.map((l) => l.key),
sourceLocale: DEFAULT_LANGUAGE,
catalogs: [{
path: '<rootDir>/translates/{locale}',
include: ['<rootDir>/src'],
}],
format: 'po',
};
Run the extractor. And you will get an error pointing that module not found
If you rename file to plain js it works.
If you put a console.log(__dirname) in both files you will see that js files executed from it's original place, where ts file executed from insides of lingui package.
That's caused by custom TypescriptLoader for cosmiconfig defined here
|
/** Typescript loader using just typescript API and eval(), instead of using ts-node/register which is slower */ |
|
function TypeScriptLoader(filePath: string) { |
|
const tsc = require("typescript") |
|
const fileContent = fs.readFileSync(filePath, "utf-8") |
|
const { outputText } = tsc.transpileModule(fileContent, { compilerOptions: { module: tsc.ModuleKind.CommonJS }}); |
|
const configFileParsed = eval(outputText) |
|
return configFileParsed |
|
} |
Which is basically just eval the code in place.
Rationality
Although the configuration is usually very simple, it's not the case where you have a monorepo with different frameworks and tools. Writing and maintaining list of languages in every app in such monorepo could be cumbersome and easy to forget to add language in one of places. That's why we use one monorepo-wide file with all languages definitions written in TS.
Proposed solution:
Get rid of this custom, "fast" and actually, broken loader in favour of standart one.
Additional context
Describe the bug
Create typescript config file
lingui.config.tswith any import or requireRun the extractor. And you will get an error pointing that module not found
If you rename file to plain js it works.
If you put a
console.log(__dirname)in both files you will see that js files executed from it's original place, where ts file executed from insides of lingui package.That's caused by custom TypescriptLoader for cosmiconfig defined here
js-lingui/packages/conf/src/index.ts
Lines 580 to 587 in da070ba
Which is basically just eval the code in place.
Rationality
Although the configuration is usually very simple, it's not the case where you have a monorepo with different frameworks and tools. Writing and maintaining list of languages in every app in such monorepo could be cumbersome and easy to forget to add language in one of places. That's why we use one monorepo-wide file with all languages definitions written in TS.
Proposed solution:
Get rid of this custom, "fast" and actually, broken loader in favour of standart one.
Additional context
3.14.0