-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·133 lines (130 loc) · 3.71 KB
/
webpack.config.js
File metadata and controls
executable file
·133 lines (130 loc) · 3.71 KB
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
// Data
const title = 'Isomorphic'
const meta = require('./source/data/meta.json')
// Modules
const path = require('path')
const webpack = require('webpack')
const autoprefixer = require('autoprefixer')
const nodeExternals = require('webpack-node-externals')
const TerserPlugin = require('terser-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
// Export
module.exports = ({ base }) => {
// Environment
const mode = process.env.NODE_ENV || 'development'
const isProduction = mode === 'production'
const isClient = base === 'client'
// Configuration
return new SpeedMeasurePlugin({ disabled: isProduction }).wrap({
mode,
entry: isClient
? {
main: path.join(__dirname, './source/client.js'),
vendor: ['react', 'react-dom']
}
: { server: path.join(__dirname, './source/server.js') },
output: {
path: path.resolve(__dirname, isClient ? './public' : './'),
filename: isClient ? 'scripts/[name].js' : 'server.bundle.js'
},
resolve: {
alias: { '~': path.join(__dirname, './source') },
extensions: ['.js', '.jsx', '.less', '.gql']
},
target: isClient ? 'web' : 'node',
externals: isClient ? {} : [nodeExternals()],
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules(?!\/webpack-dev-server)/,
use: ['babel-loader']
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
use: 'raw-loader'
},
{
test: /\.(css|less)$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !isProduction,
publicPath: isClient ? '' : '/public/'
}
},
{ loader: 'css-loader' },
{
loader: 'postcss-loader',
options: { plugins: [autoprefixer] }
},
{ loader: 'less-loader' }
]
},
{
test: /\.(jpe?g|png|gif|woff|svg)$/,
use: 'url-loader'
}
]
},
optimization: {
nodeEnv: mode,
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
sourceMap: true,
extractComments: true
})
]
},
performance: { hints: false },
plugins: [
new MiniCssExtractPlugin({ filename: `${isClient ? '' : 'public/'}styles/main.css` }),
...(
isClient
? [
new HtmlWebpackPlugin({
template: 'source/template.html',
filename: 'index.html',
base: '/',
title,
meta,
inject: 'body',
xhtml: true,
minify: {
html5: true,
useShortDoctype: true,
removeComments: true,
keepClosingSlash: true,
collapseWhitespace: true,
collapseInlineTagWhitespace: true
}
}),
new webpack.optimize.AggressiveMergingPlugin(),
new CompressionPlugin({
filename: '[path].gz',
algorithm: 'gzip',
test: /\.js$|\.css$/
})
]
: []
)
],
devtool: !isProduction && 'eval-source-map',
devServer: {
host: '0.0.0.0',
port: process.env.PORT || 8000,
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
hot: true,
progress: true,
open: true
}
})
}