diff --git a/packages/babel-preset-mc-app/create.js b/packages/babel-preset-mc-app/create.js index 7f1dbf31e3..ba9fbcf426 100644 --- a/packages/babel-preset-mc-app/create.js +++ b/packages/babel-preset-mc-app/create.js @@ -22,9 +22,12 @@ const defaultOptions = { // it explicitely. This will disable `core-js` for `preset-env` and the // `plugin-transform-runtime`. disableCoreJs: false, + // If `formatjs` should pre-parse defaultMessage into AST. + // https://formatjs.github.io/docs/tooling/babel-plugin/#ast + i18nAst: false, // If `formatjs` default messages should be removed from the bundle or not. // https://formatjs.github.io/docs/tooling/babel-plugin#removedefaultmessage - removeI18nDefaultMessage: false, + i18nRemoveDefaultMessage: false, }; /* eslint-disable global-require */ @@ -195,7 +198,8 @@ module.exports = function createBabePresetConfigForMcApp(api, opts = {}, env) { [ require('babel-plugin-formatjs').default, { - removeDefaultMessage: options.removeI18nDefaultMessage, + ast: options.i18nAst, + removeDefaultMessage: options.i18nRemoveDefaultMessage, }, ], ].filter(Boolean), diff --git a/packages/mc-scripts/src/commands/build-vite.ts b/packages/mc-scripts/src/commands/build-vite.ts index 4f2ef9a9b1..f0558911c9 100644 --- a/packages/mc-scripts/src/commands/build-vite.ts +++ b/packages/mc-scripts/src/commands/build-vite.ts @@ -86,9 +86,13 @@ async function run() { 'babel-plugin-formatjs', { removeDefaultMessage: - // Allow to remove default `formatjs` messages from bundles. + // Remove default `formatjs` messages from bundles. // TODO: make it a CLI option when Vite support becomes stable. - process.env.ENABLE_REMOVE_I18N_DEFAULT_MESSAGE === 'true', + process.env.ENABLE_I18N_REMOVE_DEFAULT_MESSAGE === 'true', + ast: + // Enable pre-parse default `formatjs` messages into AST. + // TODO: make it a CLI option when Vite support becomes stable. + process.env.ENABLE_I18N_AST === 'true', }, ], ], diff --git a/packages/mc-scripts/src/config/create-webpack-config-for-production.ts b/packages/mc-scripts/src/config/create-webpack-config-for-production.ts index 754b0e5b57..114325c995 100644 --- a/packages/mc-scripts/src/config/create-webpack-config-for-production.ts +++ b/packages/mc-scripts/src/config/create-webpack-config-for-production.ts @@ -40,8 +40,10 @@ const defaultToggleFlags: TWebpackConfigToggleFlagsForProduction = { // it explicitely. This will disable `core-js` for `preset-env` and the // `plugin-transform-runtime`. disableCoreJs: false, - // Allow to remove default `formatjs` messages from bundles. - removeI18nDefaultMessage: false, + // Pre-parse default `formatjs` messages into AST + i18nAst: false, + // Remove default `formatjs` messages from bundles. + i18nRemoveDefaultMessage: false, }; const defaultOptions: TWebpackConfigOptions<'production'> = { entryPoint: paths.entryPoint, @@ -257,8 +259,9 @@ function createWebpackConfigForProduction( { runtime: hasJsxRuntime() ? 'automatic' : 'classic', disableCoreJs: mergedOptions.toggleFlags.disableCoreJs, - removeI18nDefaultMessage: - mergedOptions.toggleFlags.removeI18nDefaultMessage, + i18nAst: mergedOptions.toggleFlags.i18nAst, + i18nRemoveDefaultMessage: + mergedOptions.toggleFlags.i18nRemoveDefaultMessage, }, ], ], diff --git a/packages/mc-scripts/src/types.ts b/packages/mc-scripts/src/types.ts index a1c177ec48..608901c808 100644 --- a/packages/mc-scripts/src/types.ts +++ b/packages/mc-scripts/src/types.ts @@ -60,9 +60,13 @@ export type TWebpackConfigToggleFlagsForDevelopment = { */ disableCoreJs?: boolean; /** - * Allow to remove default `formatjs` messages from bundles. + * Pre-parse default `formatjs` messages into AST */ - removeI18nDefaultMessage?: boolean; + i18nAst?: boolean; + /** + * Remove default `formatjs` messages from bundles. + */ + i18nRemoveDefaultMessage?: boolean; }; export type TWebpackConfigToggleFlagsForProduction =