-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `babel-plugin-formatjs` to `babel-preset-mc-app` and `mc-scripts` # Conflicts: # pnpm-lock.yaml * refactor: add `i18nAst` flag * test: add babel-plugin-formatjs test cases # Conflicts: # pnpm-lock.yaml * fix: describe opt-in flags
- Loading branch information
Showing
8 changed files
with
385 additions
and
85 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/babel-preset-mc-app': minor | ||
'@commercetools-frontend/mc-scripts': minor | ||
--- | ||
|
||
Add `babel-plugin-formatjs` to avoid bloating bundles with useless data from `formatjs` messages (`description`, `defaultMessage` props). Opt-in flags: | ||
|
||
- i18nAst: pre-parse defaultMessage into AST for faster runtime perf | ||
- i18nRemoveDefaultMessage: remove defaultMessage field in generated js after extraction |
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,94 @@ | ||
import { transformSync } from '@babel/core'; | ||
import getBabePresetConfigForMcAppForProduction from './production'; | ||
|
||
describe('babel-plugin-formatjs', () => { | ||
const code = ` | ||
import { defineMessages } from 'react-intl'; | ||
const messages = defineMessages({ | ||
welcome: { | ||
id: 'app.welcome', | ||
defaultMessage: 'Welcome, {name}!', | ||
description: 'Message to greet the user by name', | ||
}, | ||
}); | ||
`; | ||
|
||
it('should remove description by default', () => { | ||
const config = getBabePresetConfigForMcAppForProduction(null); | ||
|
||
const result = transformSync(code, { | ||
filename: 'dummy-file-name.js', | ||
presets: config.presets, | ||
plugins: config.plugins, | ||
}); | ||
|
||
expect(result.code).toMatchInlineSnapshot(` | ||
""use strict"; | ||
var _reactIntl = require("react-intl"); | ||
const messages = (0, _reactIntl.defineMessages)({ | ||
welcome: { | ||
id: "app.welcome", | ||
defaultMessage: "Welcome, {name}!" | ||
} | ||
});" | ||
`); | ||
}); | ||
|
||
it('should remove defaultMessage when i18nRemoveDefaultMessage is true', () => { | ||
const config = getBabePresetConfigForMcAppForProduction(null, { | ||
i18nRemoveDefaultMessage: true, | ||
}); | ||
|
||
const result = transformSync(code, { | ||
filename: 'dummy-file-name.js', | ||
presets: config.presets, | ||
plugins: config.plugins, | ||
}); | ||
|
||
expect(result.code).toMatchInlineSnapshot(` | ||
""use strict"; | ||
var _reactIntl = require("react-intl"); | ||
const messages = (0, _reactIntl.defineMessages)({ | ||
welcome: { | ||
id: "app.welcome" | ||
} | ||
});" | ||
`); | ||
}); | ||
|
||
it('should parse defaultMessage into AST when i18nAst is true', () => { | ||
const config = getBabePresetConfigForMcAppForProduction(null, { | ||
i18nAst: true, | ||
}); | ||
|
||
const result = transformSync(code, { | ||
filename: 'dummy-file-name.js', | ||
presets: config.presets, | ||
plugins: config.plugins, | ||
}); | ||
|
||
expect(result.code).toMatchInlineSnapshot(` | ||
""use strict"; | ||
var _reactIntl = require("react-intl"); | ||
const messages = (0, _reactIntl.defineMessages)({ | ||
welcome: { | ||
id: "app.welcome", | ||
defaultMessage: [{ | ||
"type": 0, | ||
"value": "Welcome, " | ||
}, { | ||
"type": 1, | ||
"value": "name" | ||
}, { | ||
"type": 0, | ||
"value": "!" | ||
}] | ||
} | ||
});" | ||
`); | ||
}); | ||
}); |
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
Oops, something went wrong.