Webpack Config


前端工程化使用的打包工具:webpack

https://webpack.js.org/

rollup、parcel、vercel、vite、gulp…(用rust、go写的打包工具…Rspack、esbuild)

其它部分都会live coding,除了这部分内容,直接copy一下,前端应用开发使用的打包工具有很多,webpack生态目前仍然是最大的,其它封装更成熟的应用开发框架比如next.js、angular这些框架会自己集成应用的基本配置能力在框架内,各种打包工具也基本都会兼容支持webpack的配置项,这里不做过多的介绍。

const dotenv = require("dotenv");
dotenv.config();

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");

module.exports = {
    entry: "./src/index.ts", // bundle"s entry point
    output: {
        path: path.resolve(__dirname, "dist"), // output directory
        filename: "[name].js", // name of the generated bundle
    },
    resolve: {
        extensions: [".js", ".ts", ".json"],
    },

    mode: "development",

    module: {
        rules: [
            {
                test: /\\.ts$/,
                loader: "ts-loader",
                exclude: /node_modules/,
            },

            {
                test: /\\.css$/i,
                use: ["style-loader", "css-loader"],
            },
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: "./src/index.html",
            inject: "body",
        }),
        new webpack.DefinePlugin({
            'process.env.CONTRACT_ADDRESS': JSON.stringify(process.env.CONTRACT_ADDRESS),
            'process.env.DEBUG': JSON.stringify(process.env.DEBUG),
        }),
    ],

    devServer: {
        historyApiFallback: true,
        port:8080,
        hot:true
    }
};

添加一些Webpack依赖


yarn add -D webpack webpack-cli ts-loader html-webpack-plugin dotenv webpack-dev-server

tsconfig.json


{
    "compilerOptions": {
        "resolveJsonModule": true,
        "esModuleInterop": true
    }
}

Metamask交互