Skip to content

Commit

Permalink
refactor: add i18nAst flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ragafus committed Dec 9, 2024
1 parent c603ce7 commit a32f3df
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
8 changes: 6 additions & 2 deletions packages/babel-preset-mc-app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 6 additions & 2 deletions packages/mc-scripts/src/commands/build-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
],
],
Expand Down
8 changes: 6 additions & 2 deletions packages/mc-scripts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit a32f3df

Please sign in to comment.