-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(message-loader): add a simple plugin for vite (#3511)
- Loading branch information
1 parent
454c829
commit bcdd553
Showing
6 changed files
with
45 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@commercetools-frontend/mc-scripts': minor | ||
--- | ||
|
||
Adds a `vite` plugin to compile i18n messages when building | ||
|
||
When both `ENABLE_EXPERIMENTAL_VITE_BUNDLER` and `ENABLE_I18N_MESSAGE_COMPILATION` are set to `true` on the `process.env`, `vite-plugin-i18n-message-compilation` will transform any file matching `**/i18n/data/*.json` using the `compile` function from `@formatjs/cli-lib` when building. | ||
|
||
Previously added `ENABLE_WEBPACK_LOADER_I18N_MESSAGE_COMPILATION` has been renamed to `ENABLE_I18N_MESSAGE_COMPILATION` for consistency. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type TMessageFormat = 'simple' | 'transifex'; | ||
|
||
function getI18nMessageFormat(source: string): TMessageFormat { | ||
const messageContents = JSON.parse(source); | ||
const firstValue = Object.values(messageContents)[0]; | ||
return typeof firstValue === 'string' ? 'simple' : 'transifex'; | ||
} | ||
|
||
export default getI18nMessageFormat; |
22 changes: 22 additions & 0 deletions
22
packages/mc-scripts/src/vite-plugins/vite-plugin-i18n-message-compilation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { compile } from '@formatjs/cli-lib'; | ||
import { createFilter } from '@rollup/pluginutils'; | ||
import type { Plugin } from 'vite'; | ||
import getI18nMessageFormat from '../utils/get-i18n-message-format'; | ||
|
||
function vitePluginI18nMessageCompilation(): Plugin { | ||
const filter = createFilter('**/i18n/data/*.json'); | ||
return { | ||
name: 'vite-plugin-i18n-message-compilation', | ||
enforce: 'pre', | ||
async transform(code, id) { | ||
if (process.env.ENABLE_I18N_MESSAGE_COMPILATION && filter(id)) { | ||
const format = getI18nMessageFormat(code); | ||
const res = await compile([id], { ast: true, format }); | ||
return { code: res, map: null }; | ||
} | ||
return null; | ||
}, | ||
}; | ||
} | ||
|
||
export default vitePluginI18nMessageCompilation; |
7 changes: 2 additions & 5 deletions
7
packages/mc-scripts/src/webpack-loaders/i18n-message-compilation-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters