waveterm/webpack/webpack.web.js

140 lines
4.6 KiB
JavaScript
Raw Normal View History

const webpack = require("webpack");
2023-08-22 06:37:04 +02:00
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const LodashModuleReplacementPlugin = require("lodash-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const webpackMerge = require("webpack-merge");
2022-06-08 02:25:35 +02:00
const path = require("path");
const moment = require("dayjs");
const VERSION = require("../version.js");
2022-06-08 02:25:35 +02:00
function makeBuildStr() {
let buildStr = moment().format("YYYYMMDD-HHmmss");
// console.log("waveterm:web " + VERSION + " build " + buildStr);
return buildStr;
}
const BUILD = makeBuildStr();
let BundleAnalyzerPlugin = null;
if (process.env.WEBPACK_ANALYZE) {
BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
}
var webCommon = {
2022-06-08 02:25:35 +02:00
entry: {
waveterm: ["./src/index.ts", "./src/app/app.less"],
2022-06-08 02:25:35 +02:00
},
module: {
rules: [
{
test: /\.tsx?$/,
// exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
[
"@babel/preset-env",
{
2023-08-22 06:37:04 +02:00
targets:
"defaults and not ie > 0 and not op_mini all and not op_mob > 0 and not kaios > 0 and not and_qq > 0 and not and_uc > 0 and not baidu > 0",
2022-06-08 02:25:35 +02:00
},
],
"@babel/preset-react",
2023-08-22 06:37:04 +02:00
"@babel/preset-typescript",
],
2022-06-08 02:25:35 +02:00
plugins: [
2023-08-22 06:37:04 +02:00
["@babel/transform-runtime", { regenerator: true }],
2022-06-08 02:25:35 +02:00
"@babel/plugin-transform-react-jsx",
2023-08-22 06:37:04 +02:00
["@babel/plugin-proposal-decorators", { legacy: true }],
["@babel/plugin-proposal-class-properties", { loose: true }],
["@babel/plugin-proposal-private-methods", { loose: true }],
["@babel/plugin-proposal-private-property-in-object", { loose: true }],
2022-06-08 02:25:35 +02:00
"babel-plugin-jsx-control-statements",
],
},
},
},
{
test: /\.css$/,
2023-08-22 06:37:04 +02:00
use: ["style-loader", "css-loader"],
2022-06-08 02:25:35 +02:00
},
{
test: /\.less$/,
2023-08-22 06:37:04 +02:00
use: [{ loader: MiniCssExtractPlugin.loader }, "css-loader", "less-loader"],
2022-06-08 02:25:35 +02:00
},
{
test: /\.svg$/,
use: [{ loader: "@svgr/webpack", options: { icon: true, svgo: false } }, "file-loader"],
},
{
test: /\.md$/,
type: "asset/source",
},
{
test: /\.(png|jpe?g|gif)$/i,
type: "asset/resource",
},
2023-08-22 06:37:04 +02:00
],
2022-06-08 02:25:35 +02:00
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".mjs", ".cjs", ".wasm", ".json", ".less", ".css"],
},
};
var webDev = webpackMerge.merge(webCommon, {
mode: "development",
output: {
path: path.resolve(__dirname, "../dist-dev"),
filename: "[name].js",
},
devtool: "source-map",
devServer: {
static: {
directory: path.join(__dirname, "../public"),
},
port: 9000,
headers: {
"Cache-Control": "no-store",
},
},
2022-06-08 02:25:35 +02:00
plugins: [
2023-08-22 06:37:04 +02:00
new MiniCssExtractPlugin({ filename: "[name].css", ignoreOrder: true }),
new LodashModuleReplacementPlugin(),
new webpack.DefinePlugin({
__WAVETERM_DEV__: "true",
__WAVETERM_VERSION__: JSON.stringify(VERSION),
__WAVETERM_BUILD__: JSON.stringify("devbuild"),
}),
2022-06-08 02:25:35 +02:00
],
watchOptions: {
aggregateTimeout: 200,
2022-06-08 02:25:35 +02:00
},
});
var webProd = webpackMerge.merge(webCommon, {
mode: "production",
output: {
path: path.resolve(__dirname, "../dist"),
filename: "[name].js",
},
devtool: "source-map",
plugins: [
new MiniCssExtractPlugin({ filename: "[name].css", ignoreOrder: true }),
new LodashModuleReplacementPlugin(),
new webpack.DefinePlugin({
__WAVETERM_DEV__: "false",
__WAVETERM_VERSION__: JSON.stringify(VERSION),
__WAVETERM_BUILD__: JSON.stringify(BUILD),
}),
],
optimization: {
minimize: true,
},
});
if (BundleAnalyzerPlugin != null) {
webProd.plugins.push(new BundleAnalyzerPlugin());
}
module.exports = { webDev: webDev, webProd: webProd };