const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require('webpack');
// 根据API_ENV环境不同分为debugger开发代理(api),development为使用dev的api,production为使用线上api
function getIp() {
const interfaces = require('os').networkInterfaces(); // 在开发环境中获取局域网中的本机iP地址
let IPAdress = '';
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
IPAdress = alias.address;
}
}
}
return IPAdress;
}
const proxyHost = {
localhost: 'http://192.168.16.252:8093', //链接本地调试
development: 'https://arthas.mttop.cn',
production: 'https://mt.mttop.cn',
feature: 'http://192.168.8.108:8096',
mock: `http://${getIp()}:8000`,
};
const proxy_env = proxyHost[process.env.PROXY_ENV];
module.exports = {
mode: 'development',
entry: './src/test/index.tsx',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
module: {
rules: [
{
test: /\.tsx?$/,
include: [path.resolve(__dirname, 'src')],
use: ['ts-loader'],
},
{
test: /\.jsx?$/,
include: [path.resolve(__dirname, 'src')],
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
],
},
{
test: /\.less$/,
include: [path.resolve(__dirname, 'src')],
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
},
},
'less-loader',
],
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
},
],
},
{
test: /\.(jpg|png|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 10240, // 超过10kb打包为图片
},
},
},
],
},
plugins: [
new webpack.ProgressPlugin(),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html',
}),
],
devServer: {
proxy: {
'/nodeApi': {
target: `${proxy_env}${process.env.PROXY_ENV !== 'localhost' ? '/nodeApi' : ''}`,
changeOrigin: true,
pathRewrite: { '^/nodeApi': '' },
},
'/ws': {
target: `${proxyHost[process.env.BUILD_ENV]}/cms`,
changeOrigin: true,
pathRewrite: { '^/ws': '' },
ws: true,
},
'/adminApi': {
target: `${proxy_env}${process.env.PROXY_ENV !== 'localhost' ? '/adminApi' : ''}`,
changeOrigin: true,
pathRewrite: { '^/adminApi': '' },
},
'/loginApi': {
target: `${proxy_env}${process.env.PROXY_ENV !== 'localhost' ? '/loginApi' : ''}`,
changeOrigin: true,
pathRewrite: { '^/loginApi': '' },
},
'/crmApi': {
target: `${proxy_env}${process.env.PROXY_ENV !== 'localhost' ? '/crmApi' : ''}`,
changeOrigin: true,
pathRewrite: { '^/crmApi': '' },
},
'/approvalApi': {
target: `${proxy_env}${process.env.PROXY_ENV !== 'localhost' ? '/approvalApi' : ''}`,
changeOrigin: true,
pathRewrite: { '^/approvalApi': '' },
},
},
contentBase: path.join(__dirname, 'dist'),
compress: false,
hot: true,
port: 8094,
},
};