-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.js
162 lines (154 loc) · 3.84 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
const forEach = require('lodash/forEach');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const mixWebpackConfig = require('laravel-mix/setup/webpack.config');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
/**
* @typedef {import('webpack').Configuration} Configuration
*/
const buildDefinitions = env => {
const defineMap = {
API_URL: '/api',
FB_PAGE_ID: '',
FB_APP_ID: '',
FILESTACK_KEY: '',
FINGERPRINT_API_KEY: '',
FINGERPRINT_STORAGE_KEY: '',
RECAPTCHA_KEY: '',
};
let definitions = {
'process.env.NODE_ENV': JSON.stringify(
process.env.NODE_ENV || 'development'
),
};
forEach(defineMap, (defaultVariable, name) => {
definitions[name] = JSON.stringify(env[name] || defaultVariable);
});
return definitions;
};
/** @type {(env: {}, argv: {}) => Configuration} */
const webConfig = (env = {}, argv = {}) => ({
name: 'web',
devtool: argv.production ? '' : 'cheap-eval-source-map',
entry: {
app: path.resolve(__dirname, './angular/js/app.js'),
},
output: {
path: path.resolve(__dirname, './public/assets/web'),
filename: '[name].js',
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'eslint-loader',
options: {
emitWarning: !argv.production,
},
},
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
},
},
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'resolve-url-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
],
},
{
test: /\.(png|jpg|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 8192,
},
},
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
{
test: /\.html$/,
use: {
loader: 'html-loader',
options: {
minimize: !!argv.production,
},
},
},
],
},
resolve: {},
plugins: [
new webpack.DefinePlugin(buildDefinitions(env)),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './angular/app.html'),
filename: path.resolve(
__dirname,
`./public/${env.devserver ? 'index' : 'app'}.html`
),
}),
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
!argv.production ? new webpack.HotModuleReplacementPlugin() : '',
].filter(v => v),
devServer: {
allowedHosts: ['localhost', 'nuswhispers.local', 'nuswhispers.com'],
contentBase: path.resolve(__dirname, './public'),
hot: true,
historyApiFallback: true,
},
optimization: {
minimizer: [new OptimizeCSSAssetsPlugin()],
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
enforce: true,
},
},
},
},
});
/** @type {Configuration} */
const adminConfig = { ...mixWebpackConfig, name: 'admin' };
module.exports = [webConfig, adminConfig];