-
Notifications
You must be signed in to change notification settings - Fork 12
/
webpack.base.js
162 lines (151 loc) · 4.9 KB
/
webpack.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint-disable */
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const entryList = glob.sync('src/page/*/index.js');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const isAnalyze = process.env.npm_config_argv.includes('analyze');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
let cliEntry = process.env.npm_config_argv.match(/--entry=([\w]+)/);
let entrys = {};
entryList.forEach(function(file) {
const name = file.replace(/src\/(page\/.*)\/index.js/ig, '$1');
entrys[name] = './' + file;
});
if (cliEntry) {
const pageName = cliEntry[1];
const result = {};
for (let i in entrys) {
if (i.includes(pageName)) {
result[i] = entrys[i];
}
}
entrys = result;
}
// console.log(entrys);
const sourcePath = path.join(__dirname, '/src');
let plugins = [
new LodashModuleReplacementPlugin(),
];
// const HtmlWebpackPlugins = [
// new HtmlWebpackPlugin({
// filename: '/page/decorate.html',
// template: './src/page/decorate/index.html',
// chunks: ['decorate']
// }),
// new HtmlWebpackPlugin({
// filename: '/page/canvas.html',
// template: './src/page/canvas/index.html',
// chunks: ['canvas'],
// }),
// new HtmlWebpackPlugin({
// filename: '/page/learn.html',
// template: './src/page/learn/index.html',
// chunks: ['learn']
// }),
// new HtmlWebpackPlugin({
// filename: '/page/preview.html',
// template: './src/page/preview/index.html',
// chunks: ['preview']
// }),
// new HtmlWebpackPlugin({
// filename: '/page/manage.html',
// template: './src/page/manage/index.html',
// chunks: ['manage']
// })
// ]
// plugins = plugins.concat(HtmlWebpackPlugins);
if (isAnalyze) {
plugins.push(new BundleAnalyzerPlugin());
}
let imageLoaderConfig = 'file-loader?hash=sha512&digest=hex&name=images/[name].[ext]';
module.exports = {
entry: entrys,
// entry: {
// 'page/decorate': './src/page/decorate/index.js',
// },
output: {
path: path.resolve(__dirname + '/dist'),
filename: '[name].bundle.js',
// chunkFilename: '[id].chunk.js',
publicPath: `${require('./config').staticAddress}/`,
},
stats: 'minimal',
optimization: {
splitChunks: {
cacheGroups: {
// 排除的chunk(通常是import()引入的)
// 'intersection-observer': {
// name: 'intersection-observer',
// test: /[\\/]node_modules[\\/]intersection-observer/,
// priority: 20,
// },
// 首先: 打包node_modules中的文件
vendor: {
name: "vendor",
// 后视正则把需要异步载入的chunk断下来
test: /[\\/]node_modules[\\/](?!intersection-observer|n-zepto)/,
chunks(chunk) {
// 浏览页面单独打包
return !['page/view', 'page/login', 'page/register'].includes(chunk.name);
},
priority: 10,
},
viewVendor: {
name: "viewVendor",
test: /[\\/]node_modules[\\/](?!n-zepto)/,
chunks(chunk) {
// 浏览页面单独打包
return chunk.name === 'page/view';
},
priority: 10,
}
}
}
},
module: {
rules: [{
test: /\.tsx?$/,
include: [
path.resolve(__dirname, 'src'),
],
loader: ["awesome-typescript-loader", ]
},
{
test: /\.jsx?/,
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'config.js'),
],
use: ['babel-loader', ]
},
// {
// test: /\.html/,
// exclude: /node_modules/,
// use: 'html-loader'
// }
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
disable: true, // [email protected] and newer
},
},
],
}, {
test: /\.(woff|woff2|ttf|eot)$/,
use: 'file-loader'
},
],
},
// externals: {
// jquery: 'jQuery',
// },
resolve: {
extensions: ['.js', '.json', '.jsx', '.ts', '.tsx', '.d.ts'],
},
plugins,
};