forked from slimkit/plus-small-screen-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
executable file
·199 lines (179 loc) · 5.43 KB
/
webpack.config.babel.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
/*
|--------------------------------------------------------
| ThinkSNS+ SPA 配置文件
|--------------------------------------------------------
|
| 配置文件使用 ES6 语法配置,这样能保证整个文档项目的语法统一性
| 修改配置文件请使用 ES6 语法对 webpack 进行配置。
|
| @author Seven Du <shiweidu@outlook.com>
|
*/
import path from 'path';
import webpack from 'webpack';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import OptimizeCSSPlugin from 'optimize-css-assets-webpack-plugin';
import WebpackLaravelMixManifest from 'webpack-laravel-mix-manifest';
import CleanWebpackPlugin from 'clean-webpack-plugin';
// 环境变量获取
const NODE_ENV = process.env.NODE_ENV || 'development';
// 是否是正式环境编译
const isProd = NODE_ENV === 'production';
// 各项资源地址定义
const assetsRoot = path.resolve(__dirname, 'src');
const buildAssetsRoot = path.resolve(__dirname, 'dist');
// 入口配置
const entry = {
app: path.resolve(assetsRoot, 'index.js')
};
function cssLoaders(options = {}) {
const cssLoader = {
loader: 'css-loader',
options: {
minimize: isProd,
sourceMap: options.sourceMap
}
};
// generate loader string to be used with extract text plugin
function generateLoaders(loader, loaderOptions) {
let loaders = [cssLoader];
if(loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
});
}
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader'
});
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
};
};
function styleLoaders(options = {}) {
let output = [];
const loaders = cssLoaders(options);
for(let extension in loaders) {
let loader = loaders[extension]
output.push({
test: new RegExp('\\.' + extension + '$'),
use: loader
});
}
return output;
};
// 环境插件~不同环境启用的不同插件.
const plugins = isProd ? [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: false
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({
cssProcessorOptions: {
safe: true
}
})
] : [
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
new webpack.NoEmitOnErrorsPlugin(),
];
const webpackConfig = {
devtool: isProd ? false : 'source-map',
entry: entry,
output: {
path: buildAssetsRoot,
publicPath: '../',
filename: isProd ? 'js/[chunkhash].js' : 'js/[name].js',
},
resolve: {
extensions: ['.js', '.vue', '.json'],
modules: [
assetsRoot,
path.join(__dirname, 'node_modules'),
],
alias: {
'vue$': `vue/dist/vue.common.js`,
}
},
module: {
rules: [
...styleLoaders({ sourceMap: !isProd }),
{
test: /\.vue$/,
use: [
{
loader: 'vue-loader',
options: {
loaders: cssLoaders({
sourceMap: !isProd
})
}
},
{
loader: 'iview-loader',
options: {
prefix: false
}
}
]
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [assetsRoot]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
query: {
limit: 10000,
name: 'images/[name].[ext]'
}
},
{
test: /\.(woff2?|eot|ttf|otf|woff)(\?.*)?$/,
loader: 'url-loader',
query: {
limit: 10000,
name: isProd ? 'fonts/[hash].[ext]' : 'fonts/[name].[ext]'
}
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(NODE_ENV),
},
}),
// extract css into its own file
new ExtractTextPlugin({
filename: isProd ? 'css/[chunkhash].css' : 'css/[name].css'
}),
new WebpackLaravelMixManifest(),
new webpack.optimize.OccurrenceOrderPlugin(),
new CleanWebpackPlugin(
['./*'], // 匹配删除的文件
{
root: buildAssetsRoot, // 根目录
verbose: true, // 开启在控制台输出信息
dry: false // 启用删除文件
}
),
...plugins,
]
};
export default webpackConfig;