Skip to content

Instantly share code, notes, and snippets.

@chrono-meter
Created November 22, 2024 13:51
Show Gist options
  • Select an option

  • Save chrono-meter/e92920515b010d01bd5d32d2facf21d0 to your computer and use it in GitHub Desktop.

Select an option

Save chrono-meter/e92920515b010d01bd5d32d2facf21d0 to your computer and use it in GitHub Desktop.
How to use `@wordpress/react-i18n`.
/**
* Even though some module is not included in WordPress core, `wp-scripts` treats it as an external module.
*
* The following code will cause an error like: `Uncaught TypeError: Cannot read properties of undefined (reading 'I18nProvider')`.
* ```js
* import { I18nProvider } from '@wordpress/react-i18n';
* ```
*/
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
const notWpCoreBundledModules = [
'@wordpress/react-i18n',
];
defaultConfig.plugins = [
...defaultConfig.plugins.filter(plugin => plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'),
new DependencyExtractionWebpackPlugin({
requestToExternalModule(request) {
if (notWpCoreBundledModules.includes(request)) {
return false;
}
// With `useDefaults: true`, `return;` means default behavior.
},
requestToExternal(request) {
if (notWpCoreBundledModules.includes(request)) {
return false;
}
// With `useDefaults: true`, `return;` means default behavior.
},
}),
];
module.exports = defaultConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment