-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomposeWebpackConfig.ts
114 lines (110 loc) · 2.93 KB
/
composeWebpackConfig.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as webpack from 'webpack';
import * as pathUtils from 'path';
import { merge } from 'webpack-merge';
const BLACKLISTED_PLUGINS = ['ManifestPlugin'];
const composeWebpackConfig = (
baseConfig: webpack.Configuration,
entry: webpack.Entry,
executionPath: string,
decoratorFile: string | undefined,
getSbOption: <T>(key: string, fallbackValue: T) => T,
publicPath: string = '__bojagi_public_path__/'
): webpack.Configuration => {
const { entry: baseEntry, plugins, ...baseConfigWithoutEntry } = baseConfig;
const filteredPlugins = plugins?.filter(
plugin => !BLACKLISTED_PLUGINS.includes(plugin.constructor?.name)
);
return merge(
// Bojagi decorator
{
module: {
rules: decoratorFile
? [
{
test: pathUtils.resolve(executionPath, decoratorFile),
use: [
{
loader: `bojagi-expose-loader`,
options: {
symbol: 'bojagiDecorator',
},
},
],
},
]
: [],
},
},
// Get Globals
{
module: {
rules: [
{
test: pathUtils.resolve(__dirname, '../storybook/getGlobals.js'),
use: [
{
loader: `bojagi-expose-loader`,
options: {
symbol: 'bojagiSbGlobals',
},
},
],
},
],
},
},
{
...baseConfigWithoutEntry,
plugins: filteredPlugins,
},
{
entry,
output: {
path: pathUtils.join(process.cwd(), 'bojagi'),
filename: '[name].js',
publicPath,
globalObject: 'window',
},
resolve: {
alias: {
'storybook-folder': pathUtils.resolve(getSbOption('configDir', './.storybook') || ''),
},
},
resolveLoader: {
alias: {
'component-extract-loader': pathUtils.resolve(__dirname, 'componentExtractLoader'),
'bojagi-expose-loader': pathUtils.resolve(__dirname, 'exposeLoader'),
reactDom: 'react-dom',
},
},
externals: {
react: 'react',
'react-dom': 'reactDom',
},
optimization: {
minimize: true,
concatenateModules: false,
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
plugins: [
new webpack.NormalModuleReplacementPlugin(
/@storybook\/addons/,
require.resolve('@bojagi/cli/fakeStorybookAddons.js')
),
new webpack.NormalModuleReplacementPlugin(
/@storybook\/react$/,
require.resolve('@bojagi/cli/fakeStorybookReact.js')
),
],
}
);
};
export default composeWebpackConfig;