-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.base.js
136 lines (124 loc) · 4 KB
/
webpack.config.base.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
/**
* webpack基础配置。
* 在 webpack.config.dev.js 和 webpack.config.prod.js 文件中被引入。
*/
var path = require("path");
// var webpack = require('webpack');
//引入样式抽离插件
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
var config = {
//基础目录,入口起点会相对于此目录查找
context: __dirname,
entry: {
// Add as many entry points as you have container-react-components here
app: './html/app.js',
vendors: ['react'] //指定第三方插件打包到 vendors.js 中
},
output: {
//打包文件输出目录
path: path.resolve('./dist/bundles/'),
filename: "[name].js",
chunkFilename: "[name].bundle.js",
//访问静态资源的基础路径
publicPath: '/bundles/'
},
externals: [
], // add all vendor libs
optimization: {
splitChunks: {
minChunks: 2, //Minimum number of chunks that must share a module before splitting.
cacheGroups: {
commons: {
name:"commons",
},
// Extracting all CSS/less in a single file
// styles: {
// name: 'styles',
// test: /\.(c|le)ss$/,
// chunks: 'all',
// enforce: true,
// },
}
}
},
plugins: [
], // add all common plugins here
module: {
rules:[
{
test: /\.html$/,
use: 'raw-loader'
},
{
test: /\.(gif|png|jpg)$/,
use: 'url-loader?limit=8192'
},
// the url-loader uses DataUrls. url-loader封装了file-loader。
//小于(limit/1024)kb的woff/wpff2文件被编码成DataURL,并内联到代码中.
//大于这个限制的文件会使用file-loader打包。通过http请求加载。
{
test: /\.(woff|woff2|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: 'url-loader?limit=81920'
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: ['babel-loader']
},
] // add all common loaders here
},
resolve: {
modules:['node_modules', 'bower_components'],
extensions: ['.js', '.jsx', '.css']
}
};
module.exports = (env, argv) => {
var devMode = argv.mode === 'development' // whether is development mode
config.plugins.push(
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[name].css' : '[name].[hash].css',
// ignoreOrder: false, // Enable to remove warnings about conflicting order
})
)
config.module.rules.push(
{
test: /\.css$/,
use: [
// 'style-loader', //creates style nodes from JS strings
{
loader: MiniCssExtractPlugin.loader,
options: {
// only enable hot in development
hmr: devMode,
// if hmr does not work, this is a forceful method.
reloadAll: true,
},
},
{ loader: 'css-loader', options: { sourceMap: devMode} }, // translates CSS into CommonJS
{ loader: 'postcss-loader', options: { sourceMap: devMode?'inline':false}}, // autoprefix and minify css
],
},
{
test: /\.less$/i,
use: [
// 'style-loader', //creates style nodes from JS strings
{
loader: MiniCssExtractPlugin.loader,
options: {
// only enable hot in development
hmr: devMode,
// if hmr does not work, this is a forceful method.
reloadAll: true,
},
},
{ loader: 'css-loader', options: { sourceMap: devMode}}, // translates CSS into CommonJS
{ loader: 'postcss-loader', options: { sourceMap: devMode?'inline':false}}, // autoprefix and minify css
{ loader: 'less-loader', options: { sourceMap: devMode }} // compiles Less to CSS
]
}
)
return config;
};