-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
131 lines (126 loc) · 4.59 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
'use strict';
const webpack = require('webpack'); // webpack
const path = require('path'); // path
const CopyWebpackPlugin = require('copy-webpack-plugin'); // 复制文件和目录
const commonsPlugin = new webpack.optimize.CommonsChunkPlugin('vendors'); // 提取公共部分
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 快速生成html
const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); // 压缩代码
const ExtractTextPlugin = require("extract-text-webpack-plugin"); // 提取css到单独的文件
const root_path = path.resolve(__dirname);
const app_path = path.resolve(root_path, 'app/entry.js');
const build_path = path.resolve(root_path, 'dist/');
// var currentTarget = process.env.npm_lifecycle_event;
module.exports = {
entry: {
bundle: [app_path],
main: ['vue', 'vue-router', 'axios']
},
output: {
path: build_path,
filename: 'src/[name].[hash:8].js'
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new ExtractTextPlugin({
filename: "css/bundle.[hash:8].css",
disable: false,
allChunks: true
}),
commonsPlugin,
new CopyWebpackPlugin([
{
from: root_path + '/app/index.html',
to: root_path + '/dist/'
},
]),
new UglifyJSPlugin(),
// 开发环境的模块热替换
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
filename: root_path + '/dist/index.html',
template: root_path + '/app/index.html',
favicon: root_path + '/app/favicon.ico',
inject: 'body',
hash: true
//favicon: path.resolve('./', './src/favicon.ico')
})
],
module: {
loaders: [
{
test: /\.css$/,
loader: "style-loader!css-loader?sourceMap!",
include: ['node build/dev-server.js']
},
{
test: /\.less$/,
loader: "style-loader!css-loader!less-loader"
},
{
test: /\.js$/,
loader: "babel-loader",
exclude: /node_modules/
},
{
test: /\.css$/,
// loader: "style-loader!css-loader",
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
{
test: /\.scss$/,
// loader: "style-loader!css-loader!sass-loader",
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "file-loader"
})
},
// 图片转化,小于8K自动转化为base64的编码
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: "url-loader",
options: {
limit: 8192,
name:"images/[hash:8].[name].[ext]"
}
},
{
test: /\.(woff|woff2|ttf|eot)$/,
loader: 'file-loader',
options: {
outputPath: '/css/fonts/',
name: '[hash:8].[name].[ext]'
}
},
// 使用vue-loader 加载 .vue 结尾的文件
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'scss': 'style-loader!css-loader!sass-loader'
}
}
}
]
},
resolve: {
// require时省略的扩展名,如:require('app') 不需要app.js
extensions: ['.js', '.vue'],
// 别名,可以直接使用别名来代表设定的路径以及其他
alias: {
vueView: path.resolve(root_path, 'app/view'),
styleScss: path.resolve(root_path, 'app/style'),
// 运行时构建不包含模板编译器,因此不支持 template 选项,只能用 render 选项,但即使使用运行时构建,在单文件组件中也依然
// 可以写模板,因为单文件组件的模板会在构建时预编译为 render 函数。
'vue': 'vue/dist/vue.js'
}
},
devtool: false
};