Skip to content

Custom TypescriptLoader for cosmic config breaks import/require module resolution #1295

@timofei-iatsenko

Description

@timofei-iatsenko

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

  • jsLingui version 3.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions