monorepo-microservice-rbac/apps/aorta/scripts/webpack.common.ts
2023-12-26 17:08:36 +08:00

87 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Configuration, DefinePlugin } from "webpack";
import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import * as dotenv from "dotenv";
import fontsLoader from "./loaders/fonts.loader";
import jsonLoader from "./loaders/json.loader";
import imgLoader from "./loaders/img.loader";
import babelLoader from "./loaders/babel.loader";
import { cssLoader, lessLoader } from "./loaders/style.loader";
import CopyWebpackPlugin from "copy-webpack-plugin";
// 加载配置文件
const envConfig = dotenv.config({
path: path.resolve(__dirname, "../env/.env." + process.env.BASE_ENV),
});
const baseConfig: Configuration = {
entry: path.join(__dirname, "../src/index.tsx"), // 入口文件
// 打包出口文件
output: {
filename: "static/js/[name].[chunkhash:8].js", // 每个输出js的名称
path: path.join(__dirname, "../dist"), // 打包结果输出路径
clean: true, // webpack4需要配置clean-webpack-plugin来删除dist文件,webpack5内置了
publicPath: "/", // 打包后文件的公共前缀路径
},
// loader 配置
module: {
rules: [
babelLoader,
cssLoader,
lessLoader,
jsonLoader,
imgLoader,
fontsLoader,
],
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"],
// 别名需要配置两个地方,这里和 tsconfig.json
alias: {
"@": path.join(__dirname, "../src"),
"@@": path.join(__dirname, "../core"),
},
},
// plugins
plugins: [
new HtmlWebpackPlugin({
// 复制 'index.html' 文件并自动引入打包输出的所有资源js/css
template: path.join(__dirname, "../public/index.html"),
inject: true, // 自动注入静态资源
hash: true,
cache: false,
// 压缩html资源
minify: {
removeAttributeQuotes: true,
collapseWhitespace: true, //去空格
removeComments: true, // 去注释
minifyJS: true, // 在脚本元素和事件属性中缩小JavaScript(使用UglifyJS)
minifyCSS: true, // 缩小CSS样式元素和样式属性
},
}),
new DefinePlugin({
"process.env": JSON.stringify(envConfig.parsed),
"process.env.BASE_ENV": JSON.stringify(process.env.BASE_ENV),
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.join(__dirname, "../", "node_modules", "web-workers"),
to: path.join(__dirname, "dist", "itk", "web-workers"),
},
{
from: path.join(__dirname, "../", "node_modules", "itk-image-io"),
to: path.join(__dirname, "dist", "itk", "image-io"),
},
{
from: path.join(__dirname, "../", "node_modules", "itk-mesh-io"),
to: path.join(__dirname, "dist", "itk", "mesh-io"),
},
],
}),
],
};
export default baseConfig;