forked from Pera-Swarm/visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
183 lines (164 loc) · 6.21 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Global imports
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
// Paths
const entry = './src/js/app.js';
const includePath = path.join(__dirname, 'src/js');
const nodeModulesPath = path.join(__dirname, 'node_modules');
let outputPath = path.join(__dirname, 'src/public/js');
module.exports = (env) => {
// Dev environment
let devtool = 'eval';
let mode = 'development';
let stats = 'minimal';
let plugins = [
new webpack.DefinePlugin({
__ENV__: JSON.stringify(env.NODE_ENV)
})
];
// Prod environment
if (env.NODE_ENV === 'prod') {
devtool = 'hidden-source-map';
mode = 'production';
stats = 'none';
outputPath = `${__dirname}/build/js`;
}
console.log('Webpack build -');
console.log(` - ENV: ${env.NODE_ENV}`);
console.log(` - outputPath ${outputPath}`);
console.log(` - includePath ${includePath}`);
console.log(` - nodeModulesPath: ${nodeModulesPath}`);
return {
// Here the application starts executing
// and webpack starts bundling
entry: [entry],
// options related to how webpack emits results
output: {
// the target directory for all output files
// must be an absolute path (use the Node.js path module)
path: outputPath,
// the url to the output directory resolved relative to the HTML page
publicPath: 'js',
// the filename template for entry chunks
filename: 'app.js'
},
// Webpack 4 mode helper
mode,
// configuration regarding modules
module: {
// rules for modules (configure loaders, parser options, etc.)
rules: [
{
test: /\.js$/,
enforce: 'pre',
use: ['source-map-loader']
}
],
rules: [
{
// these are matching conditions, each accepting a regular expression or string
// test and include have the same behavior, both must be matched
// exclude must not be matched (takes preference over test and include)
// Best practices:
// - Use RegExp only in test and for filename matching
// - Use arrays of absolute paths in include and exclude
// - Try to avoid exclude and prefer include
test: /\.js?$/,
// the loader which should be applied, it'll be resolved relative to the context
// -loader suffix is no longer optional in webpack2 for clarity reasons
// see webpack 1 upgrade guide
use: {
loader: 'babel-loader'
},
include: includePath,
exclude: nodeModulesPath
},
{
test: /\.(s*)css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: 'css'
}
},
'css-loader',
'postcss-loader',
'sass-loader'
]
}
]
},
// options for resolving module requests
// (does not apply to resolving to loaders)
resolve: {
// directories where to look for modules,
modules: ['node_modules', path.resolve(__dirname, 'src')],
// extensions that are used
extensions: ['.js', '.json']
},
performance: {
hints: 'warning'
},
// lets you precisely control what bundle information gets displayed
stats,
// enhance debugging by adding meta info for the browser devtools
// source-map most detailed at the expense of build speed.
devtool,
devServer: {
contentBase: 'src/public'
},
plugins: plugins.concat(
new HtmlWebpackPlugin({
title: 'Three.js Webpack ES6 Boilerplate',
template: path.join(__dirname, 'src/html/index.html'),
filename: '../index.html',
env: env.NODE_ENV
}),
new MiniCssExtractPlugin({
filename: '../css/[name].css',
chunkFilename: '../css/[id].css'
}),
new CopyPlugin({
patterns: [
{ from: 'node_modules/three/examples/js/libs/stats.min.js' },
{ from: 'node_modules/three/examples/js/libs/dat.gui.min.js' },
{ from: 'src/public/assets/favicon.ico', to: '../favicon.ico' }
]
})
),
optimization: {
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
],
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\\/]node_modules[\\\/]/,
name: 'vendors',
chunks: 'all'
},
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true
}
}
}
}
};
};