-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathwebpack.config.babel.js
127 lines (116 loc) · 3.96 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
'use strict';
var ExtractTextPlugin = require("extract-text-webpack-plugin"); //css单独打包
var HtmlWebpackPlugin = require('html-webpack-plugin'); //生成html
var webpack = require('webpack');
const path = require('path');
const svgDirs = [
require.resolve('antd-mobile').replace(/warn\.js$/, ''), // 1. 属于 antd-mobile 内置 svg 文件
path.resolve(__dirname, 'src/images') // 2. 自己私人的 svg 存放目录
];
console.log(process.env.NODE_ENV);
let conf = {
devtool: 'eval',
entry: {
app: [
__dirname + '/src/index.js' //唯一入口文件
]
},
output: {
path: __dirname + '/build', //打包后的文件存放的地方
filename: '[name].[chunkhash:8].js', //打包后输出文件的文件名
// publicPath:__dirname+'/public',
chunkFilename: '[name]-[id].[chunkhash:8].js'
},
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader'],
include: /src/
},
{
test: /\.(png|jpg)$/,
use: 'url-loader?limit=8192&name=src/images/[name].[ext]'
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ['css-loader', 'postcss-loader']
})
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ['css-loader', 'postcss-loader', 'sass-loader']
})
},
{
test: /\.(svg)$/i,
use: 'svg-sprite-loader',
include: svgDirs, // 把 svgDirs 路径下的所有 svg 文件交给 svg-sprite-loader 插件处理
},
]
},
// devServer: {
// contentBase: './', //本地服务器所加载的页面所在的目录
// host: 'localhost',
// port: 8888,
// historyApiFallback: true, //不跳转
// inline: true, //实时刷新
// proxy: {
// '/api': {
// target: 'https://cnodejs.org/api/v1',
// secure: false,
// changeOrigin: true,
// host: 'cnodejs.org'
// }
// }
// },
plugins: [
new webpack.optimize.ModuleConcatenationPlugin(),
new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML
filename: './index.html', //生成的html存放路径,相对于 path
template: './src/templates/index.html', //html模板路径
hash: true, //为静态资源生成hash值
}),
new webpack.LoaderOptionsPlugin({
options: {
postcss: function () {
return [
require('postcss-pxtorem')({
rootValue: 100,
propWhiteList: []
}),
require('autoprefixer')
];
}
}
}),
new ExtractTextPlugin('main.css'),
],
resolve: {
modules: ['node_modules', path.join(__dirname, '../node_modules')],
extensions: ['.web.js', '.js', '.json'],
},
};
if (process.env.NODE_ENV) {
conf.devtool = 'cheap-source-map';
conf.plugins.push(new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks(module) {
// any required modules inside node_modules are extracted to vendor
return (module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, './node_modules')
) === 0);
}
}));
conf.plugins.push(new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity
}));
}
module.exports = conf;