-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
iframe-webpack.config.js
140 lines (134 loc) · 4.21 KB
/
iframe-webpack.config.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import path from 'path';
import { DefinePlugin, HotModuleReplacementPlugin, ProgressPlugin } from 'webpack';
import Dotenv from 'dotenv-webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
import WatchMissingNodeModulesPlugin from 'react-dev-utils/WatchMissingNodeModulesPlugin';
import TerserWebpackPlugin from 'terser-webpack-plugin';
import CoreJSUpgradeWebpackPlugin from 'corejs-upgrade-webpack-plugin';
import VirtualModulePlugin from 'webpack-virtual-modules';
import resolveFrom from 'resolve-from';
import babelLoader from '../common/babel-loader';
import { loadEnv, nodeModulesPaths } from '../config/utils';
import { getPreviewBodyHtml, getPreviewHeadHtml } from '../utils/template';
import { toRequireContextString } from './to-require-context';
const reactPaths = {};
try {
reactPaths.react = path.dirname(resolveFrom(process.cwd(), 'react/package.json'));
reactPaths['react-dom'] = path.dirname(resolveFrom(process.cwd(), 'react-dom/package.json'));
} catch (e) {
//
}
export default ({
configDir,
babelOptions,
entries,
stories,
outputDir = path.join('.', 'public'),
quiet,
packageJson,
configType,
framework,
}) => {
const { raw, stringified } = loadEnv({ production: true });
const isProd = configType === 'PRODUCTION';
return {
mode: isProd ? 'production' : 'development',
bail: isProd,
devtool: '#cheap-module-source-map',
entry: entries,
output: {
path: path.resolve(process.cwd(), outputDir),
filename: '[name].[hash].bundle.js',
publicPath: '',
},
plugins: [
stories && stories.length
? new VirtualModulePlugin({
[path.resolve(path.join(configDir, `generated-entry.js`))]: `
import { configure } from '@storybook/${framework}';
module._StorybookPreserveDecorators = true;
configure([${stories.map(toRequireContextString).join(',')}
], module);
`,
})
: null,
new HtmlWebpackPlugin({
filename: `iframe.html`,
chunksSortMode: 'none',
alwaysWriteToDisk: true,
inject: false,
templateParameters: (compilation, files, options) => ({
compilation,
files,
options,
version: packageJson.version,
globals: {},
headHtmlSnippet: getPreviewHeadHtml(configDir, process.env),
dlls: [],
bodyHtmlSnippet: getPreviewBodyHtml(configDir, process.env),
}),
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
template: require.resolve(`../templates/index.ejs`),
}),
new DefinePlugin({
'process.env': stringified,
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
}),
isProd ? null : new WatchMissingNodeModulesPlugin(nodeModulesPaths),
isProd ? null : new HotModuleReplacementPlugin(),
new CaseSensitivePathsPlugin(),
quiet ? null : new ProgressPlugin(),
new Dotenv({ silent: true }),
new CoreJSUpgradeWebpackPlugin({ resolveFrom: __dirname }),
].filter(Boolean),
module: {
rules: [
babelLoader(babelOptions),
{
test: /\.md$/,
use: [
{
loader: require.resolve('raw-loader'),
},
],
},
],
},
resolve: {
extensions: ['.mjs', '.js', '.jsx', '.json'],
modules: ['node_modules'].concat(raw.NODE_PATH || []),
alias: {
'babel-runtime/core-js/object/assign': require.resolve('core-js/es/object/assign'),
...reactPaths,
},
},
optimization: {
splitChunks: {
chunks: 'all',
},
runtimeChunk: true,
minimizer: [
new TerserWebpackPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
mangle: false,
keep_fnames: true,
},
}),
],
},
performance: {
hints: isProd ? 'warning' : false,
},
};
};