-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.dev.base.js
140 lines (134 loc) · 3.32 KB
/
webpack.config.dev.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
137
138
139
const path = require("path")
const fs = require("fs")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const srcRoot = path.resolve(__dirname, "src")
const containerDir = path.resolve(srcRoot, "modules")
const mainFile = "index.js"
const autoprefixer = require("autoprefixer")
const pxtorem = require("postcss-pxtorem")
const postcssConfig = {
loader: "postcss-loader",
options: {
plugins: () => [
autoprefixer({ browsers: ["> 1%", "last 4 versions"] }),
pxtorem({
rootValue: 100,
propWhiteList: [],
})
]
}
}
function getHtmlArray(entryMap) {
const htmlArray = []
Object.keys(entryMap).forEach((key) => {
const fullPathName = path.resolve(containerDir, key)
if (fs.existsSync(fullPathName)) {
htmlArray.push(new HtmlWebpackPlugin({
filename: key + ".html",
template: "./src/index.html",
chunks: ["common", key],
minify: true,
webfunny: process.argv[6]
}))
}
})
return htmlArray
}
function getEntry() {
const entryMap = {}
fs.readdirSync(containerDir).forEach((pathname) => {
const fullPathName = path.resolve(containerDir, pathname)
const stat = fs.statSync(fullPathName)
const fileName = path.resolve(fullPathName, mainFile)
if (stat.isDirectory() && fs.existsSync(fileName)) {
entryMap[pathname] = fileName
}
})
return entryMap
}
const entryMap = getEntry()
const htmlArray = getHtmlArray(entryMap)
const baseConfig = {
entry: entryMap,
resolve: {
extensions: [".js", ".jsx"]
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: [{ loader: "babel-loader" }],
//把对.js文件的处理转交给id为babel的HappyPack实例
// use: 'happypack/loader?id=babel-application-js',
exclude: /node_modules/,
include: srcRoot
},
{
test: /\.js?$/,
exclude: /node_modules | lib/,
loader: "eslint-loader"
},
{
test: /\.(scss|css)$/,
use: [MiniCssExtractPlugin.loader, {
loader: "css-loader", options: {
minimize: true, modules: false,
localIdentName: "[path][name]__[local]--[hash:base64:5]"
}
},
"sass-loader",
postcssConfig
],
include: srcRoot
},
// {
// test: /\.(png|jpg|jpeg|gif)$/,
// use: 'url-loader?limit=8192&name=./images/[name].[hash].[ext]',
// include: srcRoot
// },
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: "babel-loader",
},
{
loader: "@svgr/webpack",
options: {
babel: false,
icon: true,
},
},
],
},
{
test: /\.(png|jpg|jpeg|webp|gif)$/,
use: [
{
loader: "url-loader",
options: {
name: "[path][name].[ext]",
limit: 2000
}
}
]
}
]
},
optimization: {
splitChunks: {
cacheGroups: {
common: {
test: /[\\/]node_modules[\\/]/,
chunks: "all",
name: "common"
}
}
}
},
}
module.exports = {
htmlArray: htmlArray,
baseConfig: baseConfig
}