-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.conf.js
More file actions
98 lines (90 loc) · 2.41 KB
/
webpack.conf.js
File metadata and controls
98 lines (90 loc) · 2.41 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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const webpack = require('webpack')
const merge = require('webpack-merge')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const pkg = require('./package.json')
const SRC = path.resolve(__dirname, 'src')
const DIST = path.resolve(__dirname, 'dist')
module.exports = function (env = {}, argv) {
// env 来自于 https://webpack.js.org/api/cli/#environment-options
// argv 是 webpack 启动参数,其中 mode 来自于 --mode 参数
const PROD = argv.mode === 'production'
const config = {
mode: argv.mode,
entry: path.join(SRC, 'index.ts'),
target: 'web',
resolve: {
mainFields: ['browser', 'module', 'main'],
extensions: ['.ts', '.js', '.json']
},
output: {
path: DIST,
filename: `${pkg.name}.umd.js`,
libraryTarget: 'umd',
library: 'ABCWallet',
libraryExport: 'default',
},
optimization: {
minimize: false
},
module: {
rules: [{
test: /\.(js|ts)$/,
include: [SRC],
use: {
loader: 'ts-loader',
options: {
transpileOnly: true,
onlyCompileBundledFiles: true
}
}
}]
},
plugins: [
new webpack.DefinePlugin({
NODE_RUNTIME: JSON.stringify(false),
WEB_RUNTIME: JSON.stringify(true)
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'public', 'index.html'),
inject: false,
minify: false,
})
],
devtool: false,
devServer: {
host: '0.0.0.0',
disableHostCheck: true,
contentBase: [
path.resolve(__dirname, 'public'),
path.resolve(__dirname, 'dist'),
],
}
}
// 不直接定义 port ,这样在本地开发时会自动挑选合适的 port
if (process.env.PORT) {
config.devServer.port = process.env.PORT
}
if (PROD) {
// 生成 .min 格式
const minifiedConfig = merge(config, {
output: {
filename: `${pkg.name}.umd.min.js`
},
optimization: {
minimize: true
},
devtool: 'source-map',
})
// 生成 bundle 分析报告
if (env.analysis) {
minifiedConfig.plugins.push(new BundleAnalyzerPlugin())
}
return [config, minifiedConfig]
}
else {
return config
}
}