gt-react-native 11.1.8 → 11.1.9

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.
@@ -5,7 +5,17 @@ const require_plugin_dir_utils_resolveLocales = require("./utils/resolveLocales.
5
5
  let path = require("path");
6
6
  path = require_runtime.__toESM(path);
7
7
  //#region src/plugin-dir/index.ts
8
- function plugin(babel, { locales, config, configFilePath, entryPointFilePath = path.resolve(process.cwd(), "src", "App.tsx"), excludePolyfills = [] }) {
8
+ function isForceablePolyfill(polyfill) {
9
+ return polyfill in require_plugin_dir_types.FORCED_POLYFILL_IMPORTS;
10
+ }
11
+ function getPolyfillImport(polyfill, forcePolyfills) {
12
+ if (isForceablePolyfill(polyfill) && forcePolyfills?.includes(polyfill)) return require_plugin_dir_types.FORCED_POLYFILL_IMPORTS[polyfill];
13
+ return polyfill;
14
+ }
15
+ function getPolyfillAliases(polyfill) {
16
+ return isForceablePolyfill(polyfill) ? [polyfill, require_plugin_dir_types.FORCED_POLYFILL_IMPORTS[polyfill]] : [polyfill];
17
+ }
18
+ function plugin(babel, { locales, config, configFilePath, entryPointFilePath = path.resolve(process.cwd(), "src", "App.tsx"), excludePolyfills = [], forcePolyfills = [] }) {
9
19
  const { types: t } = babel;
10
20
  return {
11
21
  name: "gt-react-native/plugin",
@@ -16,12 +26,16 @@ function plugin(babel, { locales, config, configFilePath, entryPointFilePath = p
16
26
  config,
17
27
  configFilePath
18
28
  });
19
- const imports = [...require_plugin_dir_types.POLYFILLS.filter((polyfill) => !excludePolyfills.includes(polyfill)), ...resolvedLocales.flatMap((locale) => require_plugin_dir_types.LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`))];
29
+ const polyfillImports = require_plugin_dir_types.POLYFILLS.filter((polyfill) => !excludePolyfills.includes(polyfill)).map((polyfill) => ({
30
+ aliases: getPolyfillAliases(polyfill),
31
+ source: getPolyfillImport(polyfill, forcePolyfills)
32
+ }));
33
+ const localeImports = resolvedLocales.flatMap((locale) => require_plugin_dir_types.LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`));
20
34
  const existingImports = /* @__PURE__ */ new Set();
21
35
  programPath.node.body.forEach((node) => {
22
- if (t.isImportDeclaration(node) && typeof node.source.value === "string" && imports.includes(node.source.value)) existingImports.add(node.source.value);
36
+ if (t.isImportDeclaration(node) && typeof node.source.value === "string") existingImports.add(node.source.value);
23
37
  });
24
- const importsToAdd = imports.filter((imp) => !existingImports.has(imp));
38
+ const importsToAdd = [...polyfillImports.filter(({ aliases }) => aliases.every((alias) => !existingImports.has(alias))).map(({ source }) => source), ...localeImports.filter((source) => !existingImports.has(source))];
25
39
  if (importsToAdd.length > 0) {
26
40
  const newImports = importsToAdd.map((importPath) => t.importDeclaration([], t.stringLiteral(importPath)));
27
41
  programPath.node.body.unshift(...newImports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["resolveLocales","POLYFILLS","LOCALE_POLYFILLS"],"sources":["../../../src/plugin-dir/index.ts"],"sourcesContent":["import * as path from 'path';\nimport type { PluginObj, types } from '@babel/core';\nimport { LOCALE_POLYFILLS, POLYFILLS, type PluginOptions } from './types';\nimport { resolveLocales } from './utils/resolveLocales';\n\nexport function plugin(\n babel: { types: typeof types },\n {\n locales,\n config,\n configFilePath,\n entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx'),\n excludePolyfills = [],\n }: PluginOptions\n): PluginObj {\n const { types: t } = babel;\n\n return {\n name: 'gt-react-native/plugin',\n visitor: {\n Program(programPath, state) {\n const currentFilePath = path.resolve(\n state.filename || state.file.opts.filename || ''\n );\n\n // Only apply polyfills to files that import gt-react-native or generaltranslation\n if (currentFilePath !== entryPointFilePath) {\n return;\n }\n\n const resolvedLocales = resolveLocales({\n locales,\n config,\n configFilePath,\n });\n\n // TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()\n const imports = [\n ...POLYFILLS.filter(\n (polyfill) => !excludePolyfills.includes(polyfill)\n ),\n ...resolvedLocales.flatMap((locale) =>\n LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`)\n ),\n ];\n\n const existingImports = new Set<string>();\n programPath.node.body.forEach((node) => {\n if (\n t.isImportDeclaration(node) &&\n typeof node.source.value === 'string' &&\n imports.includes(node.source.value)\n ) {\n existingImports.add(node.source.value);\n }\n });\n\n const importsToAdd = imports.filter((imp) => !existingImports.has(imp));\n\n if (importsToAdd.length > 0) {\n const newImports = importsToAdd.map((importPath) =>\n t.importDeclaration([], t.stringLiteral(importPath))\n );\n\n programPath.node.body.unshift(...newImports);\n }\n },\n },\n };\n}\n"],"mappings":";;;;;;;AAKA,SAAgB,OACd,OACA,EACE,SACA,QACA,gBACA,qBAAqB,KAAK,QAAQ,QAAQ,KAAK,EAAE,OAAO,UAAU,EAClE,mBAAmB,EAAE,IAEZ;CACX,MAAM,EAAE,OAAO,MAAM;AAErB,QAAO;EACL,MAAM;EACN,SAAS,EACP,QAAQ,aAAa,OAAO;AAM1B,OALwB,KAAK,QAC3B,MAAM,YAAY,MAAM,KAAK,KAAK,YAAY,GAI7B,KAAK,mBACtB;GAGF,MAAM,kBAAkBA,wCAAAA,eAAe;IACrC;IACA;IACA;IACD,CAAC;GAGF,MAAM,UAAU,CACd,GAAGC,yBAAAA,UAAU,QACV,aAAa,CAAC,iBAAiB,SAAS,SAAS,CACnD,EACD,GAAG,gBAAgB,SAAS,WAC1BC,yBAAAA,iBAAiB,KAAK,eAAe,GAAG,WAAW,GAAG,SAAS,CAChE,CACF;GAED,MAAM,kCAAkB,IAAI,KAAa;AACzC,eAAY,KAAK,KAAK,SAAS,SAAS;AACtC,QACE,EAAE,oBAAoB,KAAK,IAC3B,OAAO,KAAK,OAAO,UAAU,YAC7B,QAAQ,SAAS,KAAK,OAAO,MAAM,CAEnC,iBAAgB,IAAI,KAAK,OAAO,MAAM;KAExC;GAEF,MAAM,eAAe,QAAQ,QAAQ,QAAQ,CAAC,gBAAgB,IAAI,IAAI,CAAC;AAEvE,OAAI,aAAa,SAAS,GAAG;IAC3B,MAAM,aAAa,aAAa,KAAK,eACnC,EAAE,kBAAkB,EAAE,EAAE,EAAE,cAAc,WAAW,CAAC,CACrD;AAED,gBAAY,KAAK,KAAK,QAAQ,GAAG,WAAW;;KAGjD;EACF"}
1
+ {"version":3,"file":"index.js","names":["FORCED_POLYFILL_IMPORTS","resolveLocales","POLYFILLS","LOCALE_POLYFILLS"],"sources":["../../../src/plugin-dir/index.ts"],"sourcesContent":["import * as path from 'path';\nimport type { PluginObj, types } from '@babel/core';\nimport {\n FORCED_POLYFILL_IMPORTS,\n LOCALE_POLYFILLS,\n POLYFILLS,\n type ForceablePolyfill,\n type Polyfill,\n type PluginOptions,\n} from './types';\nimport { resolveLocales } from './utils/resolveLocales';\n\nfunction isForceablePolyfill(\n polyfill: Polyfill\n): polyfill is ForceablePolyfill {\n return polyfill in FORCED_POLYFILL_IMPORTS;\n}\n\nfunction getPolyfillImport(\n polyfill: Polyfill,\n forcePolyfills: PluginOptions['forcePolyfills']\n): string {\n if (isForceablePolyfill(polyfill) && forcePolyfills?.includes(polyfill)) {\n return FORCED_POLYFILL_IMPORTS[polyfill];\n }\n\n return polyfill;\n}\n\nfunction getPolyfillAliases(polyfill: Polyfill): string[] {\n return isForceablePolyfill(polyfill)\n ? [polyfill, FORCED_POLYFILL_IMPORTS[polyfill]]\n : [polyfill];\n}\n\nexport function plugin(\n babel: { types: typeof types },\n {\n locales,\n config,\n configFilePath,\n entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx'),\n excludePolyfills = [],\n forcePolyfills = [],\n }: PluginOptions\n): PluginObj {\n const { types: t } = babel;\n\n return {\n name: 'gt-react-native/plugin',\n visitor: {\n Program(programPath, state) {\n const currentFilePath = path.resolve(\n state.filename || state.file.opts.filename || ''\n );\n\n // Only apply polyfills to files that import gt-react-native or generaltranslation\n if (currentFilePath !== entryPointFilePath) {\n return;\n }\n\n const resolvedLocales = resolveLocales({\n locales,\n config,\n configFilePath,\n });\n\n // TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()\n const polyfillImports = POLYFILLS.filter(\n (polyfill) => !excludePolyfills.includes(polyfill)\n ).map((polyfill) => ({\n aliases: getPolyfillAliases(polyfill),\n source: getPolyfillImport(polyfill, forcePolyfills),\n }));\n const localeImports = resolvedLocales.flatMap((locale) =>\n LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`)\n );\n\n const existingImports = new Set<string>();\n programPath.node.body.forEach((node) => {\n if (\n t.isImportDeclaration(node) &&\n typeof node.source.value === 'string'\n ) {\n existingImports.add(node.source.value);\n }\n });\n\n const importsToAdd = [\n ...polyfillImports\n .filter(({ aliases }) =>\n aliases.every((alias) => !existingImports.has(alias))\n )\n .map(({ source }) => source),\n ...localeImports.filter((source) => !existingImports.has(source)),\n ];\n\n if (importsToAdd.length > 0) {\n const newImports = importsToAdd.map((importPath) =>\n t.importDeclaration([], t.stringLiteral(importPath))\n );\n\n programPath.node.body.unshift(...newImports);\n }\n },\n },\n };\n}\n"],"mappings":";;;;;;;AAYA,SAAS,oBACP,UAC+B;AAC/B,QAAO,YAAYA,yBAAAA;;AAGrB,SAAS,kBACP,UACA,gBACQ;AACR,KAAI,oBAAoB,SAAS,IAAI,gBAAgB,SAAS,SAAS,CACrE,QAAOA,yBAAAA,wBAAwB;AAGjC,QAAO;;AAGT,SAAS,mBAAmB,UAA8B;AACxD,QAAO,oBAAoB,SAAS,GAChC,CAAC,UAAUA,yBAAAA,wBAAwB,UAAU,GAC7C,CAAC,SAAS;;AAGhB,SAAgB,OACd,OACA,EACE,SACA,QACA,gBACA,qBAAqB,KAAK,QAAQ,QAAQ,KAAK,EAAE,OAAO,UAAU,EAClE,mBAAmB,EAAE,EACrB,iBAAiB,EAAE,IAEV;CACX,MAAM,EAAE,OAAO,MAAM;AAErB,QAAO;EACL,MAAM;EACN,SAAS,EACP,QAAQ,aAAa,OAAO;AAM1B,OALwB,KAAK,QAC3B,MAAM,YAAY,MAAM,KAAK,KAAK,YAAY,GAI7B,KAAK,mBACtB;GAGF,MAAM,kBAAkBC,wCAAAA,eAAe;IACrC;IACA;IACA;IACD,CAAC;GAGF,MAAM,kBAAkBC,yBAAAA,UAAU,QAC/B,aAAa,CAAC,iBAAiB,SAAS,SAAS,CACnD,CAAC,KAAK,cAAc;IACnB,SAAS,mBAAmB,SAAS;IACrC,QAAQ,kBAAkB,UAAU,eAAe;IACpD,EAAE;GACH,MAAM,gBAAgB,gBAAgB,SAAS,WAC7CC,yBAAAA,iBAAiB,KAAK,eAAe,GAAG,WAAW,GAAG,SAAS,CAChE;GAED,MAAM,kCAAkB,IAAI,KAAa;AACzC,eAAY,KAAK,KAAK,SAAS,SAAS;AACtC,QACE,EAAE,oBAAoB,KAAK,IAC3B,OAAO,KAAK,OAAO,UAAU,SAE7B,iBAAgB,IAAI,KAAK,OAAO,MAAM;KAExC;GAEF,MAAM,eAAe,CACnB,GAAG,gBACA,QAAQ,EAAE,cACT,QAAQ,OAAO,UAAU,CAAC,gBAAgB,IAAI,MAAM,CAAC,CACtD,CACA,KAAK,EAAE,aAAa,OAAO,EAC9B,GAAG,cAAc,QAAQ,WAAW,CAAC,gBAAgB,IAAI,OAAO,CAAC,CAClE;AAED,OAAI,aAAa,SAAS,GAAG;IAC3B,MAAM,aAAa,aAAa,KAAK,eACnC,EAAE,kBAAkB,EAAE,EAAE,EAAE,cAAc,WAAW,CAAC,CACrD;AAED,gBAAY,KAAK,KAAK,QAAQ,GAAG,WAAW;;KAGjD;EACF"}
@@ -11,6 +11,11 @@ const POLYFILLS = [
11
11
  "@formatjs/intl-datetimeformat/polyfill",
12
12
  "@formatjs/intl-datetimeformat/add-all-tz"
13
13
  ];
14
+ const FORCED_POLYFILL_IMPORTS = {
15
+ "@formatjs/intl-displaynames/polyfill": "@formatjs/intl-displaynames/polyfill-force",
16
+ "@formatjs/intl-listformat/polyfill": "@formatjs/intl-listformat/polyfill-force",
17
+ "@formatjs/intl-relativetimeformat/polyfill": "@formatjs/intl-relativetimeformat/polyfill-force"
18
+ };
14
19
  const LOCALE_POLYFILLS = [
15
20
  `@formatjs/intl-displaynames/locale-data`,
16
21
  `@formatjs/intl-listformat/locale-data`,
@@ -20,6 +25,7 @@ const LOCALE_POLYFILLS = [
20
25
  `@formatjs/intl-datetimeformat/locale-data`
21
26
  ];
22
27
  //#endregion
28
+ exports.FORCED_POLYFILL_IMPORTS = FORCED_POLYFILL_IMPORTS;
23
29
  exports.LOCALE_POLYFILLS = LOCALE_POLYFILLS;
24
30
  exports.POLYFILLS = POLYFILLS;
25
31
 
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":[],"sources":["../../../src/plugin-dir/types.ts"],"sourcesContent":["export const POLYFILLS = [\n '@formatjs/intl-getcanonicallocales/polyfill',\n '@formatjs/intl-locale/polyfill',\n '@formatjs/intl-displaynames/polyfill',\n '@formatjs/intl-listformat/polyfill',\n '@formatjs/intl-pluralrules/polyfill-force', // https://github.com/formatjs/formatjs/issues/4463\n '@formatjs/intl-numberformat/polyfill',\n '@formatjs/intl-relativetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/add-all-tz',\n] as const;\n\nexport const LOCALE_POLYFILLS = [\n `@formatjs/intl-displaynames/locale-data`,\n `@formatjs/intl-listformat/locale-data`,\n `@formatjs/intl-pluralrules/locale-data`,\n `@formatjs/intl-numberformat/locale-data`,\n `@formatjs/intl-relativetimeformat/locale-data`,\n `@formatjs/intl-datetimeformat/locale-data`,\n] as const;\n\nexport interface PluginOptions {\n /* List of locales to polyfill */\n locales?: string[];\n /* Gt config object */\n config?: { defaultLocale: string; locales: string[] } & Record<\n string,\n unknown\n >;\n /* Path to the gt config file */\n configFilePath?: string;\n /* Resolved from package.json */\n entryPointFilePath?: string;\n /* Polyfills to exclude */\n excludePolyfills?: (typeof POLYFILLS)[number][];\n}\n"],"mappings":";;AAAA,MAAa,YAAY;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAa,mBAAmB;CAC9B;CACA;CACA;CACA;CACA;CACA;CACD"}
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../../src/plugin-dir/types.ts"],"sourcesContent":["export const POLYFILLS = [\n '@formatjs/intl-getcanonicallocales/polyfill',\n '@formatjs/intl-locale/polyfill',\n '@formatjs/intl-displaynames/polyfill',\n '@formatjs/intl-listformat/polyfill',\n '@formatjs/intl-pluralrules/polyfill-force', // https://github.com/formatjs/formatjs/issues/4463\n '@formatjs/intl-numberformat/polyfill',\n '@formatjs/intl-relativetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/add-all-tz',\n] as const;\n\nexport type Polyfill = (typeof POLYFILLS)[number];\n\nexport const FORCED_POLYFILL_IMPORTS = {\n '@formatjs/intl-displaynames/polyfill':\n '@formatjs/intl-displaynames/polyfill-force',\n '@formatjs/intl-listformat/polyfill':\n '@formatjs/intl-listformat/polyfill-force',\n '@formatjs/intl-relativetimeformat/polyfill':\n '@formatjs/intl-relativetimeformat/polyfill-force',\n} as const satisfies Partial<Record<Polyfill, string>>;\n\nexport type ForceablePolyfill = keyof typeof FORCED_POLYFILL_IMPORTS;\n\nexport const LOCALE_POLYFILLS = [\n `@formatjs/intl-displaynames/locale-data`,\n `@formatjs/intl-listformat/locale-data`,\n `@formatjs/intl-pluralrules/locale-data`,\n `@formatjs/intl-numberformat/locale-data`,\n `@formatjs/intl-relativetimeformat/locale-data`,\n `@formatjs/intl-datetimeformat/locale-data`,\n] as const;\n\nexport interface PluginOptions {\n /* List of locales to polyfill */\n locales?: string[];\n /* Gt config object */\n config?: { defaultLocale: string; locales: string[] } & Record<\n string,\n unknown\n >;\n /* Path to the gt config file */\n configFilePath?: string;\n /* Resolved from package.json */\n entryPointFilePath?: string;\n /* Polyfills to exclude */\n excludePolyfills?: Polyfill[];\n /* Polyfills that should bypass runtime capability detection */\n forcePolyfills?: ForceablePolyfill[];\n}\n"],"mappings":";;AAAA,MAAa,YAAY;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAID,MAAa,0BAA0B;CACrC,wCACE;CACF,sCACE;CACF,8CACE;CACH;AAID,MAAa,mBAAmB;CAC9B;CACA;CACA;CACA;CACA;CACA;CACD"}
@@ -2,5 +2,5 @@ import type { PluginObj, types } from '@babel/core';
2
2
  import { type PluginOptions } from './types';
3
3
  export declare function plugin(babel: {
4
4
  types: typeof types;
5
- }, { locales, config, configFilePath, entryPointFilePath, excludePolyfills, }: PluginOptions): PluginObj;
5
+ }, { locales, config, configFilePath, entryPointFilePath, excludePolyfills, forcePolyfills, }: PluginOptions): PluginObj;
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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,EAA+B,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAG1E,wBAAgB,MAAM,CACpB,KAAK,EAAE;IAAE,KAAK,EAAE,OAAO,KAAK,CAAA;CAAE,EAC9B,EACE,OAAO,EACP,MAAM,EACN,cAAc,EACd,kBAAkE,EAClE,gBAAqB,GACtB,EAAE,aAAa,GACf,SAAS,CAuDX"}
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,EAML,KAAK,aAAa,EACnB,MAAM,SAAS,CAAC;AA0BjB,wBAAgB,MAAM,CACpB,KAAK,EAAE;IAAE,KAAK,EAAE,OAAO,KAAK,CAAA;CAAE,EAC9B,EACE,OAAO,EACP,MAAM,EACN,cAAc,EACd,kBAAkE,EAClE,gBAAqB,EACrB,cAAmB,GACpB,EAAE,aAAa,GACf,SAAS,CA8DX"}
@@ -1,8 +1,18 @@
1
- import { LOCALE_POLYFILLS, POLYFILLS } from "./types.js";
1
+ import { FORCED_POLYFILL_IMPORTS, LOCALE_POLYFILLS, POLYFILLS } from "./types.js";
2
2
  import { resolveLocales } from "./utils/resolveLocales.js";
3
3
  import * as path$1 from "path";
4
4
  //#region src/plugin-dir/index.ts
5
- function plugin(babel, { locales, config, configFilePath, entryPointFilePath = path$1.resolve(process.cwd(), "src", "App.tsx"), excludePolyfills = [] }) {
5
+ function isForceablePolyfill(polyfill) {
6
+ return polyfill in FORCED_POLYFILL_IMPORTS;
7
+ }
8
+ function getPolyfillImport(polyfill, forcePolyfills) {
9
+ if (isForceablePolyfill(polyfill) && forcePolyfills?.includes(polyfill)) return FORCED_POLYFILL_IMPORTS[polyfill];
10
+ return polyfill;
11
+ }
12
+ function getPolyfillAliases(polyfill) {
13
+ return isForceablePolyfill(polyfill) ? [polyfill, FORCED_POLYFILL_IMPORTS[polyfill]] : [polyfill];
14
+ }
15
+ function plugin(babel, { locales, config, configFilePath, entryPointFilePath = path$1.resolve(process.cwd(), "src", "App.tsx"), excludePolyfills = [], forcePolyfills = [] }) {
6
16
  const { types: t } = babel;
7
17
  return {
8
18
  name: "gt-react-native/plugin",
@@ -13,12 +23,16 @@ function plugin(babel, { locales, config, configFilePath, entryPointFilePath = p
13
23
  config,
14
24
  configFilePath
15
25
  });
16
- const imports = [...POLYFILLS.filter((polyfill) => !excludePolyfills.includes(polyfill)), ...resolvedLocales.flatMap((locale) => LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`))];
26
+ const polyfillImports = POLYFILLS.filter((polyfill) => !excludePolyfills.includes(polyfill)).map((polyfill) => ({
27
+ aliases: getPolyfillAliases(polyfill),
28
+ source: getPolyfillImport(polyfill, forcePolyfills)
29
+ }));
30
+ const localeImports = resolvedLocales.flatMap((locale) => LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`));
17
31
  const existingImports = /* @__PURE__ */ new Set();
18
32
  programPath.node.body.forEach((node) => {
19
- if (t.isImportDeclaration(node) && typeof node.source.value === "string" && imports.includes(node.source.value)) existingImports.add(node.source.value);
33
+ if (t.isImportDeclaration(node) && typeof node.source.value === "string") existingImports.add(node.source.value);
20
34
  });
21
- const importsToAdd = imports.filter((imp) => !existingImports.has(imp));
35
+ const importsToAdd = [...polyfillImports.filter(({ aliases }) => aliases.every((alias) => !existingImports.has(alias))).map(({ source }) => source), ...localeImports.filter((source) => !existingImports.has(source))];
22
36
  if (importsToAdd.length > 0) {
23
37
  const newImports = importsToAdd.map((importPath) => t.importDeclaration([], t.stringLiteral(importPath)));
24
38
  programPath.node.body.unshift(...newImports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["path"],"sources":["../../../src/plugin-dir/index.ts"],"sourcesContent":["import * as path from 'path';\nimport type { PluginObj, types } from '@babel/core';\nimport { LOCALE_POLYFILLS, POLYFILLS, type PluginOptions } from './types';\nimport { resolveLocales } from './utils/resolveLocales';\n\nexport function plugin(\n babel: { types: typeof types },\n {\n locales,\n config,\n configFilePath,\n entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx'),\n excludePolyfills = [],\n }: PluginOptions\n): PluginObj {\n const { types: t } = babel;\n\n return {\n name: 'gt-react-native/plugin',\n visitor: {\n Program(programPath, state) {\n const currentFilePath = path.resolve(\n state.filename || state.file.opts.filename || ''\n );\n\n // Only apply polyfills to files that import gt-react-native or generaltranslation\n if (currentFilePath !== entryPointFilePath) {\n return;\n }\n\n const resolvedLocales = resolveLocales({\n locales,\n config,\n configFilePath,\n });\n\n // TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()\n const imports = [\n ...POLYFILLS.filter(\n (polyfill) => !excludePolyfills.includes(polyfill)\n ),\n ...resolvedLocales.flatMap((locale) =>\n LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`)\n ),\n ];\n\n const existingImports = new Set<string>();\n programPath.node.body.forEach((node) => {\n if (\n t.isImportDeclaration(node) &&\n typeof node.source.value === 'string' &&\n imports.includes(node.source.value)\n ) {\n existingImports.add(node.source.value);\n }\n });\n\n const importsToAdd = imports.filter((imp) => !existingImports.has(imp));\n\n if (importsToAdd.length > 0) {\n const newImports = importsToAdd.map((importPath) =>\n t.importDeclaration([], t.stringLiteral(importPath))\n );\n\n programPath.node.body.unshift(...newImports);\n }\n },\n },\n };\n}\n"],"mappings":";;;;AAKA,SAAgB,OACd,OACA,EACE,SACA,QACA,gBACA,qBAAqBA,OAAK,QAAQ,QAAQ,KAAK,EAAE,OAAO,UAAU,EAClE,mBAAmB,EAAE,IAEZ;CACX,MAAM,EAAE,OAAO,MAAM;AAErB,QAAO;EACL,MAAM;EACN,SAAS,EACP,QAAQ,aAAa,OAAO;AAM1B,OALwBA,OAAK,QAC3B,MAAM,YAAY,MAAM,KAAK,KAAK,YAAY,GAI7B,KAAK,mBACtB;GAGF,MAAM,kBAAkB,eAAe;IACrC;IACA;IACA;IACD,CAAC;GAGF,MAAM,UAAU,CACd,GAAG,UAAU,QACV,aAAa,CAAC,iBAAiB,SAAS,SAAS,CACnD,EACD,GAAG,gBAAgB,SAAS,WAC1B,iBAAiB,KAAK,eAAe,GAAG,WAAW,GAAG,SAAS,CAChE,CACF;GAED,MAAM,kCAAkB,IAAI,KAAa;AACzC,eAAY,KAAK,KAAK,SAAS,SAAS;AACtC,QACE,EAAE,oBAAoB,KAAK,IAC3B,OAAO,KAAK,OAAO,UAAU,YAC7B,QAAQ,SAAS,KAAK,OAAO,MAAM,CAEnC,iBAAgB,IAAI,KAAK,OAAO,MAAM;KAExC;GAEF,MAAM,eAAe,QAAQ,QAAQ,QAAQ,CAAC,gBAAgB,IAAI,IAAI,CAAC;AAEvE,OAAI,aAAa,SAAS,GAAG;IAC3B,MAAM,aAAa,aAAa,KAAK,eACnC,EAAE,kBAAkB,EAAE,EAAE,EAAE,cAAc,WAAW,CAAC,CACrD;AAED,gBAAY,KAAK,KAAK,QAAQ,GAAG,WAAW;;KAGjD;EACF"}
1
+ {"version":3,"file":"index.js","names":["path"],"sources":["../../../src/plugin-dir/index.ts"],"sourcesContent":["import * as path from 'path';\nimport type { PluginObj, types } from '@babel/core';\nimport {\n FORCED_POLYFILL_IMPORTS,\n LOCALE_POLYFILLS,\n POLYFILLS,\n type ForceablePolyfill,\n type Polyfill,\n type PluginOptions,\n} from './types';\nimport { resolveLocales } from './utils/resolveLocales';\n\nfunction isForceablePolyfill(\n polyfill: Polyfill\n): polyfill is ForceablePolyfill {\n return polyfill in FORCED_POLYFILL_IMPORTS;\n}\n\nfunction getPolyfillImport(\n polyfill: Polyfill,\n forcePolyfills: PluginOptions['forcePolyfills']\n): string {\n if (isForceablePolyfill(polyfill) && forcePolyfills?.includes(polyfill)) {\n return FORCED_POLYFILL_IMPORTS[polyfill];\n }\n\n return polyfill;\n}\n\nfunction getPolyfillAliases(polyfill: Polyfill): string[] {\n return isForceablePolyfill(polyfill)\n ? [polyfill, FORCED_POLYFILL_IMPORTS[polyfill]]\n : [polyfill];\n}\n\nexport function plugin(\n babel: { types: typeof types },\n {\n locales,\n config,\n configFilePath,\n entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx'),\n excludePolyfills = [],\n forcePolyfills = [],\n }: PluginOptions\n): PluginObj {\n const { types: t } = babel;\n\n return {\n name: 'gt-react-native/plugin',\n visitor: {\n Program(programPath, state) {\n const currentFilePath = path.resolve(\n state.filename || state.file.opts.filename || ''\n );\n\n // Only apply polyfills to files that import gt-react-native or generaltranslation\n if (currentFilePath !== entryPointFilePath) {\n return;\n }\n\n const resolvedLocales = resolveLocales({\n locales,\n config,\n configFilePath,\n });\n\n // TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()\n const polyfillImports = POLYFILLS.filter(\n (polyfill) => !excludePolyfills.includes(polyfill)\n ).map((polyfill) => ({\n aliases: getPolyfillAliases(polyfill),\n source: getPolyfillImport(polyfill, forcePolyfills),\n }));\n const localeImports = resolvedLocales.flatMap((locale) =>\n LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`)\n );\n\n const existingImports = new Set<string>();\n programPath.node.body.forEach((node) => {\n if (\n t.isImportDeclaration(node) &&\n typeof node.source.value === 'string'\n ) {\n existingImports.add(node.source.value);\n }\n });\n\n const importsToAdd = [\n ...polyfillImports\n .filter(({ aliases }) =>\n aliases.every((alias) => !existingImports.has(alias))\n )\n .map(({ source }) => source),\n ...localeImports.filter((source) => !existingImports.has(source)),\n ];\n\n if (importsToAdd.length > 0) {\n const newImports = importsToAdd.map((importPath) =>\n t.importDeclaration([], t.stringLiteral(importPath))\n );\n\n programPath.node.body.unshift(...newImports);\n }\n },\n },\n };\n}\n"],"mappings":";;;;AAYA,SAAS,oBACP,UAC+B;AAC/B,QAAO,YAAY;;AAGrB,SAAS,kBACP,UACA,gBACQ;AACR,KAAI,oBAAoB,SAAS,IAAI,gBAAgB,SAAS,SAAS,CACrE,QAAO,wBAAwB;AAGjC,QAAO;;AAGT,SAAS,mBAAmB,UAA8B;AACxD,QAAO,oBAAoB,SAAS,GAChC,CAAC,UAAU,wBAAwB,UAAU,GAC7C,CAAC,SAAS;;AAGhB,SAAgB,OACd,OACA,EACE,SACA,QACA,gBACA,qBAAqBA,OAAK,QAAQ,QAAQ,KAAK,EAAE,OAAO,UAAU,EAClE,mBAAmB,EAAE,EACrB,iBAAiB,EAAE,IAEV;CACX,MAAM,EAAE,OAAO,MAAM;AAErB,QAAO;EACL,MAAM;EACN,SAAS,EACP,QAAQ,aAAa,OAAO;AAM1B,OALwBA,OAAK,QAC3B,MAAM,YAAY,MAAM,KAAK,KAAK,YAAY,GAI7B,KAAK,mBACtB;GAGF,MAAM,kBAAkB,eAAe;IACrC;IACA;IACA;IACD,CAAC;GAGF,MAAM,kBAAkB,UAAU,QAC/B,aAAa,CAAC,iBAAiB,SAAS,SAAS,CACnD,CAAC,KAAK,cAAc;IACnB,SAAS,mBAAmB,SAAS;IACrC,QAAQ,kBAAkB,UAAU,eAAe;IACpD,EAAE;GACH,MAAM,gBAAgB,gBAAgB,SAAS,WAC7C,iBAAiB,KAAK,eAAe,GAAG,WAAW,GAAG,SAAS,CAChE;GAED,MAAM,kCAAkB,IAAI,KAAa;AACzC,eAAY,KAAK,KAAK,SAAS,SAAS;AACtC,QACE,EAAE,oBAAoB,KAAK,IAC3B,OAAO,KAAK,OAAO,UAAU,SAE7B,iBAAgB,IAAI,KAAK,OAAO,MAAM;KAExC;GAEF,MAAM,eAAe,CACnB,GAAG,gBACA,QAAQ,EAAE,cACT,QAAQ,OAAO,UAAU,CAAC,gBAAgB,IAAI,MAAM,CAAC,CACtD,CACA,KAAK,EAAE,aAAa,OAAO,EAC9B,GAAG,cAAc,QAAQ,WAAW,CAAC,gBAAgB,IAAI,OAAO,CAAC,CAClE;AAED,OAAI,aAAa,SAAS,GAAG;IAC3B,MAAM,aAAa,aAAa,KAAK,eACnC,EAAE,kBAAkB,EAAE,EAAE,EAAE,cAAc,WAAW,CAAC,CACrD;AAED,gBAAY,KAAK,KAAK,QAAQ,GAAG,WAAW;;KAGjD;EACF"}
@@ -1,4 +1,11 @@
1
1
  export declare const POLYFILLS: readonly ["@formatjs/intl-getcanonicallocales/polyfill", "@formatjs/intl-locale/polyfill", "@formatjs/intl-displaynames/polyfill", "@formatjs/intl-listformat/polyfill", "@formatjs/intl-pluralrules/polyfill-force", "@formatjs/intl-numberformat/polyfill", "@formatjs/intl-relativetimeformat/polyfill", "@formatjs/intl-datetimeformat/polyfill", "@formatjs/intl-datetimeformat/add-all-tz"];
2
+ export type Polyfill = (typeof POLYFILLS)[number];
3
+ export declare const FORCED_POLYFILL_IMPORTS: {
4
+ readonly '@formatjs/intl-displaynames/polyfill': "@formatjs/intl-displaynames/polyfill-force";
5
+ readonly '@formatjs/intl-listformat/polyfill': "@formatjs/intl-listformat/polyfill-force";
6
+ readonly '@formatjs/intl-relativetimeformat/polyfill': "@formatjs/intl-relativetimeformat/polyfill-force";
7
+ };
8
+ export type ForceablePolyfill = keyof typeof FORCED_POLYFILL_IMPORTS;
2
9
  export declare const LOCALE_POLYFILLS: readonly ["@formatjs/intl-displaynames/locale-data", "@formatjs/intl-listformat/locale-data", "@formatjs/intl-pluralrules/locale-data", "@formatjs/intl-numberformat/locale-data", "@formatjs/intl-relativetimeformat/locale-data", "@formatjs/intl-datetimeformat/locale-data"];
3
10
  export interface PluginOptions {
4
11
  locales?: string[];
@@ -8,6 +15,7 @@ export interface PluginOptions {
8
15
  } & Record<string, unknown>;
9
16
  configFilePath?: string;
10
17
  entryPointFilePath?: string;
11
- excludePolyfills?: (typeof POLYFILLS)[number][];
18
+ excludePolyfills?: Polyfill[];
19
+ forcePolyfills?: ForceablePolyfill[];
12
20
  }
13
21
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/plugin-dir/types.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,mYAUZ,CAAC;AAEX,eAAO,MAAM,gBAAgB,kRAOnB,CAAC;AAEX,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,CAC5D,MAAM,EACN,OAAO,CACR,CAAC;IAEF,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B,gBAAgB,CAAC,EAAE,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;CACjD"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/plugin-dir/types.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,mYAUZ,CAAC;AAEX,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;AAElD,eAAO,MAAM,uBAAuB;;;;CAOkB,CAAC;AAEvD,MAAM,MAAM,iBAAiB,GAAG,MAAM,OAAO,uBAAuB,CAAC;AAErE,eAAO,MAAM,gBAAgB,kRAOnB,CAAC;AAEX,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,CAC5D,MAAM,EACN,OAAO,CACR,CAAC;IAEF,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B,gBAAgB,CAAC,EAAE,QAAQ,EAAE,CAAC;IAE9B,cAAc,CAAC,EAAE,iBAAiB,EAAE,CAAC;CACtC"}
@@ -10,6 +10,11 @@ const POLYFILLS = [
10
10
  "@formatjs/intl-datetimeformat/polyfill",
11
11
  "@formatjs/intl-datetimeformat/add-all-tz"
12
12
  ];
13
+ const FORCED_POLYFILL_IMPORTS = {
14
+ "@formatjs/intl-displaynames/polyfill": "@formatjs/intl-displaynames/polyfill-force",
15
+ "@formatjs/intl-listformat/polyfill": "@formatjs/intl-listformat/polyfill-force",
16
+ "@formatjs/intl-relativetimeformat/polyfill": "@formatjs/intl-relativetimeformat/polyfill-force"
17
+ };
13
18
  const LOCALE_POLYFILLS = [
14
19
  `@formatjs/intl-displaynames/locale-data`,
15
20
  `@formatjs/intl-listformat/locale-data`,
@@ -19,6 +24,6 @@ const LOCALE_POLYFILLS = [
19
24
  `@formatjs/intl-datetimeformat/locale-data`
20
25
  ];
21
26
  //#endregion
22
- export { LOCALE_POLYFILLS, POLYFILLS };
27
+ export { FORCED_POLYFILL_IMPORTS, LOCALE_POLYFILLS, POLYFILLS };
23
28
 
24
29
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":[],"sources":["../../../src/plugin-dir/types.ts"],"sourcesContent":["export const POLYFILLS = [\n '@formatjs/intl-getcanonicallocales/polyfill',\n '@formatjs/intl-locale/polyfill',\n '@formatjs/intl-displaynames/polyfill',\n '@formatjs/intl-listformat/polyfill',\n '@formatjs/intl-pluralrules/polyfill-force', // https://github.com/formatjs/formatjs/issues/4463\n '@formatjs/intl-numberformat/polyfill',\n '@formatjs/intl-relativetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/add-all-tz',\n] as const;\n\nexport const LOCALE_POLYFILLS = [\n `@formatjs/intl-displaynames/locale-data`,\n `@formatjs/intl-listformat/locale-data`,\n `@formatjs/intl-pluralrules/locale-data`,\n `@formatjs/intl-numberformat/locale-data`,\n `@formatjs/intl-relativetimeformat/locale-data`,\n `@formatjs/intl-datetimeformat/locale-data`,\n] as const;\n\nexport interface PluginOptions {\n /* List of locales to polyfill */\n locales?: string[];\n /* Gt config object */\n config?: { defaultLocale: string; locales: string[] } & Record<\n string,\n unknown\n >;\n /* Path to the gt config file */\n configFilePath?: string;\n /* Resolved from package.json */\n entryPointFilePath?: string;\n /* Polyfills to exclude */\n excludePolyfills?: (typeof POLYFILLS)[number][];\n}\n"],"mappings":";AAAA,MAAa,YAAY;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAa,mBAAmB;CAC9B;CACA;CACA;CACA;CACA;CACA;CACD"}
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../../src/plugin-dir/types.ts"],"sourcesContent":["export const POLYFILLS = [\n '@formatjs/intl-getcanonicallocales/polyfill',\n '@formatjs/intl-locale/polyfill',\n '@formatjs/intl-displaynames/polyfill',\n '@formatjs/intl-listformat/polyfill',\n '@formatjs/intl-pluralrules/polyfill-force', // https://github.com/formatjs/formatjs/issues/4463\n '@formatjs/intl-numberformat/polyfill',\n '@formatjs/intl-relativetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/polyfill',\n '@formatjs/intl-datetimeformat/add-all-tz',\n] as const;\n\nexport type Polyfill = (typeof POLYFILLS)[number];\n\nexport const FORCED_POLYFILL_IMPORTS = {\n '@formatjs/intl-displaynames/polyfill':\n '@formatjs/intl-displaynames/polyfill-force',\n '@formatjs/intl-listformat/polyfill':\n '@formatjs/intl-listformat/polyfill-force',\n '@formatjs/intl-relativetimeformat/polyfill':\n '@formatjs/intl-relativetimeformat/polyfill-force',\n} as const satisfies Partial<Record<Polyfill, string>>;\n\nexport type ForceablePolyfill = keyof typeof FORCED_POLYFILL_IMPORTS;\n\nexport const LOCALE_POLYFILLS = [\n `@formatjs/intl-displaynames/locale-data`,\n `@formatjs/intl-listformat/locale-data`,\n `@formatjs/intl-pluralrules/locale-data`,\n `@formatjs/intl-numberformat/locale-data`,\n `@formatjs/intl-relativetimeformat/locale-data`,\n `@formatjs/intl-datetimeformat/locale-data`,\n] as const;\n\nexport interface PluginOptions {\n /* List of locales to polyfill */\n locales?: string[];\n /* Gt config object */\n config?: { defaultLocale: string; locales: string[] } & Record<\n string,\n unknown\n >;\n /* Path to the gt config file */\n configFilePath?: string;\n /* Resolved from package.json */\n entryPointFilePath?: string;\n /* Polyfills to exclude */\n excludePolyfills?: Polyfill[];\n /* Polyfills that should bypass runtime capability detection */\n forcePolyfills?: ForceablePolyfill[];\n}\n"],"mappings":";AAAA,MAAa,YAAY;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAID,MAAa,0BAA0B;CACrC,wCACE;CACF,sCACE;CACF,8CACE;CACH;AAID,MAAa,mBAAmB;CAC9B;CACA;CACA;CACA;CACA;CACA;CACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gt-react-native",
3
- "version": "11.1.8",
3
+ "version": "11.1.9",
4
4
  "description": "An i18n package for React Native",
5
5
  "main": "./dist/commonjs/index.js",
6
6
  "module": "./dist/module/index.js",
@@ -93,7 +93,7 @@
93
93
  "@formatjs/intl-pluralrules": "^5.4.6",
94
94
  "@formatjs/intl-relativetimeformat": "^11.4.13",
95
95
  "@generaltranslation/format": "0.1.5",
96
- "@generaltranslation/react-core": "11.1.8",
96
+ "@generaltranslation/react-core": "11.1.9",
97
97
  "@generaltranslation/supported-locales": "2.1.17",
98
98
  "generaltranslation": "9.1.4",
99
99
  "gt-i18n": "1.0.14"
@@ -1,8 +1,38 @@
1
1
  import * as path from 'path';
2
2
  import type { PluginObj, types } from '@babel/core';
3
- import { LOCALE_POLYFILLS, POLYFILLS, type PluginOptions } from './types';
3
+ import {
4
+ FORCED_POLYFILL_IMPORTS,
5
+ LOCALE_POLYFILLS,
6
+ POLYFILLS,
7
+ type ForceablePolyfill,
8
+ type Polyfill,
9
+ type PluginOptions,
10
+ } from './types';
4
11
  import { resolveLocales } from './utils/resolveLocales';
5
12
 
13
+ function isForceablePolyfill(
14
+ polyfill: Polyfill
15
+ ): polyfill is ForceablePolyfill {
16
+ return polyfill in FORCED_POLYFILL_IMPORTS;
17
+ }
18
+
19
+ function getPolyfillImport(
20
+ polyfill: Polyfill,
21
+ forcePolyfills: PluginOptions['forcePolyfills']
22
+ ): string {
23
+ if (isForceablePolyfill(polyfill) && forcePolyfills?.includes(polyfill)) {
24
+ return FORCED_POLYFILL_IMPORTS[polyfill];
25
+ }
26
+
27
+ return polyfill;
28
+ }
29
+
30
+ function getPolyfillAliases(polyfill: Polyfill): string[] {
31
+ return isForceablePolyfill(polyfill)
32
+ ? [polyfill, FORCED_POLYFILL_IMPORTS[polyfill]]
33
+ : [polyfill];
34
+ }
35
+
6
36
  export function plugin(
7
37
  babel: { types: typeof types },
8
38
  {
@@ -11,6 +41,7 @@ export function plugin(
11
41
  configFilePath,
12
42
  entryPointFilePath = path.resolve(process.cwd(), 'src', 'App.tsx'),
13
43
  excludePolyfills = [],
44
+ forcePolyfills = [],
14
45
  }: PluginOptions
15
46
  ): PluginObj {
16
47
  const { types: t } = babel;
@@ -35,27 +66,34 @@ export function plugin(
35
66
  });
36
67
 
37
68
  // TODO: smart imports based on if the polyfill is required, do this as a wrapper around AppRegistry.registerComponent()
38
- const imports = [
39
- ...POLYFILLS.filter(
40
- (polyfill) => !excludePolyfills.includes(polyfill)
41
- ),
42
- ...resolvedLocales.flatMap((locale) =>
43
- LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`)
44
- ),
45
- ];
69
+ const polyfillImports = POLYFILLS.filter(
70
+ (polyfill) => !excludePolyfills.includes(polyfill)
71
+ ).map((polyfill) => ({
72
+ aliases: getPolyfillAliases(polyfill),
73
+ source: getPolyfillImport(polyfill, forcePolyfills),
74
+ }));
75
+ const localeImports = resolvedLocales.flatMap((locale) =>
76
+ LOCALE_POLYFILLS.map((localeData) => `${localeData}/${locale}`)
77
+ );
46
78
 
47
79
  const existingImports = new Set<string>();
48
80
  programPath.node.body.forEach((node) => {
49
81
  if (
50
82
  t.isImportDeclaration(node) &&
51
- typeof node.source.value === 'string' &&
52
- imports.includes(node.source.value)
83
+ typeof node.source.value === 'string'
53
84
  ) {
54
85
  existingImports.add(node.source.value);
55
86
  }
56
87
  });
57
88
 
58
- const importsToAdd = imports.filter((imp) => !existingImports.has(imp));
89
+ const importsToAdd = [
90
+ ...polyfillImports
91
+ .filter(({ aliases }) =>
92
+ aliases.every((alias) => !existingImports.has(alias))
93
+ )
94
+ .map(({ source }) => source),
95
+ ...localeImports.filter((source) => !existingImports.has(source)),
96
+ ];
59
97
 
60
98
  if (importsToAdd.length > 0) {
61
99
  const newImports = importsToAdd.map((importPath) =>
@@ -10,6 +10,19 @@ export const POLYFILLS = [
10
10
  '@formatjs/intl-datetimeformat/add-all-tz',
11
11
  ] as const;
12
12
 
13
+ export type Polyfill = (typeof POLYFILLS)[number];
14
+
15
+ export const FORCED_POLYFILL_IMPORTS = {
16
+ '@formatjs/intl-displaynames/polyfill':
17
+ '@formatjs/intl-displaynames/polyfill-force',
18
+ '@formatjs/intl-listformat/polyfill':
19
+ '@formatjs/intl-listformat/polyfill-force',
20
+ '@formatjs/intl-relativetimeformat/polyfill':
21
+ '@formatjs/intl-relativetimeformat/polyfill-force',
22
+ } as const satisfies Partial<Record<Polyfill, string>>;
23
+
24
+ export type ForceablePolyfill = keyof typeof FORCED_POLYFILL_IMPORTS;
25
+
13
26
  export const LOCALE_POLYFILLS = [
14
27
  `@formatjs/intl-displaynames/locale-data`,
15
28
  `@formatjs/intl-listformat/locale-data`,
@@ -32,5 +45,7 @@ export interface PluginOptions {
32
45
  /* Resolved from package.json */
33
46
  entryPointFilePath?: string;
34
47
  /* Polyfills to exclude */
35
- excludePolyfills?: (typeof POLYFILLS)[number][];
48
+ excludePolyfills?: Polyfill[];
49
+ /* Polyfills that should bypass runtime capability detection */
50
+ forcePolyfills?: ForceablePolyfill[];
36
51
  }