@discord/metro-intl-transformer 0.2.0 → 0.2.1-canary.178a94c

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.
Files changed (2) hide show
  1. package/babel-plugin.js +21 -12
  2. package/package.json +2 -2
package/babel-plugin.js CHANGED
@@ -8,32 +8,41 @@ const { isMessageDefinitionsFile, hashMessageKey } = require('@discord/intl-load
8
8
  * `.messages.js` files are considered and affected.
9
9
  *
10
10
  * @param {any} babel - The Babel core object.
11
+ * @param {{
12
+ * extraImports: Record<string, string[]>
13
+ * }} options
11
14
  * @returns {{visitor: import("babel__traverse").Visitor}} A visitor object for the Babel transform.
12
15
  */
13
- module.exports = function metroIntlTransformerPlugin(babel) {
16
+ module.exports = function metroIntlTransformerPlugin(babel, options) {
14
17
  /** @type {{types: import("@babel/types")}} */
15
18
  const { types: t } = babel;
19
+ const { extraImports } = options;
16
20
 
17
21
  return {
18
22
  visitor: {
19
23
  ImportDeclaration(path, _state) {
20
24
  const importSource = path.node.source.value;
25
+ const isDefinition = isMessageDefinitionsFile(importSource);
26
+ const extraImportSpecifiers = extraImports[importSource] ?? [];
21
27
  // This transformer only handles usages of intl messages, so only
22
- // imports of definitions files need to be handled.
23
- if (!isMessageDefinitionsFile(importSource)) {
28
+ // imports of definitions files and configured extra specifiers need to
29
+ // be handled.
30
+ if (!isDefinition && extraImportSpecifiers.length === 0) {
24
31
  return;
25
32
  }
26
33
 
27
- const defaultImport = path.node.specifiers.find(
28
- (specifier) => specifier.type === 'ImportDefaultSpecifier',
29
- );
30
- const bindingName = defaultImport?.local.name;
31
- if (bindingName == null) {
32
- return;
33
- }
34
+ const specifiers = path.node.specifiers
35
+ .filter(
36
+ (specifier) =>
37
+ (isDefinition && specifier.type === 'ImportDefaultSpecifier') ||
38
+ extraImportSpecifiers.includes(specifier.local.name),
39
+ )
40
+ .map((specifier) => specifier.local.name);
34
41
 
35
- const binding = path.scope.bindings[bindingName];
36
- for (const reference of binding.referencePaths) {
42
+ const bindingReferences = specifiers.flatMap(
43
+ (binding) => path.scope.bindings[binding]?.referencePaths ?? [],
44
+ );
45
+ for (const reference of bindingReferences) {
37
46
  const parent = reference.parent;
38
47
  // We only care about member expressions that use the `.` syntax or
39
48
  // use string literals, since other syntaxes could be invalid.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/metro-intl-transformer",
3
- "version": "0.2.0",
3
+ "version": "0.2.1-canary.178a94c",
4
4
  "license": "MIT",
5
5
  "description": "Metro/Babel transformer plugin for compiling, bundling, and minifying intl message definitions and usages using intl-message-database.",
6
6
  "exports": {
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "debug": "^4.3.6",
13
- "@discord/intl-loader-core": "0.2.0"
13
+ "@discord/intl-loader-core": "0.2.1-rc.2"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@babel/types": "^7.24.9",