-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
426 lines (395 loc) · 9.38 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const webpack = require('webpack');
const {globSync} = require("glob");
const glob = require("glob");
const path = require("path");
const fs = require("fs");
const frp = require("mmhk-frp");
const inquirer = require('inquirer');
const prompt = inquirer.createPromptModule();
const http = require('http');
const FRP_ENDPOINT = process.env.FRP_ENDPOINT || 'localhost';
const FRP_ENDPOINT_PORT = process.env.FRP_ENDPOINT_PORT || 7000;
const FRP_API_PORT = process.env.FRP_ENDPOINT_PORT || 7001;
const FRP_API_USER = process.env.FRP_API_USER || 'admin';
const FRP_API_PWD = process.env.FRP_API_PWD || 'admin';
const FRP_PUBLIC_DOMAIN = process.env.FRP_PUBLIC_DOMAIN || 'localhost';
const isDevServer = process.env.WEBPACK_DEV_SERVER || process.env.WEBPACK_SERVE;
const checkSubDomainExist = (domain) => {
const auth = `${FRP_API_USER}:${FRP_API_PWD}`;
return new Promise((resolve, reject) => {
const req = http.get({
hostname: FRP_ENDPOINT,
port: FRP_API_PORT,
path: '/api/proxy/http',
headers: { 'Content-Type': 'application/json' },
auth: auth,
}, (resp) => {
resp.setEncoding('utf8');
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let json = {};
try {
json = JSON.parse(data);
resolve(json);
} catch (err) {
reject(err)
}
});
});
req.on('error', (err) => {
console.error(`http error: ${err}`);
reject(err);
});
req.end();
})
.then((data) => {
const list = Array.from(data.proxies || []);
if (list.find((row) => {
return row.name === domain && row.status === 'online';
})) {
return Promise.resolve(true);
}
return Promise.resolve(false);
})
}
/*
* SplitChunksPlugin is enabled by default and replaced
* deprecated CommonsChunkPlugin. It automatically identifies modules which
* should be splitted of chunk by heuristics using module duplication count and
* module category (i. e. node_modules). And splits the chunks…
*
* It is safe to remove "splitChunks" from the generated configuration
* and was added as an educational example.
*
* https://webpack.js.org/plugins/split-chunks-plugin/
*
*/
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const {InjectManifest} = require('workbox-webpack-plugin');
const config = {
mode: 'development',
entry: [
'./src/test/main.js',
],
output: {
path: path.resolve(__dirname, 'docs'),
chunkFilename: 'js/[name].js',
filename: 'js/[name].js',
assetModuleFilename: 'assets/[hash][ext][query]',
publicPath: isDevServer ? "auto" : '/pwa-installer/',
},
resolve: {
alias: {
'vue$': (process.env.NODE_ENV === 'development' ?
"vue/dist/vue.esm.js" :
"vue/dist/vue.min.js"),
'env$': process.env.NODE_ENV === 'production' ?
path.resolve(__dirname, 'src/test/env.production.js') :
path.resolve(__dirname, 'src/test/env.development.js'),
}
},
plugins: [
new webpack.ProgressPlugin(),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: 'css/[name].css',
// chunkFilename: 'css/[id].css',
ignoreOrder: false, // Enable to remove warnings about conflicting order
}),
new CleanWebpackPlugin(),
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: isDevServer ? path.resolve(__dirname, 'src/test/index.html') : path.resolve(__dirname, 'public/docs.html'),
hash: false,
})
],
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /.(js)$/,
include: [
path.resolve(__dirname, 'src'),
],
exclude: /(node_modules|webpack)/,
use: [
{
loader: 'babel-loader',
options: {
plugins: [
[
"@babel/plugin-transform-template-literals", {
loose: true
}],
"@babel/plugin-transform-runtime",
"@babel/plugin-syntax-dynamic-import"
],
presets: [
[
'@babel/preset-env',
{
modules: false,
useBuiltIns: "usage",
corejs: 3
}
]
]
}
},
],
},
{
test: /\.html$/,
use: [
{ loader: 'html-loader' },
]
},
{
test: /\.s[ac]ss$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
["autoprefixer"],
],
},
sourceMap: true,
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
}
},
],
},
{
test: /\.(mov|mp4|mp3|wav|ogg|pdf)$/i,
type: "asset/resource",
generator: {
filename: 'assets/videos/[hash][ext][query]'
},
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/i,
type: 'asset/resource',
generator: {
filename: 'assets/fonts/[hash][ext][query]'
},
parser: {
dataUrlCondition: {
maxSize: 4 * 1024 // 4kb
}
},
},
{
test: /\.(|png|jpe?g|gif)$/i,
type: 'asset',
generator: {
filename: 'assets/img/[hash][ext][query]'
},
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
},
]
},
optimization: {
splitChunks: {
chunks: 'async',
minChunks: 1,
name: false
},
minimize: false,
},
devtool: "source-map",
// watch: process.env.NODE_ENV === 'development',
watchOptions: {
ignored: /(node_modules|webpack)/
},
devServer: {
open: true,
compress: true,
allowedHosts: [
"localhost",
".demo2.mixmedia.com",
],
static: {
serveIndex: true,
}
}
};
// const unixTimeInSeconds = Math.floor(Date.now() / 1000);
const pwaConfig = [
new FaviconsWebpackPlugin({
// Your source logo (required)
logo: path.resolve(__dirname, 'public/assets/logo.png'),
// Enable caching and optionally specify the path to store cached data
// Note: disabling caching may increase build times considerably
cache: true,
inject: true,
mode: 'webapp', // optional can be 'webapp' or 'light' - 'webapp' by default
devMode: 'webapp', // optional can be 'webapp' or 'light' - 'light' by default
prefix: '',
outputPath: `assets/favicon`,
publicPath: isDevServer ? `/assets/favicon` : `/pwa-installer/assets/favicon`,
favicons: {
start_url: "/",
appName: 'PWA installer Demo',
appShort: 'PWA installer Demo',
appDescription: "PWA demo",
display: "standalone",
background: '#ffffff',
theme_color: '#ffffff',
version: "1.0",
icons: {
android: true,
appleIcon: true,
appleStartup: false,
favicons: true,
windows: true,
},
orientation: "landscape",
}
}), new InjectManifest({
maximumFileSizeToCacheInBytes: 30 * 1024 * 1024,
swSrc: path.resolve(__dirname, "src/test/service-worker.js"),
swDest: isDevServer ? "service-worker.js" : path.resolve(__dirname, "docs/service-worker.js"),
})
];
if (!isDevServer) {
config.plugins.unshift(new webpack.DefinePlugin({
__PWA_DISABLED__ : JSON.stringify(false),
}));
config.plugins.push(...pwaConfig);
module.exports = config;
return;
}
module.exports = prompt([
{
type: 'list',
name: 'public',
message: '请问是否允外网访问',
choices: [
{
name: "允许",
value: true
},
{
name: "不需要",
value: false
},
],
},
{
type: 'input',
name: 'subdomain',
message: '请配一个霸气的域名',
validate: (input) => {
return /^([a-z0-9\-]{4,})$/i.test(input);
},
when: ({public}) => {
return public;
}
},
{
type: 'list',
name: 'usePWA',
message: '是否使用PWA组件',
choices: [
{
name: "使用",
value: true
},
{
name: "不需要",
value: false
},
],
},
]).then(({public, subdomain, usePWA}) => {
if (usePWA) {
config.plugins.push(...pwaConfig);
}
config.plugins.unshift(new webpack.DefinePlugin({
__PWA_DISABLED__ : JSON.stringify(!(usePWA)),
}));
return Promise.resolve({
...config,
devServer: {
...config.devServer,
client: !public ? {} : {
webSocketURL: `https://${subdomain}.${FRP_PUBLIC_DOMAIN}/ws`,
},
open: !public ? true : {
target: `https://${subdomain}.${FRP_PUBLIC_DOMAIN}`,
},
allowedHosts: [
`.${FRP_PUBLIC_DOMAIN}`,
],
onListening: (devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
if (!public) {
return;
}
const addr = devServer.server.address();
console.log('set domain:', subdomain);
checkSubDomainExist(subdomain)
.then((exist) => {
if (!exist) {
let conf = {
common: {
serverPort: FRP_ENDPOINT_PORT,
serverAddr: FRP_ENDPOINT,
},
};
conf[subdomain] = {
type: "http",
localIp: "127.0.0.1",
localPort: addr.port,
subdomain,
};
return frp.startClient(conf);
}
return Promise.reject(new Error('已经有人使用此霸气的名字'));
})
.catch((err) => {
console.error(err);
return Promise.reject(err);
})
},
}
})
})