Skip to content

Commit c50a06e

Browse files
authored
fix(): extractors CLI validation and accept require and require.resolve (#1126)
1 parent d688329 commit c50a06e

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

packages/cli/src/api/extractors/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ export default function extract(
2727
): boolean {
2828
const extractorsToExtract = options.extractors ?? DEFAULT_EXTRACTORS
2929

30-
return extractorsToExtract.some((ext) => {
30+
return extractorsToExtract.some((e) => {
31+
let ext: ExtractorType = e;
32+
if (typeof e === "string") {
33+
// in case of the user using require.resolve in their extractors, we require that module
34+
ext = require(e)
35+
if ((ext as any).default) {
36+
ext = (ext as any).default
37+
}
38+
}
3139
if (!ext.match(filename)) return false
3240

3341
let spinner

packages/conf/src/__snapshots__/index.test.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ Object {
120120
plugins: Array [],
121121
presets: Array [],
122122
},
123+
extractors: Array [
124+
Array [],
125+
Array [
126+
babel,
127+
],
128+
Array [
129+
Object {
130+
extract: [Function],
131+
match: [Function],
132+
},
133+
],
134+
],
123135
fallbackLocales: Object {
124136
en-gb: en,
125137
},

packages/conf/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export type LinguiConfig = {
4545
extractBabelOptions: Record<string, unknown>
4646
compilerBabelOptions: GeneratorOptions
4747
fallbackLocales?: FallbackLocales
48-
extractors?: ExtractorType[]
48+
extractors?: ExtractorType[] | string[]
4949
format: CatalogFormat
5050
formatOptions: CatalogFormatOptions
5151
locales: string[]
@@ -82,6 +82,14 @@ export const defaultConfig: LinguiConfig = {
8282
minimal: true,
8383
}
8484
},
85+
extractors: multipleValidOptions(
86+
[],
87+
["babel"],
88+
[{
89+
match: (fileName: string) => false,
90+
extract: (filename: string, targetDir: string, options?: any) => {}
91+
} as ExtractorType]
92+
),
8593
extractBabelOptions: { plugins: [], presets: [] },
8694
fallbackLocales: {},
8795
format: "po",

0 commit comments

Comments
 (0)