Apply correct transform on webpack config

This commit is contained in:
Klemen Slavič 2022-07-22 22:14:55 +02:00
parent 35f56f8c35
commit ea6fbd4af3

View File

@ -18,6 +18,19 @@ export function babelDefault(config: TransformOptions): TransformOptions {
}
export function webpackFinal(config: Configuration): Configuration {
const tsxRule = config.module.rules.find((rule) => (rule.test as RegExp).test?.('main.tsx'));
tsxRule.use = (tsxRule.use as any).map((entry: any) => {
let newPlugins = entry.options.plugins;
if (entry.loader?.includes('babel-loader')) {
newPlugins = (entry.options as any).plugins.map((plugin: any) => {
if (plugin[0]?.includes?.('@babel/plugin-transform-react-jsx')) {
return [plugin[0], { importSource: 'preact', runtime: 'automatic' }];
}
return plugin;
});
}
return { ...entry, options: { ...entry.options, plugins: newPlugins } };
});
return {
...config,
resolve: {
@ -27,6 +40,7 @@ export function webpackFinal(config: Configuration): Configuration {
react: path.dirname(require.resolve('preact/compat/package.json')),
'react-dom/test-utils': path.dirname(require.resolve('preact/test-utils/package.json')),
'react-dom': path.dirname(require.resolve('preact/compat/package.json')),
'react/jsx-runtime': path.dirname(require.resolve('preact/jsx-runtime/package.json')),
},
},
};