-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
166 lines (152 loc) · 4.75 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
'use strict';
/*jshint esversion:6*/
/*jshint node:true*/
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanPlugin = require('clean-webpack-plugin');
const pkg = require('./package.json');
const packageName = pkg.name;
const TARGET = process.env.npm_lifecycle_event; // 当前执行的命令
const DEV_PORT = 8080; //端口
const devOrigin = `http://localhost:${DEV_PORT}/`;
const isProduction = TARGET === 'build' ? true : false; // 当前是否发布产品
const PublicPath = isProduction ? 'http://webpack.jumei.com/' : devOrigin;
const jsFileName = isProduction ? '[name]-[hash].js' : '[name].js';
const cssFileName = isProduction ? '[name]-[hash].css' : '[name].css';
const commonName = isProduction ? 'common-[hash].js' : 'common.js';
const PATHS = { //定义当前目录
app: path.join(process.cwd(), 'src'),
build: path.join(process.cwd(), 'build'),
node_modules: path.join(process.cwd(), 'node_modules')
};
/**定义模块加载器**/
let cssLoaders = [
'css-loader?modules&localIdentName=[path][name]__[local]__[hash:base64:5]',
'autoprefixer-loader?browsers=last 2 versions',
];
if (isProduction) { //如果是发布产品,将CSS Name进行压缩
cssLoaders = [
'css-loader?modules&localIdentName=[hash:base64:5]',
'autoprefixer-loader?browsers=last 2 versions',
];
}
/*如果不是发布产品,则移除style-loader*/
if (!isProduction) {
cssLoaders.unshift('style-loader');
}
/***定义使用的插件****/
const plugins = [
new HtmlWebpackPlugin({
title: 'webpack demo',
template: process.cwd() + '/index.html'
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
__ENV__: JSON.stringify(process.env.NODE_ENV || 'development'),
})
];
/*如果发布产品则需要增加压缩功能*/
if (isProduction) {
plugins.push(new CleanPlugin([PATHS.build])); //清空目录
plugins.push(new ExtractTextPlugin(cssFileName, {
disable: false,
allChunks: true,
}));
plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: false
}));
plugins.push(new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}));
plugins.push(new webpack.optimize.DedupePlugin());
}
//开始配置
const config = {
devtool: (isProduction ? null : 'cheap-module-source-map'), //选择调试使用的方式,>https://webpack.github.io/docs/configuration.html
context: PATHS.app, //当前代码的路径,通常使用entry的目录
devServer: {
historyApiFallback: true,
hot: true,
inline: true,
progress: true,
stats: 'errors-only',
host: process.env.HOST,
port: DEV_PORT
}
};
//配置主入口
config.entry = {
app: PATHS.app
};
config.resolve = {
extensions: ['', '.js', '.jsx', 'css', 'scss', 'less'],
alias: {
'app': PATHS.app,
'antd': path.join(PATHS.node_modules, 'antd/dist/antd.min.js'),
'antd-style': path.join(PATHS.node_modules, 'antd/dist/antd.min.css'),
}
};
//设置输出
config.output = {
filename: jsFileName,
path: PATHS.build,
publicPath: PublicPath
};
config.module = {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['react', 'es2015'],
plugins: ["transform-class-properties"]
}
},
{
test: /\.(png|jpg|svg|woff|woff2)?(\?v=\d+.\d+.\d+)?$/,
loader: 'url-loader?limit=8192'
},
{
test: /\.(eot|ttf)$/,
loader: 'file-loader'
},
{
test: /\.less$/,
loader: "style!css!less"
}
]
};
if (isProduction) {
config.module.loaders.push({
test: /\.css$/,
include: path.resolve(__dirname, PATHS.node_modules),
loader: 'style!css?sourceMap=true'
});
config.module.loaders.push({
test: /\.css$/,
exclude: path.resolve(__dirname, PATHS.node_modules),
loader: ExtractTextPlugin.extract('style-loader', cssLoaders.join('!'))
});
} else {
config.module.loaders.push({
test: /\.css$/,
include: path.resolve(__dirname, PATHS.node_modules),
loader: 'style!css?sourceMap=true'
});
config.module.loaders.push({
test: /\.css$/,
exclude: path.resolve(__dirname, PATHS.node_modules),
loader: cssLoaders.join('!')
});
}
config.plugins = plugins;
module.exports = config;