Skip to content

Commit

Permalink
fix: dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
imranbarbhuiya committed Jul 19, 2023
1 parent dad7f33 commit 58b2196
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
28 changes: 16 additions & 12 deletions packages/core/src/parseOptionsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,22 @@ export type OptionsWithDefault = typeof defaultOption;
export async function parseOptionsFile(cliOptions: OptionsWithDefault): Promise<OptionsWithDefault> {
const config = cliOptions.config;
const configUrl = join(process.cwd(), config).replaceAll('\\', '/');
const options = await import(
`file://${configUrl}`,
configUrl.endsWith('.json')
? {
assert: {
type: 'json'
}
}
: undefined
).catch(() => ({
default: {}
}));

let options: { default: OptionsWithDefault };

if (configUrl.endsWith('.json')) {
options = await import(`file://${configUrl}`, {
assert: {
type: 'json'
}
}).catch(() => ({
default: {}
}));
} else {
options = await import(`file://${configUrl}`).catch(() => ({
default: {}
}));
}

return {
...defaultOption,
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/validateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ const importLocaleFile = async (url: string, options: OptionsWithDefault) => {

log(`Fetching translation keys from ${url}`, 'debug', options);

const promise = import(
`file://${url}`,
url.endsWith('.json')
? {
assert: {
type: 'json'
}
}
: undefined
) as Promise<{
default: Record<string, unknown>;
}>;
let promise: Promise<{ default: Record<string, unknown> }>;

if (url.endsWith('.json')) {
promise = import(`file://${url}`, {
assert: {
type: 'json'
}
});
} else {
promise = import(`file://${url}`);
}

importedFiles.set(url, promise);

Expand Down

0 comments on commit 58b2196

Please sign in to comment.