-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
232 lines (165 loc) · 4.44 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
const fs = require('fs-extra');
const path = require('path');
const url = require('url')
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AssetsPlugin = require('assets-webpack-plugin');
var cheerio = require('cheerio');
const shelljs = require('shelljs');
const pkg = require('./package.json');
var dir = path.parse(__filename).dir;
dir = dir.split(/\\/).pop();
const local_url = pkg['local-url']+dir+'/dist/';
const request = require('request');
const ENV = process.env.npm_lifecycle_event;
var config = {
publicPath : 'dist/'
,path : __dirname+'/dist/'
//,hmr : 0
,hmr :1
,static_sc:local_url
,plugins:[]
,port:2333
,filename:ENV!=='pro'? 'scripts/[name].js':'scripts/[name].js'
,chunkFilename:ENV!=='pro'?'scripts/chunk/[name]-[id].js':'scripts/chunk/[name]-[id].js'
}
config.plugins.push(function(){
this.plugin('done',function(stats){
return;
})
});
// 启用热加载时 html中的‘/xx.js’引用服务器根目录,一定要改成绝对的"http://xxx.js"
if(ENV==='dev-hmr'){
// config.plugins.push(new webpack.HotModuleReplacementPlugin());
// Object.keys(config.entry).forEach((s)=>{
// config.entry[s].unshift('webpack/hot/dev-server');
// });
}
//每次重新启动webpack命令才会清空dist目录重新构建,就算用watch也没用!!
//启动了hmr,配置static_sc的话会情况目录导致错误!为了避免!
if(ENV==='pro'){
config.filename='scripts/[name]-[chunkhash:6].js';
//config.filename='scripts/[name].js';
//config.plugins.push( new CleanPlugin(['dist/*/*.*']));
config.plugins.push( new CleanPlugin(['dist/**/*']));
//////////////
config.publicPath = '//'+pkg['pro-url']+dir+'/dist/';
}
config.plugins.push(
new webpack.BannerPlugin(`@author ${pkg.name}<${pkg.email}>\r\n@lastest ${new Date}}`)
,new AssetsPlugin({
fullPath: true
,filename:'assets.json'
,metadata: {version: 1}
,processOutput: function (assets) {
return JSON.stringify(assets);
}
})
,new webpack.DefinePlugin({
// __DEV__: JSON.stringify(process.env.npm_lifecycle_event),
'process.env.NODE_ENV': JSON.stringify(ENV==='pro'?'production':'development')
})
);
;
///
module.exports = {
// entry: config.entry
entry:{
main1:'./src/main1.js'
}
,devtool: ENV==='pro'?'source-map':'eval-source-map'
,output: {
//console.log(path.join(__dirname,'/dist/scripts'));
path: config.path
,publicPath : config.publicPath
//相对path
,chunkFilename:config.chunkFilename
,filename: config.filename
,library: 'TiaoYiTiao'
,libraryTarget: 'umd'
}
,resolve: {
extensions: ['.js', '.jsx', '.css','.png','.jpg','.less', '.gif','.webp']
,alias: {
XButton: '../Common/XButton/XButton'
}
}
,module: {
rules: [
{
test: /\.(css|less)$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: ()=>[require('autoprefixer')]
}},
'less-loader'
]
},
{
test: /\.(png|jpg|jpeg|gif|webp)$/,
use: [
{
loader: 'file-loader',
options: {
publicPath:''
,name :'img/[name].[ext]?[hash:6]'
}
}
]
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: ['env', 'stage-3', 'react']
}
},
{
test: /\.(mp3|aac|ogg|mp4|wav)$/,
use: [
{
loader: 'file-loader',
options: {
name:'media/[name].[ext]?[hash:6]'
}
}
]
},
{
test: /\.(ttf|otf|svg)$/,
use : [
{
loader:'file-loader',
options: {
name:'font/[name].[ext]?[hash:6]'
}
}
]
},
{
test: /\.json$/ ,
use:['json-loader']
}
]
}
,externals: {
}
,plugins:config.plugins
,stats: {
// Nice colored output
colors: true
}
,devServer: {
// hot: true
// ,inline: true
contentBase: path.join(__dirname)
}
}
///////////////////