-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
framework-preset-preact.ts
51 lines (48 loc) · 1.82 KB
/
framework-preset-preact.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import path from 'path';
import type { TransformOptions } from '@babel/core';
import type { Configuration } from 'webpack';
import { findDistEsm } from '@storybook/core-common';
import type { StorybookConfig } from '@storybook/core-common';
export function babelDefault(config: TransformOptions): TransformOptions {
return {
...config,
plugins: [
[
require.resolve('@babel/plugin-transform-react-jsx'),
{ importSource: 'preact', runtime: 'automatic' },
],
...config.plugins,
],
};
}
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: {
...config.resolve,
alias: {
...config.resolve.alias,
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')),
},
},
};
}
export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
};