-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathwebpack.config.js
More file actions
193 lines (186 loc) · 5.88 KB
/
Copy pathwebpack.config.js
File metadata and controls
193 lines (186 loc) · 5.88 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
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
184
185
186
187
188
189
190
191
192
193
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const package = require('./package.json');
const NODE_ENV = process.env.NODE_ENV || 'development';
const PACKAGE_VERSION = package.version;
const PACKAGE_TITLE = package.name;
const DEV_SERVER_IP = '0.0.0.0';
const DEV_SERVER_PORT = 8080;
//Clean up output directory
const CleanPlugin = require('clean-webpack-plugin');
//JavaScript Minifier (UglifyJS is no longer maintained)
const TerserPlugin = require('terser-webpack-plugin');
//Copy file and output it
const CopyPlugin = require('copy-webpack-plugin');
//Gather all generated assets
const ServiceWorkerPlugin = require('serviceworker-webpack-plugin');
//Generate html with dynamic assets
const HtmlPlugin = require('html-webpack-plugin');
//Save generated html as file
const HtmlHardDiskPlugin = require('html-webpack-harddisk-plugin');
const GLOBAL_VARS = {
'process.env.VERSION': JSON.stringify(PACKAGE_VERSION),
'process.env.TITLE': JSON.stringify(PACKAGE_TITLE),
'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
};
const OUTPUT_DIR = "dist";
const ALIAS = {
'@res': './res'
};
const MODULE_PATHS = [
//For webpack HMR dev server
'webpack-dev-server/client?http://' + DEV_SERVER_IP + ':' + DEV_SERVER_PORT,
'webpack/hot/only-dev-server',//HMR, but does NOT reload on error
//'webpack/hot/dev-server'//HMR, but reloads on error
//The used entrypoints
'./node_modules',
'./src/app'
];
const ENTRIES = {
app: './src/app/index.js'
};
const config = {
//Change this to force 'production' for optimizations (same as --mode production)
mode: NODE_ENV,
//Entry point to start bundling
entry: ENTRIES,
//Root directory of the build
context: process.cwd(),
//Target platform
target: 'web',
output: {
//Output to ./OUTPUT/bundle.js
path: path.resolve(__dirname, './' + OUTPUT_DIR),
filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
publicPath: OUTPUT_DIR + '/'
},
module: {
rules: [
{
//Load js and jsx
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{ loader: 'babel-loader' },
{ loader: 'react-hot-loader/webpack' }
]
},
{
//Load css
test: /\.css$/,
use: [
{ loader: 'style-loader' },//, options: {sourceMap: true}},
{ loader: 'css-loader', options: { importLoaders: 1, modules: 'global', localIdentName: "[path][name]__[local]--[hash:base64:5]" } }
]
}
]
},
resolve: {
//Resolve by filename without extensions
extensions: ['*', '.js', '.jsx', '.mjs'],
//Resolve by aliases
alias: ALIAS,
//Resolve by absolute path
modules: MODULE_PATHS
},
//Allows access to filename from context dir
node: { __filename: true },
//Don't include xmlhttprequest in bundle
externals: [{ xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}' }],
plugins: [
new CopyPlugin([
//Copy resource by directory...
{ from: './res/document', to: 'document' },
{ from: './res/image', to: 'image' },
{ from: './res/lang', to: 'lang' },
{ from: './res/script', to: 'script' },
{ from: './res/style', to: 'style' },
{ from: './res/theme', to: 'theme' },
//Copy remaining resources...
//NOTE: any files added here should also be added to CleanWebpackPlugin
{ from: './res/404.html', to: '../404.html' }
]),
new ServiceWorkerPlugin({
filename: '../serviceWorker.js',
entry: path.join(__dirname, './res/ServiceWorker.js'),
publicPath: './' + OUTPUT_DIR + '/',
excludes: ['**/.*', '**/*.map', '**/document/**', '**/image/**', '**/lang/**', '**/script/**', '**/style/**'],
transformOptions: serviceWorkerOption =>
{
return {
assets: serviceWorkerOption.assets,
hash: Math.random().toString(36).substr(2, 9)
};
}
}),
new HtmlPlugin({
filename: '../index.html',
template: './res/index.html',
alwaysWriteToDisk: true
}),
new HtmlHardDiskPlugin(),
new webpack.DefinePlugin(GLOBAL_VARS)
]
};
module.exports = (env, argv) =>
{
if (argv.mode === 'development')
{
//Do development stuff...
//config.devtool = 'eval-source-map';
config.plugins.push(new webpack.HotModuleReplacementPlugin());
//Webpack server options
config.devServer = {
contentBase: path.join(__dirname, '/'),//public/
//This stops serviceWorker in dev mode since its no longer https
//disableHostCheck: true,
host: DEV_SERVER_IP,
port: DEV_SERVER_PORT,
overlay: true,
hot: true,
open: true
};
}
else if (argv.mode === 'production')
{
//Do production stuff...
//config.devtool = 'inline-source-map';
config.output.filename = '[name].bundle.[chunkhash].js';
config.output.chunkFilename = '[name].bundle.[chunkhash].js',
config.plugins.push(new webpack.SourceMapDevToolPlugin({
filename: 'sourcemap/[name].bundle.js.map',
exclude: [/vendors\.bundle.*\.js$/, '../serviceWorker.js']
}));
config.plugins.push(new CleanPlugin({
cleanOnceBeforeBuildPatterns: ['../dist', '../serviceWorker.js', '../404.html', '../index.html'],
dangerouslyAllowCleanPatternsOutsideProject: true,
dry: false
}));
//Optimizations
config.optimization = {
minimizer: [new TerserPlugin({ parallel: true, cache: true, sourceMap: true })],
runtimeChunk: { name: entrypoint => 'runtime~' + entrypoint.name },
splitChunks: {
cacheGroups: {
default: false,
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
enforce: true
//minChunks: 2
},
styles: {
test: /\.css$/,
name: 'styles',
chunks: 'all',
enforce: true
}
}
}
};
}
return config;
};