@discord/metro-intl-transformer 0.11.5 → 0.12.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.
Files changed (2) hide show
  1. package/asset-plugin.js +33 -10
  2. package/package.json +3 -3
package/asset-plugin.js CHANGED
@@ -66,31 +66,51 @@ const defaultConfig = {
66
66
  * the desired configuration for that run of the bundler. If the Metro configuration for the current
67
67
  * process is at a non-default location, this method will likely fail.
68
68
  *
69
- * @returns {Promise<ResolvedIntlAssetPluginConfig>}
69
+ * @returns {Promise<[ResolvedIntlAssetPluginConfig, import('metro-config').ConfigT]>}
70
70
  */
71
71
  async function fetchConfig() {
72
72
  debug(`Loading Metro config`);
73
73
  const metroConfig = await metro.loadConfig();
74
+ const watchFolders = [metroConfig.projectRoot, ...metroConfig.watchFolders];
74
75
  const defaults = {
75
76
  ...defaultConfig,
76
- watchFolders: [metroConfig.projectRoot, ...metroConfig.watchFolders],
77
+ watchFolders,
77
78
  };
78
79
 
79
- // @ts-expect-error We're latching on `intlAssetPlugin` as a field in the transformer config.
80
- const config = metroConfig.transformer?.intlAssetPlugin;
80
+ /** @type {IntlAssetPluginConfig} */
81
+ const config =
82
+ // @ts-expect-error We're latching on `intlAssetPlugin` as a field in the transformer config.
83
+ metroConfig.transformer?.intlAssetPlugin;
81
84
  if (config == null) {
82
85
  debug('asset-plugin configuration was empty. Using defaults: %O', defaults);
83
- return { ...defaults };
86
+ return [{ ...defaults }, metroConfig];
84
87
  }
85
88
  debug(`discovered asset-plugin configuration: %O`, config);
89
+ const resolved = {
90
+ ...defaults,
91
+ ...config,
92
+ };
93
+
94
+ if (resolved.cacheDir == null) {
95
+ throw new Error(
96
+ 'metro-intl-transformer asset-plugin cacheDir configuration resolved to null. Assets cannot be compiled',
97
+ );
98
+ }
86
99
 
87
- if (config.cacheDir != null && path.isAbsolute(config.cacheDir)) {
100
+ const cacheDir = path.resolve(metroConfig.projectRoot, resolved.cacheDir);
101
+ const cacheDirIsWatched = watchFolders.some((folder) => {
102
+ const relative = path.relative(folder, resolved.cacheDir);
103
+ return !relative.startsWith('..');
104
+ });
105
+ if (!cacheDirIsWatched) {
88
106
  throw new Error(
89
- '`cacheDir` configuration for the intl Metro asset-plugin must be a relative path.',
107
+ "metro-intl-transformer cacheDir is not visible to metro's file watching. Assets cannot be used after compilation",
90
108
  );
91
109
  }
92
110
 
93
- return { ...defaults, ...config };
111
+ resolved.cacheDir = cacheDir;
112
+ debug(`Resolved cache directory to ${resolved.cacheDir}`);
113
+ return [{ ...defaults, ...config }, metroConfig];
94
114
  }
95
115
 
96
116
  const pluginConfig = fetchConfig();
@@ -101,7 +121,8 @@ let hasInitializedAllDefinitions = false;
101
121
  * @param {any} assetData
102
122
  */
103
123
  async function transformAsset(assetData) {
104
- const { cacheDir, assetExtension, format, bundleSecrets, watchFolders } = await pluginConfig;
124
+ const [config, metroConfig] = await pluginConfig;
125
+ const { cacheDir, assetExtension, format, bundleSecrets, watchFolders } = config;
105
126
 
106
127
  if (!hasInitializedAllDefinitions) {
107
128
  debug('Initializing database with all messages files within watch folders: %O', watchFolders);
@@ -123,7 +144,9 @@ async function transformAsset(assetData) {
123
144
 
124
145
  debug(`[${filename}] Processing translation asset`);
125
146
 
126
- const outputDir = path.resolve(assetData.fileSystemLocation, cacheDir);
147
+ const relativeLocation = path.relative(metroConfig.projectRoot, assetData.fileSystemLocation);
148
+ const dirHash = btoa(path.dirname(relativeLocation));
149
+ const outputDir = path.resolve(cacheDir, dirHash);
127
150
  const outputName = `${assetData.name}.${assetData.hash}.compiled.messages`;
128
151
  const outputFile = path.join(outputDir, outputName) + '.' + assetExtension;
129
152
  debug(`[${filename}] Output file path: ${outputFile}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/metro-intl-transformer",
3
- "version": "0.11.5",
3
+ "version": "0.12.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": {
@@ -14,8 +14,8 @@
14
14
  "dependencies": {
15
15
  "debug": "^4.3.6",
16
16
  "metro-config": "^0.81.0",
17
- "@discord/babel-plugin-transform-discord-intl": "0.11.5",
18
- "@discord/intl-loader-core": "0.11.5"
17
+ "@discord/babel-plugin-transform-discord-intl": "0.12.0",
18
+ "@discord/intl-loader-core": "0.12.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/debug": "^4.1.12"