web-backset.cn/apps/server/webpack.config.js

143 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-02-21 17:58:09 +08:00
/*eslint-disable*/
2023-02-17 17:59:41 +08:00
const { join } = require('path');
const path = require('path');
const { readdirSync, statSync } = require('fs');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
2023-02-22 11:09:54 +08:00
const TerserPlugin = require("terser-webpack-plugin");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
2023-02-17 17:59:41 +08:00
const isDev = process.env.RUNNING_ENV === 'dev';
2023-02-22 17:59:35 +08:00
const isProd = process.env.RUNNING_ENV === 'prod';
2023-02-17 17:59:41 +08:00
/**
* 引入 src/view/pages下的页面文件排除 _ 开头的文件夹
*/
const importEntry = () => {
const entries = {};
const rootDir = join(process.cwd(), '/view/page');
readdirSync(rootDir)
.filter(i => !i.startsWith('_'))
.forEach(file => {
if (statSync(join(rootDir, file)).isDirectory())
entries[file] = `/view/page/${file}/index.ts`;
});
return entries;
};
module.exports = {
2023-02-21 17:58:09 +08:00
entry: {
...importEntry(),
},
2023-02-17 17:59:41 +08:00
output: {
2023-02-21 17:58:09 +08:00
path: path.resolve(__dirname, 'public/'),
2023-02-17 17:59:41 +08:00
publicPath: '',
filename: '[name].js',
chunkFilename: '[id].chunk.js?[hash:8]',
2023-02-22 11:09:54 +08:00
clean: true
2023-02-17 17:59:41 +08:00
},
mode: isDev ? 'development' : 'production',
2023-02-21 17:58:09 +08:00
optimization: {
2023-02-22 11:09:54 +08:00
minimize: true,
2023-02-22 17:59:35 +08:00
minimizer: [new TerserPlugin({
terserOptions: {
compress: true,
},
})],
2023-02-21 17:58:09 +08:00
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
2023-02-22 11:09:54 +08:00
name: 'vendors',
2023-02-21 17:58:09 +08:00
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial'
},
2023-02-22 23:07:06 +08:00
// lib: {
// test(module) {
// return (
// module.size() > 50 * 1024 &&
// /node_modules[/\\]/.test(module.nameForCondition() || '')
// )
// },
// name: 'chunk-lib',
// priority: 15,
// minChunks: 1,
// reuseExistingChunk: true,
// },
2023-02-21 17:58:09 +08:00
}
},
},
2023-02-17 17:59:41 +08:00
module: {
rules: [
{
test: /\.ts?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.less$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
},
2023-02-21 17:58:09 +08:00
{
test: /\.(scss)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader'
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: () => [
require('autoprefixer')
]
}
}
},
{
loader: 'sass-loader'
}
]
},
2023-02-17 17:59:41 +08:00
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
},
},
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css?[hash:8]',
}),
new CopyPlugin([
{ from: join(process.cwd(), 'view/assets'), to: 'assets' },
]),
2023-02-22 17:59:35 +08:00
isProd && new BundleAnalyzerPlugin({
2023-02-22 11:09:54 +08:00
analyzerHost: "0.0.0.0",
analyzerPort: 8088
})
2023-02-17 17:59:41 +08:00
].filter(Boolean),
resolve: {
extensions: ['.ts', '.js', '.ejs'],
},
2023-02-21 17:58:09 +08:00
externals: {
// require("jquery") 是外部的,并且可用
// 在全局变量 jQuery 上
2023-02-22 17:59:35 +08:00
// jquery: 'jQuery',
// $: 'jQuery'
2023-02-21 17:58:09 +08:00
},
2023-02-17 17:59:41 +08:00
};