-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
225 lines (221 loc) · 6.17 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
const { resolve } = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MinaWebpackPlugin = require('./script/MinaWebpackPlugin')
const MinaRuntimePlugin = require('./script/MinaRuntimePlugin')
const { getConfig, getLoaderConfigByExtensions } = require('./script/config')
const entry = resolve(__dirname, 'src', 'app.js')
const include = new RegExp('src')
const valReplaceloader = resolve(__dirname, 'script', 'replace-val-loader.js')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
// 打包模式 ci | develop
const env = process.env.NODE_ENV
const mode = process.env.BUILD_TYPE
const envMode = mode === 'ci' ? 'production' : 'development'
const envConfig = getConfig(env, mode, isNoWaConsole)
// 提取环境配置
const { context } = envConfig
const { NO_WA_CONSOLE, CDN_ENV, OUTPUT_DIR } = context
console.log(`环境:${env} 构建类型:${mode} 构建模式:${envMode}`)
/** 注入条件编译的环境变量 */
const conditionalCompiler = {
loader: 'js-conditional-compile-loader',
options: {
isDebug: envMode === 'development',
isEnv: CDN_ENV !== 'prod',
isProd: CDN_ENV === 'prod',
isM1: env === 'M1',
isN: env === 'N',
isProdDebug: env === 'productDebug',
isNoWaConsole: NO_WA_CONSOLE,
},
}
module.exports = {
mode: envMode,
context: resolve('src'),
entry: {
app: entry,
},
output: {
path: resolve(OUTPUT_DIR),
filename: '[name].js',
publicPath: resolve(OUTPUT_DIR),
globalObject: 'wx',
},
resolve: {
extensions: ['.js'],
alias: {
'@': resolve('src'),
'mobx-miniprogram': resolve('src/miniprogram_npm/mobx-miniprogram/index.js'),
'miniprogram-computed': resolve('src/miniprogram_npm/miniprogram-computed/index.js'),
'mobx-miniprogram-bindings': resolve('src/miniprogram_npm/mobx-miniprogram-bindings/index.js'),
rfdc: resolve('src/miniprogram_npm/rfdc/index.js'),
'fast-deep-equal': resolve('src/miniprogram_npm/fast-deep-equal/index.js'),
'miniprogram-api-promise': resolve('src/miniprogram_npm/miniprogram-api-promise/index.js'),
},
},
module: {
rules: [
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: [
{
loader: valReplaceloader,
options: {
all: getLoaderConfigByExtensions(context, 'js'),
},
},
'babel-loader',
conditionalCompiler,
],
},
{
test: /\.(scss)$/,
include: /src/,
use: [
{
loader: 'file-loader',
options: {
useRelativePath: true,
name: '[path][name].wxss',
context: resolve('src'),
},
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: [resolve('src', 'styles'), resolve('src')],
},
},
},
{
loader: valReplaceloader,
options: {
all: getLoaderConfigByExtensions(context, 'scss'),
},
},
],
},
{
test: /\.(woff|woff2|eot|ttf|svg|png|gif|jpeg|jpg|wxs|wxss)\??.*$/,
include,
type: 'javascript/auto',
use: [
{
loader: 'url-loader',
options: {
limit: 50000,
},
},
],
},
{
test: /\.wxml$/,
include: /src/,
use: [
{
loader: 'file-loader',
options: {
useRelativePath: true,
name: '[path][name].wxml',
context: resolve('src'),
},
},
{
loader: valReplaceloader,
options: {
all: getLoaderConfigByExtensions(context, 'wxml'),
},
},
],
},
],
},
plugins: [
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false,
}),
new CopyWebpackPlugin({
patterns: [
{
from: '**/*',
to: './',
filter: (resourcePath) => !['.js', '.scss'].some((item) => resourcePath.endsWith(item)),
},
],
}),
new MinaWebpackPlugin({
scriptExtensions: ['.js'],
assetExtensions: ['.scss', '.png', '.wxss', '.wxml'],
}),
new MinaRuntimePlugin(),
new webpack.DefinePlugin({
__WEBPACK__ENV: JSON.stringify(process.env.NODE_ENV) || 'M1',
BUILDTYPE: JSON.stringify(process.env.BUILD_TYPE) || 'develop',
}),
// new BundleAnalyzerPlugin({
// analyzerMode: 'static',
// analyzerPort: 8091, // 运行后的端口号 可以修改
// generateStatsFile: true,
// statsOptions: { source: false },
// }),
],
optimization: {
splitChunks: {
chunks: 'all',
minSize: 30000,
minChunks: 1,
name: false,
cacheGroups: {
lib: {
test: resolve('src/lib'),
priority: -20,
name: 'lib',
},
miniProgram: {
test: resolve('src/miniprogram_npm'),
priority: -20,
name: 'miniProgram',
},
service: {
name: 'service',
test: resolve('src/service'),
priority: -25,
minChunks: 3,
},
api: {
name: 'api',
test: resolve('src/api'),
priority: -25,
minChunks: 3,
},
default: {
test: function (module) {
if (module.resource) {
if (
module.resource.match('package[a-zA-Z]*/service') ||
module.resource.match('package[a-zA-Z]*/store') ||
module.resource.match('package[a-zA-Z]*/config')
) {
return false
}
}
return true
},
minChunks: 2,
priority: -30,
reuseExistingChunk: true,
name: 'common',
},
},
},
runtimeChunk: {
name: 'runtime',
},
usedExports: true,
},
devtool: mode !== 'ci' ? 'cheap-module-source-map' : 'source-map',
}