forked from styleguidist/react-styleguidist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-webpack-config.js
123 lines (111 loc) · 3.22 KB
/
make-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
'use strict';
const path = require('path');
const webpack = require('webpack');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const merge = require('webpack-merge');
const forEach = require('lodash/forEach');
const mergeWebpackConfig = require('./utils/mergeWebpackConfig');
const StyleguidistOptionsPlugin = require('./utils/StyleguidistOptionsPlugin');
const RENDERER_REGEXP = /Renderer$/;
const sourceDir = path.resolve(__dirname, '../lib');
const htmlLoader = require.resolve('html-webpack-plugin/lib/loader');
module.exports = function(config, env) {
process.env.NODE_ENV = process.env.NODE_ENV || env;
const isProd = env === 'production';
let webpackConfig = {
entry: config.require.concat([path.resolve(sourceDir, 'index')]),
output: {
path: config.styleguideDir,
filename: 'build/[name].bundle.js',
chunkFilename: 'build/[name].js',
},
resolve: {
extensions: ['.js', '.jsx', '.json'],
alias: {
'rsg-codemirror-theme.css': `codemirror/theme/${config.highlightTheme}.css`,
},
},
plugins: [
new StyleguidistOptionsPlugin(config),
new HtmlWebpackPlugin({
title: config.title,
template: `!!${htmlLoader}!${config.template}`,
inject: true,
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(env),
},
}),
],
performance: {
hints: false,
},
};
if (isProd) {
webpackConfig = merge(webpackConfig, {
output: {
filename: 'build/bundle.[chunkhash:8].js',
chunkFilename: 'build/[name].[chunkhash:8].js',
},
plugins: [
new UglifyJSPlugin({
parallel: {
cache: true,
},
uglifyOptions: {
ie8: false,
ecma: 5,
compress: {
keep_fnames: true,
warnings: false,
},
mangle: {
keep_fnames: true,
},
},
}),
new CleanWebpackPlugin(['build'], {
root: config.styleguideDir,
verbose: config.verbose,
}),
new CopyWebpackPlugin(
config.assetsDir
? [
{
from: config.assetsDir,
},
]
: []
),
],
});
} else {
webpackConfig = merge(webpackConfig, {
entry: [require.resolve('react-dev-utils/webpackHotDevClient')],
plugins: [new webpack.HotModuleReplacementPlugin()],
});
}
if (config.webpackConfig) {
webpackConfig = mergeWebpackConfig(webpackConfig, config.webpackConfig, env);
}
// Custom style guide components
if (config.styleguideComponents) {
forEach(config.styleguideComponents, (filepath, name) => {
const fullName = name.match(RENDERER_REGEXP)
? `${name.replace(RENDERER_REGEXP, '')}/${name}`
: name;
webpackConfig.resolve.alias[`rsg-components/${fullName}`] = filepath;
});
}
// Add components folder alias at the end so users can override our components to customize the style guide
// (their aliases should be before this one)
webpackConfig.resolve.alias['rsg-components'] = path.resolve(sourceDir, 'rsg-components');
if (config.dangerouslyUpdateWebpackConfig) {
webpackConfig = config.dangerouslyUpdateWebpackConfig(webpackConfig, env);
}
return webpackConfig;
};