gt-react-native 0.0.1-alpha.12 → 0.0.1-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -11
- package/lib/module/errors-dir/constants.js +1 -1
- package/lib/module/errors-dir/constants.js.map +1 -1
- package/lib/module/errors-dir/errors.js +5 -0
- package/lib/module/errors-dir/errors.js.map +1 -0
- package/lib/module/errors-dir/warnings.js +12 -0
- package/lib/module/errors-dir/warnings.js.map +1 -0
- package/lib/module/internal.js +4 -0
- package/lib/module/internal.js.map +1 -0
- package/lib/module/plugin-dir/index.js +51 -0
- package/lib/module/plugin-dir/index.js.map +1 -0
- package/lib/module/plugin-dir/types.js +2 -0
- package/lib/module/plugin-dir/types.js.map +1 -0
- package/lib/module/plugin-dir/utils/resolveLocales.js +69 -0
- package/lib/module/plugin-dir/utils/resolveLocales.js.map +1 -0
- package/lib/module/plugin.js +1 -36
- package/lib/module/plugin.js.map +1 -1
- package/lib/module/tools/testLocalePolyfill.js.map +1 -0
- package/lib/typescript/src/errors-dir/constants.d.ts +1 -1
- package/lib/typescript/src/errors-dir/constants.d.ts.map +1 -1
- package/lib/typescript/src/errors-dir/errors.d.ts +2 -0
- package/lib/typescript/src/errors-dir/errors.d.ts.map +1 -0
- package/lib/typescript/src/errors-dir/warnings.d.ts +4 -0
- package/lib/typescript/src/errors-dir/warnings.d.ts.map +1 -0
- package/lib/typescript/src/internal.d.ts +2 -0
- package/lib/typescript/src/internal.d.ts.map +1 -0
- package/lib/typescript/src/plugin-dir/index.d.ts +6 -0
- package/lib/typescript/src/plugin-dir/index.d.ts.map +1 -0
- package/lib/typescript/src/plugin-dir/types.d.ts +10 -0
- package/lib/typescript/src/plugin-dir/types.d.ts.map +1 -0
- package/lib/typescript/src/plugin-dir/utils/resolveLocales.d.ts +13 -0
- package/lib/typescript/src/plugin-dir/utils/resolveLocales.d.ts.map +1 -0
- package/lib/typescript/src/plugin.d.ts +1 -9
- package/lib/typescript/src/plugin.d.ts.map +1 -1
- package/lib/typescript/src/tools/testLocalePolyfill.d.ts.map +1 -0
- package/package.json +7 -2
- package/src/errors-dir/constants.ts +1 -1
- package/src/errors-dir/errors.ts +3 -0
- package/src/errors-dir/warnings.ts +14 -0
- package/src/internal.ts +1 -0
- package/src/plugin-dir/index.ts +83 -0
- package/src/plugin-dir/types.ts +10 -0
- package/src/plugin-dir/utils/resolveLocales.ts +95 -0
- package/src/plugin.ts +1 -76
- package/lib/module/testLocalePolyfill.js.map +0 -1
- package/lib/typescript/src/testLocalePolyfill.d.ts.map +0 -1
- /package/lib/module/{testLocalePolyfill.js → tools/testLocalePolyfill.js} +0 -0
- /package/lib/typescript/src/{testLocalePolyfill.d.ts → tools/testLocalePolyfill.d.ts} +0 -0
- /package/src/{testLocalePolyfill.ts → tools/testLocalePolyfill.ts} +0 -0
package/README.md
CHANGED
|
@@ -8,21 +8,44 @@ An i18n package for React Native
|
|
|
8
8
|
npm install gt-react-native
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
## Local development
|
|
12
|
+
|
|
13
|
+
In order to link local librarys add this to your package.json:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"example-lib": "*"
|
|
19
|
+
},
|
|
20
|
+
"resolutions": {
|
|
21
|
+
"@generaltranslation/react-core": "portal:/absolute/path/to/test-lib"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
15
25
|
|
|
16
|
-
|
|
26
|
+
And update the `metro.config.js` in the `/exmaple` directory for local linking:
|
|
17
27
|
|
|
18
|
-
|
|
28
|
+
```js
|
|
29
|
+
const testLibAbsolutePath = '/absolute/path/to/test-lib';
|
|
30
|
+
|
|
31
|
+
const mergedConfig = {
|
|
32
|
+
...defaultConfig,
|
|
33
|
+
resolver: {
|
|
34
|
+
...defaultConfig.resolver,
|
|
35
|
+
extraNodeModules: {
|
|
36
|
+
...defaultConfig.resolver.extraNodeModules,
|
|
37
|
+
'test-lib': testLibAbsolutePath,
|
|
38
|
+
},
|
|
39
|
+
nodeModulesPaths: [
|
|
40
|
+
...defaultConfig.resolver.nodeModulesPaths,
|
|
41
|
+
`${testLibAbsolutePath}/node_modules`,
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
watchFolders: [...defaultConfig.watchFolders, testLibAbsolutePath],
|
|
45
|
+
};
|
|
19
46
|
```
|
|
20
47
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
24
|
-
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
25
|
-
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
48
|
+
If you are having issues resolving a dependency in test-lib, then provide the direct path in `extraNodeModules` mapping to `'/absolute/path/to/test-lib/node_modules/example-lib'`.
|
|
26
49
|
|
|
27
50
|
---
|
|
28
51
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["PACKAGE_NAME"],"sourceRoot":"../../../src","sources":["errors-dir/constants.ts"],"mappings":";;AAAA,OAAO,MAAMA,YAAY,GAAG,
|
|
1
|
+
{"version":3,"names":["PACKAGE_NAME"],"sourceRoot":"../../../src","sources":["errors-dir/constants.ts"],"mappings":";;AAAA,OAAO,MAAMA,YAAY,GAAG,iBAAiB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["PACKAGE_NAME","failedToReadConfigFileError","filePath"],"sourceRoot":"../../../src","sources":["errors-dir/errors.ts"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,gBAAa;AAC1C,OAAO,MAAMC,2BAA2B,GAAIC,QAAgB,IAC1D,GAAGF,YAAY,uCAAuCE,QAAQ,GAAG","ignoreList":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { libraryDefaultLocale } from 'generaltranslation/internal';
|
|
4
|
+
import { PACKAGE_NAME } from "./constants.js";
|
|
5
|
+
export const resolveLocalesFailedWarning = `${PACKAGE_NAME}: Failed to resolve locales for polyfill. Falling back to ${libraryDefaultLocale}.
|
|
6
|
+
Specify a list of locales for the gt-react-native babel plugin by:
|
|
7
|
+
(1) Providing a list of locales
|
|
8
|
+
(2) Providing your GT Config to the plugin
|
|
9
|
+
(3) Providing the path to your GT Config file.`;
|
|
10
|
+
export const couldNotLocateConfigWarning = filePath => `${PACKAGE_NAME}: Could not locate GT Config at ${filePath}.`;
|
|
11
|
+
export const invalidLocalesWarning = invalidLocales => `${PACKAGE_NAME}: Invalid locales found in GT Config: ${invalidLocales.join(', ')}.`;
|
|
12
|
+
//# sourceMappingURL=warnings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["libraryDefaultLocale","PACKAGE_NAME","resolveLocalesFailedWarning","couldNotLocateConfigWarning","filePath","invalidLocalesWarning","invalidLocales","join"],"sourceRoot":"../../../src","sources":["errors-dir/warnings.ts"],"mappings":";;AAAA,SAASA,oBAAoB,QAAQ,6BAA6B;AAClE,SAASC,YAAY,QAAQ,gBAAa;AAE1C,OAAO,MAAMC,2BAA2B,GAAG,GAAGD,YAAY,6DAA6DD,oBAAoB;AAC3I;AACA;AACA;AACA,+CAA+C;AAE/C,OAAO,MAAMG,2BAA2B,GAAIC,QAAgB,IAC1D,GAAGH,YAAY,mCAAmCG,QAAQ,GAAG;AAE/D,OAAO,MAAMC,qBAAqB,GAAIC,cAAwB,IAC5D,GAAGL,YAAY,yCAAyCK,cAAc,CAACC,IAAI,CAAC,IAAI,CAAC,GAAG","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["testLocalePolyfill"],"sourceRoot":"../../src","sources":["internal.ts"],"mappings":";;AAAA,SAASA,kBAAkB,QAAQ,+BAA4B","ignoreList":[]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { resolveLocales } from "./utils/resolveLocales.js";
|
|
5
|
+
export default function (babel, {
|
|
6
|
+
locales,
|
|
7
|
+
config,
|
|
8
|
+
configFilePath,
|
|
9
|
+
entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx')
|
|
10
|
+
}) {
|
|
11
|
+
const {
|
|
12
|
+
types: t
|
|
13
|
+
} = babel;
|
|
14
|
+
return {
|
|
15
|
+
name: 'gt-react-native/plugin',
|
|
16
|
+
visitor: {
|
|
17
|
+
Program(programPath, state) {
|
|
18
|
+
const currentFilePath = path.resolve(state.filename || state.file.opts.filename || '');
|
|
19
|
+
|
|
20
|
+
// Only apply polyfills to files that import gt-react-native or generaltranslation
|
|
21
|
+
if (currentFilePath !== entryPointFilePath) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
console.log('[GT-REACT-NATIVE] Applying polyfills to', currentFilePath);
|
|
25
|
+
const resolvedLocales = resolveLocales({
|
|
26
|
+
locales,
|
|
27
|
+
config,
|
|
28
|
+
configFilePath
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// const resolvedLocales = ['fr'];
|
|
32
|
+
// TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()
|
|
33
|
+
const imports = ['@formatjs/intl-getcanonicallocales/polyfill', '@formatjs/intl-locale/polyfill', '@formatjs/intl-displaynames/polyfill', '@formatjs/intl-listformat/polyfill', '@formatjs/intl-pluralrules/polyfill-force',
|
|
34
|
+
// https://github.com/formatjs/formatjs/issues/4463
|
|
35
|
+
'@formatjs/intl-numberformat/polyfill', '@formatjs/intl-relativetimeformat/polyfill', '@formatjs/intl-datetimeformat/polyfill', '@formatjs/intl-datetimeformat/add-all-tz', ...resolvedLocales.flatMap(locale => [`@formatjs/intl-displaynames/locale-data/${locale}`, `@formatjs/intl-listformat/locale-data/${locale}`, `@formatjs/intl-pluralrules/locale-data/${locale}`, `@formatjs/intl-numberformat/locale-data/${locale}`, `@formatjs/intl-relativetimeformat/locale-data/${locale}`, `@formatjs/intl-datetimeformat/locale-data/${locale}`])];
|
|
36
|
+
const existingImports = new Set();
|
|
37
|
+
programPath.node.body.forEach(node => {
|
|
38
|
+
if (t.isImportDeclaration(node) && typeof node.source.value === 'string' && imports.includes(node.source.value)) {
|
|
39
|
+
existingImports.add(node.source.value);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
const importsToAdd = imports.filter(imp => !existingImports.has(imp));
|
|
43
|
+
if (importsToAdd.length > 0) {
|
|
44
|
+
const newImports = importsToAdd.map(importPath => t.importDeclaration([], t.stringLiteral(importPath)));
|
|
45
|
+
programPath.node.body.unshift(...newImports);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["path","resolveLocales","babel","locales","config","configFilePath","entryPointFilePath","resolve","process","cwd","types","t","name","visitor","Program","programPath","state","currentFilePath","filename","file","opts","console","log","resolvedLocales","imports","flatMap","locale","existingImports","Set","node","body","forEach","isImportDeclaration","source","value","includes","add","importsToAdd","filter","imp","has","length","newImports","map","importPath","importDeclaration","stringLiteral","unshift"],"sourceRoot":"../../../src","sources":["plugin-dir/index.ts"],"mappings":";;AAAA,OAAO,KAAKA,IAAI,MAAM,MAAM;AAG5B,SAASC,cAAc,QAAQ,2BAAwB;AAEvD,eAAe,UACbC,KAA8B,EAC9B;EACEC,OAAO;EACPC,MAAM;EACNC,cAAc;EACdC,kBAAkB,GAAGN,IAAI,CAACO,OAAO,CAACC,OAAO,CAACC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS;AACpD,CAAC,EACL;EACX,MAAM;IAAEC,KAAK,EAAEC;EAAE,CAAC,GAAGT,KAAK;EAE1B,OAAO;IACLU,IAAI,EAA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AMG,YAAY,GAAGb,OAAO,CAACc,MAAM,CAAEC,GAAG,IAAK,CAACZ,eAAe,CAACa,GAAG,CAACD,GAAG,CAAC,CAAC;QAEvE,IAAIF,YAAY,CAACI,MAAM,GAAG,CAAC,EAAE;UAC3B,MAAMC,UAAU,GAAGL,YAAY,CAACM,GAAG,CAAEC,UAAU,IAC7CjC,CAAC,CAACkC,iBAAiB,CAAC,EAAE,EAAElC,CAAC,CAACmC,aAAa,CAACF,UAAU,CAAC,CACrD,CAAC;UAED7B,WAAW,CAACc,IAAI,CAACC,IAAI,CAACiB,OAAO,CAAC,GAAGL,UAAU,CAAC;QAC9C;MACF;IACF;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../../src","sources":["plugin-dir/types.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { couldNotLocateConfigWarning, invalidLocalesWarning, resolveLocalesFailedWarning } from "../../errors-dir/warnings.js";
|
|
5
|
+
import { libraryDefaultLocale } from 'generaltranslation/internal';
|
|
6
|
+
import { failedToReadConfigFileError } from "../../errors-dir/errors.js";
|
|
7
|
+
import { getSupportedLocale } from '@generaltranslation/supported-locales';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
const DEFAULT_CONFIG_FILE_PATHS = [path.join(process.cwd(), 'gt.config.json'), path.join(process.cwd(), '.locadex', 'gt.config.json')];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Given a list of locales or a config object or a path to a config file, return a list of locales to polyfill.
|
|
13
|
+
* Preference order: locales > config > configFilePaths > library default locale
|
|
14
|
+
*/
|
|
15
|
+
export function resolveLocales({
|
|
16
|
+
locales,
|
|
17
|
+
config,
|
|
18
|
+
configFilePath
|
|
19
|
+
}) {
|
|
20
|
+
let result = [];
|
|
21
|
+
|
|
22
|
+
// Resolve locales from the given options
|
|
23
|
+
if (locales) {
|
|
24
|
+
result = Array.from(new Set(locales));
|
|
25
|
+
} else if (config) {
|
|
26
|
+
result = Array.from(new Set([config.defaultLocale, ...config.locales]));
|
|
27
|
+
} else {
|
|
28
|
+
result = resolveLocalesFromConfig(configFilePath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Validate the result
|
|
32
|
+
const invalidLocales = result.filter(locale => !getSupportedLocale(locale));
|
|
33
|
+
if (invalidLocales.length) {
|
|
34
|
+
console.warn(invalidLocalesWarning(invalidLocales));
|
|
35
|
+
result = result.filter(locale => !invalidLocales.includes(locale));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Fallback to default locale if no locales were found
|
|
39
|
+
if (result.length === 0) {
|
|
40
|
+
console.warn(resolveLocalesFailedWarning);
|
|
41
|
+
result = [libraryDefaultLocale];
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Resolve locales from a config file
|
|
48
|
+
* @param configFilePath - The path to the config file
|
|
49
|
+
* @returns A list of locales
|
|
50
|
+
*/
|
|
51
|
+
function resolveLocalesFromConfig(configFilePath) {
|
|
52
|
+
const configFilePaths = [...(configFilePath ? [configFilePath] : []), ...DEFAULT_CONFIG_FILE_PATHS];
|
|
53
|
+
let result = [];
|
|
54
|
+
for (const filePath of configFilePaths) {
|
|
55
|
+
try {
|
|
56
|
+
if (!fs.existsSync(filePath)) {
|
|
57
|
+
console.warn(couldNotLocateConfigWarning(filePath));
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const resolvedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
61
|
+
result = Array.from(new Set([...(resolvedConfig.defaultLocale ? [resolvedConfig.defaultLocale] : []), ...(resolvedConfig.locales || [])]));
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error(failedToReadConfigFileError(filePath), error);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=resolveLocales.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["fs","couldNotLocateConfigWarning","invalidLocalesWarning","resolveLocalesFailedWarning","libraryDefaultLocale","failedToReadConfigFileError","getSupportedLocale","path","DEFAULT_CONFIG_FILE_PATHS","join","process","cwd","resolveLocales","locales","config","configFilePath","result","Array","from","Set","defaultLocale","resolveLocalesFromConfig","invalidLocales","filter","locale","length","console","warn","includes","configFilePaths","filePath","existsSync","resolvedConfig","JSON","parse","readFileSync","error"],"sourceRoot":"../../../../src","sources":["plugin-dir/utils/resolveLocales.ts"],"mappings":";;AAAA,OAAOA,EAAE,MAAM,IAAI;AACnB,SACEC,2BAA2B,EAC3BC,qBAAqB,EACrBC,2BAA2B,QACtB,8BAA2B;AAClC,SAASC,oBAAoB,QAAQ,6BAA6B;AAClE,SAASC,2BAA2B,QAAQ,4BAAyB;AACrE,SAASC,kBAAkB,QAAQ,uCAAuC;AAC1E,OAAOC,IAAI,MAAM,MAAM;AAEvB,MAAMC,yBAAyB,GAAG,CAChCD,IAAI,CAACE,IAAI,CAACC,OAAO,CAACC,GAAG,CAAC,CAAC,EAAE,gBAAgB,CAAC,EAC1CJ,IAAI,CAACE,IAAI,CAACC,OAAO,CAACC,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,gBAAgB,CAAC,CACvD;;AAED;AACA;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAAC;EAC7BC,OAAO;EACPC,MAAM;EACNC;AAKF,CAAC,EAAY;EACX,IAAIC,MAAgB,GAAG,EAAE;;EAEzB;EACA,IAAIH,OAAO,EAAE;IACXG,MAAM,GAAGC,KAAK,CAACC,IAAI,CAAC,IAAIC,GAAG,CAACN,OAAO,CAAC,CAAC;EACvC,CAAC,MAAM,IAAIC,MAAM,EAAE;IACjBE,MAAM,GAAGC,KAAK,CAACC,I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AACC,IAAI,CACjB,IAAIC,GAAG,CAAC,CACN,IAAIa,cAAc,CAACZ,aAAa,GAC5B,CAACY,cAAc,CAACZ,aAAa,CAAC,GAC9B,EAAE,CAAC,EACP,IAAIY,cAAc,CAACnB,OAAO,IAAI,EAAE,CAAC,CAClC,CACH,CAAC;IACH,CAAC,CAAC,OAAOuB,KAAK,EAAE;MACdV,OAAO,CAACU,KAAK,CAAC/B,2BAA2B,CAACyB,QAAQ,CAAC,EAAEM,KAAK,CAAC;MAC3D;IACF;EACF;EACA,OAAOpB,MAAM;AACf","ignoreList":[]}
|
package/lib/module/plugin.js
CHANGED
|
@@ -1,39 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
export default function (babel, {
|
|
5
|
-
targetFile,
|
|
6
|
-
locales
|
|
7
|
-
}) {
|
|
8
|
-
const {
|
|
9
|
-
types: t
|
|
10
|
-
} = babel;
|
|
11
|
-
const targetFilePath = targetFile;
|
|
12
|
-
console.log('plugin', targetFilePath);
|
|
13
|
-
return {
|
|
14
|
-
name: 'gt-react-native/plugin',
|
|
15
|
-
visitor: {
|
|
16
|
-
Program(programPath, state) {
|
|
17
|
-
const currentFilePath = path.resolve(state.filename || state.file.opts.filename || '');
|
|
18
|
-
if (!targetFilePath || currentFilePath !== path.resolve(targetFilePath)) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const imports = ['@formatjs/intl-getcanonicallocales/polyfill', '@formatjs/intl-locale/polyfill', '@formatjs/intl-displaynames/polyfill', '@formatjs/intl-listformat/polyfill', '@formatjs/intl-pluralrules/polyfill-force',
|
|
22
|
-
// https://github.com/formatjs/formatjs/issues/4463
|
|
23
|
-
'@formatjs/intl-numberformat/polyfill', '@formatjs/intl-relativetimeformat/polyfill', '@formatjs/intl-datetimeformat/polyfill', '@formatjs/intl-datetimeformat/add-all-tz', ...locales.flatMap(locale => [`@formatjs/intl-displaynames/locale-data/${locale}`, `@formatjs/intl-listformat/locale-data/${locale}`, `@formatjs/intl-pluralrules/locale-data/${locale}`, `@formatjs/intl-numberformat/locale-data/${locale}`, `@formatjs/intl-relativetimeformat/locale-data/${locale}`, `@formatjs/intl-datetimeformat/locale-data/${locale}`])];
|
|
24
|
-
const existingImports = new Set();
|
|
25
|
-
programPath.node.body.forEach(node => {
|
|
26
|
-
if (t.isImportDeclaration(node) && typeof node.source.value === 'string' && imports.includes(node.source.value)) {
|
|
27
|
-
existingImports.add(node.source.value);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
const importsToAdd = imports.filter(imp => !existingImports.has(imp));
|
|
31
|
-
if (importsToAdd.length > 0) {
|
|
32
|
-
const newImports = importsToAdd.map(importPath => t.importDeclaration([], t.stringLiteral(importPath)));
|
|
33
|
-
programPath.node.body.unshift(...newImports);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
}
|
|
3
|
+
export { default } from "./plugin-dir/index.js";
|
|
39
4
|
//# sourceMappingURL=plugin.js.map
|
package/lib/module/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["default"],"sourceRoot":"../../src","sources":["plugin.ts"],"mappings":";;AAAA,SAASA,OAAO,QAAQ,uBAAc","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["testLocalePolyfill","locale","problems","global","Intl","push","apis","forEach","a","dtfSupported","DateTimeFormat","supportedLocalesOf","nfSupported","NumberFormat","group","decimal","parts","formatToParts","find","p","type","value","e","Error","message","String","pr","PluralRules","sample","select","example","number","format","date","dateStyle","Date","weekday","result","ok","length","console","log","warn"],"sourceRoot":"../../../src","sources":["tools/testLocalePolyfill.ts"],"mappings":";;AAAA,OAAO,SAASA,kBAAkBA,CAACC,MAAM,GAAG,OAAO,EAAE;EACnD,MAAMC,QAAQ,GAAG,EAAE;;EAEnB;EACA,IAAI,CAACC,M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AAE,EAAE6B,OAAO,CAAC;EAClE,CAAC,MAAM;IACLU,OAAO,CAACE,IAAI,CACV,sCAAsCzC,MAAM,GAAG,EAC/CC,QAAQ,EACR4B,OACF,CAAC;EACH;EACA,OAAOO,MAAM;AACf","ignoreList":[]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const PACKAGE_NAME = "gt-react";
|
|
1
|
+
export declare const PACKAGE_NAME = "gt-react-native";
|
|
2
2
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/errors-dir/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/errors-dir/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../../src/errors-dir/errors.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,2BAA2B,GAAI,UAAU,MAAM,WACO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"warnings.d.ts","sourceRoot":"","sources":["../../../../src/errors-dir/warnings.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,2BAA2B,QAIO,CAAC;AAEhD,eAAO,MAAM,2BAA2B,GAAI,UAAU,MAAM,WACG,CAAC;AAEhE,eAAO,MAAM,qBAAqB,GAAI,gBAAgB,MAAM,EAAE,WACwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../../../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { PluginObj, types } from '@babel/core';
|
|
2
|
+
import type { PluginOptions } from './types';
|
|
3
|
+
export default function (babel: {
|
|
4
|
+
types: typeof types;
|
|
5
|
+
}, { locales, config, configFilePath, entryPointFilePath, }: PluginOptions): PluginObj;
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugin-dir/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG7C,MAAM,CAAC,OAAO,WACZ,KAAK,EAAE;IAAE,KAAK,EAAE,OAAO,KAAK,CAAA;CAAE,EAC9B,EACE,OAAO,EACP,MAAM,EACN,cAAc,EACd,kBAAkE,GACnE,EAAE,aAAa,GACf,SAAS,CAqEX"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/plugin-dir/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAE5B,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB,MAAM,CAAC,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE5E,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a list of locales or a config object or a path to a config file, return a list of locales to polyfill.
|
|
3
|
+
* Preference order: locales > config > configFilePaths > library default locale
|
|
4
|
+
*/
|
|
5
|
+
export declare function resolveLocales({ locales, config, configFilePath, }: {
|
|
6
|
+
locales?: string[];
|
|
7
|
+
config?: {
|
|
8
|
+
defaultLocale: string;
|
|
9
|
+
locales: string[];
|
|
10
|
+
} & Record<string, any>;
|
|
11
|
+
configFilePath?: string;
|
|
12
|
+
}): string[];
|
|
13
|
+
//# sourceMappingURL=resolveLocales.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveLocales.d.ts","sourceRoot":"","sources":["../../../../../src/plugin-dir/utils/resolveLocales.ts"],"names":[],"mappings":"AAgBA;;;GAGG;AACH,wBAAgB,cAAc,CAAC,EAC7B,OAAO,EACP,MAAM,EACN,cAAc,GACf,EAAE;IACD,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5E,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,GAAG,MAAM,EAAE,CA0BX"}
|
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
interface PluginOptions {
|
|
3
|
-
targetFile: string;
|
|
4
|
-
locales: string[];
|
|
5
|
-
}
|
|
6
|
-
export default function (babel: {
|
|
7
|
-
types: typeof types;
|
|
8
|
-
}, { targetFile, locales }: PluginOptions): PluginObj;
|
|
9
|
-
export {};
|
|
1
|
+
export { default } from './plugin-dir';
|
|
10
2
|
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testLocalePolyfill.d.ts","sourceRoot":"","sources":["../../../../src/tools/testLocalePolyfill.ts"],"names":[],"mappings":"AAAA,wBAAgB,kBAAkB,CAAC,MAAM,SAAU;;;;;;;;;;EAiFlD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gt-react-native",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.13",
|
|
4
4
|
"description": "An i18n package for React Native",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -15,7 +15,11 @@
|
|
|
15
15
|
"types": "./lib/typescript/src/plugin.d.ts",
|
|
16
16
|
"default": "./lib/module/plugin.js"
|
|
17
17
|
},
|
|
18
|
-
"./
|
|
18
|
+
"./internal": {
|
|
19
|
+
"source": "./src/internal.ts",
|
|
20
|
+
"types": "./lib/typescript/src/internal.d.ts",
|
|
21
|
+
"default": "./lib/module/internal.js"
|
|
22
|
+
}
|
|
19
23
|
},
|
|
20
24
|
"files": [
|
|
21
25
|
"src",
|
|
@@ -23,6 +27,7 @@
|
|
|
23
27
|
"android",
|
|
24
28
|
"ios",
|
|
25
29
|
"cpp",
|
|
30
|
+
"package.json",
|
|
26
31
|
"*.podspec",
|
|
27
32
|
"!ios/build",
|
|
28
33
|
"!android/build",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const PACKAGE_NAME = 'gt-react';
|
|
1
|
+
export const PACKAGE_NAME = 'gt-react-native';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { libraryDefaultLocale } from 'generaltranslation/internal';
|
|
2
|
+
import { PACKAGE_NAME } from './constants';
|
|
3
|
+
|
|
4
|
+
export const resolveLocalesFailedWarning = `${PACKAGE_NAME}: Failed to resolve locales for polyfill. Falling back to ${libraryDefaultLocale}.
|
|
5
|
+
Specify a list of locales for the gt-react-native babel plugin by:
|
|
6
|
+
(1) Providing a list of locales
|
|
7
|
+
(2) Providing your GT Config to the plugin
|
|
8
|
+
(3) Providing the path to your GT Config file.`;
|
|
9
|
+
|
|
10
|
+
export const couldNotLocateConfigWarning = (filePath: string) =>
|
|
11
|
+
`${PACKAGE_NAME}: Could not locate GT Config at ${filePath}.`;
|
|
12
|
+
|
|
13
|
+
export const invalidLocalesWarning = (invalidLocales: string[]) =>
|
|
14
|
+
`${PACKAGE_NAME}: Invalid locales found in GT Config: ${invalidLocales.join(', ')}.`;
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { testLocalePolyfill } from './tools/testLocalePolyfill';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import type { PluginObj, types } from '@babel/core';
|
|
3
|
+
import type { PluginOptions } from './types';
|
|
4
|
+
import { resolveLocales } from './utils/resolveLocales';
|
|
5
|
+
|
|
6
|
+
export default function (
|
|
7
|
+
babel: { types: typeof types },
|
|
8
|
+
{
|
|
9
|
+
locales,
|
|
10
|
+
config,
|
|
11
|
+
configFilePath,
|
|
12
|
+
entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx'),
|
|
13
|
+
}: PluginOptions
|
|
14
|
+
): PluginObj {
|
|
15
|
+
const { types: t } = babel;
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
name: 'gt-react-native/plugin',
|
|
19
|
+
visitor: {
|
|
20
|
+
Program(programPath, state) {
|
|
21
|
+
const currentFilePath = path.resolve(
|
|
22
|
+
state.filename || state.file.opts.filename || ''
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
// Only apply polyfills to files that import gt-react-native or generaltranslation
|
|
26
|
+
if (currentFilePath !== entryPointFilePath) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
console.log('[GT-REACT-NATIVE] Applying polyfills to', currentFilePath);
|
|
31
|
+
|
|
32
|
+
const resolvedLocales = resolveLocales({
|
|
33
|
+
locales,
|
|
34
|
+
config,
|
|
35
|
+
configFilePath,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// const resolvedLocales = ['fr'];
|
|
39
|
+
// TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()
|
|
40
|
+
const imports = [
|
|
41
|
+
'@formatjs/intl-getcanonicallocales/polyfill',
|
|
42
|
+
'@formatjs/intl-locale/polyfill',
|
|
43
|
+
'@formatjs/intl-displaynames/polyfill',
|
|
44
|
+
'@formatjs/intl-listformat/polyfill',
|
|
45
|
+
'@formatjs/intl-pluralrules/polyfill-force', // https://github.com/formatjs/formatjs/issues/4463
|
|
46
|
+
'@formatjs/intl-numberformat/polyfill',
|
|
47
|
+
'@formatjs/intl-relativetimeformat/polyfill',
|
|
48
|
+
'@formatjs/intl-datetimeformat/polyfill',
|
|
49
|
+
'@formatjs/intl-datetimeformat/add-all-tz',
|
|
50
|
+
...resolvedLocales.flatMap((locale) => [
|
|
51
|
+
`@formatjs/intl-displaynames/locale-data/${locale}`,
|
|
52
|
+
`@formatjs/intl-listformat/locale-data/${locale}`,
|
|
53
|
+
`@formatjs/intl-pluralrules/locale-data/${locale}`,
|
|
54
|
+
`@formatjs/intl-numberformat/locale-data/${locale}`,
|
|
55
|
+
`@formatjs/intl-relativetimeformat/locale-data/${locale}`,
|
|
56
|
+
`@formatjs/intl-datetimeformat/locale-data/${locale}`,
|
|
57
|
+
]),
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
const existingImports = new Set<string>();
|
|
61
|
+
programPath.node.body.forEach((node) => {
|
|
62
|
+
if (
|
|
63
|
+
t.isImportDeclaration(node) &&
|
|
64
|
+
typeof node.source.value === 'string' &&
|
|
65
|
+
imports.includes(node.source.value)
|
|
66
|
+
) {
|
|
67
|
+
existingImports.add(node.source.value);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const importsToAdd = imports.filter((imp) => !existingImports.has(imp));
|
|
72
|
+
|
|
73
|
+
if (importsToAdd.length > 0) {
|
|
74
|
+
const newImports = importsToAdd.map((importPath) =>
|
|
75
|
+
t.importDeclaration([], t.stringLiteral(importPath))
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
programPath.node.body.unshift(...newImports);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface PluginOptions {
|
|
2
|
+
/* List of locales to polyfill */
|
|
3
|
+
locales?: string[];
|
|
4
|
+
/* Gt config object */
|
|
5
|
+
config?: { defaultLocale: string; locales: string[] } & Record<string, any>;
|
|
6
|
+
/* Path to the gt config file */
|
|
7
|
+
configFilePath?: string;
|
|
8
|
+
/* Resolved from package.json */
|
|
9
|
+
entryPointFilePath?: string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import {
|
|
3
|
+
couldNotLocateConfigWarning,
|
|
4
|
+
invalidLocalesWarning,
|
|
5
|
+
resolveLocalesFailedWarning,
|
|
6
|
+
} from '../../errors-dir/warnings';
|
|
7
|
+
import { libraryDefaultLocale } from 'generaltranslation/internal';
|
|
8
|
+
import { failedToReadConfigFileError } from '../../errors-dir/errors';
|
|
9
|
+
import { getSupportedLocale } from '@generaltranslation/supported-locales';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_CONFIG_FILE_PATHS = [
|
|
13
|
+
path.join(process.cwd(), 'gt.config.json'),
|
|
14
|
+
path.join(process.cwd(), '.locadex', 'gt.config.json'),
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Given a list of locales or a config object or a path to a config file, return a list of locales to polyfill.
|
|
19
|
+
* Preference order: locales > config > configFilePaths > library default locale
|
|
20
|
+
*/
|
|
21
|
+
export function resolveLocales({
|
|
22
|
+
locales,
|
|
23
|
+
config,
|
|
24
|
+
configFilePath,
|
|
25
|
+
}: {
|
|
26
|
+
locales?: string[];
|
|
27
|
+
config?: { defaultLocale: string; locales: string[] } & Record<string, any>;
|
|
28
|
+
configFilePath?: string;
|
|
29
|
+
}): string[] {
|
|
30
|
+
let result: string[] = [];
|
|
31
|
+
|
|
32
|
+
// Resolve locales from the given options
|
|
33
|
+
if (locales) {
|
|
34
|
+
result = Array.from(new Set(locales));
|
|
35
|
+
} else if (config) {
|
|
36
|
+
result = Array.from(new Set([config.defaultLocale, ...config.locales]));
|
|
37
|
+
} else {
|
|
38
|
+
result = resolveLocalesFromConfig(configFilePath);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Validate the result
|
|
42
|
+
const invalidLocales = result.filter((locale) => !getSupportedLocale(locale));
|
|
43
|
+
if (invalidLocales.length) {
|
|
44
|
+
console.warn(invalidLocalesWarning(invalidLocales));
|
|
45
|
+
result = result.filter((locale) => !invalidLocales.includes(locale));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Fallback to default locale if no locales were found
|
|
49
|
+
if (result.length === 0) {
|
|
50
|
+
console.warn(resolveLocalesFailedWarning);
|
|
51
|
+
result = [libraryDefaultLocale];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolve locales from a config file
|
|
59
|
+
* @param configFilePath - The path to the config file
|
|
60
|
+
* @returns A list of locales
|
|
61
|
+
*/
|
|
62
|
+
function resolveLocalesFromConfig(configFilePath: string | undefined) {
|
|
63
|
+
const configFilePaths = [
|
|
64
|
+
...(configFilePath ? [configFilePath] : []),
|
|
65
|
+
...DEFAULT_CONFIG_FILE_PATHS,
|
|
66
|
+
];
|
|
67
|
+
let result: string[] = [];
|
|
68
|
+
|
|
69
|
+
for (const filePath of configFilePaths) {
|
|
70
|
+
try {
|
|
71
|
+
if (!fs.existsSync(filePath)) {
|
|
72
|
+
console.warn(couldNotLocateConfigWarning(filePath));
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const resolvedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) as {
|
|
77
|
+
defaultLocale?: string;
|
|
78
|
+
locales?: string[];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
result = Array.from(
|
|
82
|
+
new Set([
|
|
83
|
+
...(resolvedConfig.defaultLocale
|
|
84
|
+
? [resolvedConfig.defaultLocale]
|
|
85
|
+
: []),
|
|
86
|
+
...(resolvedConfig.locales || []),
|
|
87
|
+
])
|
|
88
|
+
);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error(failedToReadConfigFileError(filePath), error);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -1,76 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { PluginObj, types } from '@babel/core';
|
|
3
|
-
|
|
4
|
-
interface PluginOptions {
|
|
5
|
-
targetFile: string;
|
|
6
|
-
locales: string[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export default function (
|
|
10
|
-
babel: { types: typeof types },
|
|
11
|
-
{ targetFile, locales }: PluginOptions
|
|
12
|
-
): PluginObj {
|
|
13
|
-
const { types: t } = babel;
|
|
14
|
-
const targetFilePath = targetFile;
|
|
15
|
-
|
|
16
|
-
console.log('plugin', targetFilePath);
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
name: 'gt-react-native/plugin',
|
|
20
|
-
visitor: {
|
|
21
|
-
Program(programPath, state) {
|
|
22
|
-
const currentFilePath = path.resolve(
|
|
23
|
-
state.filename || state.file.opts.filename || ''
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
!targetFilePath ||
|
|
28
|
-
currentFilePath !== path.resolve(targetFilePath)
|
|
29
|
-
) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const imports = [
|
|
34
|
-
'@formatjs/intl-getcanonicallocales/polyfill',
|
|
35
|
-
'@formatjs/intl-locale/polyfill',
|
|
36
|
-
'@formatjs/intl-displaynames/polyfill',
|
|
37
|
-
'@formatjs/intl-listformat/polyfill',
|
|
38
|
-
'@formatjs/intl-pluralrules/polyfill-force', // https://github.com/formatjs/formatjs/issues/4463
|
|
39
|
-
'@formatjs/intl-numberformat/polyfill',
|
|
40
|
-
'@formatjs/intl-relativetimeformat/polyfill',
|
|
41
|
-
'@formatjs/intl-datetimeformat/polyfill',
|
|
42
|
-
'@formatjs/intl-datetimeformat/add-all-tz',
|
|
43
|
-
...locales.flatMap((locale) => [
|
|
44
|
-
`@formatjs/intl-displaynames/locale-data/${locale}`,
|
|
45
|
-
`@formatjs/intl-listformat/locale-data/${locale}`,
|
|
46
|
-
`@formatjs/intl-pluralrules/locale-data/${locale}`,
|
|
47
|
-
`@formatjs/intl-numberformat/locale-data/${locale}`,
|
|
48
|
-
`@formatjs/intl-relativetimeformat/locale-data/${locale}`,
|
|
49
|
-
`@formatjs/intl-datetimeformat/locale-data/${locale}`,
|
|
50
|
-
]),
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const existingImports = new Set<string>();
|
|
54
|
-
programPath.node.body.forEach((node) => {
|
|
55
|
-
if (
|
|
56
|
-
t.isImportDeclaration(node) &&
|
|
57
|
-
typeof node.source.value === 'string' &&
|
|
58
|
-
imports.includes(node.source.value)
|
|
59
|
-
) {
|
|
60
|
-
existingImports.add(node.source.value);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const importsToAdd = imports.filter((imp) => !existingImports.has(imp));
|
|
65
|
-
|
|
66
|
-
if (importsToAdd.length > 0) {
|
|
67
|
-
const newImports = importsToAdd.map((importPath) =>
|
|
68
|
-
t.importDeclaration([], t.stringLiteral(importPath))
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
programPath.node.body.unshift(...newImports);
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
}
|
|
1
|
+
export { default } from './plugin-dir';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["testLocalePolyfill","locale","problems","global","Intl","push","apis","forEach","a","dtfSupported","DateTimeFormat","supportedLocalesOf","nfSupported","NumberFormat","group","decimal","parts","formatToParts","find","p","type","value","e","Error","message","String","pr","PluralRules","sample","select","example","number","format","date","dateStyle","Date","weekday","result","ok","length","console","log","warn"],"sourceRoot":"../../src","sources":["testLocalePolyfill.ts"],"mappings":";;AAAA,OAAO,SAASA,kBAAkBA,CAACC,MAAM,GAAG,OAAO,EAAE;EACnD,MAAMC,QAAQ,GAAG,EAAE;;EAEnB;EACA,IAAI,CAACC,MAAM,CAACC,IAAI,EAAEF,QAAQ,CAACG,IAAI,CAAC,uBAAuB,CAAC;EACxD,MAAMC,IAAI,GAAG,CACX,QAAQ,EACR,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,oBAAoB,EACpB,YAAY,EACZ,cAAc,CACf;EACDA,IAAI,CAACC,OAAO,CAAEC,CAAC,IAAK;IAClB;IACA,IAAI,CAACJ,IAAI,CAACI,CAAC,CAAC,EAAEN,QAAQ,CAACG,IAAI,CAAC,QAAQG,CAAC,UAAU,CAAC;EAClD,CAAC,CAAC;;EAEF;EACA,MAAMC,YAAY,GAChBL,IAAI,CAACM,cAAc,CAACC,kBAAkB,CAAC,CAACV,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,KAAKA,MAAM;EAChE,MAAMW,WAAW,GACfR,IAAI,CAACS,YAAY,CAACF,kBAAkB,CAAC,CAACV,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,KAAKA,MAAM;EAC9D,IAAI,CAACQ,YAAY,EAAEP,QAAQ,CAACG,IAAI,CAAC,GAAGJ,MAAM,kCAAkC,CAAC;EAC7E,IAAI,CAACW,WAAW,EAAEV,QAAQ,CAACG,IAAI,CAAC,GAAGJ,MAAM,gCAAgC,CAAC;;EAE1E;EACA,IAAIa,KAAK,EAAEC,OAAO;EAClB,IAAI;IACF,MAA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I,CAAC,YAAY,CACvB,CAAC;IACDC,OAAO,EAAE,IAAIhC,IAAI,CAACM,cAAc,CAACT,MAAM,EAAE;MAAEmC,OAAO,EAAE;IAAO,CAAC,CAAC,CAACJ,MAAM,CAClE,IAAIG,IAAI,CAAC,YAAY,CACvB;EACF,CAAC;EAED,MAAME,MAAM,GAAG;IACbC,EAAE,EAAEpC,QAAQ,CAACqC,MAAM,KAAK,CAAC;IACzBrC,QAAQ;IACRY,KAAK;IACLC,OAAO;IACPe;EACF,CAAC;EAED,IAAIO,MAAM,CAACC,EAAE,EAAE;IACbE,OAAO,CAACC,GAAG,CAAC,kCAAkCxC,MAAM,EAAE,EAAE6B,OAAO,CAAC;EAClE,CAAC,MAAM;IACLU,OAAO,CAACE,IAAI,CACV,sCAAsCzC,MAAM,GAAG,EAC/CC,QAAQ,EACR4B,OACF,CAAC;EACH;EACA,OAAOO,MAAM;AACf","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"testLocalePolyfill.d.ts","sourceRoot":"","sources":["../../../src/testLocalePolyfill.ts"],"names":[],"mappings":"AAAA,wBAAgB,kBAAkB,CAAC,MAAM,SAAU;;;;;;;;;;EAiFlD"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|