-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
113 lines (102 loc) · 3 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
const path = require('path');
const { promisify } = require('util');
const webpack = require('webpack');
const { getIfUtils, removeEmpty } = require('webpack-config-utils');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const rmrf = promisify(require('rimraf'));
const cp = require('recursive-copy');
const mkdirp = require('make-dir');
const { UglifyJsPlugin } = webpack.optimize;
const { HotModuleReplacementPlugin } = webpack;
const { ifProduction, ifDevelopment } = getIfUtils(process.env.NODE_ENV);
const extractCss = new ExtractTextWebpackPlugin('css/bundle.css');
/**
* Returns a path joined to the current __dirname
* (Use as tagged template literal)
*/
const _ = strings => path.join(__dirname, strings[0]);
/**
* Remove the contents of the server/public directory
*/
const cleanPublicDirectory = async () => {
await rmrf(_`server/public`);
await mkdirp(_`server/public`);
};
/**
* Copy needed directories to server/public
*/
const copyToPublicDirectory = async () => {
await cp(_`src/assets`, _`server/public/assets`).catch(console.error);
};
/**
* Generate the webpack config file to export later
*/
const generateWebpackConfig = async () => {
//
await cleanPublicDirectory();
await copyToPublicDirectory();
const config = {
entry: {
bundle: [_`src/js/index.js`, _`src/css/index.css`],
'vendors-game': [_`src/js/vendors/game.js`],
'vendors-channel': [_`src/js/vendors/channel.js`],
},
output: {
filename: 'js/[name].js',
path: _`server/public`,
publicPath: '/',
},
devtool: 'sourcemap',
module: {
rules: removeEmpty([
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [{ loader: 'babel-loader' }],
},
{
test: /\.(svg|png|jpe?g|gif|webp)$/,
loader: 'url-loader',
options: {
limit: 1000,
context: './src',
name: '[path][name].[ext]',
},
},
{
test: /\.(mp3|mp4|wav)$/,
loader: 'file-loader',
options: {
context: './src',
name: '[path][name].[ext]',
},
},
ifDevelopment({
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
}),
ifProduction({
test: /\.css$/,
use: extractCss.extract({
fallback: 'style-loader',
use: ['css-loader', 'postcss-loader'],
}),
}),
]),
},
plugins: removeEmpty([
new HtmlWebpackPlugin({
template: _`src/index.html`,
filename: _`server/public/index.html`,
inject: false,
}),
new webpack.EnvironmentPlugin(['NODE_ENV']),
ifProduction(new UglifyJsPlugin({ sourceMap: true, comments: false })),
ifProduction(new HotModuleReplacementPlugin()),
ifProduction(extractCss),
]),
};
return config;
};
module.exports = generateWebpackConfig();