webpak4编译TS

63 阅读1分钟

TS版本4.7以后可能会存在的问题

e4805942e8661fca4f6aeca135031f8.jpg

依赖版本

6c364c533a01b628db689507f699e04.jpg

webpack配置

const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWbpackPlugin = require("html-webpack-plugin");
const path = require("path");

const isPord = process.env.NODE_ENV === "production";

function resolve(dir) {
  return path.resolve(__dirname, "..", dir);
}

module.exports = {
  mode: isPord ? "production" : "development",
  entry: {
    app: "./src/main.ts",
  },

  output: {
    path: resolve("dist"),
    filename: "[name].[contenthash:8].js",
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        loader: "ts-loader",
      },
    ],
  },
  plugins: [
    new HtmlWbpackPlugin({
      template: "./public/index.html",
    }),
    new CleanWebpackPlugin({}),
  ],
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  devtool: isPord ? "cheap-module-source-map" : "cheap-module-eval-source-map",

  devServer: {
    host: "localhost",
    stats: "errors-only",
    port: 8081,
    open: true,
  },
};