Skip to content

Commit d459252

Browse files
committed
refactor(config): make isESMModule reusable and add with { type: 'json' } to esm dictionaries
1 parent 4a97071 commit d459252

File tree

6 files changed

+15
-10
lines changed

6 files changed

+15
-10
lines changed

packages/@intlayer/chokidar/src/transpiler/dictionary_to_main/generateDictionaryListContent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const generateDictionaryListContent = (
2323
// Import all dictionaries
2424
dictionariesRef.forEach((dictionary) => {
2525
if (format === 'esm')
26-
content += `import ${dictionary.hash} from '${dictionary.relativePath}';\n`;
26+
content += `import ${dictionary.hash} from '${dictionary.relativePath}' with { type: 'json' };\n`;
2727
if (format === 'cjs')
2828
content += `const ${dictionary.hash} = require('${dictionary.relativePath}');\n`;
2929
});

packages/@intlayer/config/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ export type {
2929
} from './types/config';
3030
export { Locales } from './types/locales';
3131
export type { LocalesValues } from './types/locales';
32-
export { ESMxCJSRequire } from './utils/ESMxCJSRequire';
32+
export { ESMxCJSRequire, isESModule } from './utils/ESMxCJSRequire';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRequire } from 'module';
22

3-
const isESModule = typeof import.meta.url === 'string';
3+
export const isESModule = typeof import.meta.url === 'string';
44
export const ESMxCJSRequire = isESModule
55
? createRequire(import.meta.url)
66
: require;

packages/@intlayer/dictionaries-entry/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* The alias allow hot reload the app (such as nextjs) on any dictionary change.
55
*/
66

7-
import { ESMxCJSRequire, getConfiguration } from '@intlayer/config';
7+
import { ESMxCJSRequire, getConfiguration, isESModule } from '@intlayer/config';
88
import { existsSync } from 'fs';
99
import { join } from 'path';
1010
// @ts-ignore intlayer declared for module augmentation
@@ -13,7 +13,10 @@ import type { IntlayerDictionaryTypesConnector } from 'intlayer';
1313
export const getDictionaries = () => {
1414
const { content } = getConfiguration();
1515

16-
const dictionariesPath = join(content.mainDir, `dictionaries.cjs`);
16+
const dictionariesPath = join(
17+
content.mainDir,
18+
isESModule ? 'dictionaries.mjs' : 'dictionaries.cjs'
19+
);
1720

1821
let dictionaries = {};
1922
if (existsSync(dictionariesPath)) {

packages/@intlayer/unmerged-dictionaries-entry/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* The alias allow hot reload the app (such as nextjs) on any dictionary change.
55
*/
66

7-
import { ESMxCJSRequire, getConfiguration } from '@intlayer/config';
7+
import { ESMxCJSRequire, getConfiguration, isESModule } from '@intlayer/config';
88
import { existsSync } from 'fs';
99
import { join } from 'path';
1010
// @ts-ignore intlayer declared for module augmentation
@@ -13,7 +13,10 @@ import type { Dictionary, IntlayerDictionaryTypesConnector } from 'intlayer';
1313
export const getUnmergedDictionaries = () => {
1414
const { content } = getConfiguration();
1515

16-
const dictionariesPath = join(content.mainDir, `unmerged_dictionaries.cjs`);
16+
const dictionariesPath = join(
17+
content.mainDir,
18+
isESModule ? 'unmerged-dictionaries.mjs' : 'unmerged-dictionaries.cjs'
19+
);
1720

1821
let dictionaries: Record<
1922
IntlayerDictionaryTypesConnector['key'],

packages/@intlayer/webpack/src/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import { isESModule } from '@intlayer/config';
2+
import crypto from 'crypto-js';
13
import path from 'path';
24
import { fileURLToPath } from 'url';
3-
import crypto from 'crypto-js';
45

56
/**
67
* Set the __dirname global variable to make the config work in both ESM and CJS environments
78
*/
89
export const defineDirname = () => {
9-
const isESModule = typeof import.meta.url === 'string';
10-
1110
const filename = isESModule
1211
? fileURLToPath(import.meta.url)
1312
: require('url').pathToFileURL(__filename).toString();

0 commit comments

Comments
 (0)