-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
68 lines (59 loc) · 2.13 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Configuration } from 'webpack';
import { Options as SWCLoaderOptions, JsMinifyOptions } from '@swc/core';
import { Options as StorybookOptions } from '@storybook/core-common';
import merge from 'deepmerge';
import { replaceLoader, replaceMinimizer, disableSourceMap } from './transformers';
export interface StoryBookAddonSwcOptions {
enable: boolean;
enableSwcLoader: boolean;
enableSwcMinify: boolean;
swcLoaderOptions: SWCLoaderOptions;
swcMinifyOptions: JsMinifyOptions;
}
const isProduction = process.env.NODE_ENV === 'production';
const defaultOptions: StoryBookAddonSwcOptions = {
enable: true,
enableSwcLoader: true,
enableSwcMinify: true,
swcLoaderOptions: {
isModule: 'unknown',
sourceMaps: !isProduction,
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
transform: {
react: {
runtime: 'automatic',
},
},
},
},
swcMinifyOptions: {
compress: {
inline: 0,
},
},
};
const includeSwcConfig = (config: Configuration, options: StoryBookAddonSwcOptions): Configuration => {
if (!options.enable) return config;
const transformers = [];
if (options.enableSwcLoader) transformers.push(replaceLoader(options.swcLoaderOptions));
if (options.enableSwcMinify) transformers.push(replaceMinimizer(options.swcMinifyOptions));
if (!options.swcLoaderOptions.sourceMaps) transformers.push(disableSourceMap);
return transformers.reduce((previous, current) => {
return current(previous);
}, config);
};
const addonFilePattern = /storybook-addon-swc(.(cjs|esm))?.js/;
const getAddonOptions = (options: StorybookOptions): StoryBookAddonSwcOptions => {
const addonOptions = options.presetsList?.find(preset => addonFilePattern.test(preset.name)).options ?? {};
return merge(defaultOptions, addonOptions);
};
export const webpack = (config: Configuration, options: StorybookOptions): Configuration => {
return includeSwcConfig(config, getAddonOptions(options));
};
export const managerWebpack = (config: Configuration, options: StorybookOptions): Configuration => {
return includeSwcConfig(config, getAddonOptions(options));
};