Skip to content

Commit

Permalink
fix(conf): lazy load cosmiconfig's TypeScriptLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
thekip committed Feb 3, 2023
1 parent a8c110d commit 956d652
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
12 changes: 12 additions & 0 deletions packages/conf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,17 @@
],
"devDependencies": {
"ts-node": "^10.9.1"
},
"peerDependencies": {
"ts-node": ">=10",
"typescript": ">=4"
},
"peerDependenciesMeta": {
"ts-node": {
"optional": true
},
"typescript": {
"optional": true
}
}
}
50 changes: 37 additions & 13 deletions packages/conf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -85,7 +84,7 @@ export const defaultConfig: LinguiConfig = {
minified: true,
jsescOption: {
minimal: true,
}
},
},
extractBabelOptions: { plugins: [], presets: [] },
fallbackLocales: {},
Expand All @@ -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,
Expand Down Expand Up @@ -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" },
Expand All @@ -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),
},
}

Expand Down Expand Up @@ -575,5 +597,7 @@ export function catalogMigration(
return newConfig
}

const pipe = (...functions: Array<Function>) => (args: any): any =>
functions.reduce((arg, fn) => fn(arg), args)
const pipe =
(...functions: Array<Function>) =>
(args: any): any =>
functions.reduce((arg, fn) => fn(arg), args)

0 comments on commit 956d652

Please sign in to comment.