-
Notifications
You must be signed in to change notification settings - Fork 93
/
webpack.config.js
163 lines (141 loc) · 5.15 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
162
163
const path = require('path');
const childProcess = require('child_process');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const hasha = require('hasha');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const webpack = require('webpack');
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
const pkg = require('./package.json');
module.exports = (env, {
mode,
}) => {
const DEV = /development|dev/i.test(mode);
const PROD = /production|prod/i.test(mode);
const COMMIT_HASH = childProcess.execSync('git rev-parse HEAD').toString().trim();
const BUILD_DATE = new Date();
const config = {
devtool: DEV ? 'eval-source-map' : 'source-map',
entry: {
main: [
'./src/app/main.js',
'./src/app/main.scss',
],
},
/*
// TODO: Integrate with ESLint:
// https://github.com/electron-react-boilerplate/electron-react-boilerplate/issues/1321
resolve: {
alias: {
common: path.resolve(__dirname, 'src/common/'),
},
},
*/
output: {
filename: PROD ? '[name].[contenthash].js' : '[name].[fullhash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: './',
},
devServer: {
static: {
directory: path.resolve(__dirname, 'static'),
},
devMiddleware: {
publicPath: '/slotjs/',
// When sharing the site using ssh -R 80:localhost:8080 ssh.localhost.run
// disableHostCheck: true,
},
client: {
overlay: {
warnings: false,
errors: false,
},
},
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
}, {
test: /\.scss/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
}, {
test: /\.ejs$/,
exclude: /node_modules/,
use: {
loader: 'ejs-compiled-loader',
},
}],
},
plugins: [
new ESLintPlugin({ fix: true }),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, 'dist/index.html'),
template: path.resolve(__dirname, 'src/app/components/app/app.template.ejs'),
title: 'SlotJS \\ Circular slot machine mobile-first SPA built using JavaScript, CSS variables and Emojis!',
description: pkg.description,
favicon: path.resolve(__dirname, 'static/favicon.ico'),
inlineSource: '.(js|css)$', // Inline JS and CSS.
minify: PROD,
meta: {
author: pkg.author.name,
description: pkg.description,
},
// We can use templateParameters if more options are required, but it will override all the above.
}),
new MiniCssExtractPlugin({
filename: PROD ? '[name].[contenthash].css' : '[name].[fullhash].css',
}),
new StyleLintPlugin({
fix: true,
}),
new CopyWebpackPlugin({
patterns: [{
from: 'static',
}],
}),
// Defines variables available globally that Webpack can evaluate in compilation time and remove dead code:
// new webpack.DefinePlugin({}),
// Same as before, but sets properties inside `process.env` specifically:
new webpack.EnvironmentPlugin({
DEV,
PROD,
BUILD_DATE,
COMMIT_HASH,
}),
// new BundleAnalyzerPlugin(),
],
optimization: {
minimize: true,
// Extract all styles in a single file:
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
},
},
},
minimizer: PROD ? [
'...',
new CssMinimizerPlugin(),
] : [],
},
};
// if (PROD) {}
return config;
};