diff --git a/packages/conf/package.json b/packages/conf/package.json index ed09839bf..085b03502 100644 --- a/packages/conf/package.json +++ b/packages/conf/package.json @@ -34,5 +34,17 @@ ], "devDependencies": { "ts-node": "^10.9.1" + }, + "peerDependencies": { + "ts-node": ">=10", + "typescript": ">=4" + }, + "peerDependenciesMeta": { + "ts-node": { + "optional": true + }, + "typescript": { + "optional": true + } } } diff --git a/packages/conf/src/index.ts b/packages/conf/src/index.ts index 9384eeff2..43f47a497 100644 --- a/packages/conf/src/index.ts +++ b/packages/conf/src/index.ts @@ -2,9 +2,8 @@ import type { GeneratorOptions } from "@babel/core" import path from "path" import fs from "fs" import chalk from "chalk" -import { cosmiconfigSync } from "cosmiconfig" +import { cosmiconfigSync, type LoaderSync } from "cosmiconfig" import { multipleValidOptions, validate } from "jest-validate" -import { TypeScriptLoader } from "cosmiconfig-typescript-loader"; export type CatalogFormat = "lingui" | "minimal" | "po" | "csv" @@ -85,7 +84,7 @@ export const defaultConfig: LinguiConfig = { minified: true, jsescOption: { minimal: true, - } + }, }, extractBabelOptions: { plugins: [], presets: [] }, fallbackLocales: {}, @@ -97,13 +96,26 @@ export const defaultConfig: LinguiConfig = { rootDir: ".", runtimeConfigModule: ["@lingui/core", "i18n"], sourceLocale: "", - service: { name: "", apiKey: "" } + service: { name: "", apiKey: "" }, } function configExists(path) { return path && fs.existsSync(path) } +function TypeScriptLoader(): LoaderSync { + let loaderInstance: LoaderSync + return (filepath, content) => { + if (!loaderInstance) { + const { TypeScriptLoader } = + require("cosmiconfig-typescript-loader") as typeof import("cosmiconfig-typescript-loader") + loaderInstance = TypeScriptLoader() + } + + return loaderInstance(filepath, content) + } +} + export function getConfig({ cwd, configPath, @@ -166,12 +178,17 @@ const exampleConfig = { extractors: multipleValidOptions( [], ["babel"], - [{ - match: (fileName: string) => false, - extract: (filename: string, targetDir: string, options?: any) => {} - } as ExtractorType] + [ + { + match: (fileName: string) => false, + extract: (filename: string, targetDir: string, options?: any) => {}, + } as ExtractorType, + ] + ), + runtimeConfigModule: multipleValidOptions( + { i18n: ["@lingui/core", "i18n"], Trans: ["@lingui/react", "Trans"] }, + ["@lingui/core", "i18n"] ), - runtimeConfigModule: multipleValidOptions({i18n: ["@lingui/core", "i18n"], Trans: ["@lingui/react", "Trans"]}, ["@lingui/core", "i18n"]), fallbackLocales: multipleValidOptions( {}, { "en-US": "en" }, @@ -184,10 +201,15 @@ const exampleConfig = { rootMode: "rootmode", plugins: ["plugin"], presets: ["preset"], - targets: multipleValidOptions({}, '> 0.5%', ['> 0.5%', 'not dead'], undefined), + targets: multipleValidOptions( + {}, + "> 0.5%", + ["> 0.5%", "not dead"], + undefined + ), assumptions: multipleValidOptions({}, undefined), browserslistConfigFile: multipleValidOptions(true, undefined), - browserslistEnv: multipleValidOptions('.browserslistrc', undefined), + browserslistEnv: multipleValidOptions(".browserslistrc", undefined), }, } @@ -575,5 +597,7 @@ export function catalogMigration( return newConfig } -const pipe = (...functions: Array) => (args: any): any => - functions.reduce((arg, fn) => fn(arg), args) +const pipe = + (...functions: Array) => + (args: any): any => + functions.reduce((arg, fn) => fn(arg), args)