-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.dev.js
133 lines (125 loc) · 3.84 KB
/
webpack.dev.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
'use strict';
const glob = require('glob');
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); //提取 css 到单独文件
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 生成 html,并进行压缩
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin') //提取外部资源
// console.log(path.resolve(__dirname, './dist')); // /Users/zuoqi/Desktop/Codes/postcss-test-project/dist
// console.log(path.join(__dirname, './dist')); // /Users/zuoqi/Desktop/Codes/postcss-test-project/dist
// console.log(path.resolve(__dirname, '/dist')); // /dist
// console.log(path.join(__dirname, '/dist')); // /Users/zuoqi/Desktop/Codes/postcss-test-project/dist
// console.log(path.resolve(__dirname, 'dist')); // /Users/zuoqi/Desktop/Codes/postcss-test-project/dist
// console.log(path.join(__dirname, 'dist')); // /Users/zuoqi/Desktop/Codes/postcss-test-project/dist
const setMPA = () => {
const entry = {};
const htmlWebpackPlugins = [];
// 找到符合条件的路径
const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
console.log('entryFiles', entryFiles);
entryFiles.forEach(entryFile => {
const match = entryFile.match(/src\/(.*)\/index\.js/);
const pageName = match && match[1];
entry[pageName] = entryFile;
htmlWebpackPlugins.push(
new HtmlWebpackPlugin({
template: path.join(__dirname, `src/${pageName}/index.html`),
filename: `${pageName}.html`,
chunks: [pageName],
minify: {
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
removeComments: true,
}
})
)
})
return {
entry,
htmlWebpackPlugins,
}
}
const { entry, htmlWebpackPlugins } = setMPA();
module.exports = {
entry,
output: {
filename: '[name].js',
path: path.join(__dirname, './dist'),
},
externals: {
'@txdfe/at': 'var AT',
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
},
{
test: /\.(css|scss)$/,
use: [
MiniCssExtractPlugin.loader, //使用 MiniCssExtractPlugin 就不用 style-loader 了,作用冲突
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]__[local]--[hash:base64:5]',
},
},
},
'resolve-url-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
sourceMap: true, // 结合resolve-url-loader使用必填,详见https://www.npmjs.com/package/resolve-url-loader
prependData: '@import "~@txdfe/at-theme/teambition/scss/index.scss";\n',
},
},
]
},
{
test: /\.(png|jpg|gif|jpeg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240, // 小于 10kb 采用 base64 编码,减少 http 请求次数
}
}
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: 'file-loader'
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].min.css',
chunkFilename: '[id].css',
}),
].concat(htmlWebpackPlugins).concat([
new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'react',
entry: '//cdn.jsdelivr.net/npm/[email protected]/umd/react.development.js',
global: 'React',
},
{
module: 'react-dom',
entry: '//cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.development.js',
global: 'ReactDOM',
},
{
module: '@txdfe/at',
entry: '//cdn.jsdelivr.net/npm/@txdfe/[email protected]/build/at.min.js',
global: 'AT',
},
],
})
]),
mode: 'development',
};