diff --git a/packages/babel-preset/intl-preset.js b/packages/babel-preset/intl-preset.js index b86d3af3..d6d2eb09 100644 --- a/packages/babel-preset/intl-preset.js +++ b/packages/babel-preset/intl-preset.js @@ -3,6 +3,30 @@ const path = require('path'); const { interpolateName } = require('@formatjs/ts-transformer'); const intlPlugin = require('babel-plugin-formatjs').default; +const stringify = require('fast-json-stable-stringify'); + +function mergeDuplicates(messages) { + const seen = new Map(); + + return messages.filter((message) => { + const other = seen.get(message.id); + + seen.set(message.id, message); + + if (!other) return true; + + if ( + stringify(message.description) !== stringify(other.description) || + message.defaultMessage !== other.defaultMessage + ) { + throw new Error( + `Duplicate message id: "${message.id}", but the \`description\` and/or \`defaultMessage\` are different.`, + ); + } + // if they are the same, filter this one out + return false; + }); +} module.exports = function intlPreset(_, options = {}) { const { @@ -56,6 +80,8 @@ module.exports = function intlPreset(_, options = {}) { mkdirSync(path.join(messagesDir, dir), { recursive: true }); + messages = mergeDuplicates(messages); + writeFileSync( path.join(messagesDir, dir, `${name}.json`), JSON.stringify(messages, null, 2), diff --git a/packages/babel-preset/package.json b/packages/babel-preset/package.json index 137ed86a..48f30a35 100644 --- a/packages/babel-preset/package.json +++ b/packages/babel-preset/package.json @@ -37,6 +37,7 @@ "babel-plugin-formatjs": "^10.3.25", "babel-plugin-polyfill-corejs3": "^0.5.3", "browserslist": "^4.21.3", + "fast-json-stable-stringify": "^2.1.0", "lodash": "^4.17.21" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index 750490d8..642bccc0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -21,15 +21,6 @@ text-table "^0.2.0" yargs "^17.1.1" -"@4c/jest-preset@^1.8.0": - version "1.8.0" - resolved "https://registry.yarnpkg.com/@4c/jest-preset/-/jest-preset-1.8.0.tgz#79e1fa5dfefa642808c6d65ab7908ea4e9394ec6" - integrity sha512-qBrBrbR/YiOleNJErwaMqCP+cw4YGqXF3R+EIgLg/La28sf9fuhCaYjN9OrUBLjKwF6HkdMfPrd7M0iSV9NPHA== - dependencies: - babel-jest "^28.1.3" - find-up "^5.0.0" - find-yarn-workspace-root "^2.0.0" - "@ampproject/remapping@^2.1.0": version "2.2.0" resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.0.tgz#56c133824780de3174aed5ab6834f3026790154d" @@ -4626,7 +4617,7 @@ fast-glob@^3.2.9: merge2 "^1.3.0" micromatch "^4.0.4" -fast-json-stable-stringify@^2.0.0: +fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633" integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==