-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.dev.js
More file actions
executable file
·150 lines (141 loc) · 3.21 KB
/
Copy pathwebpack.config.dev.js
File metadata and controls
executable file
·150 lines (141 loc) · 3.21 KB
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
const path = require("path");
const webpack = require('webpack');
const fs = require("fs");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const srcRoot = path.resolve(__dirname, 'src');
const devPath = path.resolve(__dirname, 'dev');
const pageDir = path.resolve(srcRoot, 'page');
const mainFile = "index.js";
//多入口
function getEntry() {
let entryMap = {};
fs.readdirSync(pageDir).forEach((pathname) => {
let fullPathName = path.resolve(pageDir, pathname);
let stat = fs.statSync(fullPathName);
let fileName = path.resolve(fullPathName, mainFile);
//判断是文件并且入口文件存在
if (stat.isDirectory() && fs.existsSync(fileName)) {
entryMap[pathname] = fileName;
}
});
return entryMap;
}
//多入口的html加载
function getHtmlArray(entryMap) {
let htmlArray = [];
Object.keys(entryMap).forEach(function (key) {
let fullPathName = path.resolve(pageDir, key);
let fileName = path.resolve(fullPathName, key + '.html')
if (fs.existsSync(fileName)) {
htmlArray.push(new HtmlWebpackPlugin({
chunks: ["common" ,key], // 注意这里的key就是chunk
filename: key + '.html',
template: fileName,
inlineSource: '.(js|css)'
}))
}
});
return htmlArray;
}
const entryMap = getEntry();
const htmlArray = getHtmlArray(entryMap);
module.exports = {
mode: 'development',
devServer: {
contentBase: devPath,
hot: true,
open: true,
port: 8080,
// host: "192.168.0.2",
historyApiFallback: {
disableDotRule: true,
}
},
devtool: 'cheap-module-eval-source-map',
//去掉后缀名称
resolve: {
extensions: ['.js','.jsx'],
alias: {
component: path.resolve(srcRoot, 'component'),
static: path.resolve(srcRoot, 'static')
},
},
entry: entryMap,
output: {
path: devPath,
filename: '[name].js'
},
module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: "/node_modules/",
use: [
{
loader: 'babel-loader',
options: {
plugins: ['react-hot-loader/babel'],
}
},
// {
// loader: 'eslint-loader'
// }
],
include: srcRoot,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
include: srcRoot
},
{
test: /\.less$/,
use: ['style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2, //less中通过import引入的less
// modules: true, //css module
}
},
{
loader: 'px2rem-loader',
options: {
remUni: 75,
remPrecision: 6,
}
},
{
loader: 'less-loader',
},
{
loader: 'sass-resources-loader',
options: {
resources: srcRoot + '/component/common.less'
}
},
],
include: srcRoot
},
{
test: /\.(png|jpg|jpeg|gif)$/,
use: 'url-loader?limit=8192',
include: srcRoot
},
]
},
optimization: {
splitChunks: {
cacheGroups: {
common: {
test: /[\\/]node_modules[\\/]/,
chunks: "all",
name: "common"
},
}
}
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
].concat(htmlArray)
}