-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
161 lines (157 loc) · 4.35 KB
/
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { resolve } = require('path')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FaviconsWebpackPlugin = require('favicons-webpack-plugin')
const Dotenv = require('dotenv-webpack')
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin')
const nodeExternals = require('webpack-node-externals')
const devMode = process.env.NODE_ENV === 'development'
const entry = ssr =>
ssr
? {
server: resolve(__dirname, 'src/server.tsx')
}
: {
client: resolve(__dirname, 'src/client.tsx')
}
const output = ssr => (ssr ? '[name].js' : 'js/[name].js')
const target = ssr => (ssr ? 'node' : 'web')
const plugin = ssr =>
ssr
? []
: [
new CleanWebpackPlugin(),
new FaviconsWebpackPlugin({
logo: './logo.svg',
prefix: 'icons/',
// Emit all stats of the generated icons
emitStats: false,
// The name of the json containing all favicon information
statsFilename: 'iconstats.json',
// Generate a cache file with control hashes and
// don't rebuild the favicons until those hashes change
persistentCache: true,
// Inject the html into the html-webpack-plugin
inject: true,
// favicon background color (see https://github.com/haydenbleasel/favicons#usage)
background: '#fff',
// favicon app title (see https://github.com/haydenbleasel/favicons#usage)
title: 'React hooks hackernews',
// which icons should be generated (see https://github.com/haydenbleasel/favicons#usage)
icons: {
android: true,
appleIcon: true,
appleStartup: true,
coast: false,
favicons: true,
firefox: true,
opengraph: false,
twitter: false,
yandex: false,
windows: false
}
}),
new HtmlWebpackPlugin({
meta: {
viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no',
'theme-color': '#24d'
},
base: {
href: '/'
},
title: 'react hooks hackernews',
template: 'template.html',
hash: true,
minify: true,
cache: !devMode
})
]
const externals = ssr => (ssr ? [nodeExternals()] : [])
const baseConfig = ssr => ({
mode: devMode ? 'development' : 'production',
entry: entry(ssr),
output: {
filename: output(ssr),
chunkFilename: output(ssr),
path: resolve(__dirname, 'build')
},
target: target(ssr),
module: {
rules: [
{
enforce: 'pre',
test: /\.(j|t)sx?$/,
exclude: /node_modules/,
use: [
{
loader: 'eslint-loader',
options: {
failOnWarning: true
}
}
]
},
{ test: /\.jsx?$/, exclude: /node_modules/, use: 'babel-loader' },
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true
}
}
]
},
{
test: /\.s?css$/,
exclude: /node_modules/,
use: [
'style-loader',
'postcss-loader',
'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss-loader',
'resolve-url-loader',
'sass-loader'
]
},
{ test: /\.html$/, exclude: /node_modules/, use: 'html-loader' },
{
test: /\.svg$/,
exclude: /node_modules/,
enforce: 'pre',
use: [
{
loader: 'svg-sprite-loader'
},
{
loader: 'svgo-loader',
options: {
externalConfig: 'svgo-config.yml'
}
}
]
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
alias: {
'@': resolve(__dirname, 'src')
},
plugins: [
new TsconfigPathsPlugin({
configFile: resolve(__dirname, 'tsconfig.json')
})
]
},
externals: externals(ssr),
plugins: [
new Dotenv({
path: devMode ? `development.env` : `production.env`,
safe: false
}),
...plugin(ssr)
]
})
module.exports = baseConfig