-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
275 lines (232 loc) · 7.45 KB
/
webpack.common.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* here is two main things for webpack:
* the targets which will produce different packages
* the second one is createConfig function inside this
* function we are triming and modifiying the passed
* options then do throw generation of the configuration.
* You can find therwe two other helper functions: getRules
* and getPlugins. In these two helper function a lot of
* consideration are directed to "options.mode".
*
* At the end you can find a devServer property is specified
* to the configuration object "config", putting the
* webpack-dev-server options: host, port, hot, etc...
*/
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
/*::
type Target = {|
name: string, // the name of output JS/CSS
entry: string, // the path to the entry point
library?: string // the name of the exported module
|};
*/
/**
* List of targets to build
*/
const targets /*: Array<Target> */ = [
{
name: 'lazyUpdate',
// entry: './src/index.js', // polyfill for some code such as async function.
entry: ['@babel/polyfill', './src/index.js'], // polyfill for some code such as async function.
library: 'lazyUpdate',
}
];
/**
* dev, minimize, devServer
* Create a webpack config for given target
*/
function createConfig(target, options) /*: Object */ {
let dev = options.mode === 'development' || options.mode === 'dev-server',
devServer = options.mode === 'dev-server',
analyze = options.mode === 'analyze';
void function prepareOptions(){
if(dev){
let devServerPort = process.argv.indexOf('--port') > -1 ? process.argv[process.argv.indexOf('--port') + 1] : 7936;
let devServerHost = process.argv.indexOf('--host') > -1 ? process.argv[process.argv.indexOf('--host') + 1] : 'localhost';
let devServerHot = process.argv.indexOf('--hot') > -1;
let port = process.env.PORT || 8080;
let host = process.env.HOST || 'localhost';
options = { host, port, devServerPort, devServerHost, devServerHot,
...options
};
}
}();
let minimize = !dev;
function getPlugins(){
var devServerPlugins=[], devPlugins=[], analyzePlugins=[];
if(devServer){
devServerPlugins = [
];
if(options.hot){
const webpack = require('webpack');
serverConfig.plugins.concat([
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
]);
}
}
if(dev){
let BrowserSyncPlugin = require('browser-sync-webpack-plugin');
devPlugins = [
new BrowserSyncPlugin({
host: options.host,
port: options.port,
proxy: `http://${options.devServerHost}:${options.devServerPort}/`,
}, {
reload: false,
}),
...devServerPlugins
];
}
if(analyze){
analyzePlugins = [
new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin) (),
];
}
return [
new HtmlWebpackPlugin({
title: 'lazy drawings update',
template: './index.html',
}),
!dev && new MiniCssExtractPlugin({
filename: minimize ? '[name]-[hash].min.css' : '[name]-[hash].css',
}),
!devServer && new (require('clean-webpack-plugin').CleanWebpackPlugin) (),
...devPlugins,
...analyzePlugins
].filter(Boolean);
}
function getRules(){
// // use only necessary fonts, overridable by environment variables
// // from the least supported to the most supported
// const browserslist = require('browserslist')();
// const caniuse = require('caniuse-lite');
// const fonts = ['woff2', 'woff', 'ttf'];
// let isCovered = false;
// for (const font of fonts) {
// const override = process.env[`USE_${font.toUpperCase()}`];
// const useFont = override === "true" || override !== "false" && !isCovered;
// lessOptions.modifyVars[`use-${font}`] = useFont;
// const support = caniuse.feature(caniuse.features[font]).stats;
// isCovered = isCovered || useFont && browserslist.every(browser => {
// const [name, version] = browser.split(' ');
// return !support[name] || support[name][version] === 'y';
// });
// }
const cssLoaders /*: Array<Object> */ = [{ loader: 'css-loader' }];
if (minimize) {
cssLoaders[0].options = { importLoaders: 1 };
cssLoaders.push({
loader: 'postcss-loader',
options: { plugins: [require('cssnano')()] },
});
}
return [
{
test: /\.css$/,
use: [
dev ? 'style-loader' : MiniCssExtractPlugin.loader,
...cssLoaders,
],
},
{
test: /\.sass$/,
use: [
dev ? 'style-loader' : MiniCssExtractPlugin.loader,
...cssLoaders,
{
loader: 'sass-loader',
// options: lessOptions,
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.?worker\.(:?js|mjs|ejs|cjs)$/,
exclude: /node_modules/,
use: [
{
loader: 'worker-loader',
options: {
filename: minimize ? '[name]-[hash].min.js' : '[name]-[hash].js',
}
}, // 'babel-loader'
],
},
// {
// test: /\.(ttf|woff|woff2)$/,
// use: [{
// loader: 'file-loader',
// options: {
// name: 'fonts/[name].[ext]',
// },
// }],
// },
];
}
let config = {
mode: dev ? 'development' : 'production',
context: __dirname,
entry: {
[target.name]: target.entry,
},
output: {
filename: minimize ? '[name]-[hash].min.js' : '[name]-[hash].js',
library: target.library,
libraryTarget: 'umd',
libraryExport: 'default',
// Enable output modules to be used in browser or Node.
// See: https://github.com/webpack/webpack/issues/6522
globalObject: "(typeof self !== 'undefined' ? self : this)",
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: getRules()
},
// externals: 'katex',
plugins: getPlugins(),
devtool: dev && 'inline-source-map',
optimization: {
minimize,
minimizer: [
new TerserPlugin({
terserOptions: {
output: {
// ascii_only: true,
},
},
}),
],
},
performance: {
hints: false,
},
};
if(devServer){
config.devServer = {
contentBase: [__dirname],
// Allow server to be accessed from anywhere, which is useful for
// testing. This potentially reveals the source code to the world,
// but this should not be a concern for testing open-source software.
disableHostCheck: true,
host: options.devServerHost,
port: options.devServerPort,
hot: options.devServerHot,
stats: {
colors: true,
},
};
}
return config;
}
module.exports = {
targets,
createConfig,
};