@discord/metro-intl-transformer 0.6.1 → 0.7.0

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/asset-plugin.js CHANGED
@@ -48,7 +48,7 @@ async function fetchConfig() {
48
48
  let metroConfig;
49
49
  try {
50
50
  metroConfig = require(metroConfigPath);
51
- debug(`Successfully loaded config: %o`, metroConfig);
51
+ debug(`Successfully loaded config metro config`);
52
52
  } catch {
53
53
  debug(`Failed to load Metro configuration. Using defaults instead.`);
54
54
  return defaultConfig;
@@ -56,8 +56,10 @@ async function fetchConfig() {
56
56
 
57
57
  const config = metroConfig.transformer?.intlAssetPlugin;
58
58
  if (config == null) {
59
+ debug('asset-plugin configuration was empty. Using defaults: %O', defaultConfig);
59
60
  return defaultConfig;
60
61
  }
62
+ debug(`discovered asset-plugin configuration: %O`, config);
61
63
 
62
64
  if (config.cacheDir != null && path.isAbsolute(config.cacheDir)) {
63
65
  throw new Error(
@@ -79,11 +81,11 @@ async function transformAsset(assetData) {
79
81
  const filename = assetData.files[0] ?? '';
80
82
  // If this isn't a translations file or if it's already a compiled artifact, then we don't want
81
83
  // to do any more processing on it.
82
- if (
83
- filename === '' ||
84
- !isMessageTranslationsFile(filename) ||
85
- /\.compiled.messages\./.test(filename)
86
- ) {
84
+ if (filename === '' || !isMessageTranslationsFile(filename)) {
85
+ return assetData;
86
+ }
87
+ if (/\.compiled.messages\./.test(filename)) {
88
+ debug(`[${filename}] Compiled messages asset needs no further processing`);
87
89
  return assetData;
88
90
  }
89
91
 
package/index.js CHANGED
@@ -37,11 +37,11 @@ function transformToString({
37
37
  `.compiled.messages.${getTranslationAssetExtension()}`,
38
38
  );
39
39
 
40
- debug('Locale map created: %O', result.translationsLocaleMap);
41
40
  debug(
42
41
  `[${filename}] Resolving source file to compiled translations file ${compiledSourcePath}`,
43
42
  );
44
43
  result.translationsLocaleMap['en-US'] = compiledSourcePath;
44
+ debug('Locale map created: %O', result.translationsLocaleMap);
45
45
 
46
46
  return new MessageDefinitionsTransformer({
47
47
  messageKeys: result.messageKeys,
package/package.json CHANGED
@@ -1,21 +1,18 @@
1
1
  {
2
2
  "name": "@discord/metro-intl-transformer",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
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": {
7
7
  ".": "./index.js",
8
- "./asset-plugin": "./asset-plugin.js",
9
- "./babel-plugin": "./babel-plugin.js",
10
- "./babel-traverse-message-accesses": "./babel-traverse-message-accesses.js"
8
+ "./asset-plugin": "./asset-plugin.js"
11
9
  },
12
10
  "dependencies": {
13
11
  "debug": "^4.3.6",
14
- "@discord/intl-loader-core": "0.6.1"
12
+ "@discord/babel-plugin-transform-discord-intl": "0.7.0",
13
+ "@discord/intl-loader-core": "0.7.0"
15
14
  },
16
15
  "devDependencies": {
17
- "@babel/types": "^7.24.9",
18
- "@types/babel__traverse": "7.20.6",
19
16
  "@types/debug": "^4.1.12"
20
17
  }
21
18
  }
package/babel-plugin.js DELETED
@@ -1,35 +0,0 @@
1
- const { hashMessageKey } = require('@discord/intl-loader-core');
2
- const { traverseMessageAccesses } = require('./babel-traverse-message-accesses');
3
-
4
- /**
5
- * Babel plugin for obfuscating and minifying intl message _usages_, like
6
- * turning `intl.format(t.SOME_LONG_MESSAGE_KEY_NAME)` into `intl.format(t['f21c/3'])`.
7
- * This transform gets applied to every file in the project, but only member
8
- * expressions of imports from `.messages.js` files are considered and affected.
9
- *
10
- * Configuration for `extraImports` differs from swc-intl-message-transformer in that
11
- * paths here are resolved, _absolute_ paths to the desired file, since babel
12
- * will often have re-written the AST to resolve import aliases by the time
13
- * this plugin runs.
14
- *
15
- * @param {any} babel - The Babel core object.
16
- * @returns {{visitor: import("babel__traverse").Visitor}} A visitor object for the Babel transform.
17
- */
18
- module.exports = function metroIntlTransformerPlugin(babel) {
19
- /** @type {{types: import("@babel/types")}} */
20
- const { types: t } = babel;
21
-
22
- return {
23
- visitor: traverseMessageAccesses((access, messageName) => {
24
- if (messageName == null) {
25
- throw new Error(
26
- '[INTL] Encountered a member expression with neither an identifier nor string literal member node',
27
- );
28
- }
29
-
30
- // Then hash it up and re-write the member with the hashed version.
31
- access.computed = true;
32
- access.property = t.stringLiteral(hashMessageKey(messageName));
33
- }),
34
- };
35
- };
@@ -1,77 +0,0 @@
1
- const nativePath = require('node:path');
2
- const { isMessageDefinitionsFile } = require('@discord/intl-loader-core');
3
-
4
- /**
5
- * Babel visitor for traversing all intl message accesses in a file. This visitor can be applied to
6
- * every file in a project, but only member expressions of imports from `.messages.js` files and any
7
- * imports as specified in the `extraImports` configuration are considered and affected.
8
- *
9
- * Configuration for `extraImports` differs from swc-intl-message-transformer in that
10
- * paths here are resolved, _absolute_ paths to the desired file, since babel
11
- * will often have re-written the AST to resolve import aliases by the time
12
- * this plugin runs.
13
- *
14
- * @param {(node: import('@babel/types').MemberExpression, messageName: string | undefined) => void} callback
15
- * Callback invoked for every message access that's encountered.
16
- * @returns {import("babel__traverse").Visitor} A visitor object for a Babel transform.
17
- */
18
- function traverseMessageAccesses(callback) {
19
- return {
20
- ImportDeclaration(path, state) {
21
- const importSource = nativePath.resolve(
22
- // @ts-expect-error state is untyped but contains `file`
23
- nativePath.dirname(state.file.opts.filename),
24
- path.node.source.value,
25
- );
26
- const isDefinition = isMessageDefinitionsFile(importSource);
27
- // @ts-expect-error state is untyped but contains `opts` from the config.
28
- const extraImportSpecifiers = state.opts?.extraImports?.[importSource] ?? [];
29
- // This transformer only handles usages of intl messages, so only
30
- // imports of definitions files and configured extra specifiers need to
31
- // be handled.
32
- if (!isDefinition && extraImportSpecifiers.length === 0) {
33
- return;
34
- }
35
-
36
- const specifiers = path.node.specifiers
37
- .filter(
38
- (specifier) =>
39
- (isDefinition && specifier.type === 'ImportDefaultSpecifier') ||
40
- extraImportSpecifiers.includes(specifier.local.name),
41
- )
42
- .map((specifier) => specifier.local.name);
43
-
44
- const bindingReferences = specifiers.flatMap(
45
- (binding) => path.scope.bindings[binding]?.referencePaths ?? [],
46
- );
47
- for (const reference of bindingReferences) {
48
- const parent = reference.parent;
49
- // We only care about member expressions that use the `.` syntax or
50
- // use string literals, since other syntaxes could be invalid.
51
- if (
52
- parent.type !== 'MemberExpression' ||
53
- (parent.computed && parent.property.type !== 'StringLiteral')
54
- ) {
55
- continue;
56
- }
57
-
58
- // We just want the actual value of the member being accessed.
59
- /** @type {string | undefined} */
60
- const messageName = (() => {
61
- switch (parent.property.type) {
62
- case 'StringLiteral':
63
- return parent.property.value;
64
- case 'Identifier':
65
- return parent.property.name;
66
- default:
67
- return undefined;
68
- }
69
- })();
70
-
71
- callback(parent, messageName);
72
- }
73
- },
74
- };
75
- }
76
-
77
- module.exports = { traverseMessageAccesses };