forked from DylanJu/react-pure-ssr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.client.js
75 lines (63 loc) · 2.15 KB
/
webpack.client.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
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const LoadablePlugin = require('@loadable/webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
const devMode = process.env.NODE_ENV !== 'production';
const hotMiddlewareScript = `webpack-hot-middleware/client?name=web&path=/__webpack_hmr&timeout=20000&reload=true`;
const styledComponentsTransformer = createStyledComponentsTransformer();
const getEntryPoint = target => {
if (target === 'node') {
return ['./src/App.tsx'];
}
return devMode ? [hotMiddlewareScript, './src/index.tsx'] : ['./src/index.tsx'];
};
const getConfig = target => ({
mode: devMode ? 'development' : 'production',
name: target,
target,
entry: getEntryPoint(target),
output: {
path: path.resolve(__dirname, `dist/${target}`),
filename: '[name].js',
publicPath: '/web/',
libraryTarget: target === 'node' ? 'commonjs2' : undefined,
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [
'babel-loader',
{
loader: 'ts-loader',
options: {
getCustomTransformers: () => ({ before: [styledComponentsTransformer] }),
},
},
],
},
{
test: /\.(scss|css)$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
],
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
pages: path.resolve('src/pages/'),
components: path.resolve('src/components/'),
actions: path.resolve('src/store/actions/'),
reducers: path.resolve('src/store/reducers/'),
util: path.resolve('src/util/'),
},
},
plugins:
target === 'web'
? [new LoadablePlugin(), new webpack.HotModuleReplacementPlugin(), new MiniCssExtractPlugin()]
: [new LoadablePlugin(), new MiniCssExtractPlugin()],
externals: target === 'node' ? ['@loadable/component', nodeExternals()] : undefined,
});
module.exports = [getConfig('web'), getConfig('node')];